/** * Helper method to turn the map of values into a {@link JsonObject} for further use. * * @param values the values to transform. * @return the created json object. */ private static JsonObject valuesToJsonObject(final Map<String, ByteIterator> values) { JsonObject result = JsonObject.create(); for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { result.put(entry.getKey(), entry.getValue().toString()); } return result; }
@Override public void save(final Session session) { JsonObject json = JsonObject.from(session.attributes()); // session metadata json.put("_accessedAt", session.accessedAt()); json.put("_createdAt", session.createdAt()); json.put("_savedAt", session.savedAt()); JsonDocument doc = JsonDocument.create(N1Q.qualifyId(SESSION, session.id()), expiry, json); bucket.upsert(doc); }
Bucket bucket = cbCluster.openBucket("default",""); try { JsonObject content = JsonObject.empty().put("name", "Michael"); JsonDocument doc = JsonDocument.create("docId", content); JsonDocument inserted = bucket.insert(doc);
/** * Executes a N1QL kernelTransaction with named parameters. * * @param statement the raw kernelTransaction string to execute (containing named * placeholders: $param1, $param2, ...) * @param parameterNames the placeholders' names in kernelTransaction * @param parameterValues the values for the named placeholders in kernelTransaction * @return the list of {@link JsonObject}s retrieved by this query * @see N1qlQuery#parameterized(Statement, JsonObject) */ public List<JsonObject> executeParametrizedStatement(String statement, List<String> parameterNames, List<Object> parameterValues) { JsonObject namedParams = JsonObject.create(); for (int param = 0; param < parameterNames.size(); param++) { namedParams.put(parameterNames.get(param), parameterValues.get(param)); } ParameterizedN1qlQuery query = N1qlQuery.parameterized(statement, namedParams); return executeQuery(query); }
/** * Iterate and populate json object. * * @param entity * the entity * @param iterator * the iterator * @return the json object */ private JsonObject iterateAndPopulateJsonObject(Object entity, Iterator<Attribute> iterator, String tableName) { JsonObject obj = JsonObject.create(); while (iterator.hasNext()) { Attribute attribute = iterator.next(); Field field = (Field) attribute.getJavaMember(); Object value = PropertyAccessorHelper.getObject(entity, field); obj.put(((AbstractAttribute) attribute).getJPAColumnName(), value); } obj.put(CouchbaseConstants.KUNDERA_ENTITY, tableName); return obj; }
@Override protected void injectParams(JsonObject input) { input.put("match_all", (String) null); } }
@Override protected void injectParams(JsonObject input) { input.put("match_none", (String) null); } }
@Test public void testInsertAndRemoveDocument() { PasswordAuthenticator passwordAuthenticator = new PasswordAuthenticator(USERNAME, PASSWORD); try (CouchbaseConnection connection = new CouchbaseConnection(Arrays.asList("couchbase://localhost"), passwordAuthenticator, BUCKET_NAME, null, ENV);) { JsonDocument document = connection.upsert("test", JsonObject.create().put("test", "test").toString()); Assert.assertEquals("test", document.content().get("test")); connection.remove("test"); Assert.assertNull(connection.get("test")); } }
@Override protected void injectParams(JsonObject input) { if (docIds.isEmpty()) { throw new IllegalArgumentException("DocID query needs at least one document ID"); } JsonArray ids = JsonArray.from(docIds); input.put("ids", ids); } }
/** * Attempt to convert a {@link Map} to a {@link JsonObject} value and store it, identified by the field name. * * @param name the name of the JSON field. * @param value the value of the JSON field. * @return the {@link JsonObject}. * @see #from(Map) */ public JsonObject put(String name, Map<String, ?> value) { return put(name, JsonObject.from(value)); }
/** * Stores a {@link JsonArray} value identified by the field name. * * @param name the name of the JSON field. * @param value the value of the JSON field. * @return the {@link JsonObject}. */ public JsonObject put(String name, List<?> value) { return put(name, JsonArray.from(value)); }
@Override public byte[] exportAnalyticsDeferredResultHandle(AnalyticsDeferredResultHandle handle) { try { JsonObject jsonObject = JsonObject.create(); jsonObject.put("v", 1); jsonObject.put("uri", handle.getStatusHandleUri()); return JacksonTransformers.MAPPER.writeValueAsBytes(jsonObject); } catch (JsonProcessingException e) { throw new IllegalStateException("Cannot convert handle to Json String", e); } }
@Override public DocumentEntity insert(DocumentEntity entity, Duration ttl) { requireNonNull(entity, "entity is required"); requireNonNull(ttl, "ttl is required"); JsonObject jsonObject = convert(entity); Document id = entity.find(ID_FIELD) .orElseThrow(() -> new CouchbaseNoKeyFoundException(entity.toString())); String prefix = getPrefix(id, entity.getName()); jsonObject.put(KEY_FIELD, prefix); bucket.upsert(JsonDocument.create(prefix, (int) ttl.getSeconds(), jsonObject)); return entity; }
private static Consumer<Document> toJsonObject(JsonObject jsonObject) { return d -> { Object value = ValueUtil.convert(d.getValue()); if (Document.class.isInstance(value)) { convertDocument(jsonObject, d, value); } else if (Iterable.class.isInstance(value)) { convertIterable(jsonObject, d, value); } else { jsonObject.put(d.getName(), value); } }; }
/** {@inheritDoc} */ @Override public JsonDocument toStore(Feature feature) { if (feature == null) return null; JsonObject jsonObject; try { jsonObject = TRANSCODER.stringToJsonObject(feature.toJson()); jsonObject.put("_class", Feature.class.getCanonicalName()); } catch (Exception e) { throw new FeatureAccessException("Cannot parse the feature", e); } return JsonDocument.create(feature.getUid(), jsonObject); }
@Override public void unlock(LockConfiguration lockConfiguration) { JsonDocument document = bucket.get(lockConfiguration.getName()); document.content().put(LOCK_UNTIL, toIsoString(lockConfiguration.getUnlockTime())); bucket.replace(document); } }