Search in sources :

Example 1 with DeleteResponse

use of io.etcd.jetcd.kv.DeleteResponse in project jetcd by coreos.

the class KVTest method testDelete.

@Test
public void testDelete() throws Exception {
    // Put content so that we actually have something to delete
    testPut();
    ByteSequence keyToDelete = SAMPLE_KEY;
    // count keys about to delete
    CompletableFuture<GetResponse> getFeature = kvClient.get(keyToDelete);
    GetResponse resp = getFeature.get();
    // delete the keys
    CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(keyToDelete);
    DeleteResponse delResp = deleteFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(resp.getKvs().size());
}
Also used : DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 2 with DeleteResponse

use of io.etcd.jetcd.kv.DeleteResponse 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 DeleteResponse

use of io.etcd.jetcd.kv.DeleteResponse in project jetcd by coreos.

the class KVNamespaceTest method deleteKVWithAssertion.

private static void deleteKVWithAssertion(KV kvClient, ByteSequence key, ByteSequence prevValue) throws Exception {
    CompletableFuture<DeleteResponse> deleteFuture = kvClient.delete(key, DeleteOption.newBuilder().withPrevKV(true).build());
    DeleteResponse deleteResponse = deleteFuture.get();
    assertThat(deleteResponse.getDeleted()).isEqualTo(1);
    assertThat(deleteResponse.getPrevKvs().size()).isEqualTo(1);
    assertThat(deleteResponse.getPrevKvs().get(0).getKey()).isEqualTo(key);
    assertThat(deleteResponse.getPrevKvs().get(0).getValue()).isEqualTo(prevValue);
    assertNonexistentKey(kvClient, key);
}
Also used : DeleteResponse(io.etcd.jetcd.kv.DeleteResponse)

Example 4 with DeleteResponse

use of io.etcd.jetcd.kv.DeleteResponse in project jetcd by coreos.

the class KVImpl method delete.

@Override
public CompletableFuture<DeleteResponse> delete(ByteSequence key, DeleteOption option) {
    checkNotNull(key, "key should not be null");
    checkNotNull(option, "option should not be null");
    return execute(() -> stub.deleteRange(Requests.mapDeleteRequest(key, option, namespace)), response -> new DeleteResponse(response, namespace), Errors::isRetryable);
}
Also used : Errors(io.etcd.jetcd.support.Errors) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse)

Example 5 with DeleteResponse

use of io.etcd.jetcd.kv.DeleteResponse in project jetcd by coreos.

the class KVTest method testGetAndDeleteWithPrefix.

@Test
public void testGetAndDeleteWithPrefix() throws Exception {
    String prefix = randomString();
    ByteSequence key = bytesOf(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // verify get withPrefix.
    CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().isPrefix(true).build());
    GetResponse getResp = getFuture.get();
    assertThat(getResp.getCount()).isEqualTo(numPrefixes);
    // verify del withPrefix.
    DeleteOption deleteOpt = DeleteOption.newBuilder().isPrefix(true).build();
    CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
    DeleteResponse delResp = delFuture.get();
    assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
Also used : DeleteOption(io.etcd.jetcd.options.DeleteOption) DeleteResponse(io.etcd.jetcd.kv.DeleteResponse) TestUtil.randomString(io.etcd.jetcd.impl.TestUtil.randomString) GetResponse(io.etcd.jetcd.kv.GetResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)6 ByteSequence (io.etcd.jetcd.ByteSequence)3 GetResponse (io.etcd.jetcd.kv.GetResponse)3 Test (org.junit.jupiter.api.Test)3 TestUtil.randomString (io.etcd.jetcd.impl.TestUtil.randomString)2 KeyValue (io.etcd.jetcd.KeyValue)1 TxnResponse (io.etcd.jetcd.kv.TxnResponse)1 Cmp (io.etcd.jetcd.op.Cmp)1 Op (io.etcd.jetcd.op.Op)1 DeleteOption (io.etcd.jetcd.options.DeleteOption)1 Errors (io.etcd.jetcd.support.Errors)1