use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class IndexingServiceIntegrationTest method createData.
private void createData(GraphDatabaseService database, int numberOfNodes) {
int index = 0;
while (index < numberOfNodes) {
try (Transaction transaction = database.beginTx()) {
Node node = database.createNode(Label.label(FOOD_LABEL), Label.label(CLOTHES_LABEL), Label.label(WEATHER_LABEL));
node.setProperty(PROPERTY_NAME, "Node" + index++);
transaction.success();
}
}
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class TestTransactionEventsWithIndexes method nodeCanBeLegacyIndexedInBeforeCommit.
@Test
public void nodeCanBeLegacyIndexedInBeforeCommit() throws Exception {
// Given we have a legacy index...
GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
final Index<Node> index;
try (Transaction tx = db.beginTx()) {
index = db.index().forNodes("index");
tx.success();
}
// ... and a transaction event handler that likes to add nodes to that index
db.registerTransactionEventHandler(new TransactionEventHandler<Object>() {
@Override
public Object beforeCommit(TransactionData data) throws Exception {
Iterator<Node> nodes = data.createdNodes().iterator();
if (nodes.hasNext()) {
Node node = nodes.next();
index.add(node, "key", "value");
}
return null;
}
@Override
public void afterCommit(TransactionData data, Object state) {
}
@Override
public void afterRollback(TransactionData data, Object state) {
}
});
// When we create a node...
try (Transaction tx = db.beginTx()) {
db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS);
Node node = db.createNode();
node.setProperty("random", 42);
tx.success();
}
// Then we should be able to look it up through the index.
try (Transaction ignore = db.beginTx()) {
Node node = single(index.get("key", "value"));
assertThat(node.getProperty("random"), is((Object) 42));
}
}
use of org.neo4j.graphdb.Transaction in project neo4j by neo4j.
the class TestLuceneBatchInsert method shouldCreateAutoIndexThatIsUsableInEmbedded.
@Test
public void shouldCreateAutoIndexThatIsUsableInEmbedded() throws Exception {
BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
BatchInserterIndex index = provider.nodeIndex("node_auto_index", EXACT_CONFIG);
long id = inserter.createNode(null);
Map<String, Object> props = new HashMap<>();
props.put("name", "peter");
index.add(id, props);
index.flush();
provider.shutdown();
shutdownInserter();
switchToGraphDatabaseService(configure(GraphDatabaseSettings.node_keys_indexable, "name"), configure(GraphDatabaseSettings.relationship_keys_indexable, "relProp1,relProp2"), configure(GraphDatabaseSettings.node_auto_indexing, "true"), configure(GraphDatabaseSettings.relationship_auto_indexing, "true"));
try (Transaction tx = db.beginTx()) {
// Create the primitives
Node node1 = db.createNode();
// Add indexable and non-indexable properties
node1.setProperty("name", "bob");
// Make things persistent
tx.success();
}
try (Transaction tx = db.beginTx()) {
assertTrue(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "peter").hasNext());
assertTrue(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "bob").hasNext());
assertFalse(db.index().getNodeAutoIndexer().getAutoIndex().get("name", "joe").hasNext());
tx.success();
}
}
use of org.neo4j.graphdb.Transaction 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.Transaction in project neo4j by neo4j.
the class TestLuceneBatchInsert method testNumericValues.
@Test
public void testNumericValues() {
BatchInserterIndexProvider provider = new LuceneBatchInserterIndexProviderNewImpl(inserter);
BatchInserterIndex index = provider.nodeIndex("mine", EXACT_CONFIG);
long node1 = inserter.createNode(null);
index.add(node1, map("number", numeric(45)));
long node2 = inserter.createNode(null);
index.add(node2, map("number", numeric(21)));
index.flush();
assertContains(index.query("number", newIntRange("number", 21, 50, true, true)), node1, node2);
provider.shutdown();
switchToGraphDatabaseService();
try (Transaction transaction = db.beginTx()) {
Node n1 = db.getNodeById(node1);
db.getNodeById(node2);
Index<Node> idx = db.index().forNodes("mine");
assertContains(idx.query("number", newIntRange("number", 21, 45, false, true)), n1);
transaction.success();
}
}
Aggregations