Search in sources :

Example 6 with Index

use of org.neo4j.graphdb.index.Index in project neo4j-clean-remote-db-addon by jexp.

the class Neo4jDatabaseCleaner method getMutableIndex.

private <T extends PropertyContainer> Index<T> getMutableIndex(Index<T> index) {
    final Class<? extends Index> indexClass = index.getClass();
    if (indexClass.getName().endsWith("ReadOnlyIndexToIndexAdapter")) {
        try {
            final Field delegateIndexField = indexClass.getDeclaredField("delegate");
            delegateIndexField.setAccessible(true);
            return (Index<T>) delegateIndexField.get(index);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    } else {
        return index;
    }
}
Also used : Field(java.lang.reflect.Field) RelationshipIndex(org.neo4j.graphdb.index.RelationshipIndex) Index(org.neo4j.graphdb.index.Index)

Example 7 with Index

use of org.neo4j.graphdb.index.Index in project neo4j by neo4j.

the class IndexProviderShellApp method index.

private void index(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    NodeOrRelationship current = getCurrent(session);
    String index = getIndexName(parser);
    String key = parser.argument(1, "Key not supplied");
    Object value = parser.arguments().size() > 2 ? parser.arguments().get(2) : current.getProperty(key, null);
    if (value == null) {
        throw new ShellException("No value to index");
    }
    Index theIndex = current.isNode() ? getServer().getDb().index().forNodes(index) : getServer().getDb().index().forRelationships(index);
    theIndex.add(current.asPropertyContainer(), key, value);
}
Also used : JSONObject(org.neo4j.shell.util.json.JSONObject) Index(org.neo4j.graphdb.index.Index) ShellException(org.neo4j.shell.ShellException)

Example 8 with Index

use of org.neo4j.graphdb.index.Index in project neo4j by neo4j.

the class IndexProviderShellApp method query.

private IndexHits<PropertyContainer> query(AppCommandParser parser, Output out) throws RemoteException, ShellException {
    String index = getIndexName(parser);
    String query1 = parser.argument(1, "Key not supplied");
    String query2 = parser.argumentWithDefault(2, null);
    Index theIndex = getIndex(index, getEntityType(parser), out);
    return query2 != null ? theIndex.query(query1, query2) : theIndex.query(query1);
}
Also used : Index(org.neo4j.graphdb.index.Index)

Example 9 with Index

use of org.neo4j.graphdb.index.Index in project neo4j by neo4j.

the class IndexCreationTest method indexCreationConfigRaceCondition.

@Test
public void indexCreationConfigRaceCondition() throws Exception {
    // a couple of times to be sure.
    for (int run = 0; run < 10; run++) {
        final int r = run;
        final CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = newCachedThreadPool();
        for (int thread = 0; thread < 10; thread++) {
            executor.submit((Runnable) () -> {
                try (Transaction tx = db.beginTx()) {
                    latch.await();
                    Index<Node> index = db.index().forNodes("index" + r);
                    Node node = db.createNode();
                    index.add(node, "name", "Name");
                    tx.success();
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            });
        }
        latch.countDown();
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
        verifyThatIndexCreationTransactionIsTheFirstOne();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ExecutorService(java.util.concurrent.ExecutorService) Index(org.neo4j.graphdb.index.Index) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with Index

use of org.neo4j.graphdb.index.Index in project neo4j-apoc-procedures by neo4j-contrib.

the class IndexUpdateTransactionEventHandler method initIndexConfiguration.

// might be run from a scheduler, so we need to make sure we have a transaction
private synchronized Map<String, Map<String, Collection<Index<Node>>>> initIndexConfiguration() {
    Map<String, Map<String, Collection<Index<Node>>>> indexesByLabelAndProperty = new HashMap<>();
    try (Transaction tx = graphDatabaseService.beginTx()) {
        final IndexManager indexManager = graphDatabaseService.index();
        for (String indexName : indexManager.nodeIndexNames()) {
            final Index<Node> index = indexManager.forNodes(indexName);
            Map<String, String> indexConfig = indexManager.getConfiguration(index);
            if (Util.toBoolean(indexConfig.get("autoUpdate"))) {
                String labels = indexConfig.getOrDefault("labels", "");
                for (String label : labels.split(":")) {
                    Map<String, Collection<Index<Node>>> propertyKeyToIndexMap = indexesByLabelAndProperty.computeIfAbsent(label, s -> new HashMap<>());
                    String[] keysForLabel = indexConfig.getOrDefault("keysForLabel:" + label, "").split(":");
                    for (String property : keysForLabel) {
                        propertyKeyToIndexMap.computeIfAbsent(property, s -> new ArrayList<>()).add(index);
                    }
                }
            }
        }
        tx.success();
    }
    return indexesByLabelAndProperty;
}
Also used : ApocConfiguration(apoc.ApocConfiguration) AvailabilityGuard(org.neo4j.kernel.AvailabilityGuard) java.util(java.util) Log(org.neo4j.logging.Log) ScheduledFuture(java.util.concurrent.ScheduledFuture) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Pools(apoc.Pools) Iterables.stream(org.neo4j.helpers.collection.Iterables.stream) BlockingQueue(java.util.concurrent.BlockingQueue) StopWatch(org.apache.commons.lang3.time.StopWatch) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Supplier(java.util.function.Supplier) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) IndexManager(org.neo4j.graphdb.index.IndexManager) LabelEntry(org.neo4j.graphdb.event.LabelEntry) Iterables(org.neo4j.helpers.collection.Iterables) Stream(java.util.stream.Stream) org.neo4j.graphdb(org.neo4j.graphdb) Util(apoc.util.Util) Index(org.neo4j.graphdb.index.Index) Index(org.neo4j.graphdb.index.Index) IndexManager(org.neo4j.graphdb.index.IndexManager)

Aggregations

Index (org.neo4j.graphdb.index.Index)10 Node (org.neo4j.graphdb.Node)5 Test (org.junit.Test)3 Transaction (org.neo4j.graphdb.Transaction)3 IndexManager (org.neo4j.graphdb.index.IndexManager)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Relationship (org.neo4j.graphdb.Relationship)2 ApocConfiguration (apoc.ApocConfiguration)1 Pools (apoc.Pools)1 Util (apoc.util.Util)1 Field (java.lang.reflect.Field)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 Executors.newCachedThreadPool (java.util.concurrent.Executors.newCachedThreadPool)1