Search in sources :

Example 1 with Cmp

use of io.etcd.jetcd.op.Cmp in project jetcd by coreos.

the class KVTest method testTxn.

@Test
public void testTxn() throws Exception {
    ByteSequence sampleKey = bytesOf("txn_key");
    ByteSequence sampleValue = bytesOf("xyz");
    ByteSequence cmpValue = bytesOf("abc");
    ByteSequence putValue = bytesOf("XYZ");
    ByteSequence putValueNew = bytesOf("ABC");
    // put the original txn key value pair
    kvClient.put(sampleKey, sampleValue).get();
    // construct txn operation
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.GREATER, CmpTarget.value(cmpValue));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).commit();
    txnResp.get();
    // get the value
    GetResponse getResp = kvClient.get(sampleKey).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8));
}
Also used : Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 2 with Cmp

use of io.etcd.jetcd.op.Cmp in project jetcd by coreos.

the class KVTest method testTxnGetAndDeleteWithPrefix.

@Test
void testTxnGetAndDeleteWithPrefix() throws ExecutionException, InterruptedException {
    String prefix = randomString();
    ByteSequence sampleKey = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // always false cmp
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.EQUAL, CmpTarget.value(bytesOf("not_exists")));
    Op.PutOp putOp = Op.put(bytesOf("other_string"), bytesOf("other_value"), PutOption.DEFAULT);
    Op.GetOp getByPrefix = Op.get(sampleKey, GetOption.newBuilder().isPrefix(true).build());
    Op.DeleteOp delete = Op.delete(sampleKey, DeleteOption.newBuilder().isPrefix(true).withPrevKV(true).build());
    TxnResponse txnResponse = kvClient.txn().If(cmp).Then(putOp).Else(getByPrefix, delete).commit().get();
    List<GetResponse> getResponse = txnResponse.getGetResponses();
    assertThat(getResponse).hasSize(1);
    assertThat(getResponse.get(0).getKvs()).hasSize(10);
    assertThat(getResponse.get(0).getKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    List<DeleteResponse> deleteResponses = txnResponse.getDeleteResponses();
    assertThat(deleteResponses).hasSize(1);
    assertThat(deleteResponses.get(0).getDeleted()).isEqualTo(10);
    assertThat(deleteResponses.get(0).getPrevKvs()).anyMatch(keyValue -> keyValue.getKey().startsWith(sampleKey));
    assertThat(txnResponse.getPutResponses()).isEmpty();
}
Also used : Op(io.etcd.jetcd.op.Op) Cmp(io.etcd.jetcd.op.Cmp) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 3 with Cmp

use of io.etcd.jetcd.op.Cmp in project jetcd by coreos.

the class KVNamespaceTest method testTxn.

@Test
public void testTxn() throws Exception {
    // kvClient without namespace used as the judge for the final result
    kvClient = TestUtil.client(cluster).build().getKVClient();
    // kvClient with one namespace used to test operations with namespace
    ByteSequence namespace = ByteSequence.from(TestUtil.randomByteSequence().concat(ByteSequence.NAMESPACE_DELIMITER).getBytes());
    kvClientWithNamespace = TestUtil.client(cluster).namespace(namespace).build().getKVClient();
    // put a key in root namespace, assert that it cannot be seen using kvClient with namespace
    ByteSequence cmpKey = getNonexistentKey();
    putKVWithAssertion(kvClient, cmpKey, TestUtil.randomByteSequence(), null);
    ByteSequence key1 = getNonexistentKey();
    ByteSequence value1 = TestUtil.randomByteSequence();
    putKVWithAssertion(kvClientWithNamespace, key1, value1, null);
    ByteSequence key2 = getNonexistentKey();
    ByteSequence value2 = TestUtil.randomByteSequence();
    putKVWithAssertion(kvClientWithNamespace, key2, value2, null);
    // test comparison passes, with put operation.
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(cmpKey, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.put(key1, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).Else(Op.put(key2, TestUtil.randomByteSequence(), PutOption.newBuilder().withPrevKV().build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getPutResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getPutResponses().get(0).hasPrevKv()).isTrue();
        assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getKey()).isEqualTo(key1);
        assertThat(txnResponse.getPutResponses().get(0).getPrevKv().getValue()).isEqualTo(value1);
    }
    // test comparison fails, with get operation.
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.EQUAL, CmpTarget.version(0))).Then(Op.get(key1, GetOption.newBuilder().build())).Else(Op.get(key2, GetOption.newBuilder().build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getGetResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().size()).isEqualTo(1);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getKey()).isEqualTo(key2);
        assertThat(txnResponse.getGetResponses().get(0).getKvs().get(0).getValue()).isEqualTo(value2);
    }
    // test delete operation
    {
        Txn txn = kvClientWithNamespace.txn();
        CompletableFuture<TxnResponse> txnFuture = txn.If(new Cmp(key1, Cmp.Op.GREATER, CmpTarget.version(0))).Then(Op.delete(key2, DeleteOption.newBuilder().withPrevKV(true).build())).Else(Op.delete(key1, DeleteOption.newBuilder().withPrevKV(true).build())).commit();
        TxnResponse txnResponse = txnFuture.get();
        assertThat(txnResponse.getDeleteResponses().size()).isEqualTo(1);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().size()).isEqualTo(1);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getKey()).isEqualTo(key2);
        assertThat(txnResponse.getDeleteResponses().get(0).getPrevKvs().get(0).getValue()).isEqualTo(value2);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Cmp

