public void testAdded() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    AssertJUnit.assertEquals(2, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert !found.contains(person4) : "This should not contain object person4";

    person4 = new Person();
    person4.setName("Mighty Goat");
    person4.setBlurb("Also eats grass");

    cache1.put("mighty", person4);

    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    found = cacheQuery.list();

    AssertJUnit.assertEquals(3, found.size());
    assert found.contains(person2);
    assert found.contains(person3);
    assert found.contains(person4) : "This should now contain object person4";
  }
  public void testEventFilterAndConverter() {
    QueryFactory qf = Search.getQueryFactory(cache());

    Query query =
        qf.from(Person.class).having("age").lte(31).toBuilder().select("name", "age").build();

    EntryListener listener = new EntryListener();

    // we want our cluster listener to be notified only if the entity matches our query
    cache().addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      cache.put(i, value);
    }

    assertEquals(10, cache.size());
    assertEquals(7, listener.createEvents.size());
    assertTrue(listener.modifyEvents.isEmpty());

    for (ObjectFilter.FilterResult r : listener.createEvents) {
      assertNotNull(r.getProjection());
      assertEquals(2, r.getProjection().length);
      assertTrue((Integer) r.getProjection()[1] <= 31);
    }

    cache().removeListener(listener);
  }
  public void testModified() throws Exception {
    prepareTestData();
    assertQueryInterceptorPresent(cache2);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("playing");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    List<Object> found = cacheQuery.list();

    assert found.size() == 1 : "Expected list of size 1, was of size " + found.size();
    assert found.get(0).equals(person1);

    person1.setBlurb("Likes pizza");
    cache1.put("Navin", person1);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("pizza");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);

    found = cacheQuery.list();

    assert found.size() == 1;
    assert found.get(0).equals(person1);
  }
  public void testEventFilter() {
    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(99);

      cache().put(i, value);
    }
    assertEquals(10, cache.size());

    QueryFactory qf = Search.getQueryFactory(cache());

    Query query =
        qf.from(Person.class)
            .having("age")
            .lte(param("ageParam"))
            .toBuilder()
            .build()
            .setParameter("ageParam", 31);

    EntryListener listener = new EntryListener();

    // we want our cluster listener to be notified only if the entity matches our query
    cache().addListener(listener, Search.makeFilter(query), null);

    assertTrue(listener.createEvents.isEmpty());
    assertTrue(listener.modifyEvents.isEmpty());

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      cache().put(i, value);
    }

    assertEquals(10, cache.size());
    assertTrue(listener.createEvents.isEmpty());
    assertEquals(7, listener.modifyEvents.size());

    for (ObjectFilter.FilterResult r : listener.modifyEvents) {
      Person p = (Person) r.getInstance();
      assertTrue(p.getAge() <= 31);
    }

    cache().removeListener(listener);

    // ensure no more invocations after the listener was removed
    listener.createEvents.clear();
    listener.modifyEvents.clear();
    Person value = new Person();
    value.setName("George");
    value.setAge(30);

    cache().put(-1, value);
    assertTrue(listener.createEvents.isEmpty());
    assertTrue(listener.modifyEvents.isEmpty());
  }
  /** Using grouping and aggregation with event filters is not allowed. */
  @Test(
      expectedExceptions = ParsingException.class,
      expectedExceptionsMessageRegExp = ".*ISPN028509:.*")
  public void testDisallowGroupingAndAggregation() {
    Query query =
        Search.getQueryFactory(cache(0))
            .from(Person.class)
            .having("age")
            .gte(20)
            .select(max("age"))
            .build();

    cache(0).addListener(new EntryListener(), Search.makeFilter(query), null);
  }
  public void testEventFilter() {
    QueryFactory qf = Search.getQueryFactory(cache(0));

    Query query = qf.from(org.infinispan.query.test.Person.class).having("age").lte(31).build();

    EntryListener listener = new EntryListener();

    for (int i = 0; i < 5; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 30);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    // we want our cluster listener to be notified only if the entity matches our query
    cache(0).addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    assertEquals(9, listener.results.size());
    for (ObjectFilter.FilterResult r : listener.results) {
      Person p = (Person) r.getInstance();
      assertTrue(p.getAge() <= 31);
    }

    cache(0).removeListener(listener);

    // ensure no more invocations after the listener was removed
    listener.results.clear();
    Person value = new Person();
    value.setName("George");
    value.setAge(30);

    Object key = new MagicKey(cache(0));
    cache(0).put(key, value);
    assertEquals(0, listener.results.size());
  }
 @Override
 protected void rebuildIndexes() throws Exception {
   Cache cache = caches.get(0);
   SearchManager searchManager = Search.getSearchManager(cache);
   NotifyingFuture<Void> future = searchManager.getMassIndexer().startAsync();
   future.get();
 }
  @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);
    }
  }
