saves records in different bucket (by record type)

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-couchbase@128792 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Alessandro Pieve 2016-05-24 12:46:10 +00:00
parent 7291916f8d
commit b0116fa75c
3 changed files with 80 additions and 4 deletions

View File

@ -1,4 +1,7 @@
<ReleaseNotes>
<Changeset component="org.gcube.accounting.accounting-lib-persistence-couchbase.1-0-2" date="2016-04-08">
<Change>Saves records in different bucket</Change>
</Changeset>
<Changeset component="org.gcube.accounting.accounting-lib-persistence-couchbase.1-0-1" date="2016-04-08">
<Change>Fixed distro directory</Change>
</Changeset>

View File

@ -8,7 +8,7 @@
</parent>
<groupId>org.gcube.data.publishing</groupId>
<artifactId>document-store-lib-couchbase</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
<name>Document Store CouchBase Connector</name>
<description>Document Store Connector for CouchBase</description>

View File

@ -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 <String, Bucket> 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<String, Bucket>();
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);
}
/**