bulkGet in java sdk 2.x with preserved order

java

#1

Hi.
In java sdk 1.x there was a useful “bulkget”. This disappeared in the sdk 2.x. Threre is, however, a recipe on http://docs.couchbase.com/developer/java-2.0/documents-bulk.html ; specifically

public List<JsonDocument> bulkGet(final Collection<String> ids) {
return Observable
    .from(ids)
    .flatMap(new Func1<String, Observable<JsonDocument>>() {
        @Override
        public Observable<JsonDocument> call(String id) {
            return bucket.async().get(id);
        }
    })
    .toList()
    .toBlocking()
    .single();
}

I used this recipe in my application.
unfortunately I found a issue in what I expected by the function: the order of the returned values is different from the order of the input keys.

How can I ensure the returned values are in the same order of the input?


#2

The easiest way is to replace the flatMap with concatMap, but it is expected to be a little slower - if concatMap doesn’t work for you we can look at other possible solutions :slight_smile:


#3

Also if you use a recent enough version of RxJava (either directly or through a recent version of the SDK), you might have a better middle ground between ordering and performance by using concatMapEager instead of flatMap/ concatMap.