Exemple #9
0
  public static List<Object> performSearch(
      Select command, Class<?> type, String cacheName, CacheContainerWrapper cache)
      throws TranslatorException {

    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Using Lucene Searching."); // $NON-NLS-1$

    // Map<?, ?> cache,
    SearchManager searchManager = Search.getSearchManager((Cache<?, ?>) cache.getCache(cacheName));

    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(type).get();

    BooleanJunction<BooleanJunction> junction = queryBuilder.bool();
    boolean createdQueries = buildQueryFromWhereClause(command.getWhere(), junction, queryBuilder);

    Query query = null;
    if (createdQueries) {
      query = junction.createQuery();
    } else {
      query = queryBuilder.all().createQuery();
    }

    CacheQuery cacheQuery = searchManager.getQuery(query, type); // rootNodeType

    List<Object> results = cacheQuery.list();
    if (results == null || results.isEmpty()) {
      return Collections.emptyList();
    }

    return results;
  }
 protected void checkIndex(int expectedNumber, Class<?> entity) {
   for (Cache<?, ?> c : caches) {
     SearchManager searchManager = Search.getSearchManager(c);
     assertEquals(
         searchManager.getQuery(new MatchAllDocsQuery(), entity).getResultSize(), expectedNumber);
   }
 }
 private List searchByName(String name, Cache c) {
   SearchManager sm = Search.getSearchManager(c);
   CacheQuery q = sm.getQuery(SEntity.searchByName(name), SEntity.class);
   int resultSize = q.getResultSize();
   List l = q.list();
   assert l.size() == resultSize;
   return q.list();
 }
 public void setCache(Cache<?, ?> cache, EmbeddedCacheManager cacheManager) {
   this.cacheManager = cacheManager;
   this.cache = cache;
   SearchManager searchManager = Search.getSearchManager(cache);
   SearchFactory searchFactory = searchManager.getSearchFactory();
   searchFactoryImplementor = (SearchFactoryImplementor) searchFactory;
   queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
 }
 public static CacheQuery createCacheQuery(Cache m_cache, String fieldName, String searchString)
     throws ParseException {
   QueryParser qp = createQueryParser(fieldName);
   Query parsedQuery = qp.parse(searchString);
   SearchManager queryFactory = Search.getSearchManager(m_cache);
   CacheQuery cacheQuery = queryFactory.getQuery(parsedQuery);
   return cacheQuery;
 }
 @BeforeClass
 public void prepareSearchFactory() throws Exception {
   qf = Search.getSearchManager(cache);
   cache.put("195 Inter", new Car("Ferrari 195 Inter", "Rosso corsa", 2341));
   cache.put("212 Inter", new Car("Ferrari 212 Inter", "black", 4000));
   cache.put("500_Superfast", new Car("Ferrari 500_Superfast", "Rosso corsa", 4000));
   // test for duplication:
   cache.put("500_Superfast", new Car("Ferrari 500_Superfast", "Rosso corsa", 4000));
 }
  public void testRemoved() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    assert found.size() == 2;
    assert found.contains(person2);
    assert found.contains(person3) : "This should still contain object person3";

    cache1.remove(key3);

    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    found = cacheQuery.list();
  }
  public void testGetResultSize() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("playing");
    cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery);
    List<Object> found = cacheQuery.list();

    AssertJUnit.assertEquals(1, found.size());
  }
  protected CallCountingCQResultListener<Object, Object> createContinuousQuery() {
    QueryFactory<?> qf = Search.getQueryFactory(cache(0));

    Query query = qf.from(Person.class).having("age").lte(30).toBuilder().build();

    CallCountingCQResultListener<Object, Object> listener = new CallCountingCQResultListener<>();
    ContinuousQuery<Object, Object> cq = new ContinuousQuery<Object, Object>(cache(0));
    cq.addContinuousQueryListener(query, listener);
    return listener;
  }
  public void testEventFilterAndConverter() {
    QueryFactory qf = Search.getQueryFactory(cache(0));

    Query query =
        qf.from(org.infinispan.query.test.Person.class)
            .having("age")
            .lte(31)
            .select("name", "age")
            .build();

    EntryListener listener = new EntryListener();

    for (int i = 0; i < 5; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 30);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    // we want our cluster listener to be notified only if the entity matches our query
    cache(0).addListener(listener, Search.makeFilter(query), null);

    for (int i = 0; i < 10; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      Object key = new MagicKey(cache);
      cache.put(key, value);
    }

    assertEquals(9, listener.results.size());
    for (ObjectFilter.FilterResult r : listener.results) {
      assertTrue((Integer) r.getProjection()[1] <= 31);
    }

    cache(0).removeListener(listener);
  }
