Skip to content

Instantly share code, notes, and snippets.

@daschl
Created September 14, 2016 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daschl/a03e4a7751be44e84b7d6413a8e6c905 to your computer and use it in GitHub Desktop.
Save daschl/a03e4a7751be44e84b7d6413a8e6c905 to your computer and use it in GitHub Desktop.
// 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