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

info.archinnov.achilles.internal.async.AsyncUtils Maven / Gradle / Ivy

There is a newer version: 6.1.0
Show newest version
/*
 * Copyright (C) 2012-2014 DuyHai DOAN
 *
 *  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 info.archinnov.achilles.internal.async;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

import com.datastax.driver.core.ExecutionInfo;
import com.google.common.util.concurrent.MoreExecutors;
import info.archinnov.achilles.query.cql.TypedMapsWithPagingState;
import info.archinnov.achilles.type.Empty;
import info.archinnov.achilles.type.TypedMap;
import org.apache.commons.lang3.ArrayUtils;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.google.common.base.Function;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import info.archinnov.achilles.async.AchillesFuture;
import info.archinnov.achilles.internal.statement.wrapper.AbstractStatementWrapper;
import info.archinnov.achilles.options.Options;

import javax.annotation.Nullable;

public class AsyncUtils {

    public static final Function> RESULTSET_TO_ROWS = new Function>() {
        @Override
        public List apply(ResultSet resultSet) {
            List rows = new ArrayList<>();
            if (resultSet != null) {
                final int availableWithoutFetching = resultSet.getAvailableWithoutFetching();
                for (int i = 0; i < availableWithoutFetching; i++) {
                    rows.add(resultSet.one());
                }
            }
            return rows;
        }
    };

    public static final Function RESULTSET_TO_ROWS_WITH_EXECUTION_INFO = new Function() {
        @Override
        public RowsWithExecutionInfo apply(ResultSet resultSet) {
            List rows = new ArrayList<>();
            ExecutionInfo info = null;
            if (resultSet != null) {
                final int availableWithoutFetching = resultSet.getAvailableWithoutFetching();
                for (int i = 0; i < availableWithoutFetching; i++) {
                    rows.add(resultSet.one());
                }
                info = resultSet.getExecutionInfo();
            }
            return new RowsWithExecutionInfo(rows, info);
        }
    };


    public static final Function RESULTSET_TO_ROW = new Function() {
        @Override
        public Row apply(ResultSet resultSet) {
            Row row = null;
            if (resultSet != null) {
                row = resultSet.one();
            }
            return row;
        }
    };

    public static final Function> RESULTSET_TO_ITERATOR = new Function>() {
        @Override
        public Iterator apply(ResultSet resultSet) {
            Iterator iterator = new ArrayList().iterator();
            if (resultSet != null) {
                iterator = resultSet.iterator();
            }
            return iterator;
        }
    };

    public void maybeAddAsyncListeners(ListenableFuture listenableFuture, Options options, ExecutorService executorService) {
        if (options.hasAsyncListeners()) {
            for (FutureCallback callback : options.getAsyncListeners()) {
                Futures.addCallback(listenableFuture, callback, executorService);
            }
        }
    }

    public void maybeAddAsyncListeners(ListenableFuture listenableFuture, Options options) {
        maybeAddAsyncListeners(listenableFuture, options, MoreExecutors.sameThreadExecutor());
    }

    public void maybeAddAsyncListeners(ListenableFuture listenableFuture, FutureCallback[] asyncListeners, ExecutorService executorService) {
        if (ArrayUtils.isNotEmpty(asyncListeners)) {
            for (FutureCallback callback : asyncListeners) {
                Futures.addCallback(listenableFuture, callback, executorService);
            }
        }
    }

    public void maybeAddAsyncListeners(ListenableFuture listenableFuture, FutureCallback[] asyncListeners) {
        maybeAddAsyncListeners(listenableFuture, asyncListeners, MoreExecutors.sameThreadExecutor());
    }

    public  ListenableFuture transformFuture(ListenableFuture from, Function function, ExecutorService executorService) {
        return Futures.transform(from, function, executorService);
    }

    public  ListenableFuture transformFuture(ListenableFuture from, Function function) {
        return Futures.transform(from, function, MoreExecutors.sameThreadExecutor());
    }

    public  ListenableFuture transformFutureToEmpty(ListenableFuture from, ExecutorService executorService) {
        Function function = new Function() {
            @Override
            public Empty apply(V input) {
                return Empty.INSTANCE;
            }
        };
        return Futures.transform(from, function, executorService);
    }

    public  AchillesFuture buildInterruptible(ListenableFuture listenableFuture) {
        return new AchillesFuture<>(listenableFuture);
    }

    public ListenableFuture> mergeResultSetFutures(List> resultSetFutures) {
        return Futures.allAsList(resultSetFutures);
    }

    public ListenableFuture applyLoggingTracingAndCASCheck(ResultSetFuture resultSetFuture, final AbstractStatementWrapper statementWrapper, ExecutorService executorService) {

        Function tracing = new Function() {
            @Override
            public ResultSet apply(ResultSet resultSet) {
                statementWrapper.tracing(resultSet);
                return resultSet;
            }
        };

        Function LWTCheck = new Function() {
            @Override
            public ResultSet apply(ResultSet resultSet) {
                statementWrapper.checkForLWTSuccess(resultSet);
                return resultSet;
            }
        };

        final ListenableFuture tracingApplied = Futures.transform(resultSetFuture, tracing, executorService);
        return Futures.transform(tracingApplied, LWTCheck);
    }

    public static enum Singleton {
        INSTANCE;

        private final AsyncUtils instance = new AsyncUtils();

        public AsyncUtils get() {
            return instance;
        }
    }
}