Search in sources :

Example 26 with GetResult

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

the class CouchbaseArraySet method contains.

@Override
public boolean contains(Object t) {
    // TODO subpar implementation for a Set, use ARRAY_CONTAINS when available
    enforcePrimitive(t);
    try {
        GetResult result = collection.get(id, getOptions);
        JsonArray current = result.contentAs(JsonArray.class);
        for (Object in : current) {
            if (safeEquals(in, t)) {
                return true;
            }
        }
        return false;
    } catch (DocumentNotFoundException e) {
        return false;
    }
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 27 with GetResult

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

the class CouchbaseArraySet method remove.

@Override
public boolean remove(Object t) {
    enforcePrimitive(t);
    for (int i = 0; i < arraySetOptions.casMismatchRetries(); i++) {
        try {
            GetResult result = collection.get(id);
            JsonArray current = result.contentAsArray();
            long cas = result.cas();
            int index = 0;
            boolean found = false;
            for (Object next : current) {
                if (safeEquals(next, t)) {
                    found = true;
                    break;
                }
                index++;
            }
            String path = "[" + index + "]";
            if (!found) {
                return false;
            } else {
                collection.mutateIn(id, Collections.singletonList(MutateInSpec.remove(path)), arraySetOptions.mutateInOptions().cas(cas));
                return true;
            }
        } catch (CasMismatchException e) {
        // retry
        } catch (DocumentNotFoundException ex) {
            return false;
        }
    }
    throw new CouchbaseException("CouchbaseArraySet remove failed", new RetryExhaustedException("Couldn't perform remove in less than " + arraySetOptions.casMismatchRetries() + " iterations. It is likely concurrent modifications of this document are the reason"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) GetResult(com.couchbase.client.java.kv.GetResult) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) RetryExhaustedException(com.couchbase.client.core.retry.reactor.RetryExhaustedException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 28 with GetResult

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

the class HelloOsgi method start.

public void start(BundleContext ctx) {
    System.out.println("Hello world.");
    try {
        InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
        String configFilename = System.getenv("HOME") + "/log4j.properties";
        File logConfig = new File(configFilename);
        if (logConfig.exists()) {
            System.out.println("log4j.configuration " + configFilename);
            System.setProperty("log4j.configuration", "file://" + configFilename);
        } else {
            System.out.println("logging configuration file " + configFilename + " not found, continuing without");
        }
    } catch (java.lang.NoClassDefFoundError e) {
        System.out.println("Slf4JLogging is not going to work, logging will fallback to Console\n");
    }
    System.out.println("Cluster.connect...");
    cluster = Cluster.connect(seedNodes(), clusterOptions());
    Bucket bucket = cluster.bucket("travel-sample");
    Collection collection = bucket.defaultCollection();
    bucket.waitUntilReady(Duration.ofSeconds(11));
    String id = UUID.randomUUID().toString();
    collection.upsert(id, JsonObject.create().put("name", "MyName"));
    GetResult r = bucket.defaultCollection().get(id);
    System.out.println(">>>>>>>>>>>>>> " + r);
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) Bucket(com.couchbase.client.java.Bucket) Collection(com.couchbase.client.java.Collection) File(java.io.File)

Example 29 with GetResult

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

the class HelloWorld method main.

public static void main(String... args) {
    /*
     * Connect to the cluster with a hostname and credentials.
     */
    Cluster cluster = Cluster.connect("10.143.190.101", "Administrator", "password");
    /*
     * Open a bucket with the bucket name.
     */
    Bucket bucket = cluster.bucket("travel-sample");
    /*
     * Open a collection - here the default collection which is also backwards compatible to
     * servers which do not support collections.
     */
    Collection collection = bucket.defaultCollection();
    /*
     * Fetch a document from the travel-sample bucket.
     */
    GetResult airport_1291 = collection.get("airport_1291");
    /*
     * Print the fetched document.
     */
    System.err.println(airport_1291);
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) Bucket(com.couchbase.client.java.Bucket) Cluster(com.couchbase.client.java.Cluster) Collection(com.couchbase.client.java.Collection)

Example 30 with GetResult

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

the class KeyValueIntegrationTest method getAndTouch.

/**
 * This test is ignored against the mock because right now it does not bump the CAS like
 * the server does when getAndTouch is called.
 *
 * <p>Remove the ignore as soon as https://github.com/couchbase/CouchbaseMock/issues/49 is
 * fixed.</p>
 */
@Test
@IgnoreWhen(clusterTypes = { ClusterType.MOCKED })
void getAndTouch() {
    String id = UUID.randomUUID().toString();
    JsonObject expected = JsonObject.create().put("foo", true);
    MutationResult insert = collection.insert(id, expected, insertOptions().expiry(Duration.ofSeconds(10)));
    assertTrue(insert.cas() != 0);
    GetResult getAndTouch = collection.getAndTouch(id, Duration.ofSeconds(1));
    assertTrue(getAndTouch.cas() != 0);
    assertNotEquals(insert.cas(), getAndTouch.cas());
    assertEquals(expected, getAndTouch.contentAsObject());
    waitUntilCondition(() -> {
        try {
            Thread.sleep(200);
        } 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)

Aggregations

GetResult (com.couchbase.client.java.kv.GetResult)39 Test (org.junit.jupiter.api.Test)25 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)20 JsonObject (com.couchbase.client.java.json.JsonObject)18 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)15 MutationResult (com.couchbase.client.java.kv.MutationResult)11 Collection (com.couchbase.client.java.Collection)7 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)6 Bucket (com.couchbase.client.java.Bucket)5 Cluster (com.couchbase.client.java.Cluster)4 Record (org.talend.sdk.component.api.record.Record)4 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)3 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)2 TimeoutException (com.couchbase.client.core.error.TimeoutException)2 JsonArray (com.couchbase.client.java.json.JsonArray)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2