Exemple #19
0
 private CacheQuery queryByNameField(String name, Class<?> entity) {
   SearchManager sm = Search.getSearchManager(cache);
   Query query =
       sm.buildQueryBuilderForClass(entity)
           .get()
           .keyword()
           .onField("name")
           .matching(name)
           .createQuery();
   return sm.getQuery(query);
 }
 /**
  * This method filters task based on the input
  *
  * @param input - string to filter on
  * @return
  */
 public Collection<Task> filter(String input) {
   SearchManager sm = Search.getSearchManager(cache);
   QueryBuilder qb = sm.buildQueryBuilderForClass(Task.class).get();
   Query q = qb.keyword().onField("title").matching(input).createQuery();
   CacheQuery cq = sm.getQuery(q, Task.class);
   List<Task> tasks = new ArrayList<Task>();
   for (Object object : cq) {
     tasks.add((Task) object);
   }
   return tasks;
 }
  public void testSearchKeyTransformer() throws Exception {
    Cache cache1 = caches().get(0);
    Cache cache2 = caches().get(1);

    SearchManager manager = Search.getSearchManager(cache1);
    SearchManager manager1 = Search.getSearchManager(cache2);
    manager.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class);
    manager1.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class);

    prepareTestedObjects();

    TransactionManager transactionManager = cache1.getAdvancedCache().getTransactionManager();

    CustomKey3 customeKey1 = new CustomKey3(key1);
    CustomKey3 customeKey2 = new CustomKey3(key2);
    CustomKey3 customeKey3 = new CustomKey3(key3);

    // Put the 3 created objects in the cache1.
    if (transactionsEnabled()) transactionManager.begin();
    cache1.put(customeKey1, person1);
    cache1.put(customeKey2, person2);
    cache2.put(customeKey3, person3);
    if (transactionsEnabled()) transactionManager.commit();

    queryParser = createQueryParser("blurb");
    Query luceneQuery = queryParser.parse("Eats");

    CacheQuery cacheQuery = manager.getQuery(luceneQuery);

    ResultIterator found =
        cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY));

    int counter = 0;
    while (found.hasNext()) {
      found.next();
      counter++;
    }

    AssertJUnit.assertEquals(2, counter);
  }
  public void testClear() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    luceneQuery = queryParser.parse("eats");
    cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);

    Query[] queries = new Query[2];
    queries[0] = luceneQuery;

    luceneQuery = queryParser.parse("playing");
    queries[1] = luceneQuery;

    Query luceneQuery = queries[0].combine(queries);
    CacheQuery cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);
    AssertJUnit.assertEquals(3, cacheQuery.getResultSize());

    cache2.clear();

    AssertJUnit.assertEquals(3, cacheQuery.getResultSize());
    cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery);

    AssertJUnit.assertEquals(0, cacheQuery.getResultSize());
  }
 private void queryTest() {
   cache.put("jekyll", new TestEntity("Henry", "Jekyll", 1, "dissociate identity disorder"));
   cache.put("hyde", new TestEntity("Edward", "Hyde", 2, "dissociate identity disorder"));
   SearchManager sm = Search.getSearchManager(cache);
   Query query =
       sm.buildQueryBuilderForClass(TestEntity.class)
           .get()
           .keyword()
           .onField("name")
           .matching("Henry")
           .createQuery();
   CacheQuery q = sm.getQuery(query);
   assertEquals(1, q.getResultSize());
   assertEquals(TestEntity.class, q.list().get(0).getClass());
 }
  private void checkIndexPresence(Cache cache) {
    SearchIntegrator searchFactory = Search.getSearchManager(cache).unwrap(SearchIntegrator.class);

    assertTrue(searchFactory.getIndexedTypes().contains(getModelFactory().getUserImplClass()));
    assertNotNull(searchFactory.getIndexManager(getModelFactory().getUserImplClass().getName()));

    assertTrue(searchFactory.getIndexedTypes().contains(getModelFactory().getAccountImplClass()));
    assertNotNull(searchFactory.getIndexManager(getModelFactory().getAccountImplClass().getName()));

    assertTrue(
        searchFactory.getIndexedTypes().contains(getModelFactory().getTransactionImplClass()));
    assertNotNull(
        searchFactory.getIndexManager(getModelFactory().getTransactionImplClass().getName()));

    assertFalse(searchFactory.getIndexedTypes().contains(getModelFactory().getAddressImplClass()));
    assertNull(searchFactory.getIndexManager(getModelFactory().getAddressImplClass().getName()));
  }
 private void recreateCacheManager() {
   ConfigurationBuilder cfg = new ConfigurationBuilder();
   cfg.loaders()
       .preload(true)
       .passivation(true)
       .addStore()
       .cacheStore(new DummyInMemoryCacheStore())
       .purgeOnStartup(true)
       .indexing()
       .enable()
       .addProperty("default.directory_provider", "ram")
       .addProperty("lucene_version", "LUCENE_CURRENT");
   cm = TestCacheManagerFactory.createCacheManager(cfg);
   cache = cm.getCache();
   store = TestingUtil.extractComponent(cache, CacheLoaderManager.class).getCacheStore();
   search = Search.getSearchManager(cache);
 }
  public void testPutMap() throws Exception {
    prepareTestData();
    SearchManager searchManager = Search.getSearchManager(cache2);
    QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(Person.class).get();
    Query allQuery = queryBuilder.all().createQuery();
    assert searchManager.getQuery(allQuery, Person.class).list().size() == 3;

    Map<String, Person> allWrites = new HashMap<String, Person>();
    allWrites.put(key1, person1);
    allWrites.put(key2, person2);
    allWrites.put(key3, person3);

    cache2.putAll(allWrites);
    List found = searchManager.getQuery(allQuery, Person.class).list();
    AssertJUnit.assertEquals(3, found.size());

    cache2.putAll(allWrites);
    found = searchManager.getQuery(allQuery, Person.class).list();
    AssertJUnit.assertEquals(3, found.size());
  }
  public void testFullTextFilterOnOff() throws Exception {
    prepareTestData();
    queryParser = createQueryParser("blurb");
    Query luceneQuery = queryParser.parse("eats");

    CacheQuery query = Search.getSearchManager(cache1).getQuery(luceneQuery);
    FullTextFilter filter = query.enableFullTextFilter("personFilter");
    filter.setParameter("blurbText", "cheese");

    AssertJUnit.assertEquals(1, query.getResultSize());
    List result = query.list();

    Person person = (Person) result.get(0);
    AssertJUnit.assertEquals("MiniGoat", person.getName());
    AssertJUnit.assertEquals("Eats cheese", person.getBlurb());

    // Disabling the fullTextFilter.
    query.disableFullTextFilter("personFilter");
    AssertJUnit.assertEquals(2, query.getResultSize());
  }
 @Override
 protected void createCacheManagers() throws Throwable {
   ConfigurationBuilder cacheCfg = getDefaultClusteredCacheConfig(CACHE_MODE, TX_ENABLED);
   cacheCfg
       .clustering()
       .sync()
       .replTimeout(120000)
       .indexing()
       .index(Index.LOCAL)
       .addProperty("default.directory_provider", DIRECTORY_PROVIDER.toString())
       .addProperty("default.indexmanager", INDEX_MANAGER.toString())
       .addProperty("default.indexwriter.merge_factor", MERGE_FACTOR)
       .addProperty("hibernate.search.default.worker.execution", WORKER_MODE.toString())
       .addProperty("error_handler", "org.infinispan.query.helper.StaticTestingErrorHandler")
       .addProperty("lucene_version", "LUCENE_CURRENT");
   ;
   List<Cache<Integer, Transaction>> caches = createClusteredCaches(2, cacheCfg);
   cache1 = caches.get(0);
   cache2 = caches.get(1);
   massIndexer = Search.getSearchManager(cache1).getMassIndexer();
 }
  private long testEventFilterPerformance(boolean doRegisterListener) {
    final int numEntries = 100000;
    final int numListeners = 1000;
    List<NoOpEntryListener> listeners = new ArrayList<NoOpEntryListener>(numListeners);
    if (doRegisterListener) {
      Query query = makeQuery(cache(0));
      for (int i = 0; i < numListeners; i++) {
        NoOpEntryListener listener = new NoOpEntryListener();
        listeners.add(listener);
        cache(0).addListener(listener, Search.makeFilter(query), null);
      }
    }

    long startTs = System.nanoTime();
    // create entries
    for (int i = 0; i < numEntries; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      cache.put(value.getName(), value);
    }
    // update entries (with same value)
    for (int i = 0; i < numEntries; ++i) {
      Person value = new Person();
      value.setName("John");
      value.setAge(i + 25);

      Cache<Object, Person> cache = cache(i % NUM_NODES);
      cache.put(value.getName(), value);
    }
    long endTs = System.nanoTime();

    for (NoOpEntryListener listener : listeners) {
      cache(0).removeListener(listener);
    }

    return endTs - startTs;
  }
  @Override
  public List<Comment> getCommentsBySongId(String songId) {
    if (songId == null) {
      throw new IllegalArgumentException("Song id is null.");
    }
    ArrayList<Comment> comments = new ArrayList<Comment>();
    commentCache = provider.getCacheContainer().getCache("commentcache");
    SearchManager sm = Search.getSearchManager((Cache<String, Object>) commentCache);
    QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Comment.class).get();

    Query q = queryBuilder.keyword().onField("songId").matching(songId).createQuery();
    logger.debug("Lucene query: " + q);

    CacheQuery cq = sm.getQuery(q, Comment.class);

    for (Object o : cq.list()) {
      if (o instanceof Comment) {
        comments.add(((Comment) o));
      }
    }
    return comments;
  }