List of usage examples for io.netty.buffer ByteBufUtil getBytes
public static byte[] getBytes(ByteBuf buf)
From source file:org.apache.bookkeeper.stream.cli.commands.table.GetCommand.java
License:Apache License
@Override protected void run(StorageClient client, Flags flags) throws Exception { checkArgument(flags.arguments.size() >= 2, "table and key are not provided"); String tableName = flags.arguments.get(0); String key = flags.arguments.get(1); try (Table<ByteBuf, ByteBuf> table = result(client.openTable(tableName))) { long lastVersion = -1L; do {/*w w w .j av a2s . com*/ try (KeyValue<ByteBuf, ByteBuf> kv = result( table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))))) { if (null == kv) { spec.console().println("key '" + key + "' doesn't exist."); } else { if (kv.version() > lastVersion) { if (kv.isNumber()) { spec.console().println("value = " + kv.numberValue()); } else { spec.console() .println("value = " + new String(ByteBufUtil.getBytes(kv.value()), UTF_8)); } lastVersion = kv.version(); } } } if (flags.watch) { Thread.sleep(1000); } } while (flags.watch); } }
From source file:org.apache.bookkeeper.stream.storage.impl.kv.TableStoreUtils.java
License:Apache License
static byte[] newStoreKey(ByteString rKey, ByteString lKey) { boolean hasRkey = hasRKey(rKey); int keyLen;// w w w . jav a2 s. c om if (hasRkey) { keyLen = rKey.size() + lKey.size() + 2; } else { keyLen = lKey.size() + 1; } ByteBuf keyBuf = Unpooled.buffer(keyLen); if (hasRkey) { keyBuf.writeByte(HAS_ROUTING_KEY); keyBuf.writeBytes(rKey.asReadOnlyByteBuffer()); keyBuf.writeByte(SEP); keyBuf.writeBytes(lKey.asReadOnlyByteBuffer()); } else { keyBuf.writeByte(NO_ROUTING_KEY); keyBuf.writeBytes(lKey.asReadOnlyByteBuffer()); } return ByteBufUtil.getBytes(keyBuf); }
From source file:org.apache.bookkeeper.tests.integration.stream.TableClientSimpleTest.java
License:Apache License
@FlakyTest("https://github.com/apache/bookkeeper/issues/1440") public void testTableSimpleAPI() throws Exception { // Create a namespace NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder() .setDefaultStreamConf(DEFAULT_STREAM_CONF).build(); NamespaceProperties nsProps = result(adminClient.createNamespace(namespace, nsConf)); assertEquals(namespace, nsProps.getNamespaceName()); assertEquals(nsConf.getDefaultStreamConf(), nsProps.getDefaultStreamConf()); // Create a stream String streamName = testName.getMethodName() + "_stream"; StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF) .setStorageType(StorageType.TABLE).build(); StreamProperties streamProps = result(adminClient.createStream(namespace, streamName, streamConf)); assertEquals(streamName, streamProps.getStreamName()); // Open the table PTable<ByteBuf, ByteBuf> table = result(storageClient.openPTable(streamName)); byte[] rKey = "routing-key".getBytes(UTF_8); byte[] lKey = "testing-key".getBytes(UTF_8); byte[] value1 = "testing-value-1".getBytes(UTF_8); byte[] value2 = "testing-value-2".getBytes(UTF_8); // put first key ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey); ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey); ByteBuf valBuf1 = Unpooled.wrappedBuffer(value1); ByteBuf valBuf2 = Unpooled.wrappedBuffer(value2); // normal put assertNull(result(table.put(rKeyBuf, lKeyBuf, valBuf1))); // putIfAbsent failure assertArrayEquals(value1, ByteBufUtil.getBytes(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2)))); // delete failure assertFalse(result(table.delete(rKeyBuf, lKeyBuf, valBuf2))); // delete success assertTrue(result(table.delete(rKeyBuf, lKeyBuf, valBuf1))); // get// ww w. j ava 2 s.co m assertNull(result(table.get(rKeyBuf, lKeyBuf))); // putIfAbsent success assertNull(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2))); // get returns val2 assertArrayEquals(value2, ByteBufUtil.getBytes(result(table.get(rKeyBuf, lKeyBuf)))); // vPut failure try { result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 9999L)); fail("Should fail vPut if the version doesn't match"); } catch (KvApiException e) { assertEquals(Code.BAD_REVISION, e.getCode()); } // vPut success assertEquals(1L, result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 0L)).longValue()); // vDelete failure try { result(table.vDelete(rKeyBuf, lKeyBuf, 9999L)); fail("Should fail vDelete if the version doesn't match"); } catch (KvApiException e) { assertEquals(Code.BAD_REVISION, e.getCode()); } // vDelete success try (KeyValue<ByteBuf, ByteBuf> prevKv = result(table.vDelete(rKeyBuf, lKeyBuf, 1L))) { assertNotNull(prevKv); assertEquals(1L, prevKv.version()); assertArrayEquals(value1, ByteBufUtil.getBytes(prevKv.value())); } // write a range of key int numKvs = 100; rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8)); for (int i = 0; i < numKvs; i++) { lKeyBuf = getLKey(i); valBuf1 = getValue(i); result(table.put(rKeyBuf, lKeyBuf, valBuf1)); } // get ranges ByteBuf lStartKey = getLKey(20); ByteBuf lEndKey = getLKey(50); List<KeyValue<ByteBuf, ByteBuf>> kvs = result(table.range(rKeyBuf, lStartKey, lEndKey)); assertEquals(31, kvs.size()); int i = 20; for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) { assertEquals(getLKey(i), kvPair.key()); assertEquals(getValue(i), kvPair.value()); ++i; kvPair.close(); } assertEquals(51, i); // delete range kvs = result(table.deleteRange(rKeyBuf, lStartKey, lEndKey)); assertEquals(31, kvs.size()); i = 20; for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) { assertEquals(getLKey(i), kvPair.key()); assertEquals(getValue(i), kvPair.value()); ++i; kvPair.close(); } assertEquals(51, i); // get ranges again kvs = result(table.range(rKeyBuf, lStartKey, lEndKey)); assertTrue(kvs.isEmpty()); byte[] lIncrKey = "test-incr-lkey".getBytes(UTF_8); ByteBuf lIncrKeyBuf = Unpooled.wrappedBuffer(lIncrKey); // test increment for (int j = 0; j < 5; j++) { result(table.increment(rKeyBuf, lIncrKeyBuf, 100L)); long number = result(table.getNumber(rKeyBuf, lIncrKeyBuf)); assertEquals(100L * (j + 1), number); } for (int j = 5; j < 10; j++) { long number = result(table.incrementAndGet(rKeyBuf, lIncrKeyBuf, 100L)); assertEquals(100L * (j + 1), number); } }
From source file:org.apache.bookkeeper.tests.integration.stream.TableClientTest.java
License:Apache License
private void testTableAPI(String namespace, StorageAdminClient adminClient, StorageClient storageClient) throws Exception { // Create a namespace NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder() .setDefaultStreamConf(DEFAULT_STREAM_CONF).build(); NamespaceProperties nsProps = FutureUtils.result(adminClient.createNamespace(namespace, nsConf)); assertEquals(namespace, nsProps.getNamespaceName()); assertEquals(nsConf.getDefaultStreamConf(), nsProps.getDefaultStreamConf()); // Create a stream String streamName = testName.getMethodName() + "_stream"; StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF) .setStorageType(StorageType.TABLE).build(); StreamProperties streamProps = FutureUtils .result(adminClient.createStream(namespace, streamName, streamConf)); assertEquals(streamName, streamProps.getStreamName()); // Open the table PTable<ByteBuf, ByteBuf> table = FutureUtils.result(storageClient.openPTable(streamName)); byte[] rKey = "routing-key".getBytes(UTF_8); byte[] lKey = "testing-key".getBytes(UTF_8); byte[] value = "testing-value".getBytes(UTF_8); // put first key ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey); ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey); ByteBuf valBuf = Unpooled.wrappedBuffer(value); try (PutOption<ByteBuf> option = Options.putAndGet()) { try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils .result(table.put(rKeyBuf, lKeyBuf, valBuf, option))) { assertNull(putResult.prevKv()); }//from w w w . j ava 2s .co m } // put second key ByteBuf valBuf2 = Unpooled.wrappedBuffer("testing-value-2".getBytes(UTF_8)); try (PutOption<ByteBuf> option = Options.putAndGet()) { try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils .result(table.put(rKeyBuf, lKeyBuf, valBuf2, option))) { assertNotNull(putResult.prevKv()); KeyValue<ByteBuf, ByteBuf> prevKv = putResult.prevKv(); assertEquals("testing-key", new String(ByteBufUtil.getBytes(prevKv.key()), UTF_8)); assertEquals("testing-value", new String(ByteBufUtil.getBytes(prevKv.value()), UTF_8)); } } // get key try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) { try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils .result(table.get(rKeyBuf, lKeyBuf, option))) { assertEquals(1, getResult.count()); assertEquals(1, getResult.kvs().size()); KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0); assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8)); assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8)); } } // delete key try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).build()) { try (DeleteResult<ByteBuf, ByteBuf> deleteResult = FutureUtils .result(table.delete(rKeyBuf, lKeyBuf, option))) { assertEquals(1, deleteResult.numDeleted()); assertEquals(1, deleteResult.prevKvs().size()); KeyValue<ByteBuf, ByteBuf> kv = deleteResult.prevKvs().get(0); assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8)); assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8)); } } // write a range of key int numKvs = 100; rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8)); try (PutOption<ByteBuf> option = Options.blindPut()) { for (int i = 0; i < numKvs; i++) { lKeyBuf = getLKey(i); valBuf = getValue(i); FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf, option)); } } // get ranges ByteBuf lStartKey = getLKey(20); ByteBuf lEndKey = getLKey(50); try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().endKey(lEndKey).build()) { try (RangeResult<ByteBuf, ByteBuf> rangeResult = FutureUtils .result(table.get(rKeyBuf, lStartKey, option))) { assertEquals(31, rangeResult.kvs().size()); assertEquals(31, rangeResult.count()); int i = 20; for (KeyValue<ByteBuf, ByteBuf> kvPair : rangeResult.kvs()) { assertEquals(getLKey(i), kvPair.key()); assertEquals(getValue(i), kvPair.value()); ++i; } assertEquals(51, i); } } // delete range try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).endKey(lEndKey).build()) { try (DeleteResult<ByteBuf, ByteBuf> deleteRangeResult = FutureUtils .result(table.delete(rKeyBuf, lStartKey, option))) { assertEquals(31, deleteRangeResult.numDeleted()); assertEquals(31, deleteRangeResult.prevKvs().size()); int i = 20; for (KeyValue<ByteBuf, ByteBuf> kvPair : deleteRangeResult.prevKvs()) { assertEquals(getLKey(i), kvPair.key()); assertEquals(getValue(i), kvPair.value()); ++i; } assertEquals(51, i); } } // test txn byte[] lTxnKey = "txn-key".getBytes(UTF_8); ByteBuf lTxnKeyBuf = Unpooled.wrappedBuffer(lTxnKey); byte[] txnValue = "txn-value".getBytes(UTF_8); ByteBuf txnValueBuf = Unpooled.wrappedBuffer(txnValue); Txn<ByteBuf, ByteBuf> txn = table.txn(lTxnKeyBuf); CompletableFuture<TxnResult<ByteBuf, ByteBuf>> commitFuture = txn .If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))) .Then(table.opFactory().newPut(lTxnKeyBuf, txnValueBuf, table.opFactory().optionFactory().newPutOption().build())) .commit(); try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) { assertTrue(txnResult.isSuccess()); assertEquals(1, txnResult.results().size()); Result<ByteBuf, ByteBuf> opResult = txnResult.results().get(0); assertEquals(OpType.PUT, opResult.type()); } // get key try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) { try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils .result(table.get(lTxnKeyBuf, lTxnKeyBuf, option))) { assertEquals(1, getResult.count()); assertEquals(1, getResult.kvs().size()); KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0); assertEquals("txn-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8)); assertEquals("txn-value", new String(ByteBufUtil.getBytes(kv.value()), UTF_8)); } } txn = table.txn(lTxnKeyBuf); // txn failure commitFuture = txn .If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))) .Then(table.opFactory().newPut(lTxnKeyBuf, valBuf, table.opFactory().optionFactory().newPutOption().build())) .commit(); try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) { assertFalse(txnResult.isSuccess()); assertEquals(0, txnResult.results().size()); } }
From source file:org.apache.pulsar.client.impl.schema.ByteBufSchema.java
License:Apache License
@Override public byte[] encode(ByteBuf message) { if (message == null) { return null; }/* ww w . j a v a 2s . co m*/ return ByteBufUtil.getBytes(message); }
From source file:org.apache.pulsar.client.impl.schema.SchemaUtils.java
License:Apache License
public static Object toAvroObject(Object value) { if (value != null) { if (value instanceof ByteBuffer) { ByteBuffer bb = (ByteBuffer) value; byte[] bytes = new byte[bb.remaining()]; bb.duplicate().get(bytes);/* w w w . j av a2 s. co m*/ return bytes; } else if (value instanceof ByteBuf) { return ByteBufUtil.getBytes((ByteBuf) value); } else { return value; } } else { return null; } }
From source file:org.apache.pulsar.functions.worker.rest.api.ComponentImpl.java
License:Apache License
public FunctionState getFunctionState(final String tenant, final String namespace, final String functionName, final String key) { if (!isWorkerServiceAvailable()) { throwUnavailableException();// w w w. j ava 2 s .c o m } if (null == worker().getStateStoreAdminClient()) { throwStateStoreUnvailableResponse(); } // validate parameters try { validateGetFunctionStateParams(tenant, namespace, functionName, key); } catch (IllegalArgumentException e) { log.error("Invalid getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e); throw new RestException(Status.BAD_REQUEST, e.getMessage()); } String tableNs = StateUtils.getStateNamespace(tenant, namespace); String tableName = functionName; String stateStorageServiceUrl = worker().getWorkerConfig().getStateStorageServiceUrl(); if (storageClient.get() == null) { storageClient.compareAndSet(null, StorageClientBuilder .newBuilder().withSettings(StorageClientSettings.newBuilder() .serviceUri(stateStorageServiceUrl).clientName("functions-admin").build()) .withNamespace(tableNs).build()); } FunctionState value; try (Table<ByteBuf, ByteBuf> table = result(storageClient.get().openTable(tableName))) { try (KeyValue<ByteBuf, ByteBuf> kv = result(table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))))) { if (null == kv) { throw new RestException(Status.NOT_FOUND, "key '" + key + "' doesn't exist."); } else { if (kv.isNumber()) { value = new FunctionState(key, null, kv.numberValue(), kv.version()); } else { value = new FunctionState(key, new String(ByteBufUtil.getBytes(kv.value()), UTF_8), null, kv.version()); } } } } catch (Exception e) { log.error("Error while getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e); throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage()); } return value; }
From source file:org.apache.pulsar.io.kafka.connect.PulsarOffsetBackingStore.java
License:Apache License
@Override public Future<Void> set(Map<ByteBuffer, ByteBuffer> values, Callback<Void> callback) { values.forEach((key, value) -> {/*from w w w. ja va 2 s . c om*/ ByteBuf bb = Unpooled.wrappedBuffer(key); byte[] keyBytes = ByteBufUtil.getBytes(bb); bb = Unpooled.wrappedBuffer(value); byte[] valBytes = ByteBufUtil.getBytes(bb); producer.newMessage().key(new String(keyBytes, UTF_8)).value(valBytes).sendAsync(); }); return producer.flushAsync().whenComplete((ignored, cause) -> { if (null != callback) { callback.onCompletion(cause, ignored); } if (null == cause) { readToEnd(new CompletableFuture<>()); } }); }
From source file:org.apache.pulsar.io.kafka.connect.PulsarOffsetBackingStoreTest.java
License:Apache License
private void testGetSet(boolean testCallback) throws Exception { final int numKeys = 10; final List<ByteBuffer> keys = new ArrayList<>(); for (int i = 0; i < numKeys; i++) { Map<ByteBuffer, ByteBuffer> kvs = new HashMap<>(); ByteBuffer key = ByteBuffer.wrap(("test-key-" + i).getBytes(UTF_8)); keys.add(key);/* w ww. ja va 2 s. c o m*/ kvs.put(key, ByteBuffer.wrap(("test-val-" + i).getBytes(UTF_8))); CompletableFuture<Void> setCallback = new CompletableFuture<>(); offsetBackingStore.set(kvs, testCallback ? (Callback<Void>) (error, result) -> { if (null != error) { setCallback.completeExceptionally(error); } else { setCallback.complete(result); } } : null).get(); if (testCallback) { setCallback.join(); } } Map<ByteBuffer, ByteBuffer> result = offsetBackingStore.get(keys, null).get(); assertEquals(numKeys, result.size()); AtomicInteger count = new AtomicInteger(); new TreeMap<>(result).forEach((key, value) -> { int idx = count.getAndIncrement(); byte[] keyData = ByteBufUtil.getBytes(Unpooled.wrappedBuffer(key)); assertEquals(new String(keyData, UTF_8), "test-key-" + idx); byte[] valData = ByteBufUtil.getBytes(Unpooled.wrappedBuffer(value)); assertEquals(new String(valData, UTF_8), "test-val-" + idx); }); }
From source file:org.elasticsearch.http.netty4.Netty4HttpPipeliningHandlerTests.java
License:Apache License
private void assertReadHttpMessageHasContent(EmbeddedChannel embeddedChannel, String expectedContent) { FullHttpResponse response = (FullHttpResponse) embeddedChannel.outboundMessages().poll(); assertNotNull("Expected response to exist, maybe you did not wait long enough?", response); assertNotNull("Expected response to have content " + expectedContent, response.content()); String data = new String(ByteBufUtil.getBytes(response.content()), StandardCharsets.UTF_8); assertThat(data, is(expectedContent)); }