Search in sources :

Example 6 with KeyValue

use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.

the class MVCCAsyncStore method vDelete.

@Override
default CompletableFuture<KeyValue<K, V>> vDelete(K k, long expectedVersion) {
    TxnOp<K, V> op = getOpFactory().newTxn().If(newCompareVersion(CompareResult.EQUAL, k, expectedVersion)).Then(getOpFactory().newDelete(k, Options.deleteAndGet())).build();
    return txn(op).thenCompose(result -> {
        try {
            Code code = result.code();
            if (Code.OK == code && !result.isSuccess()) {
                code = Code.BAD_REVISION;
            }
            if (Code.OK == code) {
                List<Result<K, V>> subResults = result.results();
                DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) subResults.get(0);
                List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
                if (prevKvs.isEmpty()) {
                    return FutureUtils.value(null);
                } else {
                    return FutureUtils.value(prevKvs.get(0));
                }
            } else {
                return failWithCode(code, "Failed to vDelete key " + k + " (version=" + expectedVersion + ") to store " + name());
            }
        } finally {
            result.close();
        }
    });
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) Code(org.apache.bookkeeper.api.kv.result.Code) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult) Result(org.apache.bookkeeper.api.kv.result.Result) RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) CompareResult(org.apache.bookkeeper.api.kv.op.CompareResult)

Example 7 with KeyValue

use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.

the class PTableWriteView method vDelete.

default CompletableFuture<KeyValue<K, V>> vDelete(K pKey, K lKey, long expectedVersion) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareVersion(CompareResult.EQUAL, lKey, expectedVersion)).Then(opFactory().newDelete(lKey, 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 " + lKey + " (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 8 with KeyValue

use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.

the class PTableWriteView method rDelete.

default CompletableFuture<KeyValue<K, V>> rDelete(K pKey, K lKey, long expectedRevision) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareModRevision(CompareResult.EQUAL, lKey, expectedRevision)).Then(opFactory().newDelete(lKey, 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 rDelete key " + lKey + " (mod_rev = " + expectedRevision + ")"));
            }
        } 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 KeyValue

use of org.apache.bookkeeper.api.kv.result.KeyValue 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 10 with KeyValue

use of org.apache.bookkeeper.api.kv.result.KeyValue 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

KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)10 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)6 KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)5 Test (org.junit.Test)3 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)2 Code (org.apache.bookkeeper.api.kv.result.Code)2 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)2 Result (org.apache.bookkeeper.api.kv.result.Result)2 StateStoreSpec (org.apache.bookkeeper.statelib.api.StateStoreSpec)2 ByteBuf (io.netty.buffer.ByteBuf)1 PutResult (org.apache.bookkeeper.api.kv.result.PutResult)1 KV (org.apache.bookkeeper.common.kv.KV)1 MVCCStoreException (org.apache.bookkeeper.statelib.api.exceptions.MVCCStoreException)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 MutableLong (org.apache.commons.lang.mutable.MutableLong)1