List of usage examples for io.netty.buffer ByteBufUtil getBytes
public static byte[] getBytes(ByteBuf buf)
From source file:org.apache.bookkeeper.client.api.WriteHandleTest.java
License:Apache License
@Test public void testAppendBytes() throws Exception { byte[] testData = runtime.getMethodName().getBytes(UTF_8); handle.append(testData);//from w w w . j a v a 2s . c o m ByteBuf buffer = entryQueue.take(); byte[] bufferData = ByteBufUtil.getBytes(buffer); assertArrayEquals(testData, bufferData); verify(handle, times(1)).append(any(ByteBuf.class)); }
From source file:org.apache.bookkeeper.client.api.WriteHandleTest.java
License:Apache License
@Test public void testAppendBytes2() throws Exception { byte[] testData = runtime.getMethodName().getBytes(UTF_8); handle.append(testData, 1, testData.length / 2); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); ByteBuf buffer = entryQueue.take();// ww w . j a v a2 s . c om byte[] bufferData = ByteBufUtil.getBytes(buffer); assertArrayEquals(expectedData, bufferData); verify(handle, times(1)).append(any(ByteBuf.class)); }
From source file:org.apache.bookkeeper.client.api.WriteHandleTest.java
License:Apache License
@Test public void testAppendByteBuffer() throws Exception { byte[] testData = runtime.getMethodName().getBytes(UTF_8); handle.append(ByteBuffer.wrap(testData, 1, testData.length / 2)); byte[] expectedData = new byte[testData.length / 2]; System.arraycopy(testData, 1, expectedData, 0, testData.length / 2); ByteBuf buffer = entryQueue.take();/*from w ww . j a v a 2s . c o m*/ byte[] bufferData = ByteBufUtil.getBytes(buffer); assertArrayEquals(expectedData, bufferData); verify(handle, times(1)).append(any(ByteBuf.class)); }
From source file:org.apache.bookkeeper.clients.impl.kv.TestPByteBufTableImpl.java
License:Apache License
@SuppressWarnings("unchecked") @Test/* w w w.j av a 2 s . com*/ public void testBasicOperations() throws Exception { when(mockMetaRangeClient.getActiveDataRanges()).thenReturn(FutureUtils.value(streamRanges1)); ConcurrentMap<Long, PTable<ByteBuf, ByteBuf>> tableRanges = Maps.newConcurrentMap(); for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { tableRanges.put(rangeProps.getRangeId(), mock(PTable.class)); } RangeRouter<ByteBuf> mockRouter = mock(RangeRouter.class); when(mockRouter.getRange(any(ByteBuf.class))).thenAnswer(invocationOnMock -> { ByteBuf key = invocationOnMock.getArgument(0); byte[] keyData = ByteBufUtil.getBytes(key); return Bytes.toLong(keyData, 0); }); TableRangeFactory<ByteBuf, ByteBuf> trFactory = (streamProps1, rangeProps, executor, opFactory, resultFactory, kvFactory) -> tableRanges.get(rangeProps.getRangeId()); PByteBufTableImpl table = new PByteBufTableImpl(runtime.getMethodName(), streamProps, mockClientManager, scheduler.chooseThread(), trFactory, Optional.of(mockRouter)); assertEquals(0, table.getTableRanges().size()); verify(mockRouter, times(0)).setRanges(any(HashStreamRanges.class)); // initialize the table assertTrue(table == FutureUtils.result(table.initialize())); verify(mockRouter, times(1)).setRanges(eq(streamRanges1)); assertEquals(4, table.getTableRanges().size()); // test get for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) { table.get(pkey, lkey, option); verify(tableRanges.get(rangeProps.getRangeId()), times(1)).get(eq(pkey), eq(lkey), eq(option)); } } // test put for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); ByteBuf value = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); try (PutOption<ByteBuf> option = optionFactory.newPutOption().build()) { table.put(pkey, lkey, value, option); verify(tableRanges.get(rangeProps.getRangeId()), times(1)).put(eq(pkey), eq(lkey), eq(value), eq(option)); } } // test increment for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); long amount = 100L; try (IncrementOption<ByteBuf> option = optionFactory.newIncrementOption().build()) { table.increment(pkey, lkey, amount, option); verify(tableRanges.get(rangeProps.getRangeId()), times(1)).increment(eq(pkey), eq(lkey), eq(amount), same(option)); } } // test delete for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); ByteBuf lkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().build()) { table.delete(pkey, lkey, option); verify(tableRanges.get(rangeProps.getRangeId()), times(1)).delete(eq(pkey), eq(lkey), eq(option)); } } // test txn for (RangeProperties rangeProps : streamRanges1.getRanges().values()) { ByteBuf pkey = Unpooled.wrappedBuffer(Bytes.toBytes(rangeProps.getRangeId())); Txn<ByteBuf, ByteBuf> txn = table.txn(pkey); verify(tableRanges.get(rangeProps.getRangeId()), times(1)).txn(eq(pkey)); } }
From source file:org.apache.bookkeeper.common.coder.ByteArrayCoder.java
License:Apache License
@Override public byte[] decode(ByteBuf data) { return ByteBufUtil.getBytes(data); }
From source file:org.apache.bookkeeper.common.coder.StringUtf8Coder.java
License:Apache License
@Override public String decode(ByteBuf data) { byte[] bytes = ByteBufUtil.getBytes(data); return decode(bytes); }
From source file:org.apache.bookkeeper.statelib.impl.kv.KVCommandProcessor.java
License:Apache License
private void applyPutCommand(long revision, Command command, RocksdbKVStore<byte[], byte[]> store) { PutRequest putReq = command.getPutReq(); byte[] keyBytes = putReq.getKey().toByteArray(); byte[] valBytes = putReq.getValue().toByteArray(); @Cleanup("release") ByteBuf serializedValBuf = KVUtils.serialize(valBytes, revision); byte[] serializedValBytes = ByteBufUtil.getBytes(serializedValBuf); store.put(keyBytes, serializedValBytes, revision); }
From source file:org.apache.bookkeeper.statelib.impl.kv.KVCommandProcessor.java
License:Apache License
private void applyPutIfAbsentCommand(long revision, Command command, RocksdbKVStore<byte[], byte[]> store) { PutRequest putReq = command.getPutReq(); byte[] keyBytes = putReq.getKey().toByteArray(); byte[] valBytes = putReq.getValue().toByteArray(); @Cleanup("release") ByteBuf serializedValBuf = KVUtils.serialize(valBytes, revision); byte[] serializedValBytes = ByteBufUtil.getBytes(serializedValBuf); store.putIfAbsent(keyBytes, serializedValBytes, revision); }
From source file:org.apache.bookkeeper.statelib.impl.kv.RocksdbKVAsyncStore.java
License:Apache License
@Override public CompletableFuture<Void> put(K key, V value) { synchronized (this) { if (!isInitialized) { return uninitializedException(); }//from w w w .jav a 2 s. com } byte[] keyBytes = keyCoder.encode(key); byte[] valBytes = valCoder.encode(value); Command command = Command.newBuilder().setPutReq(KVUtils.newPutRequest(keyBytes, valBytes)).build(); return writeCommandReturnTxId(command).thenApplyAsync((revision) -> { ByteBuf serializedBuf = KVUtils.serialize(valBytes, revision); try { byte[] serializedBytes = ByteBufUtil.getBytes(serializedBuf); localStore.put(keyBytes, serializedBytes, revision); } finally { serializedBuf.release(); } return null; }, writeIOScheduler); }
From source file:org.apache.bookkeeper.statelib.impl.kv.RocksdbKVAsyncStore.java
License:Apache License
@Override public CompletableFuture<V> putIfAbsent(K key, V value) { byte[] keyBytes = keyCoder.encode(key); byte[] valBytes = valCoder.encode(value); Command command = Command.newBuilder().setPutIfAbsentReq(KVUtils.newPutIfAbsentRequest(keyBytes, valBytes)) .build();//from ww w . ja va2 s.c om return writeCommandReturnTxId(command).thenApplyAsync((revision) -> { ByteBuf serializedBuf = KVUtils.serialize(valBytes, revision); try { byte[] serializedBytes = ByteBufUtil.getBytes(serializedBuf); byte[] prevValue = localStore.putIfAbsent(keyBytes, serializedBytes, revision); if (null == prevValue) { return null; } else { return KVUtils.deserialize(valCoder, Unpooled.wrappedBuffer(prevValue)); } } finally { serializedBuf.release(); } }, writeIOScheduler); }