Skip to content

Commit

Permalink
fix(nouveau): extend nouveau connector as cloudant
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Mishra <gmishx@gmail.com>
  • Loading branch information
GMishx committed Sep 9, 2024
1 parent e77b1a8 commit c97502c
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ComponentSearchHandler {

public ComponentSearchHandler(Cloudant cClient, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(cClient, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class ModerationSearchHandler {

public ModerationSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ObligationElementSearchHandler {
public ObligationElementSearchHandler(Cloudant cClient, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(cClient, dbName);
// Creates the database connector and adds the lucene search view
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ObligationSearchHandler {
public ObligationSearchHandler(Cloudant cClient, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(cClient, dbName);
// Creates the database connector and adds the lucene search view
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class PackageSearchHandler {

public PackageSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class ProjectSearchHandler {

public ProjectSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ReleaseSearchHandler {

public ReleaseSearchHandler(Cloudant cClient, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(cClient, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class UserSearchHandler {
public UserSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
// Creates the database connector and adds the lucene search view
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class VendorSearchHandler {
public VendorSearchHandler(Cloudant client, String dbName) throws IOException {
// Creates the database connector and adds the lucene search view
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SpdxDocumentSearchHandler {

public SpdxDocumentSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SpdxDocumentCreationInfoSearchHandler {

public SpdxDocumentCreationInfoSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class SpdxPackageInfoSearchHandler {

public SpdxPackageInfoSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public AbstractDatabaseSearchHandler(String dbName) throws IOException {
Cloudant client = DatabaseSettings.getConfiguredClient();
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
// Create the database connector and add the search view to couchDB
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand All @@ -98,7 +98,7 @@ public AbstractDatabaseSearchHandler(String dbName) throws IOException {
public AbstractDatabaseSearchHandler(Cloudant client, String dbName) throws IOException {
DatabaseConnectorCloudant db = new DatabaseConnectorCloudant(client, dbName);
// Create the database connector and add the search view to couchDB
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME);
connector = new NouveauLuceneAwareDatabaseConnector(db, DDOC_NAME, dbName, db.getInstance().getGson());
Gson gson = db.getInstance().getGson();
NouveauDesignDocument searchView = new NouveauDesignDocument();
searchView.setId(DDOC_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.*;
import com.ibm.cloud.sdk.core.service.exception.ServiceResponseException;
Expand All @@ -21,12 +22,12 @@
import org.apache.thrift.TBase;
import org.apache.thrift.TFieldIdEnum;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.thrift.ThriftUtils;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
import org.jetbrains.annotations.NotNull;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -65,8 +66,7 @@ public String getDbName() {
public void update(Object document) {
DocumentResult resp;
if (document != null) {
if (ThriftUtils.isMapped(document.getClass())) {
AttachmentContent content = (AttachmentContent) document;
if (document instanceof AttachmentContent content) {
InputStream in = getAttachment(content.getId(), content.getFilename());
resp = this.updateWithResponse(document);
createAttachment(resp.getId(), content.getFilename(), in, content.getContentType());
Expand Down Expand Up @@ -533,7 +533,8 @@ public Document getDocumentFromPojo(Object document) {
}
Document doc = new Document();
Gson gson = this.instance.getGson();
doc.setProperties(gson.fromJson(gson.toJson(document), Map.class));
Type t = new TypeToken<Map<String, Object>>() {}.getType();
doc.setProperties(gson.fromJson(gson.toJson(document), t));
return doc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public class NouveauLuceneAwareDatabaseConnector extends LuceneAwareCouchDbConne
/**
* Constructor using a Database connector
*/
public NouveauLuceneAwareDatabaseConnector(@NotNull DatabaseConnectorCloudant db,
String ddoc) throws IOException {
super(db.getInstance().getClient(), ddoc);
public NouveauLuceneAwareDatabaseConnector(@NotNull DatabaseConnectorCloudant dbClient,
String ddoc, String db, Gson gson) throws IOException {
super(dbClient.getInstance().getClient(), ddoc, db, gson);
setResultLimit(DatabaseSettings.LUCENE_SEARCH_LIMIT);
this.connector = db;
this.connector = dbClient;
}

public boolean addDesignDoc(@NotNull NouveauDesignDocument designDocument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.eclipse.sw360.datahandler.common.CustomThriftDeserializer;
import org.eclipse.sw360.datahandler.common.CustomThriftSerializer;
import org.eclipse.sw360.datahandler.couchdb.deserializer.UsageDataDeserializer;
import org.eclipse.sw360.datahandler.thrift.attachments.*;
import org.eclipse.sw360.datahandler.thrift.changelogs.ChangeLogs;
Expand All @@ -40,6 +45,7 @@
import org.apache.thrift.TFieldIdEnum;
import com.ibm.cloud.cloudant.v1.model.Document;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -97,10 +103,6 @@ public class ThriftUtils {
UsageData.class, new UsageDataDeserializer()
);

private static final Map<Class<?>, Class<? extends DocumentWrapper<?>>> THRIFT_WRAPPED = ImmutableMap.of(
AttachmentContent.class, AttachmentContentWrapper.class
);

public static final ImmutableList<Component._Fields> IMMUTABLE_OF_COMPONENT = ImmutableList.of(
Component._Fields.CREATED_BY,
Component._Fields.CREATED_ON);
Expand All @@ -115,15 +117,13 @@ UsageData.class, new UsageDataDeserializer()
Release._Fields.CREATED_BY,
Release._Fields.CREATED_ON);

private static Gson gson;


private ThriftUtils() {
// Utility class with only static functions
}

public static boolean isMapped(Class<?> clazz) {
return THRIFT_WRAPPED.containsKey(clazz);
}

public static <T extends TBase<T, F>, F extends TFieldIdEnum> void copyField(T src, T dest, F field) {
if (src.isSet(field)) {
dest.setFieldValue(field, src.getFieldValue(field));
Expand All @@ -148,7 +148,16 @@ public static <S extends TBase<S, FS>, FS extends TFieldIdEnum, D extends TBase<
}

public static <T> Map<String, T> getIdMap(Collection<T> in) {
return Maps.uniqueIndex(in, Document::getId);
return Maps.uniqueIndex(in, value -> {
if (value instanceof Document doc) {
return doc.getId();
}
Document doc = new Document();
Gson gson = getGson();
Type t = new TypeToken<Map<String, Object>>() {}.getType();
doc.setProperties(gson.fromJson(gson.toJson(value), t));
return doc.getId();
});
}

public static <T extends TBase<T, F>, F extends TFieldIdEnum> Function<T, Object> extractField(final F field) {
Expand All @@ -170,4 +179,19 @@ public static <T extends TBase<T, F>, F extends TFieldIdEnum, R> Function<T, R>
}
};
}

private static Gson getGson() {
if (gson == null) {
GsonBuilder gsonBuilder = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping();
for (Class<?> c : THRIFT_CLASSES) {
gsonBuilder.registerTypeAdapter(c, new CustomThriftDeserializer());
gsonBuilder.registerTypeAdapter(c, new CustomThriftSerializer());
}
for (Class<?> c : THRIFT_NESTED_CLASSES) {
gsonBuilder.registerTypeAdapter(c, new CustomThriftSerializer());
}
gson = gsonBuilder.create();
}
return gson;
}
}
Loading

0 comments on commit c97502c

Please sign in to comment.