@Override public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException { final String docId = toDocumentId(key, keySerializer); final Document doc = toDocument(docId, value, valueSerializer); try { bucket.insert(doc); return true; } catch (DocumentAlreadyExistsException e) { return false; } }
@Override public <K, V> boolean replace(AtomicCacheEntry<K, V, Long> entry, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException { final Long revision = entry.getRevision().orElse(0L); final String docId = toDocumentId(entry.getKey(), keySerializer); final Document doc = toDocument(docId, entry.getValue(), valueSerializer, revision); try { if (revision < 0) { // If the document does not exist yet, try to create one. try { bucket.insert(doc); return true; } catch (DocumentAlreadyExistsException e) { return false; } } bucket.replace(doc); return true; } catch (DocumentDoesNotExistException|CASMismatchException e) { return false; } }
JsonObject content = JsonObject.empty().put("name", "Michael"); JsonDocument doc = JsonDocument.create("docId", content); JsonDocument inserted = bucket.insert(doc);
/** * Inserts a {@link JsonDocument} if it does not exist already. * * @param documentId the unique ID of the document * @param json the JSON String representing the document to store * @return the newly created {@link JsonDocument} * @see Bucket#insert(Document) */ public JsonDocument insert(String documentId, String json) { return this.bucket.insert(JsonDocument.create(documentId, JsonObject.fromJson(json))); }
@Override protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders) { JsonDocument doc = handler.getDocumentFromEntity(entityMetadata, entity, kunderaMetadata); if (!isUpdate) { bucket.insert(doc); LOGGER.debug("Inserted document with ID : " + doc.id() + " in the " + bucket.name() + " Bucket"); } else { bucket.upsert(doc); LOGGER.debug("Updated document with ID : " + doc.id() + " in the " + bucket.name() + " Bucket"); } }
@Test public void testRemove() { try { //@eclipse-formatter:off couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty())); new Couchbase().remove(HOST, BUCKET_NAME, "testRemove"); assertFalse(couchbaseBucket.exists("testRemove")); //@eclipse-formatter:on } catch (Exception e) { e.printStackTrace(); fail(); } }
public static boolean fillDB(CouchbaseCluster cluster) { Bucket couchbaseBucket = cluster.openBucket(BUCKET_NAME); couchbaseBucket.insert(JsonDocument.create("artist:vincent_van_gogh", VINCENT_VAN_GOGH)); N1qlQueryResult queryResult = couchbaseBucket.query(N1qlQuery.simple(String.format(QUERY, BUCKET_NAME), N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS))); couchbaseBucket.close(); return queryResult.info().resultCount() == 1; }
@Test public void testRemoveViaCall() { couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty())); testCall(graphDB, "CALL apoc.couchbase.remove({host}, {bucket}, 'testRemove')", map("host", HOST, "bucket", BUCKET_NAME), r -> assertFalse(couchbaseBucket.exists("testRemove"))); }
@Test public void testRemoveViaCall() { couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty())); testCall(graphDB, "CALL apoc.couchbase.remove({host}, {bucket}, 'testRemove')", map("host", HOST, "bucket", BUCKET_NAME + ":" + PASSWORD), r -> assertFalse(couchbaseBucket.exists("testRemove"))); }
/** * Create a new {@link CouchbaseArraySet}, backed by the document identified by <code>id</code> * in the given Couchbase <code>bucket</code>. Note that if the document already exists, * its content will be used as initial content for this collection. Otherwise it is created empty. * * @param id the id of the Couchbase document to back the set. * @param bucket the {@link Bucket} through which to interact with the document. */ public CouchbaseArraySet(String id, Bucket bucket) { this.id = id; this.bucket = bucket; try { this.bucket.insert(JsonArrayDocument.create(id, JsonArray.empty())); } catch (DocumentAlreadyExistsException e) { //use a pre-existing document } }
/** * Create a new {@link Bucket Couchbase-backed} List, backed by the document identified by <code>id</code> * in <code>bucket</code>. Note that if the document already exists, its content will be used as initial * content for this collection. Otherwise it is created empty. * * @param id the id of the Couchbase document to back the list. * @param bucket the {@link Bucket} through which to interact with the document. */ public CouchbaseArrayList(String id, Bucket bucket) { this.bucket = bucket; this.id = id; try { bucket.insert(JsonArrayDocument.create(id, JsonArray.empty())); } catch (DocumentAlreadyExistsException ex) { // Ignore concurrent creations, keep on moving. } }
/** * Create a new {@link Bucket Couchbase-backed} Queue, backed by the document identified by <code>id</code> * in <code>bucket</code>. Note that if the document already exists, its content will be used as initial * content for this collection. Otherwise it is created empty. * * @param id the id of the Couchbase document to back the queue. * @param bucket the {@link Bucket} through which to interact with the document. */ public CouchbaseQueue(String id, Bucket bucket) { this.bucket = bucket; this.id = id; try { bucket.insert(JsonArrayDocument.create(id, JsonArray.empty())); } catch (DocumentAlreadyExistsException ex) { // Ignore concurrent creations, keep on moving. } }
/** * Create a new {@link CouchbaseMap}, backed by the document identified by <code>id</code> * in the given Couchbase <code>bucket</code>. Note that if the document already exists, * its content will be used as initial content for this collection. Otherwise it is created empty. * * @param id the id of the Couchbase document to back the map. * @param bucket the {@link Bucket} through which to interact with the document. */ public CouchbaseMap(String id, Bucket bucket) { this.id = id; this.bucket = bucket; try { bucket.insert(JsonDocument.create(id, JsonObject.empty())); } catch (DocumentAlreadyExistsException ex) { // Ignore concurrent creations, keep on moving. } }
@Override public boolean insertRecord(LockConfiguration lockConfiguration) { try { JsonObject content = JsonObject.empty(); content.put(LOCK_NAME, lockConfiguration.getName()); content.put(LOCK_UNTIL, toIsoString(lockConfiguration.getLockAtMostUntil())); content.put(LOCKED_AT, toIsoString(Instant.now())); content.put(LOCKED_BY, getHostname()); JsonDocument document = JsonDocument.create(lockConfiguration.getName(), content); bucket.insert(document); return true; } catch (DocumentAlreadyExistsException e) { return false; } }