RawJsonDocument jsonDocument = RawJsonDocument.create(keyString, valueString); return new SingleRecordIterable<>(jsonDocument);
try { waitForMutationResponse(bucket.async().insert( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo
/** * Performs the {@link #upsert(String, String, Map)} operation via Key/Value ("upsert"). * * If this option should be used, the "-p couchbase.upsert=true" property must be set. * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status upsertKv(final String docId, final Map<String, ByteIterator> values) { waitForMutationResponse(bucket.async().upsert( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo )); return Status.OK; }
/** * Performs the {@link #update(String, String, Map)} operation via Key/Value ("replace"). * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status updateKv(final String docId, final Map<String, ByteIterator> values) { waitForMutationResponse(bucket.async().replace( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo )); return Status.OK; }
/** * Test that a single Json document can be written successfully * @throws IOException * @throws DataConversionException * @throws ExecutionException * @throws InterruptedException */ @Test(groups={"timeout"}) public void testJsonDocumentWrite() throws IOException, DataConversionException, ExecutionException, InterruptedException { CouchbaseWriter writer = new CouchbaseWriter(_couchbaseEnvironment, ConfigFactory.empty()); try { String key = "hello"; String testContent = "hello world"; HashMap<String, String> contentMap = new HashMap<>(); contentMap.put("value", testContent); Gson gson = new Gson(); String jsonString = gson.toJson(contentMap); RawJsonDocument jsonDocument = RawJsonDocument.create(key, jsonString); writer.write(jsonDocument, null).get(); RawJsonDocument returnDoc = writer.getBucket().get(key, RawJsonDocument.class); Map<String, String> returnedMap = gson.fromJson(returnDoc.content(), Map.class); Assert.assertEquals(testContent, returnedMap.get("value")); } finally { writer.close(); } }
switch (documentType) { case Json: { doc = RawJsonDocument.create(docId, new String(content, StandardCharsets.UTF_8)); break;
@Test public void shouldInsertDocument() { RawJsonDocument expected = RawJsonDocument.create(ID, DOCUMENT); getBucket().upsert(expected); RawJsonDocument result = getBucket().get(ID, RawJsonDocument.class); Assert.assertEquals(expected.content(), result.content()); }
@Override public RawJsonDocument newDocument(String id, int expiry, String content, long cas, MutationToken mutationToken) { return RawJsonDocument.create(id, expiry, content, cas, mutationToken); }
@Override public RawJsonDocument newDocument(String id, int expiry, String content, long cas) { return RawJsonDocument.create(id, expiry, content, cas); }
public void upsert(String id, String content) { bucket.upsert(RawJsonDocument.create(id, content)); }
@Override public <K, V> void put(K key, V value) { requireNonNull(key, "key is required"); requireNonNull(value, "value is required"); if (JsonValue.checkType(value)) { bucket.upsert(RawJsonDocument.create(key.toString(), JSONB.toJson(value.toString()))); } else { bucket.upsert(JsonDocument.create(key.toString(), JsonObjectCouchbaseUtil.toJson(JSONB, value))); } }
private RawJsonDocument encodeAndWrap(final CouchbaseDocument source, Long version) { String encodedContent = translationService.encode(source); if (version == null) { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent); } else { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent, version); } }
private RawJsonDocument encodeAndWrap(final CouchbaseDocument source, Long version) { String encodedContent = translationService.encode(source); if (version == null) { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent); } else { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent, version); } }
/** * Encode a {@link CouchbaseDocument} into a storable representation (JSON) then prepare * it for storage as a {@link Document}. */ private Document<String> encodeAndWrap(final CouchbaseDocument source, Long version) { String encodedContent = translationService.encode(source); if (version == null) { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent); } else { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent, version); } }
/** * Encode a {@link CouchbaseDocument} into a storable representation (JSON) then prepare * it for storage as a {@link Document}. */ private Document<String> encodeAndWrap(final CouchbaseDocument source, Long version) { String encodedContent = translationService.encode(source); if (version == null) { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent); } else { return RawJsonDocument.create(source.getId(), source.getExpiration(), encodedContent, version); } }
/** * Creates a copy from a different {@link RawJsonDocument}, but changes the document ID and content. * * @param doc the original {@link RawJsonDocument} to copy. * @param id the per-bucket unique document id. * @param content the content of the document. * @return a copied {@link RawJsonDocument} with the changed properties. */ public static RawJsonDocument from(RawJsonDocument doc, String id, String content) { return RawJsonDocument.create(id, doc.expiry(), content, doc.cas(), doc.mutationToken()); }
@Override public <K> void put(KeyValueEntity<K> entity, Duration ttl) { requireNonNull(entity, "entity is required"); requireNonNull(ttl, "ttl is required"); if (JsonValue.checkType(entity.get())) { RawJsonDocument jsonDocument = RawJsonDocument.create(entity.getKey().toString(), (int) ttl.getSeconds(), JSONB.toJson(entity.get().toString())); bucket.upsert(jsonDocument); } else { JsonObject jsonObject = JsonObjectCouchbaseUtil.toJson(JSONB, entity.get()); JsonDocument jsonDocument = JsonDocument.create(entity.getKey().toString(), (int) ttl.getSeconds(), jsonObject); bucket.upsert(jsonDocument); } }
private void createDocument(String documentId, Map<String, Object> content) throws JsonProcessingException { if (xattrs) { try { upsertXattrs(documentId, content); } catch (DocumentDoesNotExistException e) { bucket.upsert(ByteArrayDocument.create(documentId, EMPTY_BYTE_ARRAY)); upsertXattrs(documentId, content); } } else { final String json = mapper.writeValueAsString(content); bucket.upsert(RawJsonDocument.create(documentId, json)); } }
/** * Creates a copy from a different {@link RawJsonDocument}, but changes the CAS value. * * @param doc the original {@link RawJsonDocument} to copy. * @param cas the CAS (compare and swap) value for optimistic concurrency. * @return a copied {@link RawJsonDocument} with the changed properties. */ public static RawJsonDocument from(RawJsonDocument doc, long cas) { return RawJsonDocument.create(doc.id(), doc.expiry(), doc.content(), cas, doc.mutationToken()); }
private JsonNode readDocument(String documentId) throws IOException { if (xattrs) { try { final DocumentFragment<Lookup> lookup = bucket.lookupIn(documentId) .get(XATTR_NAME, new SubdocOptionsBuilder().xattr(true)) .execute(); final JsonObject content = (JsonObject) lookup.content(0); return mapper.readTree(content.toString()); } catch (Exception e) { return null; } } else { RawJsonDocument doc = bucket.get(RawJsonDocument.create(documentId)); if (doc == null) { return null; } return mapper.readTree(doc.content()); } } }