Search in sources :

Example 1 with DocumentAlreadyExistsException

use of com.couchbase.client.java.error.DocumentAlreadyExistsException in project sling by apache.

the class CouchbaseNoSqlAdapter method store.

@Override
public boolean store(NoSqlData data) {
    Bucket bucket = couchbaseClient.getBucket();
    String cacheKey = CouchbaseKey.build(data.getPath(), cacheKeyPrefix);
    JsonObject envelope = JsonObject.create();
    envelope.put(PN_PATH, data.getPath());
    envelope.put(PN_DATA, JsonObject.from(data.getProperties(MultiValueMode.LISTS)));
    // for list-children query efficiency store parent path as well
    String parentPath = ResourceUtil.getParent(data.getPath());
    if (parentPath != null) {
        envelope.put(PN_PARENT_PATH, parentPath);
    }
    JsonDocument doc = JsonDocument.create(cacheKey, envelope);
    try {
        bucket.insert(doc);
        // created
        return true;
    } catch (DocumentAlreadyExistsException ex) {
        bucket.upsert(doc);
        // updated
        return false;
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) DocumentAlreadyExistsException(com.couchbase.client.java.error.DocumentAlreadyExistsException) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Aggregations

Bucket (com.couchbase.client.java.Bucket)1 JsonDocument (com.couchbase.client.java.document.JsonDocument)1 JsonObject (com.couchbase.client.java.document.json.JsonObject)1 DocumentAlreadyExistsException (com.couchbase.client.java.error.DocumentAlreadyExistsException)1