Search in sources :

Example 6 with NamespaceProperties

use of org.apache.bookkeeper.stream.proto.NamespaceProperties in project bookkeeper by apache.

the class TableClientTest method testTableAPI.

@Test
public void testTableAPI() 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).build();
    StreamProperties streamProps = FutureUtils.result(adminClient.createStream(namespace, streamName, streamConf));
    assertEquals(streamName, streamProps.getStreamName());
    assertEquals(StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultBackendUri.toString()).build(), streamProps.getStreamConf());
    // 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());
        }
    }
    // 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());
    }
}
Also used : TxnResult(org.apache.bookkeeper.api.kv.result.TxnResult) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) ByteBuf(io.netty.buffer.ByteBuf) Endpoint(org.apache.bookkeeper.stream.proto.common.Endpoint) NamespaceProperties(org.apache.bookkeeper.stream.proto.NamespaceProperties) StreamConfiguration(org.apache.bookkeeper.stream.proto.StreamConfiguration) NamespaceConfiguration(org.apache.bookkeeper.stream.proto.NamespaceConfiguration) Test(org.junit.Test)

Aggregations

NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)6 NamespaceConfiguration (org.apache.bookkeeper.stream.proto.NamespaceConfiguration)4 Test (org.junit.Test)4 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)3 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)3 Endpoint (org.apache.bookkeeper.stream.proto.common.Endpoint)3 ByteBuf (io.netty.buffer.ByteBuf)2 ClientException (org.apache.bookkeeper.clients.exceptions.ClientException)2 NamespaceExistsException (org.apache.bookkeeper.clients.exceptions.NamespaceExistsException)2 KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)1 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)1 TxnResult (org.apache.bookkeeper.api.kv.result.TxnResult)1 StorageAdminClient (org.apache.bookkeeper.clients.admin.StorageAdminClient)1 StorageClientSettings (org.apache.bookkeeper.clients.config.StorageClientSettings)1 NamespaceNotFoundException (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException)1 StreamExistsException (org.apache.bookkeeper.clients.exceptions.StreamExistsException)1 StreamNotFoundException (org.apache.bookkeeper.clients.exceptions.StreamNotFoundException)1