public static String getStringContent(Object content) { if (content instanceof String) { return (String) content; } else if (content instanceof byte[]) { return new String((byte[]) content, StandardCharsets.UTF_8); } else if (content instanceof ByteBuf) { final ByteBuf byteBuf = (ByteBuf) content; byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); byteBuf.release(); return new String(bytes, StandardCharsets.UTF_8); } return content.toString(); }
private <V> Document toDocument(String docId, V value, Serializer<V> valueSerializer, long revision) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); valueSerializer.serialize(value, bos); final ByteBuf byteBuf = Unpooled.wrappedBuffer(bos.toByteArray()); return BinaryDocument.create(docId, byteBuf, revision); }
@Override public Iterable<TupleDocument> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { String key = inputRecord.get(keyField).toString(); GenericRecord data = (GenericRecord) inputRecord.get(dataRecordField); ByteBuffer dataBytes = (ByteBuffer) data.get(valueField); Integer flags = (Integer) data.get(flagsField); ByteBuf buffer = Unpooled.copiedBuffer(dataBytes); return new SingleRecordIterable<>(new TupleDocument(key, Tuple.create(buffer, flags))); } }
byteBuf.getBytes(byteBuf.readerIndex(), out, byteBuf.readableBytes()); byteBuf.release(); }; doc = document;
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 public void testBasicConvert() throws Exception { Schema dataRecordSchema = SchemaBuilder.record("Data") .fields() .name("data").type().bytesType().noDefault() .name("flags").type().intType().noDefault() .endRecord(); Schema schema = SchemaBuilder.record("TestRecord") .fields() .name("key").type().stringType().noDefault() .name("data").type(dataRecordSchema).noDefault() .endRecord(); GenericData.Record testRecord = new GenericData.Record(schema); String testContent = "hello world"; GenericData.Record dataRecord = new GenericData.Record(dataRecordSchema); dataRecord.put("data", ByteBuffer.wrap(testContent.getBytes(Charset.forName("UTF-8")))); dataRecord.put("flags", 0); testRecord.put("key", "hello"); testRecord.put("data", dataRecord); Converter<Schema, String, GenericRecord, TupleDocument> recordConverter = new AvroToCouchbaseTupleConverter(); TupleDocument returnDoc = recordConverter.convertRecord("", testRecord, null).iterator().next(); byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()]; returnDoc.content().value1().readBytes(returnedBytes); Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8"))); int returnedFlags = returnDoc.content().value2(); Assert.assertEquals(returnedFlags, 0); }
@Override public void onNext(D doc) { try { callbackFired.set(true); WriteResponse writeResponse = new GenericWriteResponse<D>(doc); writeResponseQueue.add(new Pair<WriteResponse, Throwable>(writeResponse, null)); callback.onSuccess(writeResponse); } finally { if (doc instanceof TupleDocument) { ((TupleDocument) doc).content().value1().release(); } } } });
@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
@Override public ByteBuf copy(int index, int length) { checkIndex(index, length); ByteBuf copy = alloc().directBuffer(length, maxCapacity()); copy.writeBytes(this, index, length); return copy; }
private void initBufWithSubpage(PooledByteBuf<T> buf, long handle, int bitmapIdx, int reqCapacity) { assert bitmapIdx != 0; int memoryMapIdx = memoryMapIdx(handle); PoolSubpage<T> subpage = subpages[subpageIdx(memoryMapIdx)]; assert subpage.doNotDestroy; assert reqCapacity <= subpage.elemSize; buf.init( this, handle, runOffset(memoryMapIdx) + (bitmapIdx & 0x3FFFFFFF) * subpage.elemSize + offset, reqCapacity, subpage.elemSize, arena.parent.threadCache()); }
inputStream = docId .map(key -> bucket.get(key, BinaryDocument.class)) .map(doc -> new ByteBufInputStream(doc.content())); break;
assertRecordWritable(record); if (record instanceof TupleDocument) { ((TupleDocument) record).content().value1().retain();
@Override byte[] allocateArray(int initialCapacity) { byte[] bytes = super.allocateArray(initialCapacity); ((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length); return bytes; }
@Override void freeArray(byte[] array) { int length = array.length; super.freeArray(array); ((UnpooledByteBufAllocator) alloc()).decrementHeap(length); } }
@Override void freeArray(byte[] array) { int length = array.length; super.freeArray(array); ((UnpooledByteBufAllocator) alloc()).decrementHeap(length); } }
@Override public Iterator<ByteBuf> iterator() { ensureAccessible(); if (components.isEmpty()) { return EMPTY_ITERATOR; } return new CompositeByteBufIterator(); }
@Override byte[] allocateArray(int initialCapacity) { byte[] bytes = super.allocateArray(initialCapacity); ((UnpooledByteBufAllocator) alloc()).incrementHeap(bytes.length); return bytes; }
private <V> V deserialize(BinaryDocument doc, Deserializer<V> valueDeserializer) throws IOException { if (doc == null) { return null; } final ByteBuf byteBuf = doc.content(); final byte[] bytes = new byte[byteBuf.readableBytes()]; byteBuf.readBytes(bytes); byteBuf.release(); return valueDeserializer.deserialize(bytes); }
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!"); } }
TupleDocument returnDoc = writer.getBucket().get("hello", TupleDocument.class); byte[] returnedBytes = new byte[returnDoc.content().value1().readableBytes()]; returnDoc.content().value1().readBytes(returnedBytes); Assert.assertEquals(returnedBytes, testContent.getBytes(Charset.forName("UTF-8")));