Search in sources :

Example 1 with Index

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

the class IndexProviderShellApp method get.

private IndexHits<PropertyContainer> get(AppCommandParser parser, Output out) throws ShellException, RemoteException {
    String index = getIndexName(parser);
    String key = parser.argument(1, "Key not supplied");
    String value = parser.argument(2, "Value not supplied");
    Index theIndex = getIndex(index, getEntityType(parser), out);
    return theIndex.get(key, value);
}
Also used : Index(org.neo4j.graphdb.index.Index)

Example 2 with Index

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

the class IndexProviderShellApp method getIndex.

private <T extends PropertyContainer> Index<T> getIndex(String indexName, Class<T> type, Output out) throws RemoteException {
    IndexManager index = getServer().getDb().index();
    boolean exists = (type.equals(Node.class) && index.existsForNodes(indexName)) || (type.equals(Relationship.class) && index.existsForRelationships(indexName));
    if (!exists) {
        if (out != null) {
            out.println("No such " + type.getSimpleName().toLowerCase() + " index '" + indexName + "'");
        }
        return null;
    }
    return (Index<T>) (type.equals(Node.class) ? index.forNodes(indexName) : index.forRelationships(indexName));
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) Index(org.neo4j.graphdb.index.Index)

Example 3 with Index

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

the class IndexProviderShellApp method remove.

private void remove(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    NodeOrRelationship current = getCurrent(session);
    String index = getIndexName(parser);
    String key = parser.argumentWithDefault(1, null);
    Object value = null;
    if (key != null) {
        value = parser.argumentWithDefault(2, null);
    }
    Index theIndex;
    if (current.isNode()) {
        theIndex = getIndex(index, Node.class, out);
    } else {
        theIndex = getIndex(index, Relationship.class, out);
    }
    if (theIndex != null) {
        if (key != null && value != null) {
            theIndex.remove(current.asPropertyContainer(), key, value);
        } else if (key != null) {
            theIndex.remove(current.asPropertyContainer(), key);
        } else {
            theIndex.remove(current.asPropertyContainer());
        }
    }
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) JSONObject(org.neo4j.shell.util.json.JSONObject) Index(org.neo4j.graphdb.index.Index)

Example 4 with Index

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

the class IndexOperationsIT method index_objects_can_be_reused_after_role_switch.

@Test
public void index_objects_can_be_reused_after_role_switch() throws Throwable {
    // GIVEN
    // -- an existing index
    String key = "key", value = "value";
    HighlyAvailableGraphDatabase master = cluster.getMaster();
    long nodeId = createNode(master, key, value, true);
    cluster.sync();
    // -- get Index and IndexManager references to all dbs
    Map<HighlyAvailableGraphDatabase, IndexManager> indexManagers = new HashMap<>();
    Map<HighlyAvailableGraphDatabase, Index<Node>> indexes = new HashMap<>();
    for (HighlyAvailableGraphDatabase db : cluster.getAllMembers()) {
        try (Transaction transaction = db.beginTx()) {
            indexManagers.put(db, db.index());
            indexes.put(db, db.index().forNodes(key));
            transaction.success();
        }
    }
    // WHEN
    // -- there's a master switch
    RepairKit repair = cluster.shutdown(master);
    indexManagers.remove(master);
    indexes.remove(master);
    cluster.await(ClusterManager.masterAvailable(master));
    cluster.await(ClusterManager.masterSeesSlavesAsAvailable(1));
    // -- the index instances should still be viable to use
    for (Map.Entry<HighlyAvailableGraphDatabase, IndexManager> entry : indexManagers.entrySet()) {
        HighlyAvailableGraphDatabase db = entry.getKey();
        try (Transaction transaction = db.beginTx()) {
            IndexManager indexManager = entry.getValue();
            assertTrue(indexManager.existsForNodes(key));
            assertEquals(nodeId, indexManager.forNodes(key).get(key, value).getSingle().getId());
        }
    }
    for (Map.Entry<HighlyAvailableGraphDatabase, Index<Node>> entry : indexes.entrySet()) {
        HighlyAvailableGraphDatabase db = entry.getKey();
        try (Transaction transaction = db.beginTx()) {
            Index<Node> index = entry.getValue();
            assertEquals(nodeId, index.get(key, value).getSingle().getId());
        }
    }
    repair.repair();
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) Index(org.neo4j.graphdb.index.Index) IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) RepairKit(org.neo4j.kernel.impl.ha.ClusterManager.RepairKit) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Index

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

the class IndexCreationTest method verifyThatIndexCreationTransactionIsTheFirstOne.

private void verifyThatIndexCreationTransactionIsTheFirstOne() throws Exception {
    PhysicalLogFile pLogFile = db.getDependencyResolver().resolveDependency(PhysicalLogFile.class);
    long version = db.getDependencyResolver().resolveDependency(LogVersionRepository.class).getCurrentLogVersion();
    db.getDependencyResolver().resolveDependency(LogRotation.class).rotateLogFile();
    db.getDependencyResolver().resolveDependency(CheckPointer.class).forceCheckPoint(new SimpleTriggerInfo("test"));
    ReadableLogChannel logChannel = pLogFile.getReader(LogPosition.start(version));
    final AtomicBoolean success = new AtomicBoolean(false);
    try (IOCursor<LogEntry> cursor = new LogEntryCursor(new VersionAwareLogEntryReader<>(), logChannel)) {
        List<StorageCommand> commandsInFirstEntry = new ArrayList<>();
        boolean startFound = false;
        while (cursor.next()) {
            LogEntry entry = cursor.get();
            if (entry instanceof LogEntryStart) {
                if (startFound) {
                    throw new IllegalArgumentException("More than one start entry");
                }
                startFound = true;
            }
            if (startFound && entry instanceof LogEntryCommand) {
                commandsInFirstEntry.add(entry.<LogEntryCommand>as().getXaCommand());
            }
            if (entry instanceof LogEntryCommit) {
                // The first COMMIT
                assertTrue(startFound);
                assertFalse("Index creation transaction wasn't the first one", commandsInFirstEntry.isEmpty());
                List<StorageCommand> createCommands = Iterators.asList(new FilteringIterator<>(commandsInFirstEntry.iterator(), item -> item instanceof IndexDefineCommand));
                assertEquals(1, createCommands.size());
                success.set(true);
                break;
            }
        }
    }
    assertTrue("Didn't find any commit record in log " + version, success.get());
}
Also used : LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) Iterators(org.neo4j.helpers.collection.Iterators) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOCursor(org.neo4j.cursor.IOCursor) Node(org.neo4j.graphdb.Node) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) ArrayList(java.util.ArrayList) StorageCommand(org.neo4j.storageengine.api.StorageCommand) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) LogRotation(org.neo4j.kernel.impl.transaction.log.rotation.LogRotation) After(org.junit.After) Transaction(org.neo4j.graphdb.Transaction) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) IndexDefineCommand(org.neo4j.kernel.impl.index.IndexDefineCommand) LogVersionRepository(org.neo4j.kernel.impl.transaction.log.LogVersionRepository) TestDirectory(org.neo4j.test.rule.TestDirectory) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) LogEntryStart(org.neo4j.kernel.impl.transaction.log.entry.LogEntryStart) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) Executors.newCachedThreadPool(java.util.concurrent.Executors.newCachedThreadPool) Assert.assertFalse(org.junit.Assert.assertFalse) FilteringIterator(org.neo4j.helpers.collection.FilteringIterator) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry) Assert.assertEquals(org.junit.Assert.assertEquals) Index(org.neo4j.graphdb.index.Index) ReadableLogChannel(org.neo4j.kernel.impl.transaction.log.ReadableLogChannel) LogEntryCommand(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommand) IndexDefineCommand(org.neo4j.kernel.impl.index.IndexDefineCommand) StorageCommand(org.neo4j.storageengine.api.StorageCommand) ArrayList(java.util.ArrayList) LogVersionRepository(org.neo4j.kernel.impl.transaction.log.LogVersionRepository) LogEntryCursor(org.neo4j.kernel.impl.transaction.log.LogEntryCursor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpleTriggerInfo(org.neo4j.kernel.impl.transaction.log.checkpoint.SimpleTriggerInfo) LogEntryCommit(org.neo4j.kernel.impl.transaction.log.entry.LogEntryCommit) CheckPointer(org.neo4j.kernel.impl.transaction.log.checkpoint.CheckPointer) LogRotation(org.neo4j.kernel.impl.transaction.log.rotation.LogRotation) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) LogEntry(org.neo4j.kernel.impl.transaction.log.entry.LogEntry)

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