Skip to content

Commit

Permalink
feat(cloudant): Migrating from ektorp to cloudant java client
Browse files Browse the repository at this point in the history
Signed-off-by: Smruti Prakash Sahoo <smruti.sahoo@siemens.com>
  • Loading branch information
smrutis1 committed Mar 17, 2021
1 parent ce69b3b commit ff7dbd4
Show file tree
Hide file tree
Showing 75 changed files with 1,217 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.eclipse.sw360.components.summary.DocumentSummary;
import org.eclipse.sw360.components.summary.SummaryType;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.thrift.users.User;

import java.util.Collection;
Expand All @@ -22,12 +24,12 @@
*
* @author cedric.bodet@tngtech.com
*/
public class SummaryAwareRepository<T> extends DatabaseRepository<T> {
public class SummaryAwareRepository<T> extends DatabaseRepositoryCloudantClient<T> {

protected final DocumentSummary<T> summary;

public SummaryAwareRepository(Class<T> type, DatabaseConnector databaseConnector, DocumentSummary<T> summary) {
super(type, databaseConnector);
public SummaryAwareRepository(Class<T> type, DatabaseConnectorCloudant databaseConnector, DocumentSummary<T> summary) {
super(databaseConnector, type);

this.summary = summary;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

package org.eclipse.sw360.datahandler.db;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import static org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet;

import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.Source;
Expand All @@ -20,16 +26,10 @@
import org.eclipse.sw360.datahandler.thrift.components.Component;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.ektorp.http.HttpClient;

import java.net.MalformedURLException;
import java.util.HashSet;
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.CommonUtils.nullToEmptySet;
import com.cloudant.client.api.CloudantClient;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

public abstract class AttachmentAwareDatabaseHandler {

Expand All @@ -39,7 +39,7 @@ protected AttachmentAwareDatabaseHandler(AttachmentDatabaseHandler attachmentDat
this.attachmentDatabaseHandler = attachmentDatabaseHandler;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@

package org.eclipse.sw360.datahandler.db;

import org.eclipse.sw360.datahandler.couchdb.DatabaseConnector;
import org.eclipse.sw360.datahandler.couchdb.DatabaseRepository;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.ektorp.DocumentOperationResult;
import org.ektorp.ViewQuery;
import org.ektorp.support.View;
import org.ektorp.support.Views;

import com.cloudant.client.api.model.Response;
import com.cloudant.client.api.views.Key;
import com.cloudant.client.api.views.ViewRequestBuilder;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -37,17 +39,15 @@
@View(name = "all", map = "function(doc) { if (doc.type == 'attachment') emit(null, doc._id) }"),
@View(name = "onlyRemotes", map = "function(doc) { if(doc.type == 'attachment' && doc.onlyRemote) { emit(null, doc) } }")
})
public class AttachmentContentRepository extends DatabaseRepository<AttachmentContent> {

public AttachmentContentRepository(DatabaseConnector db) {
super(AttachmentContent.class, db);
public class AttachmentContentRepository extends DatabaseRepositoryCloudantClient<AttachmentContent> {

initStandardDesignDocument();
public AttachmentContentRepository(DatabaseConnectorCloudant db) {
super(db, AttachmentContent.class);
}

public List<AttachmentContent> getOnlyRemoteAttachments() {
ViewQuery query = createQuery("onlyRemotes");
query.includeDocs(false);
ViewRequestBuilder query = getConnector().createQuery(AttachmentContent.class, "onlyRemotes");
query.newRequest(Key.Type.STRING, Object.class).includeDocs(false);
return queryView(query);
}

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

final List<DocumentOperationResult> documentOperationResults = deleteBulk(unusedAttachmentContents);
final List<Response> documentOperationResults = getConnector().deleteBulk(unusedAttachmentContents);
if (documentOperationResults.isEmpty()) {
requestSummary.setRequestStatus(RequestStatus.SUCCESS);
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
*/
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 org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.couchdb.AttachmentConnector;
import org.eclipse.sw360.datahandler.couchdb.DatabaseConnector;
Expand Down Expand Up @@ -46,7 +49,7 @@
* @author: alex.borodin@evosoft.com
*/
public class AttachmentDatabaseHandler {
private final DatabaseConnector db;
private final DatabaseConnectorCloudant db;
private final AttachmentContentRepository attachmentContentRepository;
private final AttachmentConnector attachmentConnector;
private final AttachmentUsageRepository attachmentUsageRepository;
Expand All @@ -56,25 +59,26 @@ public class AttachmentDatabaseHandler {

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

public AttachmentDatabaseHandler(Supplier<HttpClient> httpClient, String dbName, String attachmentDbName) throws MalformedURLException {
db = new DatabaseConnector(httpClient, attachmentDbName);
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));
attachmentContentRepository = new AttachmentContentRepository(db);
attachmentUsageRepository = new AttachmentUsageRepository(new DatabaseConnector(httpClient, dbName));
attachmentRepository = new AttachmentRepository(new DatabaseConnector(httpClient, dbName));
attachmentOwnerRepository = new AttachmentOwnerRepository(new DatabaseConnector(httpClient, dbName));
attachmentUsageRepository = new AttachmentUsageRepository(new DatabaseConnectorCloudant(httpClient, dbName));
attachmentRepository = new AttachmentRepository(new DatabaseConnectorCloudant(httpClient, dbName));
attachmentOwnerRepository = new AttachmentOwnerRepository(new DatabaseConnectorCloudant(httpClient, dbName));
}

public AttachmentConnector getAttachmentConnector(){
return attachmentConnector;
}

public AttachmentContent add(AttachmentContent attachmentContent){
attachmentContentRepository.add(attachmentContent);
String id = attachmentContentRepository.add(attachmentContent);
attachmentContent.setId(id);
return attachmentContent;
}
public List<AttachmentContent> makeAttachmentContents(List<AttachmentContent> attachmentContents) throws TException {
final List<DocumentOperationResult> documentOperationResults = attachmentContentRepository.executeBulk(attachmentContents);
final List<Response> documentOperationResults = attachmentContentRepository.executeBulk(attachmentContents);
if (!documentOperationResults.isEmpty())
log.error("Failed Attachment store results " + documentOperationResults);

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

public void makeAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws TException {
List<AttachmentUsage> sanitizedUsages = distinctAttachmentUsages(attachmentUsages);
List<DocumentOperationResult> results = attachmentUsageRepository.executeBulk(sanitizedUsages);
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be created: " + results);
}
List<Response> results = attachmentUsageRepository.executeBulk(sanitizedUsages);
/*
* if (results.isEmpty()) { throw new
* SW360Exception("Some of the usage documents could not be created: " +
* results); }
*/
}

@VisibleForTesting
Expand Down Expand Up @@ -185,7 +191,7 @@ public AttachmentUsage updateAttachmentUsage(AttachmentUsage attachmentUsage) {
}

public void updateAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws TException {
List<DocumentOperationResult> results = attachmentUsageRepository.executeBulk(attachmentUsages);
List<Response> results = attachmentUsageRepository.executeBulk(attachmentUsages);
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be updated: " + results);
}
Expand All @@ -196,9 +202,9 @@ public void deleteAttachmentUsage(AttachmentUsage attachmentUsage) {
}

public void deleteAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws SW360Exception {
List<DocumentOperationResult> results = attachmentUsageRepository.executeBulk(
attachmentUsages.stream().map(BulkDeleteDocument::of).collect(Collectors.toList()));
if (!results.isEmpty()) {
List<Response> results = attachmentUsageRepository.deleteIds(
attachmentUsages.stream().map(AttachmentUsage::getId).collect(Collectors.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 @@ -9,13 +9,19 @@
*/
package org.eclipse.sw360.datahandler.db;

import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.couchdb.DatabaseConnector;
import org.eclipse.sw360.datahandler.couchdb.DatabaseRepository;
import org.eclipse.sw360.datahandler.thrift.Source;
import org.ektorp.ViewQuery;
import org.ektorp.support.View;
import org.ektorp.support.Views;

import com.cloudant.client.api.views.Key;
import com.cloudant.client.api.views.UnpaginatedRequestBuilder;
import com.cloudant.client.api.views.ViewRequestBuilder;

import java.util.List;
import java.util.Set;

Expand All @@ -30,15 +36,15 @@
"emit(doc.attachments[i].attachmentContentId, source); } } }")
})

public class AttachmentOwnerRepository extends DatabaseRepository<Source> {
public class AttachmentOwnerRepository extends DatabaseRepositoryCloudantClient<Source> {

public AttachmentOwnerRepository(DatabaseConnector db) {
super(Source.class, db);
initStandardDesignDocument();
public AttachmentOwnerRepository(DatabaseConnectorCloudant db) {
super(db, Source.class);
}

public List<Source> getOwnersByIds(Set<String> ids) {
ViewQuery viewQuery = createQuery("attachmentOwner").includeDocs(false).keys(ids);
return queryView(viewQuery);
ViewRequestBuilder viewQuery = getConnector().createQuery(Source.class, "attachmentOwner");
UnpaginatedRequestBuilder req = viewQuery.newRequest(Key.Type.STRING, Object.class).includeDocs(false).keys((String[]) ids.toArray());
return queryView1(req);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
*/
package org.eclipse.sw360.datahandler.db;

import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.couchdb.DatabaseConnector;
import org.eclipse.sw360.datahandler.couchdb.DatabaseRepository;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.ektorp.ViewQuery;
import org.ektorp.support.View;
import org.ektorp.support.Views;

import com.cloudant.client.api.views.Key;
import com.cloudant.client.api.views.UnpaginatedRequestBuilder;
import com.cloudant.client.api.views.ViewRequestBuilder;

import java.util.List;
import java.util.Set;

Expand All @@ -30,20 +36,21 @@
"emit(doc.attachments[i].sha1, doc.attachments[i]); } } }")
})

public class AttachmentRepository extends DatabaseRepository<Attachment> {
public class AttachmentRepository extends DatabaseRepositoryCloudantClient<Attachment> {

public AttachmentRepository(DatabaseConnector db) {
super(Attachment.class, db);
initStandardDesignDocument();
public AttachmentRepository(DatabaseConnectorCloudant db) {
super(db, Attachment.class);
}

public List<Attachment> getAttachmentsByIds(Set<String> ids) {
ViewQuery viewQuery = createQuery("byid").includeDocs(false).keys(ids);
return queryView(viewQuery);
ViewRequestBuilder viewQuery = getConnector().createQuery(Attachment.class, "byid");
UnpaginatedRequestBuilder req = viewQuery.newRequest(Key.Type.STRING, Object.class).includeDocs(false).keys((String[]) ids.toArray());
return queryView1(req);
}

public List<Attachment> getAttachmentsBySha1s(Set<String> sha1s) {
ViewQuery viewQuery = createQuery("bysha1").includeDocs(false).keys(sha1s);
return queryView(viewQuery);
ViewRequestBuilder viewQuery = getConnector().createQuery(Attachment.class, "bysha1");
UnpaginatedRequestBuilder req = viewQuery.newRequest(Key.Type.STRING, Object.class).includeDocs(false).keys((String[]) sha1s.toArray());
return queryView1(req);
}
}
Loading

0 comments on commit ff7dbd4

Please sign in to comment.