Search in sources :

Example 1 with GetResult

use of com.couchbase.client.java.kv.GetResult in project ShedLock by lukas-krecan.

the class CouchbaseLockProviderIntegrationTest method assertLocked.

@Override
public void assertLocked(String lockName) {
    GetResult result = bucket.defaultCollection().get(lockName);
    JsonObject lockDocument = result.contentAsObject();
    assertThat(parse((String) lockDocument.get(LOCK_UNTIL))).isAfter(now());
    assertThat(parse((String) lockDocument.get(LOCKED_AT))).isBeforeOrEqualTo(now());
    assertThat(lockDocument.get(LOCKED_BY)).asString().isNotEmpty();
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 2 with GetResult

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

the class EntityGetWithProjection method main.

public static void main(String... args) {
    Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    Collection collection = bucket.defaultCollection();
    String id = "p01";
    GetResult result = collection.get(id, getOptions().project("name", "age", "attributes.hair"));
    Person person = result.contentAs(Person.class);
    System.out.println(person.name());
    System.out.println(person.age());
    System.out.println(person.attributes().hair());
}
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 3 with GetResult

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

the class EntityUpsertAndGet method main.

public static void main(String... args) {
    /*
     * Connect to the cluster and open a collection to work with.
     */
    Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    Collection collection = bucket.defaultCollection();
    final String id = "p01";
    Person person = new Person("Joan Deere", 28, Arrays.asList("kitty", "puppy"), new Person.Attributes("brown", new Person.Dimensions(65, 120), Collections.singletonList(new Person.Hobby("winter", "Curling", new Person.Details(new Person.Location(37.338207, -121.886330))))));
    collection.upsert(id, person, upsertOptions().expiry(Duration.ofDays(1)));
    GetResult getResult = collection.get(id, getOptions().timeout(Duration.ofSeconds(10)));
    Person read = getResult.contentAs(Person.class);
    System.out.println("Person read: " + read);
}
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 4 with GetResult

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

the class Get method main.

public static void main(String... args) {
    /*
     * Connect to the cluster and open a collection to work with.
     */
    Cluster cluster = Cluster.connect("127.0.0.1", "Administrator", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    Collection collection = bucket.defaultCollection();
    try {
        System.out.println("Found Airport: " + collection.get("airport_1291"));
    } catch (DocumentNotFoundException ex) {
        System.out.println("Airport not found!");
    }
    // -------------------------------------------------------------------------------------
    /*
     * If only a couple fields of a document are needed (a projection), then this can be provided
     * through the options.
     */
    GetResult airline = collection.get("airline_10", getOptions().project("airportname", "country"));
    System.out.println("Found airline with fields: " + airline);
    // -------------------------------------------------------------------------------------
    /*
     * In the previous examples, the expiration field has always been empty on the GetResult.
     * If you need to fetch the expiration time of a document, you can also pass it via the
     * options.
     */
    GetResult airline2 = collection.get("airline_10", getOptions().withExpiry(true));
    System.out.println("Expiration is: " + airline2.expiry());
}
Also used : DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) 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 5 with GetResult

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

the class KeyValueErrorIntegrationTest method verifyUnlockCasMismatch.

/**
 * Ignored for the mock because it still returns TMPFAIL (like the old servers)
 */
@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void verifyUnlockCasMismatch() {
    String id = UUID.randomUUID().toString();
    collection.upsert(id, "foo");
    GetResult result = collection.getAndLock(id, Duration.ofSeconds(5));
    CasMismatchException thrown = assertThrows(CasMismatchException.class, () -> collection.unlock(id, result.cas() + 1));
    assertNotNull(thrown.context());
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) 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