Couchbase 2.0.0 simple query problem

java

#1

I have been using Couchbase lite 1.4.1 without problems (other than performance issues) and saw the announcement of couchbase lite 2.0 for android, and since I deeply dislike views and prefer the new real-query approach Couchbase lite is taking I’m trying to migrate everything in my app from v1.4.1 to v2.0.0.

I use this code to save data:

    Map<String, Object> objectProperties= new HashMap<String, Object>(){
                    {
                        put("contact", new ObjectMapper().convertValue(getLead(), new TypeReference<Map<String, Object>>() {}));
                    }
                };
                MutableDocument document = new MutableDocument(objectProperties);
                MyDatabaseController.getDatabase().save(document);

I’m using jackson to convert my Object to Map since for some reason I cant use a POJO like it was in the 1.4.1 version.

And I’m using this code to retrieve (or attempt to) the saved data

Query query = QueryBuilder.select(SelectResult.all())
                        .from(DataSource.database(MyDatabaseController.getDatabase()));
                ResultSet resultSet = query.execute();
                if(resultSet.allResults().size() == 0){
                    return ERROR_NO_DATA;
                }
                for (Result result : resultSet) {
                    showInUI(ADD_ENTRY, result.getValue("contact"));
                }

Here’s the problem, everytime I read the resultSet it has a size of zero. Am I doing something wrong?


#2

Nothing looks obviously wrong, but I’d like to think this is too simple of a case for it to be a bug in the library. I imagine that the real answer is 3) Something else is going on besides what is shown here. Perhaps MyDatabaseController returning differing database instances, for example?


#3

That is exactlyl what I thought, the use case is too simple for it to be a library problem, this is pretty much the example they have in the documentation.

I’ll continue trying. Do you think the replication could have an impact over this use case? Somehow messing with the Database data?


#4

I think I found the problem. I didnt create an index and until I created one (https://developer.couchbase.com/documentation/mobile/2.0/couchbase-lite/java.html) that my query returned results.


#5

Value indices are optional and should not affect the results. @hideki Could you comment on this?


#6

Can you try same code but first REMOVE the if check on size?
In other words comment out the following block. (Explanation follows.)

In my experience with the beta (have not looked at final version sources), checking allResults.size() causes an enumeration on the resultset, and unlike version 1.x where you can RESET a result set’s enumeration (index back to 0), with 2.x you cannot.
In other words getting the size (this way) from the resultset and using the resultset ‘normally’ is mutually exclusive.
Also, for testing, also select the document id (in the select clause) and log it to console in the resultset loop. Just to rule out any unrelated problems with document props and/or gui.
-nat (not a cb employee.)


#7

Hi @jeav148,

@natgross Thank you for your comments.

For future enhancement and technical limitation, ResultSet only allows iteration. However, as many users need to know the size of results. So we introduced allResults() method. By calling allResults(), internal iterator reaches the end of the iterator.
http://docs.couchbase.com/mobile/2.0/couchbase-lite-java/com/couchbase/lite/ResultSet.html#allResults--

If your application needs to know the size of ResultSet, please try following way:

List<Result> results = resultSet.allResults();
if(results.size() == 0){
   return ERROR_NO_DATA;
}
for (Result result : results) {
    showInUI(ADD_ENTRY, result.getValue("contact"));
}

I understand this is not obvious limitation. We will consider this for a future release.

Thanks!


#8

What about sql COUNT() (and other aggregates), can I use [any of] those?
Thanks;
-nat


#9

@natgross,

count(expr) should work as expected. But querying twice could be less efficient.

http://docs.couchbase.com/mobile/2.0/couchbase-lite-java/com/couchbase/lite/Function.html#count-com.couchbase.lite.Expression-


#10

You are right. That is the problem. I was checking the size of the results and reaching the end of the iterator.

When I made it work I removed the zero-check and didnt think to much of it, I just tested to see if that is the problem and it is. Now is working as intended. Thank you all for your help.