Gil Matzov
Gil Matzov

Reputation: 213

what is the advantage/disadvantage in each way to update a lot of documents in Couchbase, Java SDK?

i have sometimes the need to update many documents at once, 10K or even more, and I am thinking what is the advantage/disadvantage in each way?

I thought of two ways to do it, and I will happy to understand what is advantage/disadvantage and to to hear on a third way.

i am looking for a good batch way to do this.

way 1, send to observable to get all the documents and do the work:

Observable
            .from(ids)
            .flatMap(new Func1<String, Observable<JsonDocument>>() {
                @Override
                public Observable<JsonDocument> call(String id) {
                    return bucket.async().get(id);
                }
            })
            .map(new Func1<JsonDocument, JsonDocument>() {
                @Override
                public JsonDocument call(JsonDocument original) {
                    // do some change in the document content
                    original.content().put("add", "content");
                    return original;
                }
            })
            .flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
                @Override
                public Observable<JsonDocument> call(JsonDocument modified) {
                    return bucket.async().replace(modified);
                }
            }).subscribe();

way 2, run on each document id and use async get and then change the document async way:

for (String id : ids){
        bucket.async()
                .get(id)
                .map(new Func1<JsonDocument, JsonDocument>() {
                    @Override
                    public JsonDocument call(JsonDocument jsonDocument) {
                    // do some change in the document content
                        jsonDocument.content().put("add", "content");
                        return jsonDocument;
                    }
                })
                .flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
                    @Override
                    public Observable<JsonDocument> call(JsonDocument modified) {
                        return bucket.async().replace(modified);
                    }
                }).subscribe();
    }

Upvotes: 1

Views: 193

Answers (1)

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28351

This could use a little perf benchmarking, but I think both approaches are fine and should be relatively close in performance.

Using bucket.async() in both case ensure that internally, the requests will rapidly be queued in the ringBuffer and fired to the server.

A 3rd way would have been similar to option 2 but with blocking API, and that would definitely perform worse.

All in all, option 1 is purely Rx and, although a little overhead may occur in Observable.from, maybe it provides the most coherent readability so I'd use this one. Plus you get a single stream and subscription, so you pay the overhead just once.

Upvotes: 1

Related Questions