Created
September 14, 2016 10:50
-
-
Save daschl/a03e4a7751be44e84b7d6413a8e6c905 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create observable for master and all the replicas | |
// The code maps it to a tuple where the boolean represents "found!" | |
// so if its not found the tuple will be <False, null> and if its found <True, JsonDocument> | |
Observable<Tuple2<Boolean, JsonDocument>> primary = bucket.async() | |
.get("doc") | |
.map(doc -> Tuple.create(true, doc)) | |
.defaultIfEmpty(Tuple.create(false, null)); | |
Observable<Tuple2<Boolean, JsonDocument>> first = bucket.async() | |
.getFromReplica("doc", ReplicaMode.FIRST) | |
.map(doc -> Tuple.create(true, doc)) | |
.defaultIfEmpty(Tuple.create(false, null)); | |
Observable<Tuple2<Boolean, JsonDocument>> second = bucket.async() | |
.getFromReplica("doc", ReplicaMode.SECOND) | |
.map(doc -> Tuple.create(true, doc)) | |
.defaultIfEmpty(Tuple.create(false, null)); | |
Observable<Tuple2<Boolean, JsonDocument>> third = bucket.async() | |
.getFromReplica("doc", ReplicaMode.THIRD) | |
.map(doc -> Tuple.create(true, doc)) | |
.defaultIfEmpty(Tuple.create(false, null)); | |
// Since we now get an event even if no document is found, indicated by the boolean | |
// in the tuple we just take the first one, it could be "false" with no doc attached | |
// to it. This could be then blocked on with an iterator or consumed as a single value | |
// too. | |
Observable<Tuple2<Boolean, JsonDocument>> firstFound = Observable | |
.merge(primary, first, second, third) | |
.first(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment