When migrating from Couchbase SDK 2 to SDK 3 certain document formats seem to have been removed. Our current development which was based on Couchbase SDK 2, has extensively used the JsonDocument format (com.couchbase.client.java.document.JsonDocument
).
How can this format or an alternative output be used in Couchbase SDK 3 to handle the below-indicated API change?
This is one of the sample classes that used JsonDocument in the existing system.
import com.couchbase.client.java.Bucket;
import java.util.concurrent.TimeUnit;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
public class CouchbaseConnector {
private Bucket bucket;
private CouchbaseConnector(Bucket bucket) {
this.bucket = bucket;
}
public static Builder builder() {
return new Builder();
}
public JsonDocument insert(String id, String jsonString) {
JsonObject content = JsonObject.fromJson(jsonString);
return insert(id, content);
}
public JsonDocument insert(String id, JsonObject jsonObject) {
JsonDocument document = JsonDocument.create(id, jsonObject);
return insert(document);
}
public JsonDocument insert(JsonDocument document) {
return bucket.insert(document);
}
public JsonDocument retrieve(String id) {
return bucket.get(id);
}
public JsonDocument remove(String id) {
try {
return bucket.remove(id);
} catch (Exception e) {
return null;
}
}
public boolean flush() {
return bucket.bucketManager().flush();
}
}
For example, to in order to handle the insertion functionality in Couchbase SDK 3, I believe can be handled as mentioned below.
public void insert(String id, String jsonString, Collection collection) {
MutationResult upsertResult =null;
JsonObject content = JsonObject.fromJson(jsonString);
try{
//Insert Document
upsertResult = collection.upsert(id, content);
}Catch (CasMismatchException e){
//To Do: the caller is expected to re-do the whole "fetch-modify-update" cycle again
}
//Retrieve the document
GetResult getResult = collection.get(id).contentAsObject();
}
But how can I return a document format similar to JsonDocument or an alternative format without breaking the current code after the migration to Couchbase SDK 3?