Search in sources :

Example 1 with IndexManager

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

the class DeleteDatabaseTest method createData.

private void createData(GraphDatabaseAPI db, int max) {
    Transaction tx = db.beginTx();
    try {
        final IndexManager indexManager = db.index();
        Node[] nodes = new Node[max];
        for (int i = 0; i < max; i++) {
            nodes[i] = db.createNode();
            final Index<Node> index = indexManager.forNodes("node_index_" + String.valueOf(i % 5));
            index.add(nodes[i], "ID", i);
        }
        Random random = new Random();
        for (int i = 0; i < max * 2; i++) {
            int from = random.nextInt(max);
            final int to = (from + 1 + random.nextInt(max - 1)) % max;
            final Relationship relationship = nodes[from].createRelationshipTo(nodes[to], DynamicRelationshipType.withName("TEST_" + i));
            final Index<Relationship> index = indexManager.forRelationships("rel_index_" + String.valueOf(i % 5));
            index.add(relationship, "ID", i);
        }
        tx.success();
    } finally {
        tx.finish();
    }
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) Transaction(org.neo4j.graphdb.Transaction) Random(java.util.Random) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship)

Example 2 with IndexManager

use of org.neo4j.graphdb.index.IndexManager 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 IndexManager

use of org.neo4j.graphdb.index.IndexManager 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 4 with IndexManager

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

the class TestLuceneBatchInsert method testSome.

@Test
public void testSome() throws Exception {
    BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
    String indexName = "users";
    BatchInserterIndex index = provider.nodeIndex(indexName, EXACT_CONFIG);
    Map<Integer, Long> ids = new HashMap<>();
    int count = 5;
    for (int i = 0; i < count; i++) {
        long id = inserter.createNode(null);
        index.add(id, map("name", "Joe" + i, "other", "Schmoe"));
        ids.put(i, id);
    }
    index.flush();
    for (int i = 0; i < count; i++) {
        assertContains(index.get("name", "Joe" + i), ids.get(i));
    }
    assertContains(index.query("name:Joe0 AND other:Schmoe"), ids.get(0));
    assertContains(index.query("name", "Joe*"), ids.values().toArray(new Long[ids.size()]));
    provider.shutdown();
    switchToGraphDatabaseService();
    try (Transaction transaction = db.beginTx()) {
        IndexManager indexManager = db.index();
        assertFalse(indexManager.existsForRelationships(indexName));
        assertTrue(indexManager.existsForNodes(indexName));
        assertNotNull(indexManager.forNodes(indexName));
        Index<Node> dbIndex = db.index().forNodes("users");
        for (int i = 0; i < count; i++) {
            assertContains(dbIndex.get("name", "Joe" + i), db.getNodeById(ids.get(i)));
        }
        Collection<Node> nodes = new ArrayList<>();
        for (long id : ids.values()) {
            nodes.add(db.getNodeById(id));
        }
        assertContains(dbIndex.query("name", "Joe*"), nodes.toArray(new Node[nodes.size()]));
        assertContains(dbIndex.query("name:Joe0 AND other:Schmoe"), db.getNodeById(ids.get(0)));
        transaction.success();
    }
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) LuceneBatchInserterIndexProviderNewImpl(org.neo4j.index.impl.lucene.legacy.LuceneBatchInserterIndexProviderNewImpl) IndexManager(org.neo4j.graphdb.index.IndexManager) LuceneBatchInserterIndexProvider(org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 5 with IndexManager

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

the class DbWrapper method deleteNodeIndex.

@Override
public void deleteNodeIndex(String name, ParcelableError err) throws RemoteException {
    try {
        checkCallerHasWritePermission();
        resumeTrx();
        try {
            IndexManager index = mDb.index();
            // this will create the index
            index.forNodes(name);
        } finally {
            suspendCurrentTrx("deleteNodeIndex");
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to delete node index '" + name + "'", e);
        err.setError(Errors.TRANSACTION, e.getMessage());
    }
}
Also used : IndexManager(org.neo4j.graphdb.index.IndexManager) RemoteException(android.os.RemoteException) InvalidTransactionException(org.neo4j.javax.transaction.InvalidTransactionException) SystemException(org.neo4j.javax.transaction.SystemException)

Aggregations

IndexManager (org.neo4j.graphdb.index.IndexManager)16 Node (org.neo4j.graphdb.Node)13 Test (org.junit.Test)10 Transaction (org.neo4j.graphdb.Transaction)6 Relationship (org.neo4j.graphdb.Relationship)4 RelationshipIndex (org.neo4j.graphdb.index.RelationshipIndex)3 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)3 RemoteException (android.os.RemoteException)2 File (java.io.File)2 HashMap (java.util.HashMap)2 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)2 Index (org.neo4j.graphdb.index.Index)2 InvalidTransactionException (org.neo4j.javax.transaction.InvalidTransactionException)2 SystemException (org.neo4j.javax.transaction.SystemException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Random (java.util.Random)1 Term (org.apache.lucene.index.Term)1 TermQuery (org.apache.lucene.search.TermQuery)1