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

info.archinnov.achilles.query.cql.AsyncNativeQuery 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.query.cql;

import com.datastax.driver.core.Statement;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import info.archinnov.achilles.async.AchillesFuture;
import info.archinnov.achilles.internal.context.ConfigurationContext;
import info.archinnov.achilles.internal.context.DaoContext;
import info.archinnov.achilles.type.Empty;
import info.archinnov.achilles.options.Options;
import info.archinnov.achilles.type.TypedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;

/**
 * Class to wrap CQL native query asynchronously
 *
 * 

 *
 *   Statement statement = new SimpleStatement("SELECT name,age_in_years FROM UserEntity WHERE id IN(?,?)");
 *   AchillesFuture> future = manager.nativeQuery(statement,10L,11L).get();
 *
 * 
* * @see Native query */ public class AsyncNativeQuery extends AbstractNativeQuery { private static final Logger log = LoggerFactory.getLogger(AsyncNativeQuery.class); public AsyncNativeQuery(DaoContext daoContext, ConfigurationContext configContext, Statement statement, Options options, Object... boundValues) { super(daoContext, configContext, statement, options, boundValues); } @Override protected AsyncNativeQuery getThis() { return this; } /** * Return found rows asynchronously. The list represents the number of returned rows The * map contains the (column name, column value) of each row. The map is * backed by a LinkedHashMap and thus preserves the columns order as they * were declared in the native query * * @return AchillesFuture<List<TypedMap>> */ public AchillesFuture> get(FutureCallback... asyncListeners) { final ListenableFuture typedMapsWithPagingFutures = super.asyncGetInternal(asyncListeners); final ListenableFuture> typedMapsFuture = asyncUtils.transformFuture(typedMapsWithPagingFutures, new Function>() { @Nullable @Override public List apply(TypedMapsWithPagingState input) { return input.getTypedMaps(); } }); return asyncUtils.buildInterruptible(typedMapsFuture); } /** * Return found rows asynchronously with paging state. The list represents the number of returned rows The * map contains the (column name, column value) of each row. The map is * backed by a LinkedHashMap and thus preserves the columns order as they * were declared in the native query * * @return AchillesFuture<TypedMapsWithPagingState> */ public AchillesFuture getWithPagingState(FutureCallback... asyncListeners) { final ListenableFuture typedMapsWithPagingFutures = super.asyncGetInternal(asyncListeners); return asyncUtils.buildInterruptible(typedMapsWithPagingFutures); } /** * Return the first found row asynchronously. The map contains the (column name, column * value) of each row. The map is backed by a LinkedHashMap and thus * preserves the columns order as they were declared in the native query * * @return AchillesFuture<TypedMap>Map */ public AchillesFuture getFirst(FutureCallback... asyncListeners) { return super.asyncGetFirstInternal(asyncListeners); } /** * Execute statement asynchronously without returning result. Useful for * INSERT/UPDATE/DELETE and DDL statements */ public AchillesFuture execute(FutureCallback... asyncListeners) { return super.asyncExecuteInternal(asyncListeners); } /** * Return an asynchronous iterator of {@link TypedMap} instance. Each instance represents a CQL row * @return Iterator<TypedMap> */ public AchillesFuture> iterator(FutureCallback... asyncListeners) { return super.asyncIterator(Optional.absent(), asyncListeners); } /** * Return an asynchronous iterator of {@link TypedMap} instance. Each instance represents a CQL row * * @param fetchSize the fetch size to set for paging * @return Iterator<TypedMap> */ public AchillesFuture> iterator(int fetchSize, FutureCallback... asyncListeners) { return super.asyncIterator(Optional.fromNullable(fetchSize), asyncListeners); } }