All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.leoplatform.sdk.oracle.OracleRowResolver Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package io.leoplatform.sdk.oracle;

import io.leoplatform.schema.Field;
import io.leoplatform.sdk.ExecutorManager;
import io.leoplatform.sdk.changes.DomainQuery;
import io.leoplatform.sdk.changes.DomainResolver;
import io.leoplatform.sdk.changes.JsonDomainData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;

@Singleton
public class OracleRowResolver implements DomainResolver {
    private static final Logger log = LoggerFactory.getLogger(OracleRowResolver.class);
    private static final int BATCH_SIZE = 1_000;

    private final DomainQuery domainQuery;
    private final JsonDomainData jsonDomainData;
    private final ExecutorManager manager;

    @Inject
    public OracleRowResolver(DomainQuery domainQuery, JsonDomainData jsonDomainData, ExecutorManager manager) {
        this.domainQuery = domainQuery;
        this.jsonDomainData = jsonDomainData;
        this.manager = manager;
    }

    @Override
    public JsonArray toResultJson(String sourceName, BlockingQueue fields) {
        return splitAsBatches(fields)
            .parallel()
            .map(b -> domainQuery.generateSql(sourceName, b))
            .map(this::toJsonAsync)
            .map(CompletableFuture::join)
            .flatMap(Collection::stream)
            .collect(Json::createArrayBuilder, JsonArrayBuilder::add, JsonArrayBuilder::addAll)
            .build();
    }

    private CompletableFuture toJsonAsync(String sql) {
        return CompletableFuture.supplyAsync(() -> jsonDomainData.toJson(sql), manager.get());
    }

    private Stream> splitAsBatches(BlockingQueue fields) {
        Builder> batchBuilder = Stream.builder();
        while (!fields.isEmpty()) {
            List batch = new LinkedList<>();
            fields.drainTo(batch, BATCH_SIZE);
            batchBuilder.accept(batch);
        }
        return batchBuilder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy