Search in sources :

Example 6 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project graphdb by neo4j-attic.

the class TestPerformanceAndSanity method makeSureFilesAreClosedProperly.

@Test
public void makeSureFilesAreClosedProperly() throws Exception {
    commitTx();
    final Index<Node> index = nodeIndex("open-files", LuceneIndexImplementation.EXACT_CONFIG);
    final long time = System.currentTimeMillis();
    final CountDownLatch latch = new CountDownLatch(30);
    for (int t = 0; t < latch.getCount(); t++) {
        new Thread() {

            public void run() {
                for (int i = 0; System.currentTimeMillis() - time < 100 * 1000; i++) {
                    if (i % 10 == 0) {
                        if (i % 100 == 0) {
                            int size = 0;
                            int type = (int) (System.currentTimeMillis() % 3);
                            if (type == 0) {
                                IndexHits<Node> itr = index.get("key", "value5");
                                try {
                                    itr.getSingle();
                                } catch (NoSuchElementException e) {
                                }
                                size = 99;
                            } else if (type == 1) {
                                IndexHits<Node> itr = index.get("key", "value5");
                                for (; itr.hasNext() && size < 5; size++) {
                                    itr.next();
                                }
                                itr.close();
                            } else {
                                IndexHits<Node> itr = index.get("key", "crap value");
                                // Iterate over the hits sometimes (it's always gonna be 0 sized)
                                if (System.currentTimeMillis() % 10 > 5) {
                                    IteratorUtil.count((Iterator<Node>) itr);
                                }
                            }
                            System.out.println("C iterated " + size + " only");
                        } else {
                            int size = IteratorUtil.count((Iterator<Node>) index.get("key", "value5"));
                            System.out.println("hit size:" + size);
                        }
                    } else {
                        Transaction tx = graphDb.beginTx();
                        try {
                            for (int ii = 0; ii < 20; ii++) {
                                Node node = graphDb.createNode();
                                index.add(node, "key", "value" + ii);
                            }
                            tx.success();
                        } finally {
                            tx.finish();
                        }
                    }
                }
                latch.countDown();
            }
        }.start();
    }
    latch.await();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) IndexHits(org.neo4j.graphdb.index.IndexHits) Iterator(java.util.Iterator) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 7 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project neo4j-mobile-android by neo4j-contrib.

the class LuceneIndex method search.

private IndexHits<Document> search(IndexSearcherRef searcherRef, Query query, QueryContext additionalParametersOrNull, IndexSearcher additionsSearcher, Collection<Long> removed) {
    try {
        if (additionsSearcher != null && !removed.isEmpty()) {
            letThroughAdditions(additionsSearcher, query, removed);
        }
        IndexSearcher searcher = additionsSearcher == null ? searcherRef.getSearcher() : new IndexSearcher(new MultiReader(searcherRef.getSearcher().getIndexReader(), additionsSearcher.getIndexReader()));
        IndexHits<Document> result = null;
        if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
            result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
        } else {
            Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
            boolean forceScore = additionalParametersOrNull == null || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
            Hits hits = new Hits(searcher, query, null, sorting, forceScore);
            result = new HitsIterator(hits);
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException("Unable to query " + this + " with " + query, e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexHits(org.neo4j.graphdb.index.IndexHits) MultiReader(org.apache.lucene.index.MultiReader) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Example 8 with IndexHits

use of org.neo4j.graphdb.index.IndexHits in project blueprints by tinkerpop.

the class Neo4jIndex method count.

/**
     * {@inheritDoc}
     * <p/>
     * The underlying Neo4j graph does not natively support this method within a transaction.
     * If the graph is not currently in a transaction, then the operation runs efficiently.
     * If the graph is in a transaction, then, for every element, a try/catch is used to determine if its in the current transaction.
     */
public long count(final String key, final Object value) {
    if (!this.graph.checkElementsInTransaction()) {
        final IndexHits hits = this.rawIndex.get(key, value);
        final long count = hits.size();
        hits.close();
        return count;
    } else {
        final CloseableIterable<T> hits = this.get(key, value);
        long count = 0;
        for (final T t : hits) {
            count++;
        }
        hits.close();
        return count;
    }
}
Also used : IndexHits(org.neo4j.graphdb.index.IndexHits)

Aggregations

IndexHits (org.neo4j.graphdb.index.IndexHits)8 IOException (java.io.IOException)4 Collection (java.util.Collection)4 ArrayList (java.util.ArrayList)2 Document (org.apache.lucene.document.Document)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 MultiSearcher (org.apache.lucene.search.MultiSearcher)2 Searcher (org.apache.lucene.search.Searcher)2 Sort (org.apache.lucene.search.Sort)2 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 MultiReader (org.apache.lucene.index.MultiReader)1 Test (org.junit.Test)1 Node (org.neo4j.graphdb.Node)1 Transaction (org.neo4j.graphdb.Transaction)1