couchbaselabs / TouchDB-Android

CouchDB-compatible mobile database; Android version
239 stars 60 forks source link

CouchDbConnector queryView(ViewQuery query) vs. queryView(ViewQuery query, Class<T> type) #85

Open bhaskarmurthy opened 11 years ago

bhaskarmurthy commented 11 years ago

When running a query using queryView(ViewQuery), I get the right results in ViewResult. However, when using queryView(ViewQuery, Class<T>), I get 0 results.

My TDView looks like:

TDView taskGroupAllView = db.getViewNamed("TaskGroup/all");
        taskGroupAllView.setMapReduceBlocks(new TDViewMapBlock() {

            @Override
            public void map(Map<String, Object> document, TDViewMapEmitBlock emitter) {
                Object type = document.get("type");
                Object groupId = document.get("groupId");
                Object createdAt = document.get("createdAt");

                if (type != null && type.equals(TaskGroup.ITEM_TYPE)) {
                    ComplexKey key = ComplexKey.of(groupId, createdAt);
                    emitter.emit(key, document.get("_id"));
                }
            }
        }, null, "1.0");

My repository looks like:

public class TaskGroupRepository extends CouchDbRepositorySupport<TaskGroup> {
    public TaskGroupRepository(CouchDbConnector db) {
        super(TaskGroup.class, db);
    }

    public List<TaskGroup> getAllByGroupId(String groupId) {
        ViewQuery query = createQuery("all")
            .descending(true)
            .startKey(ComplexKey.of(groupId, ComplexKey.emptyObject()))
            .endKey(ComplexKey.of(groupId));

        return db.queryView(query, TaskGroup.class);
    }
}

Result from queryView(ViewQuery) looks like: ignoreNotFound = false offset = 0 rows = [org.ektorp.ViewResult$Row@4155a408, org.ektorp.ViewResult$Row@4156a648] totalRows = 2 updateSeq = null

Result from queryView(ViewQuery, Class) looks like: modCount = 0

Is there a discrepancy in behaviour of these methods? Or could there be an issue in the mapping?

jchris commented 11 years ago

this looks like a duplicate of https://github.com/couchbase/couchbase-lite-android/issues/39 and or https://github.com/couchbase/couchbase-lite-android/issues/38

tleyden commented 11 years ago

I don't think this is a duplicate of those, re-opening.

tleyden commented 11 years ago

Maybe this issue will go away when we upgrade to ektorp 1.4

tleyden commented 11 years ago

Assuming 1.4 will fix this, closing as duplicate of https://github.com/couchbase/couchbase-lite-android/issues/38

stanch commented 11 years ago

I had this issue. I think the problem is that when you emit an _id in CouchDB, it is replaced with a document that has the respective id. CBLite deviates somehow. Here is a workaround (roughly translated from Scala, not tested):

ObjectMapper objectMapper = new ObjectMapper()
for (ViewResult.Row row : results.getRows()) {
  doc = objectMapper.readValue(row.getDoc(), clazz)
  // add the doc to a list
}

Namely, the difference to CouchDB is that row.getValue() just returns the _id, so you have to use row.getDoc() to fetch the document directly.

I doubt it’s an Ektorp issue.

tleyden commented 11 years ago

@stanch thanks for this insight. Re-opening based on your comment.

stanch commented 11 years ago

Do you think this could be tracked in couchbase-lite-android?

stanch commented 11 years ago

One more thing, there is an include_docs query parameter in the HTTP API. In Ektorp it is represented by new ViewQuery(). ... .includeDocs(true).

tleyden commented 11 years ago

Do you think this could be tracked in couchbase-lite-android?

@stanch sure - can you open a bug in couchbase-lite-android which points back to this one, and then close this one? (to avoid duplication)

there is an include_docs query parameter ..

Lets consider that a separate issue for now. Can you file a ticket to track it? Be explicit and give an example of the actual api and the expected api.