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

      Configuration cacheConfiguration = SecurityActions.getCacheConfiguration(cache);
      boolean isIndexed = cacheConfiguration.indexing().index().isEnabled();
      boolean isCompatMode = cacheConfiguration.compatibility().enabled();
      SearchManager searchManager =
          isIndexed ? Search.getSearchManager(cache) : null; // this also checks access permissions
      RemoteQueryEngine queryEngine =
          new RemoteQueryEngine(cache, searchManager, isCompatMode, serCtx);

      long startOffset = request.getStartOffset() == null ? -1 : request.getStartOffset();
      int maxResults = request.getMaxResults() == null ? -1 : request.getMaxResults();
      Map<String, Object> namedParameters = getNamedParameters(request);
      Query q =
          queryEngine.buildQuery(
              null, request.getJpqlString(), namedParameters, startOffset, maxResults);

      QueryResponse response = makeResponse(q);
      return ProtobufUtil.toByteArray(serCtx, response);
    } catch (IOException e) {
      throw log.errorExecutingQuery(e);
    }
  }
示例#2
0
  private List<Object> executeQuery() {
    List<Object> results;

    QueryOperation op = cache.getOperationsFactory().newQueryOperation(this);
    QueryResponse response = op.execute();
    totalResults = (int) response.getTotalResults();
    if (response.getProjectionSize() > 0) {
      results = new ArrayList<Object>(response.getResults().size() / response.getProjectionSize());
      Iterator<WrappedMessage> it = response.getResults().iterator();
      while (it.hasNext()) {
        Object[] row = new Object[response.getProjectionSize()];
        for (int i = 0; i < response.getProjectionSize(); i++) {
          row[i] = it.next().getValue();
        }
        results.add(row);
      }
    } else {
      results = new ArrayList<Object>(response.getResults().size());
      SerializationContext serCtx = getSerializationContext();
      for (WrappedMessage r : response.getResults()) {
        try {
          byte[] bytes = (byte[]) r.getValue();
          Object o = ProtobufUtil.fromWrappedByteArray(serCtx, bytes);
          results.add(o);
        } catch (IOException e) {
          throw new HotRodClientException(e);
        }
      }
    }

    return results;
  }
示例#3
0
  @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 {
        response = executeNonIndexedQuery(cache, serCtx, request);
      }

      return ProtobufUtil.toByteArray(serCtx, response);
    } catch (IOException e) {
      throw new CacheException("An exception has occurred during query execution", e);
    }
  }
  private byte[] createMarshalledUser(String name, String surname) throws IOException {
    User user = new User();
    user.setId(1);
    user.setName(name);
    user.setSurname(surname);
    user.setGender(User.Gender.MALE);
    user.setAccountIds(Collections.singleton(12));

    Address address = new Address();
    address.setStreet("Dark Alley");
    address.setPostCode("1234");
    user.setAddresses(Collections.singletonList(address));

    return ProtobufUtil.toWrappedByteArray(
        ProtobufMetadataManagerImpl.getSerializationContextInternal(cacheManager), user);
  }
