Package org.infinispan.query.remote.client

Examples of org.infinispan.query.remote.client.QueryRequest


   }

   private byte[] executeQuery(AdvancedCache<byte[], byte[]> cache, byte[] query) throws IOException {
      final SerializationContext serCtx = SerializationContextHolder.getSerializationContext();

      QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

      EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
         @Override
         public Class<?> getClassFromName(String entityName) {
            try {
               //todo [anistor] this just checks if the type is known
               serCtx.getMessageDescriptor(entityName);
            } catch (Exception e) {
               return null;
            }
            return ProtobufValueWrapper.class;
         }
      };

      SearchManager searchManager = Search.getSearchManager(cache);
      IspnLuceneProcessingChain processingChain = new IspnLuceneProcessingChain((SearchFactoryIntegrator) searchManager.getSearchFactory(), entityNamesResolver, null);
      QueryParser queryParser = new QueryParser();
      IspnLuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);

      Sort sort = null;
      if (request.getSortCriteria() != null && !request.getSortCriteria().isEmpty()) {
         SortField[] sortField = new SortField[request.getSortCriteria().size()];
         int i = 0;
         for (QueryRequest.SortCriteria sc : request.getSortCriteria()) {
            //TODO [anistor] sort type is not properly handled right now
            int sortType = SortField.STRING;
            Descriptors.FieldDescriptor field = parsingResult.getTargetType().findFieldByName(sc.getAttributePath());
            if (field != null) {
               switch (field.getJavaType()) {
                  case INT:
                  case BOOLEAN:
                  case ENUM:
                     sortType = SortField.INT;
                     break;
                  case LONG:
                     sortType = SortField.LONG;
                     break;
                  case FLOAT:
                     sortType = SortField.FLOAT;
                     break;
                  case DOUBLE:
                     sortType = SortField.DOUBLE;
                     break;
               }
            }
            sortField[i++] = new SortField(sc.getAttributePath(), sortType, !sc.isAscending());
         }
         sort = new Sort(sortField);
      }

      QueryBuilder qb = searchManager.getSearchFactory().buildQueryBuilder().forEntity(parsingResult.getTargetEntity()).get();
      Query q = qb.bool()
            .must(qb.keyword().onField(TYPE_FIELD_NAME).ignoreFieldBridge().matching(parsingResult.getTargetType().getFullName()).createQuery())
            .must(parsingResult.getQuery())
            .createQuery();

      CacheQuery cacheQuery = searchManager.getQuery(q, parsingResult.getTargetEntity());
      if (sort != null) {
         cacheQuery = cacheQuery.sort(sort);
      }
      int projSize = 0;
      if (parsingResult.getProjections() != null && !parsingResult.getProjections().isEmpty()) {
         projSize = parsingResult.getProjections().size();
         cacheQuery.projection(parsingResult.getProjections().toArray(new String[projSize]));
      }
      if (request.getStartOffset() > 0) {
         cacheQuery.firstResult((int) request.getStartOffset());
      }
      if (request.getMaxResults() > 0) {
         cacheQuery.maxResults(request.getMaxResults());
      }

      List list = cacheQuery.list();

      List<WrappedMessage> results = new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
 
