diff --git a/distro/changelog.xml b/distro/changelog.xml index 793bf08..2569b50 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,7 @@ + + Saves records in different bucket + Fixed distro directory diff --git a/pom.xml b/pom.xml index 08b050e..7907334 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.data.publishing document-store-lib-couchbase - 1.0.1-SNAPSHOT + 1.0.2-SNAPSHOT Document Store CouchBase Connector Document Store Connector for CouchBase diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceCouchBase.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceCouchBase.java index 95958a0..f67db17 100644 --- a/src/main/java/org/gcube/documentstore/persistence/PersistenceCouchBase.java +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceCouchBase.java @@ -4,6 +4,7 @@ package org.gcube.documentstore.persistence; import java.io.Serializable; +import java.util.HashMap; import java.util.Map; import org.gcube.documentstore.persistence.PersistenceBackend; @@ -20,6 +21,7 @@ import com.couchbase.client.java.document.JsonDocument; import com.couchbase.client.java.document.json.JsonObject; import com.couchbase.client.java.env.CouchbaseEnvironment; import com.couchbase.client.java.env.DefaultCouchbaseEnvironment; +import com.couchbase.client.java.query.N1qlQueryResult; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -32,6 +34,24 @@ public class PersistenceCouchBase extends PersistenceBackend { public static final String PASSWORD_PROPERTY_KEY = "password"; public static final String BUCKET_NAME_PROPERTY_KEY = "bucketName"; + /*Different bucket for aggregated*/ + public static final String BUCKET_STORAGE_NAME_PROPERTY_KEY="AggregatedStorageUsageRecord"; + public static final String BUCKET_STORAGE_TYPE="StorageUsageRecord"; + + public static final String BUCKET_SERVICE_NAME_PROPERTY_KEY="AggregatedServiceUsageRecord"; + public static final String BUCKET_SERVICE_TYPE="ServiceUsageRecord"; + + public static final String BUCKET_PORTLET_NAME_PROPERTY_KEY="AggregatedPortletUsageRecord"; + public static final String BUCKET_PORTLET_TYPE="PortletUsageRecord"; + + public static final String BUCKET_JOB_NAME_PROPERTY_KEY="AggregatedJobUsageRecord"; + public static final String BUCKET_JOB_TYPE="JobUsageRecord"; + + public static final String BUCKET_TASK_NAME_PROPERTY_KEY="AggregatedTaskUsageRecord"; + public static final String BUCKET_TASK_TYPE="TaskUsageRecord"; + + + /* The environment configuration */ protected static final CouchbaseEnvironment ENV = DefaultCouchbaseEnvironment.builder() @@ -40,8 +60,29 @@ public class PersistenceCouchBase extends PersistenceBackend { .build(); protected Cluster cluster; + + /* One Bucket for type*/ + protected Bucket bucketStorage; + protected String bucketNameStorage; + + protected Bucket bucketService; + protected String bucketNameService; + + protected Bucket bucketPortlet; + protected String bucketNamePortlet; + + protected Bucket bucketJob; + protected String bucketNameJob; + + protected Bucket bucketTask; + protected String bucketNameTask; + + /*For test */ protected Bucket bucket; + + private Map connectionMap; + /** * {@inheritDoc} */ @@ -52,17 +93,45 @@ public class PersistenceCouchBase extends PersistenceBackend { String password = configuration.getProperty(PASSWORD_PROPERTY_KEY); cluster = CouchbaseCluster.create(ENV, url); + + bucketNameStorage = configuration.getProperty(BUCKET_STORAGE_NAME_PROPERTY_KEY); + bucketNameService = configuration.getProperty(BUCKET_SERVICE_NAME_PROPERTY_KEY); + bucketNameJob = configuration.getProperty(BUCKET_JOB_NAME_PROPERTY_KEY); + bucketNamePortlet = configuration.getProperty(BUCKET_PORTLET_NAME_PROPERTY_KEY); + bucketNameTask = configuration.getProperty(BUCKET_TASK_NAME_PROPERTY_KEY); + + connectionMap = new HashMap(); + + bucketStorage = cluster.openBucket(bucketNameStorage, password); + connectionMap.put(BUCKET_STORAGE_TYPE, bucketStorage); + + bucketService = cluster.openBucket(bucketNameService, password); + connectionMap.put(BUCKET_SERVICE_TYPE, bucketService); + + bucketJob= cluster.openBucket(bucketNameJob, password); + connectionMap.put(BUCKET_JOB_TYPE, bucketJob); + + bucketPortlet= cluster.openBucket(bucketNamePortlet, password); + connectionMap.put(BUCKET_PORTLET_TYPE, bucketPortlet); + + bucketTask= cluster.openBucket(bucketNameTask, password); + connectionMap.put(BUCKET_TASK_TYPE, bucketTask); + bucket = cluster.openBucket( configuration.getProperty(BUCKET_NAME_PROPERTY_KEY), password); } - protected JsonDocument createItem(JsonObject jsonObject, String id) throws Exception { + protected JsonDocument createItem(JsonObject jsonObject, String id,String recordType) throws Exception { JsonDocument doc = JsonDocument.create(id, jsonObject); - return bucket.upsert(doc); + + return connectionMap.get(recordType).upsert(doc); + //return bucket.upsert(doc); } public static JsonNode usageRecordToJsonNode(Record record) throws Exception { ObjectMapper mapper = new ObjectMapper(); + + JsonNode node = mapper.valueToTree(record.getResourceProperties()); return node; } @@ -83,7 +152,11 @@ public class PersistenceCouchBase extends PersistenceBackend { protected void reallyAccount(Record record) throws Exception { JsonNode node = PersistenceCouchBase.usageRecordToJsonNode(record); JsonObject jsonObject = JsonObject.fromJson(node.toString()); - createItem(jsonObject, record.getId()); + + //get a bucket association + String recordType=record.getRecordType(); + + createItem(jsonObject, record.getId(),recordType); } /**