private static void setLabelSystem() { IndexDefinition indexDefinition; try (Transaction tx = graphDb.beginTx()) { Schema schema = graphDb.schema(); indexDefinition = schema.indexFor(DynamicLabel.label("Nome")).on("name").create(); tx.success(); } try (Transaction tx = graphDb.beginTx()) { Schema schema = graphDb.schema(); schema.awaitIndexOnline(indexDefinition, 10, TimeUnit.SECONDS); } }
protected void waitUntilIndexesAreOnline() { LOGGER.debug("Waiting for Indizes to Come Online."); try (Transaction ignored = graphDatabase.beginTx()) { graphDatabase.schema().awaitIndexesOnline(120, TimeUnit.SECONDS); LOGGER.debug("Indizes are online"); } }
public IndexDefinition _createIndex(Label label, String key) { Schema schema = graphDb.schema(); for (IndexDefinition index : schema.getIndexes(label)) for (String property : index.getPropertyKeys()) if (property.equals(key)) return index; // already existing return schema.indexFor(label).on(key).create(); }
public ConstraintDefinition _createConstrant(Label label, String key) { Schema schema = graphDb.schema(); for (ConstraintDefinition constraint : schema.getConstraints(label)) for (String property : constraint.getPropertyKeys()) if (property.equals(key)) return constraint; // already existing return schema.constraintFor(label).assertPropertyIsUnique(key).create(); }
@Test public void nodeCanBecomeSchemaIndexableInBeforeCommitByAddingLabel() throws Exception { // Given we have a schema index... GraphDatabaseService db = dbRule.getGraphDatabaseService(); final Label label = DynamicLabel.label("Label"); IndexDefinition index; try (Transaction tx = db.beginTx()) { index = db.schema().indexFor(label).on("indexed").create(); tx.success(); } // ... and a transaction event handler that likes to add the indexed property on nodes db.registerTransactionEventHandler( new TransactionEventHandler.Adapter<Object>() { @Override public Object beforeCommit(TransactionData data) throws Exception { Iterator<Node> nodes = data.createdNodes().iterator(); if (nodes.hasNext()) { Node node = nodes.next(); node.addLabel(label); } return null; } }); // When we create a node with the right property, but not the right label... try (Transaction tx = db.beginTx()) { db.schema().awaitIndexesOnline(10, TimeUnit.SECONDS); Node node = db.createNode(); node.setProperty("indexed", "value"); 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 = db.findNode(label, "indexed", "value"); assertThat(node.getProperty("random"), is((Object) 42)); } }
@Test public void insert() throws InterruptedException { // START SNIPPET: insert BatchInserter inserter = null; try { inserter = BatchInserters.inserter( new File("target/batchinserter-example").getAbsolutePath(), fileSystem); Label personLabel = DynamicLabel.label("Person"); inserter.createDeferredSchemaIndex(personLabel).on("name").create(); Map<String, Object> properties = new HashMap<>(); properties.put("name", "Mattias"); long mattiasNode = inserter.createNode(properties, personLabel); properties.put("name", "Chris"); long chrisNode = inserter.createNode(properties, personLabel); RelationshipType knows = DynamicRelationshipType.withName("KNOWS"); inserter.createRelationship(mattiasNode, chrisNode, knows, null); } finally { if (inserter != null) { inserter.shutdown(); } } // END SNIPPET: insert // try it out from a normal db GraphDatabaseService db = new TestGraphDatabaseFactory() .setFileSystem(fileSystem) .newImpermanentDatabase(new File("target/batchinserter-example").getAbsolutePath()); try (Transaction tx = db.beginTx()) { Label personLabelForTesting = DynamicLabel.label("Person"); Node mNode = db.findNode(personLabelForTesting, "name", "Mattias"); Node cNode = mNode .getSingleRelationship(DynamicRelationshipType.withName("KNOWS"), Direction.OUTGOING) .getEndNode(); assertThat((String) cNode.getProperty("name"), is("Chris")); assertThat(db.schema().getIndexes(personLabelForTesting).iterator().hasNext(), is(true)); } finally { db.shutdown(); } }
private File databaseWithUniquenessConstraint(String label, String propertyKey) { File storeDir = new File(targetDir.directory(), "seed"); GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(storeDir.getAbsolutePath()); try { Transaction tx = graphDb.beginTx(); try { graphDb.schema().constraintFor(label(label)).assertPropertyIsUnique(propertyKey).create(); tx.success(); } finally { tx.finish(); } } finally { graphDb.shutdown(); } return storeDir; }
/** * Defines constraints for the graph property model. Creates indexes for blocks and transactions. */ protected void createIndexesAndUniqueConstraints() { try (Transaction tx = graphDatabase.beginTx()) { Schema schema = graphDatabase.schema(); if (!schema.getIndexes().iterator().hasNext()) { LOGGER.debug("Creating Indexes and Unique Constraints."); schema.constraintFor(LabelType.Block).assertPropertyIsUnique("hash").create(); schema.constraintFor(LabelType.Block).assertPropertyIsUnique("height").create(); // No unique constraints for transactions (see BIP30) schema.indexFor(LabelType.Transaction).on("hash").create(); schema.constraintFor(LabelType.Address).assertPropertyIsUnique("hash").create(); schema.indexFor(LabelType.Coinbase).on("hash").create(); schema.indexFor(LabelType.LatestBlock).on("height").create(); } tx.success(); } }
@Override public Iterable<ConstraintDefinition> getConstraints() { return gdb.schema().getConstraints(); }
@Override public Iterable<IndexDefinition> getIndexes() { return gdb.schema().getIndexes(); }