/** * 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; }
inputStream= docId .map(key -> bucket.get(key, RawJsonDocument.class)) .map(doc -> new ByteArrayInputStream(doc.content().getBytes(StandardCharsets.UTF_8))); break;
@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"); }
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"); }
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!"); } }
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"); } }
/** * 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(); } }
if (document != null) { outputStreamCallback = out -> { final byte[] content = document.content().getBytes(StandardCharsets.UTF_8); out.write(content); updatedAttrs.put(CoreAttributes.MIME_TYPE.key(), "application/json");
RawJsonDocument doc = bucket.get("myKey", RawJsonDocument.class); String jsonValue = doc.content(); MyClass value = unmarshalToMyClass(jsonValue); //this calls eg. Jackson
@Override public <K> Optional<Value> get(K key) throws NullPointerException { requireNonNull(key, "key is required"); RawJsonDocument jsonDocument = bucket.get(key.toString(), RawJsonDocument.class); if (Objects.isNull(jsonDocument)) { return Optional.empty(); } Object value = jsonDocument.content(); return Optional.of(ValueJSON.of(value.toString())); }
@Override protected Tuple2<ByteBuf, Integer> doEncode(RawJsonDocument document) throws Exception { return Tuple.create( TranscoderUtils.encodeStringAsUtf8(document.content()), TranscoderUtils.JSON_COMPAT_FLAGS ); }
/** * 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()); } } }