Do Couchbase reactive clients guarantee order of rows in view query result?

query
java

#1

I use Couchbase Java SDK 2.2.6 with Couchbase server 4.1.

I query my view with the following code

public <T> List<T> findDocuments(ViewQuery query, String bucketAlias, Class<T> clazz) {
    // We specifically set reduce false and include docs to retrieve docs
    query.reduce(false).includeDocs();
    log.debug("Find all documents, query = {}", decode(query));
    return getBucket(bucketAlias)
            .query(query)
            .allRows()
            .stream()
            .map(row -> fromJsonDocument(row.document(), clazz))
            .collect(Collectors.toList());
}

private static <A> A fromJsonDocument(JsonDocument saved, Class<A> clazz) {
    log.debug("Retrieved json document -> {}", saved);
    A object = fromJson(saved.content(), clazz);
    return object;
}

In the logs from the fromJsonDocument method I see that rows are not always sorted by the row key. Usually they are, but sometimes they are not.

If I just run this query in browser couchbase GUI, I always receive results in expected order. Is it a bug or expected that view query results are not sorted when queried with async client? What is the behaviour in different clients, not java?


#2

The fact that includeDocs is used can effectively change the order, as the code now does an additional GET roundtrip to the server to retrieve the full doc. If there is a slight variation in latency when doing so, it can change the final order of the results (eg. doc1 takes 2ms to retrieve while doc2 takes only one, order of rows ends up being doc2, doc1).

Thanks to a user contribution, this can now be avoided by calling “includeDocsOrdered” instead of includeDocs (same parameters).

It still eagerly trigger the GETs (so we don’t wait for doc1 to come in before we start fetching doc2), but then enforce the same order as the original sequence of rows.


#3

Thanks a lot! Just want to mention that this method was added in 2.2.5.