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;
}
}
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"));
}
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);
}
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);
}
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;
}
});
}
Aggregations