use of io.etcd.jetcd.op.Cmp in project jetcd by coreos.

the class KVTest method testTxnForCmpOpNotEqual.

@Test
public void testTxnForCmpOpNotEqual() throws Exception {
    ByteSequence sampleKey = bytesOf("txn_key");
    ByteSequence sampleValue = bytesOf("xyz");
    ByteSequence cmpValue = bytesOf("abc");
    ByteSequence putValue = bytesOf("XYZ");
    ByteSequence putValueNew = bytesOf("ABC");
    // put the original txn key value pair
    kvClient.put(sampleKey, sampleValue).get();
    // construct txn operation
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(sampleKey, Cmp.Op.NOT_EQUAL, CmpTarget.value(cmpValue));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).commit();
    txnResp.get();
    // get the value
    GetResponse getResp = kvClient.get(sampleKey).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(putValue.toString(UTF_8));
}
Also used : Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 5 with Cmp

use of io.etcd.jetcd.op.Cmp in project jetcd by coreos.

the class KVTest method testNestedTxn.

@Test
public void testNestedTxn() throws Exception {
    ByteSequence foo = bytesOf("txn_foo");
    ByteSequence bar = bytesOf("txn_bar");
    ByteSequence barz = bytesOf("txn_barz");
    ByteSequence abc = bytesOf("txn_abc");
    ByteSequence oneTwoThree = bytesOf("txn_123");
    Txn txn = kvClient.txn();
    Cmp cmp = new Cmp(foo, Cmp.Op.EQUAL, CmpTarget.version(0));
    CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(foo, bar, PutOption.DEFAULT), Op.txn(null, new Op[] { Op.put(abc, oneTwoThree, PutOption.DEFAULT) }, null)).Else(Op.put(foo, barz, PutOption.DEFAULT)).commit();
    txnResp.get();
    GetResponse getResp = kvClient.get(foo).get();
    assertThat(getResp.getKvs()).hasSize(1);
    assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8));
    GetResponse getResp2 = kvClient.get(abc).get();
    assertThat(getResp2.getKvs()).hasSize(1);
    assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8));
}
Also used : Op(io.etcd.jetcd.op.Op) Cmp(io.etcd.jetcd.op.Cmp) Txn(io.etcd.jetcd.Txn) TxnResponse(io.etcd.jetcd.kv.TxnResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)6 TxnResponse (io.etcd.jetcd.kv.TxnResponse)6 Cmp (io.etcd.jetcd.op.Cmp)6 Test (org.junit.jupiter.api.Test)6 Txn (io.etcd.jetcd.Txn)5 GetResponse (io.etcd.jetcd.kv.GetResponse)4 Op (io.etcd.jetcd.op.Op)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 TestUtil.randomString (io.etcd.jetcd.impl.TestUtil.randomString)1 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)1