Search in sources :

Example 6 with MutationResult

use of com.couchbase.client.java.kv.MutationResult in project couchbase-jvm-clients by couchbase.

the class SubdocMutateIntegrationTest method reviveDocumentWithCAS2.

@IgnoreWhen(missesCapabilities = { Capabilities.SUBDOC_REVIVE_DOCUMENT })
@Test
void reviveDocumentWithCAS2() {
    String docId = docId();
    JsonObject body = JsonObject.create().put("foo", "bar");
    coll.insert(docId, body);
    MutationResult mr = coll.remove(docId);
    coll.mutateIn(docId, Arrays.asList(// Do a dummy op as server complains if do nothing
    MutateInSpec.upsert("txn", JsonObject.create()).xattr()), MutateInOptions.mutateInOptions().accessDeleted(true).cas(mr.cas()).storeSemantics(StoreSemantics.REVIVE));
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 7 with MutationResult

use of com.couchbase.client.java.kv.MutationResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method appendReactive.

@Test
void appendReactive() {
    String id = UUID.randomUUID().toString();
    byte[] helloBytes = "Hello, ".getBytes(UTF_8);
    byte[] worldBytes = "World!".getBytes(UTF_8);
    byte[] helloWorldBytes = "Hello, World!".getBytes(UTF_8);
    assertThrows(DocumentNotFoundException.class, () -> collection.reactive().binary().append(id, helloBytes).block());
    MutationResult upsert = collection.upsert(id, helloBytes, upsertOptions().transcoder(RawBinaryTranscoder.INSTANCE));
    assertTrue(upsert.cas() != 0);
    assertArrayEquals(helloBytes, collection.get(id, getOptions().transcoder(RawBinaryTranscoder.INSTANCE)).contentAs(byte[].class));
    MutationResult append = collection.reactive().binary().append(id, worldBytes).block();
    assertNotNull(append);
    assertTrue(append.cas() != 0);
    assertNotEquals(append.cas(), upsert.cas());
    assertArrayEquals(helloWorldBytes, collection.get(id, getOptions().transcoder(RawBinaryTranscoder.INSTANCE)).contentAs(byte[].class));
}
Also used : MutationResult(com.couchbase.client.java.kv.MutationResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 8 with MutationResult

use of com.couchbase.client.java.kv.MutationResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method touch.

/**
 * Right now the mock does not change the cas on touch, so we need to ignore the test
 * until https://github.com/couchbase/CouchbaseMock/issues/50 is resolved.
 */
@Test
@IgnoreWhen(clusterTypes = { ClusterType.MOCKED })
void touch() throws Exception {
    String id = UUID.randomUUID().toString();
    JsonObject expected = JsonObject.create().put("foo", true);
    MutationResult upsert = collection.upsert(id, expected, upsertOptions().expiry(Duration.ofSeconds(10)));
    assertTrue(upsert.cas() != 0);
    MutationResult touched = collection.touch(id, Duration.ofSeconds(1));
    assertNotEquals(touched.cas(), upsert.cas());
    Thread.sleep(300);
    GetResult r = collection.get(id);
    assertEquals(expected, r.contentAsObject());
    assertEquals(r.cas(), touched.cas());
    waitUntilCondition(() -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        // ignored.
        }
        try {
            collection.get(id);
            return false;
        } catch (DocumentNotFoundException knf) {
            return true;
        }
    });
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 9 with MutationResult

use of com.couchbase.client.java.kv.MutationResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method unlock.

/**
 * The mock returns TMPFAIL instead of LOCKED, so this test is ignored on the mock.
 */
@Test
@IgnoreWhen(clusterTypes = { ClusterType.MOCKED })
void unlock() {
    String id = UUID.randomUUID().toString();
    MutationResult upsert = collection.upsert(id, JsonObject.create().put("foo", true));
    assertTrue(upsert.cas() != 0);
    GetResult locked = collection.getAndLock(id, Duration.ofSeconds(30));
    TimeoutException exception = assertThrows(TimeoutException.class, () -> collection.upsert(id, JsonObject.create(), upsertOptions().timeout(Duration.ofMillis(100))));
    assertEquals(EnumSet.of(RetryReason.KV_LOCKED), exception.context().requestContext().retryReasons());
    assertThrows(CasMismatchException.class, () -> collection.unlock(id, locked.cas() + 1));
    collection.unlock(id, locked.cas());
    JsonObject expected = JsonObject.create().put("foo", false);
    MutationResult replaced = collection.replace(id, expected);
    assertTrue(replaced.cas() != 0);
    assertEquals(expected, collection.get(id).contentAsObject());
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) TimeoutException(com.couchbase.client.core.error.TimeoutException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 10 with MutationResult

use of com.couchbase.client.java.kv.MutationResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method append.

@Test
void append() {
    String id = UUID.randomUUID().toString();
    byte[] helloBytes = "Hello, ".getBytes(UTF_8);
    byte[] worldBytes = "World!".getBytes(UTF_8);
    byte[] helloWorldBytes = "Hello, World!".getBytes(UTF_8);
    assertThrows(DocumentNotFoundException.class, () -> collection.binary().append(id, helloBytes));
    MutationResult upsert = collection.upsert(id, helloBytes, upsertOptions().transcoder(RawBinaryTranscoder.INSTANCE));
    assertTrue(upsert.cas() != 0);
    assertArrayEquals(helloBytes, collection.get(id, getOptions().transcoder(RawBinaryTranscoder.INSTANCE)).contentAs(byte[].class));
    MutationResult append = collection.binary().append(id, worldBytes);
    assertTrue(append.cas() != 0);
    assertNotEquals(append.cas(), upsert.cas());
    assertArrayEquals(helloWorldBytes, collection.get(id, getOptions().transcoder(RawBinaryTranscoder.INSTANCE)).contentAs(byte[].class));
}
Also used : MutationResult(com.couchbase.client.java.kv.MutationResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

MutationResult (com.couchbase.client.java.kv.MutationResult)42 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)40 Test (org.junit.jupiter.api.Test)40 JsonObject (com.couchbase.client.java.json.JsonObject)16 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)13 GetResult (com.couchbase.client.java.kv.GetResult)11 SearchQuery.queryString (com.couchbase.client.java.search.SearchQuery.queryString)5 SearchResult (com.couchbase.client.java.search.result.SearchResult)5 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)4 TimeoutException (com.couchbase.client.core.error.TimeoutException)3 Bucket (com.couchbase.client.java.Bucket)2 Collection (com.couchbase.client.java.Collection)2 ExistsResult (com.couchbase.client.java.kv.ExistsResult)2 QueryOptions (com.couchbase.client.java.query.QueryOptions)2 QueryResult (com.couchbase.client.java.query.QueryResult)2 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)2 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 DocumentExistsException (com.couchbase.client.core.error.DocumentExistsException)1 RateLimitedException (com.couchbase.client.core.error.RateLimitedException)1