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();
}
}
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));
}
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();
}
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();
}
}
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());
}
}
Aggregations