Created
January 11, 2018 18:21
-
-
Save krebernisak/bacc604fbd563ff61778d0e1cf4e0c2f to your computer and use it in GitHub Desktop.
ES Scrollable API exercise
This file contains hidden or 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
// es connection | |
class Client { | |
Call create(Query q); | |
} | |
// immutable call object created for specific client an query | |
class Call { | |
Result execute(); | |
ScrollableCall toScrollable(long ttl); // this call makes a network request to get the first scrollable token | |
} | |
// immutable call object created for specific client, query and time parameter | |
class ScrollableCall { | |
ScrollableResult execute(); | |
<T> T collect(ScrollableCollector<T> collector); | |
} | |
// immutable result object created from | |
class ScrollableResult { | |
Result raw(); | |
ScrollableCall next(); // creates a new call with the new token | |
} | |
// simple collector interface | |
class ScrollableCollector<T> { | |
T execute(ScrollableCall call); | |
} | |
// bunch of collectors | |
class Collectors { | |
// simple implementation | |
static ScrolablleCollector<List<Result>> toList(int n) { | |
return new ScrollableCollector<List<Result>> { | |
List<Result> execute(ScrolableCall call) { | |
// iterate and do all the work n times | |
return collectionOfResults; | |
} | |
} | |
} | |
} | |
// sample | |
Query q = ...; | |
long ttl = ...; | |
int numOfPages = ...; | |
List<Result> data = client.create(q).toScrollable(ttl).collect(toList(numOfPages)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment