/** * Generate a transit url. * * @param bucket the target bucket * @return a transit url based on the bucket name and the CouchbaseClusterControllerService name */ protected String getTransitUrl(final Bucket bucket, final String docId) { return "couchbase://" + bucket.name() + "/" + docId; }
@Override public void close() { if (!_bucket.isClosed()) { try { _bucket.close(); } catch (Exception e) { log.warn("Failed to close bucket", e); } } try { _cluster.disconnect(); } catch (Exception e) { log.warn("Failed to disconnect from cluster", e); } } }
/** * 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; }
private CouchbaseCluster createCouchbaseCluster() { return CouchbaseCluster.create(getCouchbaseEnvironment(), getContainerIpAddress()); }
@Override public WriteResponse write(D record) throws IOException { try { D doc = _bucket.upsert(record); return new GenericWriteResponse(doc); } catch (Exception e) { throw new IOException("Failed to write to Couchbase cluster", e); } }
/** * 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); }
/** * Performs the {@link #upsert(String, String, Map)} operation via Key/Value ("upsert"). * * If this option should be used, the "-p couchbase.upsert=true" property must be set. * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status upsertKv(final String docId, final Map<String, ByteIterator> values) { waitForMutationResponse(bucket.async().upsert( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo )); return Status.OK; }
/** * Performs the {@link #update(String, String, Map)} operation via Key/Value ("replace"). * * @param docId the document ID * @param values the values to update the document with. * @return The result of the operation. */ private Status updateKv(final String docId, final Map<String, ByteIterator> values) { waitForMutationResponse(bucket.async().replace( RawJsonDocument.create(docId, documentExpiry, encode(values)), persistTo, replicateTo )); return Status.OK; }
@Override public Bucket openBucket(String bucketName){ if (bucketPasswords.containsKey(bucketName)) { return cluster.openBucket(bucketName, bucketPasswords.get(bucketName)); } return cluster.openBucket(bucketName); }
@Override public <K> boolean remove(K key, Serializer<K> serializer) throws IOException { try { bucket.remove(toDocumentId(key, serializer)); return true; } catch (DocumentDoesNotExistException e) { return false; } }
/** * Performs the {@link #delete(String, String)} (String, String)} operation via Key/Value ("remove"). * * @param docId the document ID. * @return The result of the operation. */ private Status deleteKv(final String docId) { waitForMutationResponse(bucket.async().remove( docId, persistTo, replicateTo )); return Status.OK; }
private void stopCluster() { getCouchbaseCluster().disconnect(); getCouchbaseEnvironment().shutdown(); }
@Override public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException { final String docId = toDocumentId(key, keySerializer); final Document doc = toDocument(docId, value, valueSerializer); try { bucket.insert(doc); return true; } catch (DocumentAlreadyExistsException e) { return false; } }
/** * Disconnect from the Couchbase cluster. */ @OnDisabled public void shutdown() { if(cluster != null){ cluster.disconnect(); cluster = null; } }
@Override public <K> boolean containsKey(K key, Serializer<K> keySerializer) throws IOException { return bucket.exists(toDocumentId(key, keySerializer)); }
@Override public <K, V> void put(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException { final String docId = toDocumentId(key, keySerializer); final Document doc = toDocument(docId, value, valueSerializer); bucket.upsert(doc); }
private Bucket openBucket(String bucketName, String password) { CouchbaseCluster cluster = getCouchbaseContainer().getCouchbaseCluster(); return cluster.openBucket(bucketName, password); } }
@Override public void delete(final String id) { bucket.remove(N1Q.qualifyId(SESSION, id)); }