RawJsonDocument jsonDocument = RawJsonDocument.create(keyString, valueString); return new SingleRecordIterable<>(jsonDocument);
private void testBasicConvert(String keyField, boolean setConfig) throws Exception { String key = "hello"; String testContent = "hello world"; Map<String, String> content = new HashMap<>(); content.put(keyField, key); content.put("value", testContent); AnyToCouchbaseJsonConverter recordConverter = new AnyToCouchbaseJsonConverter(); WorkUnitState workUnitState = mock(WorkUnitState.class); if (setConfig) { when(workUnitState.getProp(AnyToCouchbaseJsonConverter.KEY_FIELD_CONFIG)).thenReturn(keyField); when(workUnitState.contains(AnyToCouchbaseJsonConverter.KEY_FIELD_CONFIG)).thenReturn(true); recordConverter.init(workUnitState); } else { recordConverter.init(workUnitState); } RawJsonDocument returnDoc = recordConverter.convertRecord("", content, null).iterator().next(); System.out.println(returnDoc.toString()); Assert.assertEquals(key.getBytes(), returnDoc.id().getBytes(), "key should be equal"); Map<String, String> convertedMap = GSON.fromJson(returnDoc.content(), Map.class); Assert.assertEquals(key, convertedMap.get(keyField), "key in content should be equal"); Assert.assertEquals(testContent, convertedMap.get("value"), "value in content should be equal"); Assert.assertEquals(2, convertedMap.keySet().size(), "should have 2 fields"); }
@Test public void testBasicConvertIntKey() throws Exception { int key = 5; String testContent = "hello world"; Record record = new Record(key, testContent); Converter<String, String, Object, RawJsonDocument> recordConverter = new AnyToCouchbaseJsonConverter(); RawJsonDocument returnDoc = recordConverter.convertRecord("", record, null).iterator().next(); Assert.assertEquals(key+"", returnDoc.id(), "key should be equal"); Record convertedRecord = GSON.fromJson(returnDoc.content(), Record.class); Assert.assertEquals(convertedRecord.key, key); Assert.assertEquals(convertedRecord.value, testContent, "value in content should be equal"); }
/** * 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()); }
/** * Performs the {@link #read(String, String, Set, Map)} operation via Key/Value ("get"). * * @param docId the document ID * @param fields the fields to be loaded * @param result the result map where the doc needs to be converted into * @return The result of the operation. */ private Status readKv(final String docId, final Set<String> fields, final Map<String, ByteIterator> result) throws Exception { RawJsonDocument loaded = bucket.get(docId, RawJsonDocument.class); if (loaded == null) { return Status.NOT_FOUND; } decode(loaded.content(), fields, result); return Status.OK; }
/** * 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 <T>Observable<T> findBySpatialView(SpatialViewQuery query, Class<T> entityClass) { return querySpatialView(query) .flatMap(spatialViewResult -> spatialViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query due to error:" + error.toString()))) .switchIfEmpty(spatialViewResult.rows())) .map(row -> { AsyncSpatialViewRow asyncSpatialViewRow = (AsyncSpatialViewRow) row; return asyncSpatialViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)) .toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query", throwable))); }
inputStream= docId .map(key -> bucket.get(key, RawJsonDocument.class)) .map(doc -> new ByteArrayInputStream(doc.content().getBytes(StandardCharsets.UTF_8))); break;
@Override public <T>Observable<T> findBySpatialView(SpatialViewQuery query, Class<T> entityClass) { return querySpatialView(query) .flatMap(spatialViewResult -> spatialViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query due to error:" + error.toString()))) .switchIfEmpty(spatialViewResult.rows())) .map(row -> { AsyncSpatialViewRow asyncSpatialViewRow = (AsyncSpatialViewRow) row; return asyncSpatialViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)) .toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query", throwable))); }
try { waitForMutationResponse(bucket.async().insert( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo
/** * 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(); } }
void verify(Bucket bucket) throws UnsupportedEncodingException { // verify System.out.println("Starting verification procedure"); for (Map.Entry<String, byte[]> cacheEntry : verificationCache.entrySet()) { Object doc = bucket.get(cacheEntry.getKey(), recordClass); if (doc instanceof TupleDocument) { ByteBuf returnedBuf = (((TupleDocument) doc).content()).value1(); byte[] returnedBytes = new byte[returnedBuf.readableBytes()]; returnedBuf.getBytes(0, returnedBytes); Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for TupleDoc should be equal"); } else if (doc instanceof RawJsonDocument) { byte[] returnedBytes = ((RawJsonDocument) doc).content().getBytes("UTF-8"); Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for JsonDoc should be equal"); } else { Assert.fail("Returned type was neither TupleDocument nor RawJsonDocument"); } } System.out.println("Verification success!"); } }
@Override public <T>Observable<T> findByView(ViewQuery query, Class<T> entityClass) { if (!query.isIncludeDocs() || !query.includeDocsTarget().equals(RawJsonDocument.class)) { if (query.isOrderRetained()) { query.includeDocsOrdered(RawJsonDocument.class); } else { query.includeDocs(RawJsonDocument.class); } } //we'll always map the document to the entity, hence reduce never makes sense. query.reduce(false); return queryView(query) .flatMap(asyncViewResult -> asyncViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query due to error:" + error.toString()))) .switchIfEmpty(asyncViewResult.rows())) .map(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return asyncViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)).toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query", throwable))); }
/** * 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; }
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()); } } }
void onWrite(AbstractDocument doc) throws UnsupportedEncodingException { recordClass = doc.getClass(); if (doc instanceof TupleDocument) { ByteBuf outgoingBuf = (((TupleDocument) doc).content()).value1(); byte[] outgoingBytes = new byte[outgoingBuf.readableBytes()]; outgoingBuf.getBytes(0, outgoingBytes); verificationCache.put(doc.id(), outgoingBytes); } else if (doc instanceof RawJsonDocument) { verificationCache.put(doc.id(), ((RawJsonDocument) doc).content().getBytes("UTF-8")); } else { throw new UnsupportedOperationException("Can only support TupleDocument or RawJsonDocument at this time"); } }
@Override public <T>Observable<T> findByView(ViewQuery query, Class<T> entityClass) { if (!query.isIncludeDocs() || !query.includeDocsTarget().equals(RawJsonDocument.class)) { if (query.isOrderRetained()) { query.includeDocsOrdered(RawJsonDocument.class); } else { query.includeDocs(RawJsonDocument.class); } } //we'll always map the document to the entity, hence reduce never makes sense. query.reduce(false); return queryView(query) .flatMap(asyncViewResult -> asyncViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query due to error:" + error.toString()))) .switchIfEmpty(asyncViewResult.rows())) .map(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return asyncViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)).toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query", throwable))); }