couchbaselabs / couchbase-lite-android-ektorp

Ektorp adapter for couchbase-lite-android
Apache License 2.0
3 stars 2 forks source link

Views not working #6

Open hanasim opened 10 years ago

hanasim commented 10 years ago

We are using Couchbase Lite 1.0.1 (and we are using forked version of Ektorp as given in the readme of couchbase-lite-android-ektorp).

We are using Ektorp wrapper to access CBLite database. Things seems to work if we query the database by using 'find' method (CouchDbConnector from Ektorp). However, if we query the database using views (created with Ektorp annotations), it doesn't seem to work at all. The views return empty array always.

Views used to work fine in Couchbase Lite 1.0.3-beta.

This is an important issue for us to use CBL in our product. Would be grateful for an early response.

tleyden commented 10 years ago

Can you provide sample code of how you are creating views?

Also any errors in logs?

swagata-acharyya commented 10 years ago

Below are the details that you wanted. Let me know if you need anything else.

One sample view:

@View(name = "all", map = "function(doc) { if (doc.type === 'Product') emit(null, doc._id)}")

Below is how we are creating the design docs:

public void initStandardDesignDocument() {
        final CouchDbConnector db = getDatabaseConnector();
        DesignDocument standard = db.find(DesignDocument.class, designDocumentName);
        if (null == standard) {
            standard = new DesignDocument(designDocumentName);
        }
        final DesignDocument generated = new StdDesignDocumentFactory().generateFrom(this);
        final boolean changed = standard.mergeWith(generated);
        if (changed) {
            db.update(standard);
        }
        else
        {
            db.create(standard);
        }
    }

I could see the design docs getting generated in the tablet database. Exported the cblite file and verified it. All views are also created properly.

There are some errors in the log. Below is one stacktrace. I am not sure whether this is related to this issue though.

  1. This particular line keeps occurring numerous times in error log:

com.couchbase.lite.support.RemoteMultipartDownloaderRequest@42501210: executeRequest() finally

  1. This also keeps repeating
08-21 11:23:19.885: E/ChangeTracker(8127): org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
08-21 11:23:19.885: E/ChangeTracker(8127):  at [Source: org.apache.http.conn.EofSensorInputStream@4251d568; line: 1, column: 2]
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.impl.JsonParserMinimalBase._reportUnexpectedChar(JsonParserMinimalBase.java:442)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.impl.Utf8StreamParser._handleUnexpectedValue(Utf8StreamParser.java:2090)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.impl.Utf8StreamParser._nextTokenNotInObject(Utf8StreamParser.java:606)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.impl.Utf8StreamParser.nextToken(Utf8StreamParser.java:492)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2770)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2718)
08-21 11:23:19.885: E/ChangeTracker(8127):  at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1909)
08-21 11:23:19.885: E/ChangeTracker(8127):  at com.couchbase.lite.replicator.ChangeTracker.run(ChangeTracker.java:319)
08-21 11:23:19.885: E/ChangeTracker(8127):  at java.lang.Thread.run(Thread.java:841)

Thanks!

swagata-acharyya commented 10 years ago

I have created a small (quick) project to explain this issue in detail. I have followed almost the same procedure that we have followed while building our app. I have removed unnecessary and already working modules and pinpointed the View issue in code.

This app is all about creating a CBL database in tablet and then creating a record and fetch and display it in Log.

You can see the code at https://github.com/swagata-acharyya/HelloCBL

Note:

  1. It is very raw, so please concentrate the areas where we deal with CBLite. Those are almost similar to our actual app. Other aspects are somehow 'made to work' to show you something.
  2. In MainActivity.java I have provided some comments where you can see find(_id) method of DAOs work, where as getAll() does not.
  3. I have not given proper attributes in some places. I will keep adding them.
  4. I have not put any code that does the sync from cloudant. That works and I have verified it.

Please understand the code was created with real hurry to produce something for you to look at. You can however get the basic ideas on how we are using CBLite in our app.

I will keep updating the project. Do you know of any other project where the developer used ektorp with CBLite and the code or some snippets are available?

tleyden commented 10 years ago

