More Related Content Similar to CouchDB on Android (20) More from Sven Haiges (11) CouchDB on Android5. Erlang on
Android!
Source: http://couchdb.apache.org/
7. CouchDB
• document-based, RESTful HTTP API
• replication built-in
• both database and web server
• no locks, uses multi-version concurrency
control
• Map & Reduce Functions = Views
24. AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.flavor.relax" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".RelaxActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:exported="false" android:enabled="true"
android:name="com.couchbase.android.CouchbaseService" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
25. in your Activity...
private final ICouchbaseDelegate mDelegate = new ICouchbaseDelegate() {
public void couchbaseStarted(String host, int port) {
Toast.makeText(RelaxActivity.this, host + ":" + port, Toast.LENGTH_LONG).show();
}
public void installing(int completed, int total) {
Log.d("demo", "Installing... " + completed + "/" + total);
}
public void exit(String error) {
Log.d("demo", error);
}
};
26. in your Activity...
@Override
protected void onResume() {
super.onResume();
CouchbaseMobile couch = new CouchbaseMobile(getBaseContext(), mDelegate);
couchServiceConnection = couch.startCouchbase();
}
38. Todo.java
public class Todo {
private String id;
private String rev;
private String text;
private long timestamp;
public Todo(String id, String rev, String text, long timestamp) {
super();
this.id = id;
this.rev = rev;
this.text = text;
this.timestamp = timestamp;
}
...
}
39. Creating
String value = input.getText().toString();
JSONObject obj = new JSONObject();
try {
obj.put("type", "todo");
obj.put("text", value);
obj.put("timestamp", new Date().getTime());
JSONObject result = AndCouch.put(baseURL
+ DB_NAME + "/"
+ UUID.randomUUID().toString(),
obj.toString()).json;
if (result.getBoolean("ok") == true)
refreshTodos();
} catch (JSONException e) {
e.printStackTrace();
}
40. Reading (all)
try {
todos.clear();
JSONObject json = AndCouch.get(baseURL + DB_NAME + "/_design/todo/_view/all").json;
JSONArray rows = json.getJSONArray("rows");
for (int i = 0; i < rows.length(); i++) {
JSONObject todoObj = rows.getJSONObject(i);
JSONArray value = todoObj.getJSONArray("value");
todos.add(new Todo(todoObj.getString("id"), value.getString(1),
value.getString(0), todoObj.getLong("key")));
}
todos.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
41. Reading (all)
try {
todos.clear();
JSONObject json = AndCouch.get(baseURL + DB_NAME + "/_design/todo/_view/all").json;
JSONArray rows = json.getJSONArray("rows");
for (int i = 0; i < rows.length(); i++) {
JSONObject todoObj = rows.getJSONObject(i);
JSONArray value = todoObj.getJSONArray("value");
todos.add(new Todo(todoObj.getString("id"), value.getString(1),
value.getString(0), todoObj.getLong("key")));
}
todos.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
42. Design Documents
• normal json documents, but ids begin with
_design/, e.g. _design/myapp (no %2F :-)
• contain views (Map & Reduce functions) as
well as other application functions
(validation, show/list)
43. Simple Design Doc
{
"_id":"_design/todo",
"views" : {
"all" : {
"map": "function(doc) { if (doc.type && doc.type === 'todo')
{ emit(doc.timestamp, [doc.text, doc._rev]); }};"
}
}
}
44. Deleting
SparseBooleanArray spar = getListView().getCheckedItemPositions();
for (int i = 0; i < todos.getCount(); i++) {
if (spar.get(i)) {
Todo todo = todos.getItem(i);
getListView().setItemChecked(i, false);
try {
AndCouch.httpRequest("DELETE", baseURL + DB_NAME + "/" + todo.getId() + "?
rev=" + todo.getRev(),
null, new String[][] {});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
refreshTodos();
45. Remote Push
JSONObject obj = new JSONObject();
try {
obj.put("create_target", true);
obj.put("source", baseURL + DB_NAME);
obj.put("target",
"http:// hansamann.iriscouch.com/todo");
JSONObject result = AndCouch.post(baseURL + "_replicate",
obj.toString()).json;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
47. CouchApps
• JavaScript and HTML5 apps served from
CouchDB
• No middle tier, Couch is DB + application
server
• CouchApp.org hosts the CouchApp
development tool that pushes changes to a
Couch instance
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n