Skip to content

Instantly share code, notes, and snippets.

@zerho
Last active December 15, 2015 10:18
Show Gist options
  • Save zerho/5244353 to your computer and use it in GitHub Desktop.
Save zerho/5244353 to your computer and use it in GitHub Desktop.
public class ServerActivity extends Activity {
public static String TAG = "UdaDB";
//constants
public static final String DATABASE_NAME = "CoreDB";
public static final String dDocName = "local";
public static final String dDocId = "_design/" + dDocName;
public static final String userNameViewName = "nameonly";
protected ObjectMapper mapper = new ObjectMapper();
protected static TDServer server;
protected static TDListener listener;
//static inializer to ensure that touchdb:// URLs are handled properly
{
TDURLStreamHandlerFactory.registerSelfIgnoreError();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startTouchDB();
}
protected void startTouchDB() {
String filesDir = getFilesDir().getAbsolutePath();
// Start an instance of TDServer listening on port 8888
try {
server = new TDServer(filesDir);
listener = new TDListener(server, 9999);
listener.start();
} catch (IOException e) {
Log.e(TAG, "Unable to create TDServer", e);
}
//install a view definition needed by the application
TDDatabase db = server.getDatabaseNamed(DATABASE_NAME);
TDView view = db.getViewNamed(String.format("%s/%s", dDocName, userNameViewName));
view.setMapReduceBlocks(new TDViewMapBlock() {
@Override
public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
if(document.containsKey("nome")){
emitter.emit( "nome" , document.get("nome"));
}
}
}, null, "1.0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment