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

org.infinispan.client.hotrod.impl.query.RemoteQuery Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.client.hotrod.impl.query;

import static org.infinispan.client.hotrod.impl.Util.await;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import java.util.concurrent.TimeUnit;

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.exceptions.HotRodClientException;
import org.infinispan.client.hotrod.impl.InternalRemoteCache;
import org.infinispan.client.hotrod.impl.operations.QueryOperation;
import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.Closeables;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.query.dsl.QueryFactory;
import org.infinispan.query.dsl.QueryResult;
import org.infinispan.query.dsl.impl.BaseQuery;
import org.infinispan.query.remote.client.impl.BaseQueryResponse;

/**
 * @author [email protected]
 * @since 6.0
 */
public final class RemoteQuery extends BaseQuery {

   private static final Log log = LogFactory.getLog(RemoteQuery.class);

   private final InternalRemoteCache cache;
   private final SerializationContext serializationContext;

   RemoteQuery(QueryFactory queryFactory, InternalRemoteCache cache, SerializationContext serializationContext,
               String queryString) {
      super(queryFactory, queryString);
      this.cache = cache;
      this.serializationContext = serializationContext;
   }

   RemoteQuery(QueryFactory queryFactory, InternalRemoteCache cache, SerializationContext serializationContext,
               String queryString, Map namedParameters, String[] projection, long startOffset, int maxResults, boolean local) {
      super(queryFactory, queryString, namedParameters, projection, startOffset, maxResults, local);
      this.cache = cache;
      this.serializationContext = serializationContext;
   }

   @Override
   public void resetQuery() {
   }

   @Override
   public List list() {
      BaseQueryResponse response = executeRemotely(false);
      try {
         return response.extractResults(serializationContext);
      } catch (IOException e) {
         throw new HotRodClientException(e);
      }
   }

   @Override
   public QueryResult execute() {
      BaseQueryResponse response = executeRemotely(true);
      return new QueryResult<>() {
         @Override
         public OptionalLong hitCount() {
            long totalResults = response.getTotalResults();
            return totalResults == -1 ? OptionalLong.empty() : OptionalLong.of(totalResults);
         }

         @Override
         public List list() {
            try {
               return response.extractResults(serializationContext);
            } catch (IOException e) {
               throw new HotRodClientException(e);
            }
         }
      };
   }

   @Override
   public int executeStatement() {
      BaseQueryResponse response = executeRemotely(false);
      return (int) response.getTotalResults();
   }

   @Override
   public CloseableIterator iterator() {
      if (maxResults == -1 && startOffset == 0) {
         log.warnPerfRemoteIterationWithoutPagination(queryString);
      }
      return Closeables.iterator(list().iterator());
   }

   @Override
   public int getResultSize() {
      BaseQueryResponse response = executeRemotely(true);
      return (int) response.getTotalResults();
   }

   private BaseQueryResponse executeRemotely(boolean withHitCount) {
      validateNamedParameters();
      QueryOperation op = cache.getOperationsFactory().newQueryOperation(this, cache.getDataFormat(), withHitCount);
      return (BaseQueryResponse) (timeout != -1 ? await(op.execute(), TimeUnit.NANOSECONDS.toMillis(timeout)) :
            await(op.execute()));
   }

   /**
    * Get the protobuf SerializationContext or {@code null} if we are not using protobuf.
    */
   public SerializationContext getSerializationContext() {
      return serializationContext;
   }

   public RemoteCache getCache() {
      return cache;
   }

   @Override
   public String toString() {
      return "RemoteQuery{" +
            "queryString=" + queryString +
            ", namedParameters=" + namedParameters +
            ", startOffset=" + startOffset +
            ", maxResults=" + maxResults +
            ", timeout=" + timeout +
            '}';
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy