Skip to content

Instantly share code, notes, and snippets.

threadPool.submit() -> {
Spliterator<T> spliterator = EsSpliterator.from(index, query, batchSize, documentClass, slices)
StreamSupport.stream(spliterator, true)
.forEach(batch -> handle(batch));
}.get();
Spliterator<Collection<Foo>> fooSpliterator = EsSpliterator.from(index, query, batchSize, Foo.class, 2);
StreamSupport.stream(fooSpliterator, true)
.forEach(fooBatch -> handle(fooBatch));
@markusdybeck
markusdybeck / SlicedEsIterators.java
Created September 16, 2020 07:13
Two sliced es iterators
// Slice id 0, max slices 2
Iterator<Collection<Foo>> fooIterator1 = new EsIterator<>(index, query, batchSize, Foo.class, 0, 2);
// Slice id 1, max slices 2
Iterator<Collection<Foo>> fooIterator2 = new EsIterator<>(index, query, batchSize, Foo.class, 1, 2);
@markusdybeck
markusdybeck / CreateEsIterator.java
Last active September 16, 2020 07:12
EsIterator usage
Iterator<Collection<Foo>> fooIterator = new EsIterator<>(index, query, batchSize, Foo.class);
@markusdybeck
markusdybeck / EsSpliterator.java
Last active September 16, 2020 09:21
Spliterator that handles EsIterators.
class EsSpliterator<T> implements Spliterator<Collection<T>> {
private final List<EsIterator<T>> iterators = new ArrayList<>();
// constructor omitted
private static <T> EsSpliterator<T> from(String index, QueryBuilder query, int batchSize, Class<T> documentClass, int maxSlices) {
List<EsIterator<T>> iterators = IntStream.range(0, maxSlices)
.mapToObj(i -> new EsIterator<>(index, query, batchSize, documentClass, i, maxSlices))
.collect(toList());
return new EsSpliterator<>(iterators);
@markusdybeck
markusdybeck / record-with-many-arguments.java
Last active June 10, 2020 11:13
A more complex record model with more arguments
public record Product(ProductId id, String name, String description, Images images, Attributes attributes) {
public Product {
Objects.requireNonNull(id);
// ...
}
};
var product = new Product(new ProductId(1), "Nimbus 2000", "Fastest broom on the market", new Images(), new Attributes());
@markusdybeck
markusdybeck / record-compact-constructor.java
Last active June 10, 2020 12:20
Record with compact constructor
public record ProductId(long id){
public ProductId {
Require.that(id > 0);
}
};
var point1 = new Point(0, 3);
var point2 = new Point(0, 3);
var point3 = new Point(3, 0);
point1.equals(point2); // true
point1.equals(point3); // false
@markusdybeck
markusdybeck / record-point-example.java
Created June 10, 2020 09:09
Simple record example
public record Point(int x, int y) {};
var point = new Point(1, 2);
point.x(); // 1
point.y(); // 2
@markusdybeck
markusdybeck / snippet1.java
Created May 12, 2020 11:59
Medium snippet 1
public class TestExtension implements BeforeAllCallback {
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create("TestExtension");
private static final String TEST_CONTEXT_PLACEHOLDER = "testContext";
@Override
public void beforeAll(final ExtensionContext extensionContext) throws Exception {
if (TestExtensionInitializer.INSTANCE.initialize()) {
var testContext = TestContext.init();
extensionContext.getRoot()