Skip to content

Instantly share code, notes, and snippets.

@simplexityx
Created June 19, 2019 08:58
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 simplexityx/0a226ab5f76f619ea6a37f89ec7dfe7f to your computer and use it in GitHub Desktop.
Save simplexityx/0a226ab5f76f619ea6a37f89ec7dfe7f to your computer and use it in GitHub Desktop.
package com.example.couchbasetest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.couchbase.lite.BasicAuthenticator;
import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.DataSource;
import com.couchbase.lite.Database;
import com.couchbase.lite.DatabaseConfiguration;
import com.couchbase.lite.Document;
import com.couchbase.lite.Endpoint;
import com.couchbase.lite.Expression;
import com.couchbase.lite.MutableDocument;
import com.couchbase.lite.Query;
import com.couchbase.lite.QueryBuilder;
import com.couchbase.lite.Replicator;
import com.couchbase.lite.ReplicatorConfiguration;
import com.couchbase.lite.ResultSet;
import com.couchbase.lite.SelectResult;
import com.couchbase.lite.URLEndpoint;
import java.net.URI;
import java.net.URISyntaxException;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "LOG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the database (and create it if it doesn’t exist).
DatabaseConfiguration config = new DatabaseConfiguration(getApplicationContext());
Database database = null;
try {
database = new Database("mydb", config);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// Create a new document (i.e. a record) in the database.
MutableDocument mutableDoc = new MutableDocument()
.setFloat("version", 2.0F)
.setString("type", "SDK");
// Save it to the database.
try {
database.save(mutableDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
// Update a document.
mutableDoc = database.getDocument(mutableDoc.getId()).toMutable();
mutableDoc.setString("language", "Java");
try {
database.save(mutableDoc);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
Document document = database.getDocument(mutableDoc.getId());
// Log the document ID (generated by the database) and properties
Log.i(TAG, "Document ID :: " + document.getId());
Log.i(TAG, "Learning " + document.getString("language"));
// Create a query to fetch documents of type SDK.
Query query = QueryBuilder.select(SelectResult.all())
.from(DataSource.database(database))
.where(Expression.property("type").equalTo(Expression.string("SDK")));
ResultSet result = null;
try {
result = query.execute();
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
Log.i(TAG, "Number of rows :: " + result.allResults().size());
// Create replicators to push and pull changes to and from the cloud.
Endpoint targetEndpoint = null;
try {
targetEndpoint = new URLEndpoint(new URI("ws://10.0.2.2:4984/demobucket"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
ReplicatorConfiguration replConfig = new ReplicatorConfiguration(database, targetEndpoint);
replConfig.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);
// Add authentication.
replConfig.setAuthenticator(new BasicAuthenticator("admin", "pass"));
// Create replicator.
Replicator replicator = new Replicator(replConfig);
// Listen to replicator change events.
replicator.addChangeListener(change -> {
if (change.getStatus().getError() != null) {
Log.i(TAG, "Error code :: " + change.getStatus().getError().getCode());
}
});
// Start replication.
replicator.start();
}
}
{
"interface":":4984",
"logging": {
"log_file_path": "/var/tmp/sglogs",
"console": {
"log_level": "debug",
"log_keys": ["*"]
},
"error": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 180
}
},
"warn": {
"enabled": true,
"rotation": {
"max_size": 20,
"max_age": 90
}
},
"info": {
"enabled": false
},
"debug": {
"enabled": false
}
},
"databases": {
"demobucket": {
"import_docs": "continuous",
"enable_shared_bucket_access":true,
"bucket":"demobucket",
"server": "http://cb-server:8091",
"username": "admin",
"password": "password",
"num_index_replicas":0,
"users":{
"GUEST": {"disabled":true},
"admin": {"password": "password", "admin_channels": ["*"]}
},
"revs_limit":20
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment