couchbaselabs / TouchDB-Android

CouchDB-compatible mobile database; Android version

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

titanium module that use TouchDB

mariaCP opened this issue · comments

I try to use your code in my module, the problem is that my class doesn't extend activity, therefore I can't write getFilesDir().getAbsolutePath();
I try to write the code in this way:

@Kroll.method
public void example()
{
    String filesDir = "/files";
    TDServer server=null;
    TDListener listener=null;

            try {
                server = new TDServer(filesDir);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        listener = new TDListener(server, 8888);
        listener.start();
} 

but I have an IOException.. Have you any idea to help me? Thank you!
Maria

Two things:

  1. If you have IOException it helps to include the full stack trace. My guess is that it expects an absolute path and simply won't let you write to /files.
  2. I took a look at the Titanium Module that the other user on the list started, and in his KrollModule he accessed the context this way:

https://github.com/pegli/ti_touchdb/blob/master/mobile/android/src/com/obscure/TiTouchDB/TitouchdbModule.java

In the initActivity method they pass in the Activity, which is a Context:

@Override
protected void initActivity(Activity activity) {
    try {
        String filesDir = activity.getFilesDir().getAbsolutePath();
        server = new TDServer(filesDir);
    }
    catch (IOException e) {
        throw new RuntimeException("Error creating TouchDB server: " + e.getMessage());
    }
}

If I write the method in this way I have to pass the activity parameter when I call this method and I don't know how. I try to write the method in this way:

public void example()
{
    TiApplication appContext = TiApplication.getInstance();
    Activity activity = appContext.getCurrentActivity();
    System.out.println("activity: "+ activity.toString());
    String filesDir = activity.getFilesDir().getAbsolutePath();
            TDServer server=null;
            TDListener listener=null;
            try {
                server = new TDServer(filesDir);
            } catch (IOException e) {
                e.printStackTrace();
            }
        listener = new TDListener(server, 5984);
        listener.start();
}

Where is the problem? I have this error:
[ERROR]TiApplication( 295) [437,9940] Sending event: exception on thread: Thread-13 msg:java.lang.ExceptionInInitializerError; Titanium 2.1.0,2012/06/28 12:16,6e3cab6
[ERROR][TiApplication( 295)] java.lang.ExceptionInInitializerError
[ERROR][TiApplication( 295)] at com.couchbase.touchdb.listener.TDHTTPServer.serve(TDHTTPServer.java:40)
[ERROR][TiApplication( 295)] at com.couchbase.touchdb.listener.TDListener.run(TDListener.java:34)
[ERROR][TiApplication( 295)] at java.lang.Thread.run(Thread.java:1096)
[ERROR][TiApplication( 295)] Caused by: java.util.MissingResourceException: Can't find resource for bundle javax.servlet.http.LocalStrings, key en_US
[ERROR][TiApplication( 295)] at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:244)
[ERROR][TiApplication( 295)] at java.util.ResourceBundle.getBundle(ResourceBundle.java:133)
[ERROR][TiApplication( 295)] at javax.servlet.http.HttpServlet.(HttpServlet.java:97)
[ERROR][TiApplication( 295)] ... 3 more

Can you check to see if you have the servlet-api-2.4.jar file available to your project (it's one of the dependencies of the Listener module).

servlet-api-2.4.jar file is present in my module. I try to replace it with servlet-api-2.5.jar and servlet-api-2.3.jar but it doesn't work.. Have you any idea?

the complete error is the follow:

[WARN][System.err( 485)] [Wed Jul 18 14:50:32 GMT+00:00 2012] TJWS: MIME map can't be loaded:java.lang.NullPointerException
[WARN][dalvikvm( 485)] Exception Ljava/util/MissingResourceException; thrown while initializing Ljavax/servlet/http/HttpServlet;
[WARN][dalvikvm( 485)] threadid=13: thread exiting with uncaught exception (group=0x40015560)
[ERROR]TiApplication( 485) [3976,8797] Sending event: exception on thread: Thread-15 msg:java.lang.ExceptionInInitializerError; Titanium 2.1.0,2012/06/28 12:16,6e3cab6
[ERROR][TiApplication( 485)] java.lang.ExceptionInInitializerError
[ERROR][TiApplication( 485)] at com.couchbase.touchdb.listener.TDHTTPServer.serve(TDHTTPServer.java:40)
[ERROR][TiApplication( 485)] at com.couchbase.touchdb.listener.TDListener.run(TDListener.java:34)
[ERROR][TiApplication( 485)] at java.lang.Thread.run(Thread.java:1019)
[ERROR][TiApplication( 485)] Caused by: java.util.MissingResourceException: Can't find resource for bundle 'javax.servlet.http.LocalStrings_en_US', key ''
[ERROR][TiApplication( 485)] at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:225)
[ERROR][TiApplication( 485)] at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:425)
[ERROR][TiApplication( 485)] at java.util.ResourceBundle.getBundle(ResourceBundle.java:133)
[ERROR][TiApplication( 485)] at javax.servlet.http.HttpServlet.(HttpServlet.java:97)
[ERROR][TiApplication( 485)] ... 3 more
[ERROR][AndroidRuntime( 485)] FATAL EXCEPTION: Thread-15
[ERROR][AndroidRuntime( 485)] java.lang.ExceptionInInitializerError
[ERROR][AndroidRuntime( 485)] at com.couchbase.touchdb.listener.TDHTTPServer.serve(TDHTTPServer.java:40)
[ERROR][AndroidRuntime( 485)] at com.couchbase.touchdb.listener.TDListener.run(TDListener.java:34)
[ERROR][AndroidRuntime( 485)] at java.lang.Thread.run(Thread.java:1019)
[ERROR][AndroidRuntime( 485)] Caused by: java.util.MissingResourceException: Can't find resource for bundle 'javax.servlet.http.LocalStrings_en_US', key ''
[ERROR][AndroidRuntime( 485)] at java.util.ResourceBundle.missingResourceException(ResourceBundle.java:225)
[ERROR][AndroidRuntime( 485)] at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:425)
[ERROR][AndroidRuntime( 485)] at java.util.ResourceBundle.getBundle(ResourceBundle.java:133)
[ERROR][AndroidRuntime( 485)] at javax.servlet.http.HttpServlet.(HttpServlet.java:97)
[ERROR][AndroidRuntime( 485)] ... 3 more

Maria, I'm in the process of refactoring the TouchDB Titanium module for Android so it doesn't use the HTTP listener. Can you go to the module project at https://github.com/pegli/ti_touchdb and try building and running again? Note that I'm only about halfway through the refactoring process right now, but you should at least be able to get the module running in your app, create databases, and manage documents. Views and replication are not yet done.

I have tried it and it seems run but I need replication property of Touchdb therefore I can't use it..

I'll be working on replication over the next few days and will post a new build once that feature is in place.

I just posted a new build for Android that has initial support for replication. I've only tested pull replication at this time, but it would be great if you could try it out and let me know how it works for you. Here's an example of how pull works in Titanium:

var server = require('com.obscure.titouchdb');
var db = server.databaseNamed('mygreatdb');
db.ensureCreated();

var repl = db.pullFromDatabaseAtURL('http://touchbooks.iriscouch.com/books');
repl.addEventListener('progress', function(e) {
  // called multiple times during replication
  // e.type = progress or stopped
  // e.completed = number of completed docs
  // e.total = total number of docs to replicate
});
repl.addEventListener('stopped', function(e) {
  // called when replication is done
});
repl.start();

Push should work. Persistent replication is not yet implemented.

Can you give me an example of push? I try to replace pullFromDatabaseAtURL function with pushToDatabaseAtURL function but it doesn't work..

In alternative can somebody tell me if I can make push with http POST and an example of it?thank you..

Are there any errors in the log?

Do you refer to build.log? In it there isn't errors.. It simply prints "Sending 0 revisions".

I'm referring to the "adb logcat" log. TouchDB outputs a lot of data there, even during normal operation. All these messages are useful to understanding what is going on.

No, I have not error:
08-28 13:51:35.796: V/TDDatabase(1564): com.couchbase.touchdb.replicator.TDPusher$2$1@43f45108: Sent [{74eab58758ab2d4cdd8be0374a0028e2 #2-b3e9fce6b7178e6d4a0cd6f349047c60DEL}, {74eab58758ab2d4cdd8be0374a001a6d #2-961e1f1ee67bbd6678065eded2e6aedfDEL}, {74eab58758ab2d4cdd8be0374a001a4b #2-576274db65d6824374f7692505d6a453DEL}, {74eab58758ab2d4cdd8be0374a00134e #2-07a549feb293b19a29d0c8ed688c0010DEL}, {74eab58758ab2d4cdd8be0374a002d77 #2-1a8e2d5b6f84ebd67696837a792f339bDEL}, {74eab58758ab2d4cdd8be0374a006ab6 #2-4b051b8b17e5b2b85f8b598871e91c00DEL}, {74eab58758ab2d4cdd8be0374a00527b #2-45f205ef741b3628f7a08527ea35e00cDEL}, {74eab58758ab2d4cdd8be0374a005d76 #2-9966ad854193d41c8d33d6361484a1c0DEL}, {74eab58758ab2d4cdd8be0374a005250 #2-ee990bf0691062b20ee7e329f213c225DEL}, {74eab58758ab2d4cdd8be0374a004265 #2-16a18f12bec391f01a329b490afc5befDEL}, {74eab58758ab2d4cdd8be0374a003a8c #2-c95de45dac0599dcb0dd2a6f1f9d69daDEL}, {74eab58758ab2d4cdd8be0374a00311c #2-5bbbd3638d76d8970870da334639012dDEL}, {74eab58758ab2d4cdd8be0374a006e3d #2-60e1c720722e506e9b7fb3871f2d3521DEL}, {74eab58758ab2d4cdd8be0374a006f70 #2-009432c0ee1d602cda40009b1ccc8aa9DEL}, {74eab58758ab2d4cdd8be0374a00799d #2-2b582b1ef79d03b25f571773d71db060DEL}, {74eab58758ab2d4cdd8be0374a007048 #2-c0eb29ce8b8b4419bf4c996c3e6ae9b8DEL}, {74eab58758ab2d4cdd8be0374a008860 #2-34ddc0f587ddab563493a50706df801cDEL}, {74eab58758ab2d4cdd8be0374a008cd3 #2-d0bbc169005ed9e78245248edda3801eDEL}, {74eab58758ab2d4cdd8be0374a0088b8 #2-50a799943beaef00ea3156cc1ad5e1bfDEL}, {74eab58758ab2d4cdd8be0374a009106 #3-a887a1be10bda01de86ab3817609d44bDEL}, {74eab58758ab2d4cdd8be0374a00bae4 #2-227f28e38b7ff8fcbebd336607a0b9bdDEL}, {74eab58758ab2d4cdd8be0374a00ad79 #2-df01db340afcc6dfd9d3769ab4c260cbDEL}, {74eab58758ab2d4cdd8be0374a00a1f1 #2-98afdb29213d345d7e96edd387f99acfDEL}, {74eab58758ab2d4cdd8be0374a009a2d #2-d5da83e3ee01fa60bd766bf635e5686fDEL}, {74eab58758ab2d4cdd8be0374a00d2a0 #2-3f9587d836ec8d617094fb3237178c84DEL}, {74eab58758ab2d4cdd8be0374a00cf7a #2-dd58f83d9eda0b892a4db50f3daaf8b1DEL}, {74eab58758ab2d4cdd8be0374a00c159 #2-e16a47b158bfb7f69daa95226b78ea61DEL}, {74eab58758ab2d4cdd8be0374a012df5 #2-17f6408999fc5944ec310131cd278d21DEL}, {74eab58758ab2d4cdd8be0374a01276c #2-3865def7b56507b894dd8ca02b3bc6e7DEL}, {74eab58758ab2d4cdd8be0374a011777 #2-ba26e57724acb0d7ba75d1fec4dcf44eDEL}, {74eab58758ab2d4cdd8be0374a010d93 #2-27d52daf709a8b60e33d50bd177c5952DEL}, {74eab58758ab2d4cdd8be0374a010442 #2-8688f54eaaa97f477d2cad5f2e7ceb8cDEL}, {74eab58758ab2d4cdd8be0374a00fc5b #2-f0c212a3f603949b3e0eabd398a4f2e5DEL}, {74eab58758ab2d4cdd8be0374a00f379 #2-ad5b4aa27c7a03aba3d61e39c024b99fDEL}, {74eab58758ab2d4cdd8be0374a00e08f #3-a6c2b11fafaf36a53fe5397adf1ac8f6DEL}, {74eab58758ab2d4cdd8be0374a00e367 #2-59dd45289f771dc060274f07f9510cc0DEL}, {74eab58758ab2d4cdd8be0374a00ef67 #3-62acab3c89da4a3d9b325706dd355cc3DEL}, {74eab58758ab2d4cdd8be0374a013df5 #2-e8bac31a7e9b256c7cf7de75f201160fDEL}, {74eab58758ab2d4cdd8be0374a014d98 #10-56184a42-9265-4900-9976-3f8017430553}, {74eab58758ab2d4cdd8be0374a015546 #3-276012f4-e1f6-4134-82c9-0e9c621f33aa}, {74eab58758ab2d4cdd8be0374a0159b9 #3-095e0327-303d-470e-a162-b301930454b3}, {74eab58758ab2d4cdd8be0374a0163d8 #3-0d9eac32-14f2-4e82-b066-5d6315e7a986}, {74eab58758ab2d4cdd8be0374a01721a #3-4785aac3-a053-49e3-8c09-ab6a1f0d8c90}, {74eab58758ab2d4cdd8be0374a017812 #3-ff83ba36-c7b5-4f91-9455-c811659e84ee}, {74eab58758ab2d4cdd8be0374a017f9a #3-b6006364-e3cc-4dcc-979a-2b9d46675a8c}, {74eab58758ab2d4cdd8be0374a018e30 #3-6e147814-f6aa-45a1-83e8-45a97fb6dae6}, {74eab58758ab2d4cdd8be0374a019221 #3-4929fe9a-cc81-4a10-b402-7d685cba2bb4}, {74eab58758ab2d4cdd8be0374a019783 #3-5decc214-8acb-4263-8a9a-4c5b12e8f17c}, {74eab58758ab2d4cdd8be0374a01a5f0 #3-6095f0fd-64cb-4308-a05e-0d6f1f6db953}, {74eab58758ab2d4cdd8be0374a01aee1 #3-e4d56f8e-ef21-4d1c-9236-53177c7504e8}, {74eab58758ab2d4cdd8be0374a01b82b #4-140112a3-2454-48c5-a92b-b20b09c48aa7}, {_design/myDb #3-6d1972e3-fc16-490e-8675-b84ab40e2bb4}]
08-28 13:51:35.806: V/TDDatabase(1564): TDPusher[http://myIp:5984/myDb]: Setting lastSequence to 129 from( null)
08-28 13:51:35.849: V/TDDatabase(1564): TDPusher[http://myIp:5984/myDb] STOPPED
08-28 13:51:35.849: V/TDDatabase(1564): TDPusher[http://myIp:5984/myDb] checkpointing sequence=129
08-28 13:51:35.606: I/TiAPI(1564): {"type":"progress","source":{"createTarget":false,"error":null,"running":true,"status":0,"pull":false,"total":0,"completed":false,"remoteURL":"http://myIp:5984/myDb","mode":3,"_events":{"progress":{},"stopped":{}}},"total":0,"error":null,"running":true,"status":0,"completed":0}

That output looks like it successfully pushed revisions and checkpointed its sequence at 129. What does the data look like on the remote server?

I look "http://myIp:5984/_utils/database.html?myDb/_all_docs" and the property of the documents that I have changed in the database is unchanged..

Did you initiate the push after you made the changes? It looks like this replication wasn't continuous and the replicator stopped.

This is my code:
var replicationComplete = false, checkCount = 15;
try {
var query = db.getAllDocuments();
var rows = query.rows();

    var c = 0;
    var revisions = [], revisionProperties = [];
    while (row = rows.nextRow()) {
        var doc = row.document; 
        var props = doc.properties;
        props.comment = "my comment";
        doc.putProperties(props);
        var rev = doc.currentRevision;
        var p = rev.properties;
        revisions.push(rev);
        revisionProperties.push(p);
        c++;
        }

    db.putChanges(revisionProperties, revisions);

    var repl = db.pushToDatabaseAtURL('http://myIp:5984/myDb');   

    repl.addEventListener('progress', function(e) {
    Ti.API.info(JSON.stringify(e));
  });
  repl.addEventListener('stopped', function(e) {
    Ti.API.info(JSON.stringify(e));
    replicationComplete = true;
  });
  repl.start();
}
catch (e) {
    Ti.API.info("exception!");
}

var interval = setInterval(function() {
  if (replicationComplete) {
    Ti.API.info("replication done!");
    clearInterval(interval);
  }
  else if (checkCount-- < 0) {
    clearInterval(interval);
    Ti.API.info("timed out waiting for replication");
  }
}, 5000);

What do you think? thank you

I'm not familiar with this javascript API, so I cannot say. But it looks like a one-time replication to me. You'll need to ask the author ti_touchdb.

ok, thank you!