示例#5
0
  private QueryResponse executeNonIndexedQuery(
      AdvancedCache<byte[], byte[]> cache, SerializationContext serCtx, QueryRequest request)
      throws IOException {
    boolean compatMode = cache.getCacheConfiguration().compatibility().enabled();
    Class<? extends Matcher> matcherImplClass =
        compatMode ? CompatibilityReflectionMatcher.class : ProtobufMatcher.class;

    EmbeddedQuery eq =
        new EmbeddedQuery(
            cache,
            request.getJpqlString(),
            request.getStartOffset(),
            request.getMaxResults(),
            matcherImplClass);
    List<?> list = eq.list();
    int projSize = 0;
    if (eq.getProjection() != null && eq.getProjection().length > 0) {
      projSize = eq.getProjection().length;
    }
    List<WrappedMessage> results =
        new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize);
    for (Object o : list) {
      if (projSize == 0) {
        if (compatMode) {
          // if we are in compat mode then this is the real object so need to marshall it first
          o = ProtobufUtil.toWrappedByteArray(serCtx, o);
        }
        results.add(new WrappedMessage(o));
      } else {
        Object[] row = (Object[]) o;
        for (int j = 0; j < projSize; j++) {
          results.add(new WrappedMessage(row[j]));
        }
      }
    }

    QueryResponse response = new QueryResponse();
    response.setTotalResults(eq.getResultSize());
    response.setNumResults(list.size());
    response.setProjectionSize(projSize);
    response.setResults(results);

    return response;
  }
  public void testPutAndGet() throws Exception {
    User user = createUser();
    remoteCache.put(1, user);

    // try to get the object through the local cache interface and check it's the same object we put
    assertEquals(1, cache.keySet().size());
    byte[] key = (byte[]) cache.keySet().iterator().next();
    Object localObject = cache.get(key);
    assertNotNull(localObject);
    assertTrue(localObject instanceof byte[]);
    Object unmarshalledObject =
        ProtobufUtil.fromWrappedByteArray(
            ProtoStreamMarshaller.getSerializationContext(remoteCacheManager),
            (byte[]) localObject);
    assertTrue(unmarshalledObject instanceof User);
    assertUser((User) unmarshalledObject);

    // get the object through the remote cache interface and check it's the same object we put
    User fromRemoteCache = remoteCache.get(1);
    assertUser(fromRemoteCache);
  }
 @Override
 public byte[] filterAndConvert(
     Object key,
     Object oldValue,
     Metadata oldMetadata,
     Object newValue,
     Metadata newMetadata,
     EventType eventType) {
   ObjectFilter.FilterResult filterResult =
       filterAndConverter.filterAndConvert(key, newValue, newMetadata);
   if (filterResult != null) {
     try {
       return ProtobufUtil.toWrappedByteArray(
           serCtx,
           new FilterResult(
               filterResult.getInstance(),
               filterResult.getProjection(),
               filterResult.getSortProjection()));
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
   }
   return null;
 }
  public void testIndexingWithWrapper() throws Exception {
    MarshallerRegistration.registerMarshallers(
        ProtobufMetadataManagerImpl.getSerializationContext(cacheManager));

    // Store some test data:
    ProtobufValueWrapper wrapper1 =
        new ProtobufValueWrapper(createMarshalledUser("Adrian", "Nistor"));
    ProtobufValueWrapper wrapper2 =
        new ProtobufValueWrapper(createMarshalledUser("John", "Batman"));

    cache.put(1, wrapper1); // todo how do we index if the key is a byte array?
    cache.put(2, wrapper2);

    SearchManager sm = Search.getSearchManager(cache);

    SearchIntegrator searchFactory = sm.unwrap(SearchIntegrator.class);
    assertNotNull(searchFactory.getIndexManager(ProtobufValueWrapper.class.getName()));

    Query luceneQuery =
        sm.buildQueryBuilderForClass(ProtobufValueWrapper.class)
            .get()
            .keyword()
            .onField("name")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .matching("Adrian")
            .createQuery();

    List<ProtobufValueWrapper> list = sm.<ProtobufValueWrapper>getQuery(luceneQuery).list();
    assertEquals(1, list.size());
    ProtobufValueWrapper pvw = list.get(0);
    User unwrapped =
        (User)
            ProtobufUtil.fromWrappedByteArray(
                ProtobufMetadataManagerImpl.getSerializationContextInternal(cacheManager),
                pvw.getBinary());
    assertEquals("Adrian", unwrapped.getName());

    // an alternative approach ...

    Query luceneQuery2 =
        searchFactory
            .buildQueryBuilder()
            .forEntity(ProtobufValueWrapper.class)
            .get()
            .keyword()
            .onField("name")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .matching("Adrian")
            .createQuery();

    List<EntityInfo> queryEntityInfos =
        searchFactory
            .createHSQuery(luceneQuery2, ProtobufValueWrapper.class)
            .projection("surname")
            .queryEntityInfos();

    assertEquals(1, queryEntityInfos.size());
    EntityInfo entityInfo = queryEntityInfos.get(0);
    assertEquals("Nistor", entityInfo.getProjection()[0]);
  }