use of com.couchbase.client.java.document.JsonDocument in project sling by apache.
the class CouchbaseNoSqlAdapter method store.
@Override
public boolean store(NoSqlData data) {
Bucket bucket = couchbaseClient.getBucket();
String cacheKey = CouchbaseKey.build(data.getPath(), cacheKeyPrefix);
JsonObject envelope = JsonObject.create();
envelope.put(PN_PATH, data.getPath());
envelope.put(PN_DATA, JsonObject.from(data.getProperties(MultiValueMode.LISTS)));
// for list-children query efficiency store parent path as well
String parentPath = ResourceUtil.getParent(data.getPath());
if (parentPath != null) {
envelope.put(PN_PARENT_PATH, parentPath);
}
JsonDocument doc = JsonDocument.create(cacheKey, envelope);
try {
bucket.insert(doc);
// created
return true;
} catch (DocumentAlreadyExistsException ex) {
bucket.upsert(doc);
// updated
return false;
}
}
use of com.couchbase.client.java.document.JsonDocument in project incubator-gobblin by apache.
the class CouchbaseTestServer method testServer.
@Test
public static void testServer() throws InterruptedException, IOException {
CouchbaseTestServer couchbaseTestServer = new CouchbaseTestServer(TestUtils.findFreePort());
couchbaseTestServer.start();
int port = couchbaseTestServer.getPort();
int serverPort = couchbaseTestServer.getServerPort();
try {
CouchbaseEnvironment cbEnv = DefaultCouchbaseEnvironment.builder().bootstrapHttpEnabled(true).bootstrapHttpDirectPort(port).bootstrapCarrierDirectPort(serverPort).connectTimeout(TimeUnit.SECONDS.toMillis(15)).bootstrapCarrierEnabled(true).build();
CouchbaseCluster cbCluster = CouchbaseCluster.create(cbEnv, "localhost");
Bucket bucket = cbCluster.openBucket("default", "");
try {
JsonObject content = JsonObject.empty().put("name", "Michael");
JsonDocument doc = JsonDocument.create("docId", content);
JsonDocument inserted = bucket.insert(doc);
} catch (Exception e) {
Assert.fail("Should not throw exception on insert", e);
}
} finally {
couchbaseTestServer.stop();
}
}
use of com.couchbase.client.java.document.JsonDocument in project components by Talend.
the class CouchbaseInputTestIT method populateBucket.
private void populateBucket() {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().socketConnectTimeout(60000).connectTimeout(60000).keepAliveInterval(60000).build();
CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
Bucket bucket = cluster.openBucket(bucketName, password);
assertTrue(bucket.bucketManager().flush());
JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
bucket.upsert(document, PersistTo.MASTER);
bucket.close();
cluster.disconnect();
}
use of com.couchbase.client.java.document.JsonDocument in project tutorials by eugenp.
the class PersonCrudServiceIntegrationTest method givenNewHometown_whenUpdate_thenNewHometownPersisted.
@Test
public final void givenNewHometown_whenUpdate_thenNewHometownPersisted() {
// create and insert person document
JsonDocument doc = insertRandomPersonDocument();
// update person
Person expected = converter.fromDocument(doc);
String updatedHomeTown = RandomStringUtils.randomAlphabetic(12);
expected.setHomeTown(updatedHomeTown);
personService.update(expected);
// check results
JsonDocument actual = bucket.get(expected.getId());
assertNotNull(actual);
assertNotNull(actual.content());
assertEquals(expected.getHomeTown(), actual.content().getString("homeTown"));
// cleanup
bucket.remove(expected.getId());
}
use of com.couchbase.client.java.document.JsonDocument in project tutorials by eugenp.
the class PersonCrudServiceIntegrationTest method givenPersons_whenUpdateBulk_thenPersonsAreUpdated.
@Test
public final void givenPersons_whenUpdateBulk_thenPersonsAreUpdated() {
List<String> ids = new ArrayList<>();
// add some person documents
for (int i = 0; i < 5; i++) {
ids.add(insertRandomPersonDocument().id());
}
// load persons from Couchbase
List<Person> persons = new ArrayList<>();
for (String id : ids) {
persons.add(converter.fromDocument(bucket.get(id)));
}
// modify persons
for (Person person : persons) {
person.setHomeTown(RandomStringUtils.randomAlphabetic(10));
}
// perform bulk update
personService.updateBulk(persons);
// check results
for (Person person : persons) {
JsonDocument doc = bucket.get(person.getId());
assertEquals(person.getName(), doc.content().getString("name"));
assertEquals(person.getHomeTown(), doc.content().getString("homeTown"));
}
// cleanup
for (String id : ids) {
bucket.remove(id);
}
}
Aggregations