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

net.e6tech.elements.cassandra.async.AsyncPrepared Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015-2019 Futeh Kao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.e6tech.elements.cassandra.async;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.e6tech.elements.cassandra.driver.cql.AsyncResultSet;
import net.e6tech.elements.cassandra.driver.cql.Bound;
import net.e6tech.elements.cassandra.driver.cql.Prepared;
import net.e6tech.elements.common.resources.Provision;

import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class AsyncPrepared extends Async {

    private static Cache preparedStatementCache = CacheBuilder.newBuilder()
            .concurrencyLevel(Provision.cacheBuilderConcurrencyLevel)
            .initialCapacity(200)
            .maximumSize(500)
            .build();

    private Prepared prepared;

    @Override
    protected AsyncFutures createResult() {
        return new AsyncResultSetFutures<>(this, futures);
    }

    @SuppressWarnings("unchecked")
    @Override
    public AsyncResultSetFutures getResult() {
        return (AsyncResultSetFutures) super.getResult();
    }

    public AsyncPrepared prepare(String stmt) {
        try {
            return prepare(preparedStatementCache.get(stmt, () -> session.prepare(stmt)));
        } catch (ExecutionException e) {
            return prepare(session.prepare(stmt));
        }
    }

    public AsyncPrepared prepare(Prepared stmt) {
        prepared = stmt;
        return this;
    }

    public AsyncResultSetFutures execute(Consumer consumer) {
        return execute(null, consumer);
    }

    public AsyncResultSetFutures execute(D data, Consumer consumer) {
        Bound bound = prepared.bind();
        consumer.accept(bound);
        Future future = session.executeAsync(bound);
        futures.add(future);
        if (data != null)
            futuresData.put(future, data);
        return getResult();
    }

    public AsyncResultSetFutures execute(Collection collection, BiConsumer biConsumer) {
        resizeFuturesData(collection.size());
        for (D t : collection) {
            execute(t, boundStatement -> biConsumer.accept(t, boundStatement));
        }
        return getResult();
    }

    public AsyncResultSetFutures execute(D[] array, BiConsumer biConsumer) {
        resizeFuturesData(array.length);
        for (D t : array) {
            execute(t, boundStatement -> biConsumer.accept(t, boundStatement));
        }
        return getResult();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy