Search in sources :

Example 1 with Indexer

use of com.tuplejump.stargate.lucene.Indexer in project stargate-core by tuplejump.

the class PerVNodeIndexContainer method updateIndexers.

@Override
public void updateIndexers(Collection<Range<Token>> ranges) {
    writeLock.lock();
    Boolean isInfoLoggingEnabled = logger.isInfoEnabled();
    try {
        if (indexers.isEmpty()) {
            if (isInfoLoggingEnabled) {
                logger.info("Adding VNode indexers");
            }
            for (Range<Token> range : ranges) {
                String rangeStr = range.left.toString();
                AtomicLong records = Stargate.getInstance().getAtomicLong(INDEX_RECORDS + "-" + indexName + "-" + rangeStr);
                Indexer indexer = new BasicIndexer(records, analyzer, keyspace, cf, indexName, rangeStr);
                indexers.put(range, indexer);
                if (isInfoLoggingEnabled) {
                    logger.info("Added VNode indexers for range {}", range);
                }
            }
        } else {
            if (isInfoLoggingEnabled) {
                logger.info("Change in VNode indexers");
            }
            HashMap<Range<Token>, Indexer> indexersToRemove = new HashMap<>(indexers);
            for (Range<Token> range : ranges) {
                indexersToRemove.remove(range);
            }
            for (Map.Entry<Range<Token>, Indexer> entry : indexersToRemove.entrySet()) {
                if (isInfoLoggingEnabled) {
                    logger.info("Removing indexer for range {}", entry.getKey());
                }
                Indexer indexer = indexers.remove(entry.getKey());
                indexer.removeIndex();
                if (isInfoLoggingEnabled) {
                    logger.info("Removed indexer for range {}", entry.getKey());
                }
            }
        }
    } finally {
        writeLock.unlock();
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Indexer(com.tuplejump.stargate.lucene.Indexer) BasicIndexer(com.tuplejump.stargate.lucene.BasicIndexer) BasicIndexer(com.tuplejump.stargate.lucene.BasicIndexer) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range)

Example 2 with Indexer

use of com.tuplejump.stargate.lucene.Indexer in project stargate-core by tuplejump.

the class PerVNodeIndexContainer method search.

@Override
public <T> T search(SearcherCallback<T> searcherCallback) {
    List<IndexReader> indexReaders = new ArrayList<>();
    Map<Indexer, IndexSearcher> indexSearchers = new HashMap<>();
    for (Map.Entry<Range<Token>, Indexer> entry : indexers.entrySet()) {
        Range<Token> range = entry.getKey();
        boolean intersects = intersects(searcherCallback.filterRange(), searcherCallback.isSingleToken(), searcherCallback.isFullRange(), range);
        if (intersects) {
            Indexer indexer = entry.getValue();
            IndexSearcher searcher = indexer.acquire();
            indexSearchers.put(indexer, searcher);
            indexReaders.add(searcher.getIndexReader());
        }
    }
    IndexReader[] indexReadersArr = new IndexReader[indexReaders.size()];
    indexReaders.toArray(indexReadersArr);
    MultiReader multiReader = null;
    try {
        multiReader = new MultiReader(indexReadersArr, false);
        IndexSearcher allSearcher = new IndexSearcher(multiReader, executorService);
        return searcherCallback.doWithSearcher(allSearcher);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (multiReader != null)
                multiReader.close();
        } catch (IOException e) {
            logger.error("Could not close reader", e);
        }
        for (Map.Entry<Indexer, IndexSearcher> entry : indexSearchers.entrySet()) {
            entry.getKey().release(entry.getValue());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) Token(org.apache.cassandra.dht.Token) IOException(java.io.IOException) Range(org.apache.cassandra.dht.Range) IOException(java.io.IOException) Indexer(com.tuplejump.stargate.lucene.Indexer) BasicIndexer(com.tuplejump.stargate.lucene.BasicIndexer) IndexReader(org.apache.lucene.index.IndexReader)

Aggregations

BasicIndexer (com.tuplejump.stargate.lucene.BasicIndexer)2 Indexer (com.tuplejump.stargate.lucene.Indexer)2 Range (org.apache.cassandra.dht.Range)2 Token (org.apache.cassandra.dht.Token)2 IOException (java.io.IOException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 IndexReader (org.apache.lucene.index.IndexReader)1 MultiReader (org.apache.lucene.index.MultiReader)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1