krebernisak
1/11/2018 - 6:21 PM

ES Scrollable API exercise

ES Scrollable API exercise


// 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));