Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudant): upgrade to ibm cloudant sdk #2607

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
package org.eclipse.sw360.attachments;

import com.cloudant.client.api.CloudantClient;
import com.google.common.collect.ImmutableSet;
import com.ibm.cloud.cloudant.v1.Cloudant;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.db.AttachmentDatabaseHandler;
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
Expand All @@ -26,7 +26,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.eclipse.sw360.datahandler.common.SW360Assert.*;
Expand All @@ -46,8 +45,8 @@ public AttachmentHandler() throws MalformedURLException {
handler = new AttachmentDatabaseHandler(DatabaseSettings.getConfiguredClient(), DatabaseSettings.COUCH_DB_DATABASE, DatabaseSettings.COUCH_DB_ATTACHMENTS);
}

public AttachmentHandler(Supplier<CloudantClient> httpClient, String dbName, String attachmentDbName) throws MalformedURLException {
handler = new AttachmentDatabaseHandler(httpClient, dbName, attachmentDbName);
public AttachmentHandler(Cloudant client, String dbName, String attachmentDbName) throws MalformedURLException {
handler = new AttachmentDatabaseHandler(client, dbName, attachmentDbName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

import java.io.IOException;
import java.util.List;
import java.util.function.Supplier;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
import org.eclipse.sw360.datahandler.db.ChangeLogsDatabaseHandler;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
Expand All @@ -26,7 +24,7 @@
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;

import com.cloudant.client.api.CloudantClient;
import com.ibm.cloud.cloudant.v1.Cloudant;

/**
* Implementation of the Thrift service
Expand All @@ -41,7 +39,7 @@ public class ChangeLogsHandler implements ChangeLogsService.Iface {
this(DatabaseSettings.getConfiguredClient(), DatabaseSettings.COUCH_DB_CHANGE_LOGS);
}

ChangeLogsHandler(Supplier<CloudantClient> client, String dbName) throws IOException {
ChangeLogsHandler(Cloudant client, String dbName) throws IOException {
handler = new ChangeLogsDatabaseHandler(client, dbName);
}

Expand Down
5 changes: 5 additions & 0 deletions backend/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibm.cloud</groupId>
<artifactId>cloudant</artifactId>
<version>${cloudantsdk.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import com.ibm.cloud.cloudant.v1.Cloudant;
import org.apache.thrift.TBase;
import org.apache.thrift.TFieldIdEnum;
import org.eclipse.sw360.datahandler.common.SW360Utils;
Expand All @@ -31,7 +31,6 @@
import org.eclipse.sw360.datahandler.thrift.packages.Package;
import org.eclipse.sw360.datahandler.thrift.projects.Project;

import com.cloudant.client.api.CloudantClient;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

Expand All @@ -43,8 +42,8 @@ protected AttachmentAwareDatabaseHandler(AttachmentDatabaseHandler attachmentDat
this.attachmentDatabaseHandler = attachmentDatabaseHandler;
}

protected AttachmentAwareDatabaseHandler(Supplier<CloudantClient> httpClient, String dbName, String attachmentDbName) throws MalformedURLException {
this(new AttachmentDatabaseHandler(httpClient, dbName, attachmentDbName));
protected AttachmentAwareDatabaseHandler(Cloudant client, String dbName, String attachmentDbName) throws MalformedURLException {
this(new AttachmentDatabaseHandler(client, dbName, attachmentDbName));
}

protected Source toSource(Release release){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
import org.eclipse.sw360.datahandler.thrift.users.User;

import com.cloudant.client.api.model.Response;
import com.cloudant.client.api.model.DesignDocument.MapReduce;
import com.cloudant.client.api.views.Key;
import com.cloudant.client.api.views.UnpaginatedRequestBuilder;
import com.cloudant.client.api.views.ViewRequestBuilder;
import com.ibm.cloud.cloudant.v1.model.DocumentResult;
import com.ibm.cloud.cloudant.v1.model.DesignDocumentViewsMapReduce;
import com.ibm.cloud.cloudant.v1.model.PostViewOptions;

import java.util.HashMap;
import java.util.List;
Expand All @@ -44,15 +42,17 @@ public class AttachmentContentRepository extends DatabaseRepositoryCloudantClien

public AttachmentContentRepository(DatabaseConnectorCloudant db) {
super(db, AttachmentContent.class);
Map<String, MapReduce> views = new HashMap<String, MapReduce>();
Map<String, DesignDocumentViewsMapReduce> views = new HashMap<>();
views.put("onlyRemotes", createMapReduce(ONLYREMOTES, null));
views.put("all", createMapReduce(ALL, null));
initStandardDesignDocument(views, db);
}

public List<AttachmentContent> getOnlyRemoteAttachments() {
ViewRequestBuilder query = getConnector().createQuery(AttachmentContent.class, "onlyRemotes");
UnpaginatedRequestBuilder req = query.newRequest(Key.Type.STRING, Object.class).includeDocs(true);
PostViewOptions req = getConnector()
.getPostViewQueryBuilder(AttachmentContent.class, "onlyRemotes")
.includeDocs(true)
.build();
return queryView(req);
}

Expand All @@ -69,10 +69,14 @@ public RequestSummary vacuumAttachmentDB(User user, final Set<String> usedIds) {
requestSummary.setTotalElements(allAttachmentContents.size());
requestSummary.setTotalAffectedElements(unusedAttachmentContents.size());

final List<Response> documentOperationResults = getConnector().deleteBulk(unusedAttachmentContents);
final List<DocumentResult> documentOperationResults = getConnector().deleteIds(
unusedAttachmentContents
.stream()
.map(AttachmentContent::getId).collect(Collectors.toSet())
);
if (unusedAttachmentContents.isEmpty() || !documentOperationResults.isEmpty()) {
requestSummary.setRequestStatus(RequestStatus.SUCCESS);
}else{
} else {
requestSummary.setRequestStatus(RequestStatus.FAILURE);
}
return requestSummary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
*/
package org.eclipse.sw360.datahandler.db;

import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.model.Response;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.DocumentResult;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.couchdb.AttachmentConnector;
Expand All @@ -24,7 +24,6 @@
import org.eclipse.sw360.datahandler.thrift.users.User;

import org.apache.logging.log4j.Logger;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.thrift.TException;

Expand All @@ -33,7 +32,6 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.eclipse.sw360.datahandler.common.Duration.durationOf;
Expand All @@ -56,13 +54,13 @@ public class AttachmentDatabaseHandler {

private static final Logger log = LogManager.getLogger(AttachmentDatabaseHandler.class);

public AttachmentDatabaseHandler(Supplier<CloudantClient> httpClient, String dbName, String attachmentDbName) throws MalformedURLException {
db = new DatabaseConnectorCloudant(httpClient, attachmentDbName);
attachmentConnector = new AttachmentConnector(httpClient, attachmentDbName, durationOf(30, TimeUnit.SECONDS));
public AttachmentDatabaseHandler(Cloudant client, String dbName, String attachmentDbName) throws MalformedURLException {
db = new DatabaseConnectorCloudant(client, attachmentDbName);
attachmentConnector = new AttachmentConnector(client, attachmentDbName, durationOf(30, TimeUnit.SECONDS));
attachmentContentRepository = new AttachmentContentRepository(db);
attachmentUsageRepository = new AttachmentUsageRepository(new DatabaseConnectorCloudant(httpClient, dbName));
attachmentRepository = new AttachmentRepository(new DatabaseConnectorCloudant(httpClient, dbName));
attachmentOwnerRepository = new AttachmentOwnerRepository(new DatabaseConnectorCloudant(httpClient, dbName));
attachmentUsageRepository = new AttachmentUsageRepository(new DatabaseConnectorCloudant(client, dbName));
attachmentRepository = new AttachmentRepository(new DatabaseConnectorCloudant(client, dbName));
attachmentOwnerRepository = new AttachmentOwnerRepository(new DatabaseConnectorCloudant(client, dbName));
}

public AttachmentConnector getAttachmentConnector(){
Expand All @@ -74,7 +72,7 @@ public AttachmentContent add(AttachmentContent attachmentContent){
return attachmentContent;
}
public List<AttachmentContent> makeAttachmentContents(List<AttachmentContent> attachmentContents) throws TException {
final List<Response> documentOperationResults = attachmentContentRepository.executeBulk(attachmentContents);
final List<DocumentResult> documentOperationResults = attachmentContentRepository.executeBulk(attachmentContents);
if (documentOperationResults.isEmpty())
log.error("Failed Attachment store results " + documentOperationResults);

Expand All @@ -91,7 +89,7 @@ public void updateAttachmentContent(AttachmentContent attachment) throws TExcept
attachmentConnector.updateAttachmentContent(attachment);
}
public RequestSummary bulkDelete(List<String> ids) {
final List<Response> documentOperationResults = attachmentContentRepository.deleteIds(ids);
final List<DocumentResult> documentOperationResults = attachmentContentRepository.deleteIds(ids);
return CommonUtils.getRequestSummary(ids, documentOperationResults);
}
public RequestStatus deleteAttachmentContent(String attachmentId) throws TException {
Expand Down Expand Up @@ -147,9 +145,9 @@ public AttachmentUsage makeAttachmentUsage(AttachmentUsage attachmentUsage) {

public void makeAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws TException {
List<AttachmentUsage> sanitizedUsages = distinctAttachmentUsages(attachmentUsages);
List<Response> results = attachmentUsageRepository.executeBulk(sanitizedUsages);
results = results.stream().filter(res -> res.getError() != null || res.getStatusCode() != HttpStatus.SC_CREATED)
.collect(Collectors.toList());
List<DocumentResult> results = attachmentUsageRepository.executeBulk(sanitizedUsages);
results = results.stream().filter(res -> res.getError() != null || !res.isOk())
.toList();
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be created: " + results);
}
Expand Down Expand Up @@ -187,9 +185,9 @@ public AttachmentUsage updateAttachmentUsage(AttachmentUsage attachmentUsage) {
}

public void updateAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws TException {
List<Response> results = attachmentUsageRepository.executeBulk(attachmentUsages);
results = results.stream().filter(res -> res.getError() != null || res.getStatusCode() != HttpStatus.SC_CREATED)
.collect(Collectors.toList());
List<DocumentResult> results = attachmentUsageRepository.executeBulk(attachmentUsages);
results = results.stream().filter(res -> res.getError() != null || !res.isOk())
.toList();
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be updated: " + results);
}
Expand All @@ -200,10 +198,10 @@ public void deleteAttachmentUsage(AttachmentUsage attachmentUsage) throws SW360E
}

public void deleteAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws SW360Exception {
List<Response> results = attachmentUsageRepository.deleteIds(
List<DocumentResult> results = attachmentUsageRepository.deleteIds(
attachmentUsages.stream().map(AttachmentUsage::getId).collect(Collectors.toList()));
results = results.stream().filter(res -> res.getError() != null || res.getStatusCode() != HttpStatus.SC_OK)
.collect(Collectors.toList());
results = results.stream().filter(res -> res.getError() != null || !res.isOk())
.toList();
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be deleted: " + results);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.thrift.Source;

import com.cloudant.client.api.model.DesignDocument.MapReduce;
import com.cloudant.client.api.views.ViewRequestBuilder;
import com.ibm.cloud.cloudant.v1.model.DesignDocumentViewsMapReduce;
import com.ibm.cloud.cloudant.v1.model.PostViewOptions;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
Expand All @@ -31,13 +32,17 @@ public class AttachmentOwnerRepository extends DatabaseRepositoryCloudantClient<

public AttachmentOwnerRepository(DatabaseConnectorCloudant db) {
super(db, Source.class);
Map<String, MapReduce> views = new HashMap<String, MapReduce>();
Map<String, DesignDocumentViewsMapReduce> views = new HashMap<>();
views.put("attachmentOwner", createMapReduce(ATTACHMENTOWNER_VIEW_NAME, null));
initStandardDesignDocument(views, db);
}

public List<Source> getOwnersByIds(Set<String> ids) {
ViewRequestBuilder viewQuery = getConnector().createQuery(Source.class, "attachmentOwner");
return queryViewForSource(buildRequest(viewQuery, ids));
public List<Source> getOwnersByIds(@NotNull Set<String> ids) {
PostViewOptions viewQuery = getConnector()
.getPostViewQueryBuilder(Source.class, "attachmentOwner")
.includeDocs(false)
.keys(ids.stream().map(r -> (Object)r).toList())
.build();
return queryViewForSource(viewQuery);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;

import com.cloudant.client.api.model.DesignDocument.MapReduce;
import com.cloudant.client.api.views.ViewRequestBuilder;
import com.ibm.cloud.cloudant.v1.model.DesignDocumentViewsMapReduce;
import com.ibm.cloud.cloudant.v1.model.PostViewOptions;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
Expand All @@ -32,19 +33,27 @@ public class AttachmentRepository extends DatabaseRepositoryCloudantClient<Attac

public AttachmentRepository(DatabaseConnectorCloudant db) {
super(db, Attachment.class);
Map<String, MapReduce> views = new HashMap<String, MapReduce>();
Map<String, DesignDocumentViewsMapReduce> views = new HashMap<>();
views.put("byid", createMapReduce(BYID_VIEW_NAME, null));
views.put("bysha1", createMapReduce(BYSHA1_VIEW_NAME, null));
initStandardDesignDocument(views, db);
}

public List<Attachment> getAttachmentsByIds(Set<String> ids) {
ViewRequestBuilder viewQuery = getConnector().createQuery(Attachment.class, "byid");
return queryViewForAttchmnt(buildRequest(viewQuery, ids));
public List<Attachment> getAttachmentsByIds(@NotNull Set<String> ids) {
PostViewOptions viewQuery = getConnector()
.getPostViewQueryBuilder(Attachment.class, "byid")
.includeDocs(false)
.keys(ids.stream().map(r -> (Object)r).toList())
.build();
return queryViewForAttachment(viewQuery);
}

public List<Attachment> getAttachmentsBySha1s(Set<String> sha1s) {
ViewRequestBuilder viewQuery = getConnector().createQuery(Attachment.class, "bysha1");
return queryViewForAttchmnt(buildRequest(viewQuery, sha1s));
public List<Attachment> getAttachmentsBySha1s(@NotNull Set<String> sha1s) {
PostViewOptions viewQuery = getConnector()
.getPostViewQueryBuilder(Attachment.class, "bysha1")
.includeDocs(false)
.keys(sha1s.stream().map(r -> (Object)r).toList())
.build();
return queryViewForAttachment(viewQuery);
}
}
Loading