Exemple #1
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;
  }
  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);
  }
  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]);
  }