@swagata-acharyya Thanks for the sample project!

This error:

org.codehaus.jackson.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value 

seems like it's related to couchbase lite connecting to a CouchDB or Sync Gateway server, and getting errors from the _changes feed. Let's come back to that later as a separate issue. I don't think it's related to your views not working.

Regarding sample apps that use ektorp with CBLite, the only one I know is that if you look in the git history of GrocerySync-Android, at some point it used to use Ektorp, however I believe it used native views (1). So if you can find the old version, you will see examples.

TodoLite-Phonegap doesn't use ektorp, but it does use Javascript views. So if you can get that running, it would be an easy way to verify that javascript based views are working.

To verify javascript based views are working, you could also do the following:

I haven't had a chance to try to run your project yet, but I will try to give it a shot.

(1) if you are using Ektorp from a native app, you don't need to write your views in javascript. You can instead write them in native java code and call the views from Ektorp. This is simpler and I'd recommend trying to get this to work. I believe an old version of GrocerySync provides an example of doing this.

Another alternative, if you are writing a native java app (as opposed to a phonegap app), you don't even need to use Ektorp at all. You can just use the native public api as documented here: http://developer.couchbase.com/mobile/develop/references/couchbase-lite/couchbase-lite/index.html

peetersn commented 10 years ago

Hi @tleyden, (I'm in the team too).

Thanks for the great suggestions. The main reason@swagata-acharyya built this app is to give you a chance to immediately see what it going on. It would be awesome if you could give it a spin.

A few elements of answer. Our app is definitely native (we are migration from a very complex app using the legacy Couchbase mobile).

1) Regarding _changes: indeed, we are connecting to Cloudant which has (more or less) the same _changes feed structure as normal CouchDB. That's our backend until we fully stabilize the mobile side of things. Let's keep this as a separate issue. @swagata-acharyya: please create one.

2) We tried to use the native API (and we're willing to get there in the future) but the effort to migrate is so significant that we'd rather start by getting this (Ektorp) to work. I'll forward you a mail I sent earlier today to J Chris and Jessica about this. We have an operational issue (we hit the limits of Couchbase Mobile (legacy) on all fronts) to get through and we really want to migrate asap.

swagata-acharyya commented 10 years ago

Thanks a lot @tleyden for the suggestions.

I will explore more on the point where you suggested to create views with Java and use them via ektorp in our app. I have seen and explored native API and it works well. Only thing is migrating an existing app based on ektorp and couchbase mobile with javascript views to all native approach leads to a lot of refactoring.

As @peetersn mentioned, we have started to get this ektorp channel to work first. At some time in future we will perhaps need to migrate to all native API. But considering the time constraints and operational problems with existing system right now, it is quicker to go the ektorp way as it is something that is already set up and running in our app and we need some modifications in it (that we have almost done) to get going with CBLite.

So at this point this particular issue with the Views is stopping us from moving forward.

swagata-acharyya commented 10 years ago

@tleyden here is what I tried to find whether the view I created actually worked. I was using Postman extension of Chrome for this.

  1. Deployed liteserve project in android tab.
  2. POSTed the design document with view "all"
  3. POSTed some documents that satisfy my "all" criteria. Basically a type check.
  4. Then performed a GET on the db with the design doc and view. Something like:

http://[TAB IP]:5984/hello/_design/SimplePojo/_view/all?include_docs=true

It returned the docs that satisfied the criteria.

So I can confirm the view that we are creating indeed works. But for some reason when I access them with ektorp and CBLite they always return a blank array.

Thanks!

tleyden commented 10 years ago

Can you do the following?

swagata-acharyya commented 10 years ago

Here are the links:

  1. https://gist.github.com/swagata-acharyya/834f2ee62fc3cdb0fcc4 is from the sample app where it is unable to get the data
  2. https://gist.github.com/swagata-acharyya/7a4ea3e5f8c9fe0d0700 is from Liteserv project logcat output.

Thanks!

peetersn commented 10 years ago

Hi @tleyden,

Did you have by any chance some time to look into this issue? As said before please let me and Swagata know if you need more info.

Thanks.

Nicolas