Skip to content

Instantly share code, notes, and snippets.

@ToxicBakery
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToxicBakery/3a9025b75aeaa2f1b10f to your computer and use it in GitHub Desktop.
Save ToxicBakery/3a9025b75aeaa2f1b10f to your computer and use it in GitHub Desktop.
Couchbase (Java) vs Couchbase Mobile (Android)
// Comparison of data management
// Given a simple object
class MyObject {
String name;
int value;
}
// Create an instance with some values
MyObject myObjectInstance = new MyObject();
myObjectInstance.name = "One";
myObjectInstance.value = 1;
Couchbase:
//////////////////////////////
// Create Document
client.set("myObjectInstance", myObjectInstance).get();
// Retrieve Document
MyObject myObjectInstance = (MyObject) client.getDocument("myObjectInstance");
// Update Document
MyObject myObjectInstance = (MyObject) client.getDocument("myObjectInstance");
myObjectInstance.name = "newName";
client.set("myObjectInstance", myObjectInstance);
//////////////////////////////
Couchbase Mobile:
//////////////////////////////
// Define Document
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", myObjectInstance.name);
map.put("value", myObjectInstance.value);
// Create Document
Document document = database.getDocument("myObjectInstance");
UnsavedRevision rev = document.createRevision();
rev.setUserProperties(map);
rev.save();
// Retrieve Document
Document document = database.getDocument("myObjectInstance");
MyObject obj = new MyObject();
obj.name = document.getProperty("name");
obj.value = document.getProperty("value");
// Update Document
Document document = database.getDocument("myObjectInstance");
UnsavedRevision rev = document.createRevision();
rev.getUserProperties().put("name", "newName");
rev.save();
//////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment