Search in sources :

Example 6 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class TableWriteView method putIfAbsent.

default CompletableFuture<V> putIfAbsent(K pKey, K lKey, V value) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareValue(CompareResult.EQUAL, lKey, null)).Then(opFactory().newPut(lKey, value, opFactory().optionFactory().newPutOption().build())).Else(newGet(lKey));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                return FutureUtils.value(null);
            } else {
                RangeResult<K, V> rangeResult = (RangeResult<K, V>) result.results().get(0);
                if (rangeResult.kvs().isEmpty()) {
                    return FutureUtils.exception(new KvApiException(Code.UNEXPECTED, "Key " + lKey + " not found when putIfAbsent failed"));
                } else {
                    return FutureUtils.value(retain(rangeResult.kvs().get(0).value()));
                }
            }
        } finally {
            result.close();
        }
    });
}
Also used : RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException)

Example 7 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class TableWriteView method vPut.

default CompletableFuture<Long> vPut(K pKey, K lKey, V value, long expectedVersion) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareVersion(CompareResult.EQUAL, lKey, expectedVersion)).Then(opFactory().newPut(lKey, value, opFactory().optionFactory().newPutOption().build()));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                return FutureUtils.value(expectedVersion + 1);
            } else {
                return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to vPut(" + lKey + ", " + value + ")@version=" + expectedVersion));
            }
        } finally {
            result.close();
        }
    });
}
Also used : KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException)

Example 8 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class TableWriteView method vDelete.

default CompletableFuture<KeyValue<K, V>> vDelete(K key, long expectedVersion) {
    Txn<K, V> txn = txn(key);
    txn.If(opFactory().compareVersion(CompareResult.EQUAL, key, expectedVersion)).Then(opFactory().newDelete(key, Options.deleteAndGet()));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) result.results().get(0);
                if (deleteResult.prevKvs().isEmpty()) {
                    return FutureUtils.value(null);
                } else {
                    List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
                    return FutureUtils.value(prevKvs.get(0));
                }
            } else {
                return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to vDelete key " + key + " (version = " + expectedVersion + ")"));
            }
        } finally {
            result.close();
        }
    });
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult)

Example 9 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class TableClientSimpleTest method testTableSimpleAPI.

@Test
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).build();
    StreamProperties streamProps = 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 = 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
    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);
    }
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) StreamProperties(org.apache.bookkeeper.stream.proto.StreamProperties) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException) 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

KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)9 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)5 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)4 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)2 ByteBuf (io.netty.buffer.ByteBuf)1 NamespaceConfiguration (org.apache.bookkeeper.stream.proto.NamespaceConfiguration)1 NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)1 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)1 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)1 Endpoint (org.apache.bookkeeper.stream.proto.common.Endpoint)1 Test (org.junit.Test)1