/** * Performs the {@link #read(String, String, Set, Map)} operation via Key/Value ("get"). * * @param docId the document ID * @param fields the fields to be loaded * @param result the result map where the doc needs to be converted into * @return The result of the operation. */ private Status readKv(final String docId, final Set<String> fields, final Map<String, ByteIterator> result) throws Exception { RawJsonDocument loaded = bucket.get(docId, RawJsonDocument.class); if (loaded == null) { return Status.NOT_FOUND; } decode(loaded.content(), fields, result); return Status.OK; }
@Override public RawJsonDocument doInBucket() { if (entity.isTouchOnRead()) { return client.getAndTouch(id, entity.getExpiry(), RawJsonDocument.class); } else { return client.get(id, RawJsonDocument.class); } } });
@Override public <K, V> V get(K key, Serializer<K> keySerializer, Deserializer<V> valueDeserializer) throws IOException { final String docId = toDocumentId(key, keySerializer); final BinaryDocument doc = bucket.get(BinaryDocument.create(docId)); return deserialize(doc, valueDeserializer); }
/** * A convenient method to retrieve String value when Document type is unknown. * This method uses LegacyDocument to get, then tries to convert content based on its class. * @param bucket the bucket to get a document * @param id the id of the target document * @return String representation of the stored value, or null if not found */ public static String getStringContent(Bucket bucket, String id) { final LegacyDocument doc = bucket.get(LegacyDocument.create(id)); if (doc == null) { return null; } final Object content = doc.content(); return getStringContent(content); }
@Override public <K, V> AtomicCacheEntry<K, V, Long> fetch(K key, Serializer<K> keySerializer, Deserializer<V> valueDeserializer) throws IOException { final String docId = toDocumentId(key, keySerializer); final BinaryDocument doc = bucket.get(BinaryDocument.create(docId)); if (doc == null) { return null; } final V value = deserialize(doc, valueDeserializer); return new AtomicCacheEntry<>(key, value, doc.cas()); }
void verify(Bucket bucket) throws UnsupportedEncodingException { // verify System.out.println("Starting verification procedure"); for (Map.Entry<String, byte[]> cacheEntry : verificationCache.entrySet()) { Object doc = bucket.get(cacheEntry.getKey(), recordClass); if (doc instanceof TupleDocument) { ByteBuf returnedBuf = (((TupleDocument) doc).content()).value1(); byte[] returnedBytes = new byte[returnedBuf.readableBytes()]; returnedBuf.getBytes(0, returnedBytes); Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for TupleDoc should be equal"); } else if (doc instanceof RawJsonDocument) { byte[] returnedBytes = ((RawJsonDocument) doc).content().getBytes("UTF-8"); Assert.assertEquals(returnedBytes, cacheEntry.getValue(), "Returned content for JsonDoc should be equal"); } else { Assert.fail("Returned type was neither TupleDocument nor RawJsonDocument"); } } System.out.println("Verification success!"); } }
/** * Test that a single Json document can be written successfully * @throws IOException * @throws DataConversionException * @throws ExecutionException * @throws InterruptedException */ @Test(groups={"timeout"}) public void testJsonDocumentWrite() throws IOException, DataConversionException, ExecutionException, InterruptedException { CouchbaseWriter writer = new CouchbaseWriter(_couchbaseEnvironment, ConfigFactory.empty()); try { String key = "hello"; String testContent = "hello world"; HashMap<String, String> contentMap = new HashMap<>(); contentMap.put("value", testContent); Gson gson = new Gson(); String jsonString = gson.toJson(contentMap); RawJsonDocument jsonDocument = RawJsonDocument.create(key, jsonString); writer.write(jsonDocument, null).get(); RawJsonDocument returnDoc = writer.getBucket().get(key, RawJsonDocument.class); Map<String, String> returnedMap = gson.fromJson(returnDoc.content(), Map.class); Assert.assertEquals(testContent, returnedMap.get("value")); } finally { writer.close(); } }
/** * Retrieves a {@link JsonDocument} by its unique ID. * * @param documentId the unique ID of the document * @return the found {@link JsonDocument} or null if not found * @see Bucket#get(String) */ public JsonDocument get(String documentId) { return this.bucket.get(documentId); }
@Test public void shouldInsertDocument() { RawJsonDocument expected = RawJsonDocument.create(ID, DOCUMENT); getBucket().upsert(expected); RawJsonDocument result = getBucket().get(ID, RawJsonDocument.class); Assert.assertEquals(expected.content(), result.content()); }
TupleDocument returnDoc = writer.getBucket().get("hello", TupleDocument.class);
@Override public Object find(Class entityClass, Object key) { EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass); JsonDocument doc; String id = generateJsonDocId(entityMetadata.getTableName(), key.toString()); doc = bucket.get(id); LOGGER.debug("Found result for ID : " + key.toString() + " in the " + bucket.name() + " Bucket"); if (doc == null) { return null; } else { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); return handler.getEntityFromDocument(entityClass, doc.content(), entityType); } }
@Override public int size() { //TODO use subdoc GET_COUNT when available JsonArrayDocument current = bucket.get(id, JsonArrayDocument.class); return current.content().size(); }
@Override public boolean contains(Object t) { //TODO subpar implementation for a Set, use ARRAY_CONTAINS when available enforcePrimitive(t); JsonArrayDocument current = bucket.get(id, JsonArrayDocument.class); for (Object in : current.content()) { if (safeEquals(in, t)) { return true; } } return false; }
/** {@inheritDoc} */ @Override public Feature read(String uid) { assertFeatureExist(uid); return FEATURE_MAPPER.fromStore(getFeatureBucket().get(uid)); }
/** {@inheritDoc} */ @Override public Property<?> readProperty(String name) { assertPropertyExist(name); return PROPERTY_MAPPER.fromStore(getPropertyBucket().get(name)); }
private static void checkEachDocument(final Map<String, Document> expectedDocuments, final List<String> allDocumentIds, final Bucket bucket) { for (final String id : allDocumentIds) { final Object real = bucket.get(id); final Object expected = toJson(expectedDocuments.get(id).getDocument()); if (!deepEquals(real, expected)) { throw FailureHandler.createFailure( "Expected element # %s # is not found but # %s # was found.", toJson(expected), toJson(real)); } } }
/** {@inheritDoc} */ @Override public void disable(String uid) { assertFeatureExist(uid); Feature f1 = FEATURE_MAPPER.fromStore(getFeatureBucket().get(uid)); f1.disable(); update(f1); }
/** {@inheritDoc} */ @Override public void enable(String uid) { assertFeatureExist(uid); Feature f1 = FEATURE_MAPPER.fromStore(getFeatureBucket().get(uid)); f1.enable(); update(f1); }