View Full Code Here


   }

   @Override
   protected QueryResponse executeOperation(Transport transport) {
      HeaderParams params = writeHeader(transport, QUERY_REQUEST);
      QueryRequest queryRequest = new QueryRequest();
      queryRequest.setJpqlString(remoteQuery.getJpqlString());
      queryRequest.setStartOffset(remoteQuery.getStartOffset());
      queryRequest.setMaxResults(remoteQuery.getMaxResults());

      SerializationContext serCtx = remoteQuery.getSerializationContext();
      byte[] requestBytes;
      try {
         requestBytes = ProtobufUtil.toByteArray(serCtx, queryRequest);
View Full Code Here

   }

   private byte[] executeQuery(AdvancedCache<byte[], byte[]> cache, byte[] query) throws IOException {
      final SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());

      QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

      SearchManager searchManager = Search.getSearchManager(cache);
      Query luceneQuery;
      List<String> projections;
      Class targetEntity;
      Descriptors.Descriptor messageDescriptor;

      QueryParser queryParser = new QueryParser();
      if (cache.getCacheConfiguration().compatibility().enabled()) {
         final QueryInterceptor queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(entityName);
               Class clazz = messageMarshaller.getJavaClass();
               Boolean isIndexed = queryInterceptor.getKnownClasses().get(clazz);
               return isIndexed != null && isIndexed ? clazz : null;
            }
         };

         LuceneProcessingChain processingChain = new LuceneProcessingChain((SearchFactoryIntegrator) searchManager.getSearchFactory(), entityNamesResolver, null);
         LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);

         MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(parsingResult.getTargetEntity());
         messageDescriptor = serCtx.getMessageDescriptor(messageMarshaller.getTypeName());
         targetEntity = parsingResult.getTargetEntity();
         projections = parsingResult.getProjections();
         luceneQuery = parsingResult.getQuery();
      } else {
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               try {
                  //todo [anistor] this just checks if the type is known
                  serCtx.getMessageDescriptor(entityName);
               } catch (Exception e) {
                  return null;
               }
               return ProtobufValueWrapper.class;
            }
         };

         IspnLuceneProcessingChain processingChain = new IspnLuceneProcessingChain(serCtx, (SearchFactoryIntegrator) searchManager.getSearchFactory(), entityNamesResolver, null);
         IspnLuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);
         messageDescriptor = parsingResult.getTargetType();
         targetEntity = parsingResult.getTargetEntity();
         projections = parsingResult.getProjections();

         QueryBuilder qb = searchManager.getSearchFactory().buildQueryBuilder().forEntity(targetEntity).get();
         luceneQuery = qb.bool()
               .must(qb.keyword().onField(TYPE_FIELD_NAME).ignoreFieldBridge().matching(messageDescriptor.getFullName()).createQuery())
               .must(parsingResult.getQuery())
               .createQuery();
      }

      CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, targetEntity);

      if (request.getSortCriteria() != null && !request.getSortCriteria().isEmpty()) {
         SortField[] sortField = new SortField[request.getSortCriteria().size()];
         int i = 0;
         for (QueryRequest.SortCriteria sc : request.getSortCriteria()) {
            //TODO [anistor] sort type is not properly handled right now
            Descriptors.FieldDescriptor field = getFieldDescriptor(messageDescriptor, sc.getAttributePath());
            int sortType = SortField.STRING;
            if (field != null) {
               switch (field.getJavaType()) {
                  case INT:
                  case BOOLEAN:
                  case ENUM:
                     sortType = SortField.INT;
                     break;
                  case LONG:
                     sortType = SortField.LONG;
                     break;
                  case FLOAT:
                     sortType = SortField.FLOAT;
                     break;
                  case DOUBLE:
                     sortType = SortField.DOUBLE;
                     break;
               }
            }
            sortField[i++] = new SortField(sc.getAttributePath(), sortType, !sc.isAscending());
         }
         cacheQuery = cacheQuery.sort(new Sort(sortField));
      }

      int projSize = 0;
      if (projections != null && !projections.isEmpty()) {
         projSize = projections.size();
         cacheQuery = cacheQuery.projection(projections.toArray(new String[projSize]));
      }
      if (request.getStartOffset() > 0) {
         cacheQuery = cacheQuery.firstResult((int) request.getStartOffset());
      }
      if (request.getMaxResults() > 0) {
         cacheQuery = cacheQuery.maxResults(request.getMaxResults());
      }

      List list = cacheQuery.list();
      List<WrappedMessage> results = new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
      for (Object o : list) {
View Full Code Here

   @Override
   public byte[] query(AdvancedCache<byte[], byte[]> cache, byte[] query) {
      try {
         SerializationContext serCtx = ProtobufMetadataManager.getSerializationContextInternal(cache.getCacheManager());
         QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

         QueryResponse response;
         if (cache.getCacheConfiguration().indexing().index().isEnabled()) {
            response = executeQuery(cache, serCtx, request);
         } else {
View Full Code Here

   @Override
   public byte[] query(AdvancedCache<byte[], byte[]> cache, byte[] query) {
      try {
         SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());
         QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

         QueryResponse response;
         if (cache.getCacheConfiguration().indexing().index().isEnabled()) {
            response = executeQuery(cache, serCtx, request);
         } else {
View Full Code Here

   }

   @Override
   protected QueryResponse executeOperation(Transport transport) {
      HeaderParams params = writeHeader(transport, QUERY_REQUEST);
      QueryRequest queryRequest = new QueryRequest();
      queryRequest.setJpqlString(remoteQuery.getJpqlString());
      queryRequest.setStartOffset(remoteQuery.getStartOffset());
      queryRequest.setMaxResults(remoteQuery.getMaxResults());
      if (remoteQuery.getSortCriteria() != null && !remoteQuery.getSortCriteria().isEmpty()) {
         List<QueryRequest.SortCriteria> scl = new ArrayList<QueryRequest.SortCriteria>();
         for (SortCriteria sc : remoteQuery.getSortCriteria()) {
            QueryRequest.SortCriteria sc2 = new QueryRequest.SortCriteria();
            sc2.setAttributePath(sc.getAttributePath());
            sc2.setAscending(sc.getSortOrder() == SortOrder.ASC);
            scl.add(sc2);
         }
         queryRequest.setSortCriteria(scl);
      }
      byte[] requestBytes;
      try {
         requestBytes = ProtobufUtil.toByteArray(remoteQuery.getCache().getRemoteCacheManager().getSerializationContext(), queryRequest);
      } catch (IOException e) {
View Full Code Here

   }

   @Override
   protected QueryResponse executeOperation(Transport transport) {
      HeaderParams params = writeHeader(transport, QUERY_REQUEST);
      QueryRequest queryRequest = new QueryRequest();
      queryRequest.setJpqlString(remoteQuery.getJpqlString());
      queryRequest.setStartOffset(remoteQuery.getStartOffset());
      queryRequest.setMaxResults(remoteQuery.getMaxResults());
      if (remoteQuery.getSortCriteria() != null && !remoteQuery.getSortCriteria().isEmpty()) {
         List<QueryRequest.SortCriteria> scl = new ArrayList<QueryRequest.SortCriteria>();
         for (SortCriteria sc : remoteQuery.getSortCriteria()) {
            QueryRequest.SortCriteria sc2 = new QueryRequest.SortCriteria();
            sc2.setAttributePath(sc.getAttributePath());
            sc2.setAscending(sc.getSortOrder() == SortOrder.ASC);
            scl.add(sc2);
         }
         queryRequest.setSortCriteria(scl);
      }
      SerializationContext serCtx = remoteQuery.getSerializationContext();
      byte[] requestBytes;
      try {
         requestBytes = ProtobufUtil.toByteArray(serCtx, queryRequest);
View Full Code Here

   }

   private byte[] executeQuery(AdvancedCache<byte[], byte[]> cache, byte[] query) throws IOException {
      final SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());

      QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

      SearchManager searchManager = Search.getSearchManager(cache);
      Query luceneQuery;
      List<String> projections;
      Class targetEntity;
      Descriptors.Descriptor messageDescriptor;

      QueryParser queryParser = new QueryParser();
      SearchFactoryIntegrator searchFactory = (SearchFactoryIntegrator) searchManager.getSearchFactory();
      if (cache.getCacheConfiguration().compatibility().enabled()) {
         final QueryInterceptor queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(entityName);
               Class clazz = messageMarshaller.getJavaClass();
               return queryInterceptor.isIndexed(clazz) ? clazz : null;
            }
         };

         LuceneProcessingChain processingChain = new LuceneProcessingChain.Builder(searchFactory, entityNamesResolver)
               .buildProcessingChainForClassBasedEntities();

         LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);

         MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(parsingResult.getTargetEntity());
         messageDescriptor = serCtx.getMessageDescriptor(messageMarshaller.getTypeName());
         targetEntity = parsingResult.getTargetEntity();
         projections = parsingResult.getProjections();
         luceneQuery = parsingResult.getQuery();
      } else {
         EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
            @Override
            public Class<?> getClassFromName(String entityName) {
               return serCtx.canMarshall(entityName) ? ProtobufValueWrapper.class : null;
            }
         };

         FieldBridgeProvider fieldBridgeProvider = new FieldBridgeProvider() {
            @Override
            public FieldBridge getFieldBridge(String type, String propertyPath) {
               Descriptors.Descriptor md = serCtx.getMessageDescriptor(type);
               Descriptors.FieldDescriptor fd = getFieldDescriptor(md, propertyPath);
               switch (fd.getType()) {
                  case DOUBLE:
                     return new NullEncodingDoubleNumericFieldBridge(NULL_TOKEN);
                  case FLOAT:
                     return new NullEncodingFloatNumericFieldBridge(NULL_TOKEN);
                  case INT64:
                  case UINT64:
                  case FIXED64:
                  case SFIXED64:
                  case SINT64:
                     return new NullEncodingLongNumericFieldBridge(NULL_TOKEN);
                  case INT32:
                  case FIXED32:
                  case UINT32:
                  case SFIXED32:
                  case SINT32:
                  case BOOL:
                  case ENUM:
                     return new NullEncodingIntegerNumericFieldBridge(NULL_TOKEN);
                  case STRING:
                  case BYTES:
                  case GROUP:
                  case MESSAGE:
                     return new NullEncodingTwoWayFieldBridge(BridgeFactory.STRING, NULL_TOKEN);
               }
               return null;
            }
         };

         LuceneProcessingChain processingChain = new LuceneProcessingChain.Builder(searchFactory, entityNamesResolver)
               .buildProcessingChainForDynamicEntities(fieldBridgeProvider);
         LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);
         targetEntity = parsingResult.getTargetEntity();
         messageDescriptor = serCtx.getMessageDescriptor(parsingResult.getTargetEntityName());
         projections = parsingResult.getProjections();

         QueryBuilder qb = searchManager.getSearchFactory().buildQueryBuilder().forEntity(targetEntity).get();
         luceneQuery = qb.bool()
               .must(qb.keyword().onField(TYPE_FIELD_NAME).ignoreFieldBridge().ignoreAnalyzer().matching(messageDescriptor.getFullName()).createQuery())
               .must(parsingResult.getQuery())
               .createQuery();
      }

      CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, targetEntity);

      if (request.getSortCriteria() != null && !request.getSortCriteria().isEmpty()) {
         SortField[] sortField = new SortField[request.getSortCriteria().size()];
         int i = 0;
         for (QueryRequest.SortCriteria sc : request.getSortCriteria()) {
            //TODO [anistor] sort type is not properly handled right now
            Descriptors.FieldDescriptor field = getFieldDescriptor(messageDescriptor, sc.getAttributePath());
            int sortType = SortField.STRING;
            if (field != null) {
               switch (field.getJavaType()) {
                  case INT:
                  case BOOLEAN:
                  case ENUM:
                     sortType = SortField.INT;
                     break;
                  case LONG:
                     sortType = SortField.LONG;
                     break;
                  case FLOAT:
                     sortType = SortField.FLOAT;
                     break;
                  case DOUBLE:
                     sortType = SortField.DOUBLE;
                     break;
               }
            }
            sortField[i++] = new SortField(sc.getAttributePath(), sortType, !sc.isAscending());
         }
         cacheQuery = cacheQuery.sort(new Sort(sortField));
      }

      int projSize = 0;
      if (projections != null && !projections.isEmpty()) {
         projSize = projections.size();
         cacheQuery = cacheQuery.projection(projections.toArray(new String[projSize]));
      }
      if (request.getStartOffset() > 0) {
         cacheQuery = cacheQuery.firstResult((int) request.getStartOffset());
      }
      if (request.getMaxResults() > 0) {
         cacheQuery = cacheQuery.maxResults(request.getMaxResults());
      }

      List list = cacheQuery.list();
      List<WrappedMessage> results = new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
      for (Object o : list) {
View Full Code Here

   @Override
   public byte[] query(AdvancedCache<byte[], byte[]> cache, byte[] query) {
      try {
         SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());
         QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);

         QueryResponse response;
         if (cache.getCacheConfiguration().indexing().index().isEnabled()) {
            response = executeQuery(cache, serCtx, request);
         } else {
View Full Code Here

   }

   @Override
   protected QueryResponse executeOperation(Transport transport) {
      HeaderParams params = writeHeader(transport, QUERY_REQUEST);
      QueryRequest queryRequest = new QueryRequest();
      queryRequest.setJpqlString(remoteQuery.getJpqlString());
      queryRequest.setStartOffset(remoteQuery.getStartOffset());
      queryRequest.setMaxResults(remoteQuery.getMaxResults());
      if (remoteQuery.getSortCriteria() != null && !remoteQuery.getSortCriteria().isEmpty()) {
         List<QueryRequest.SortCriteria> scl = new ArrayList<QueryRequest.SortCriteria>();
         for (SortCriteria sc : remoteQuery.getSortCriteria()) {
            QueryRequest.SortCriteria sc2 = new QueryRequest.SortCriteria();
            sc2.setAttributePath(sc.getAttributePath());
            sc2.setAscending(sc.getSortOrder() == SortOrder.ASC);
            scl.add(sc2);
         }
         queryRequest.setSortCriteria(scl);
      }
      SerializationContext serCtx = remoteQuery.getSerializationContext();
      byte[] requestBytes;
      try {
         requestBytes = ProtobufUtil.toByteArray(serCtx, queryRequest);
View Full Code Here

TOP

Related Classes of org.infinispan.query.remote.client.QueryRequest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.