use of org.neo4j.graphdb.Label in project neo4j by neo4j.
the class LabelMutation method perform.
@Override
public void perform(long nodeId, String value) {
Node node = db.getNodeById(nodeId);
Label label = Label.label(value);
if (node.hasLabel(label)) {
node.removeLabel(label);
} else {
node.addLabel(label);
}
}
use of org.neo4j.graphdb.Label in project neo4j by neo4j.
the class IdReuseTest method sequentialOperationRelationshipIdReuse.
@Test
public void sequentialOperationRelationshipIdReuse() {
Label marker = Label.label("marker");
long relationship1 = createRelationship(marker);
long relationship2 = createRelationship(marker);
long relationship3 = createRelationship(marker);
assertEquals("Ids should be sequential", relationship1 + 1, relationship2);
assertEquals("Ids should be sequential", relationship2 + 1, relationship3);
final IdController idMaintenanceController = getIdMaintenanceController();
deleteRelationshipByLabelAndRelationshipType(marker);
idMaintenanceController.maintenance();
assertEquals("Relationships have reused id", relationship1, createRelationship(marker));
assertEquals("Relationships have reused id", relationship2, createRelationship(marker));
assertEquals("Relationships have reused id", relationship3, createRelationship(marker));
}
use of org.neo4j.graphdb.Label in project neo4j by neo4j.
the class IdReuseTest method relationshipIdReusableOnlyAfterTransactionFinish.
@Test
public void relationshipIdReusableOnlyAfterTransactionFinish() {
Label testLabel = Label.label("testLabel");
long relationshipId = createRelationship(testLabel);
final IdController idMaintenanceController = getIdMaintenanceController();
try (Transaction transaction = dbRule.beginTx();
ResourceIterator<Node> nodes = dbRule.findNodes(testLabel)) {
List<Node> nodeList = Iterators.asList(nodes);
for (Node node : nodeList) {
Iterable<Relationship> relationships = node.getRelationships(TestRelationshipType.MARKER);
for (Relationship relationship : relationships) {
relationship.delete();
}
}
idMaintenanceController.maintenance();
Node node1 = dbRule.createNode(testLabel);
Node node2 = dbRule.createNode(testLabel);
Relationship relationshipTo = node1.createRelationshipTo(node2, TestRelationshipType.MARKER);
assertNotEquals("Relatioships should have different ids.", relationshipId, relationshipTo.getId());
transaction.success();
}
}
use of org.neo4j.graphdb.Label in project neo4j by neo4j.
the class ConstraintIndexConcurrencyTest method shouldNotAllowConcurrentViolationOfConstraint.
@Test
public void shouldNotAllowConcurrentViolationOfConstraint() throws Exception {
// Given
GraphDatabaseAPI graphDb = db.getGraphDatabaseAPI();
Supplier<Statement> statementSupplier = graphDb.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
Label label = label("Foo");
String propertyKey = "bar";
String conflictingValue = "baz";
// a constraint
try (Transaction tx = graphDb.beginTx()) {
graphDb.schema().constraintFor(label).assertPropertyIsUnique(propertyKey).create();
tx.success();
}
// When
try (Transaction tx = graphDb.beginTx()) {
// create a statement and perform a lookup
Statement statement = statementSupplier.get();
int labelId = statement.readOperations().labelGetForName(label.name());
int propertyKeyId = statement.readOperations().propertyKeyGetForName(propertyKey);
NewIndexDescriptor index = NewIndexDescriptorFactory.uniqueForLabel(labelId, propertyKeyId);
statement.readOperations().indexQuery(index, IndexQuery.exact(index.schema().getPropertyId(), "The value is irrelevant, we just want to perform some sort of lookup against this index"));
// then let another thread come in and create a node
threads.execute(db -> {
try (Transaction transaction = db.beginTx()) {
db.createNode(label).setProperty(propertyKey, conflictingValue);
transaction.success();
}
return null;
}, graphDb).get();
// before we create a node with the same property ourselves - using the same statement that we have
// already used for lookup against that very same index
long node = statement.dataWriteOperations().nodeCreate();
statement.dataWriteOperations().nodeAddLabel(node, labelId);
try {
statement.dataWriteOperations().nodeSetProperty(node, property(propertyKeyId, conflictingValue));
fail("exception expected");
}// Then
catch (UniquePropertyValueValidationException e) {
assertEquals(ConstraintDescriptorFactory.uniqueForLabel(labelId, propertyKeyId), e.constraint());
IndexEntryConflictException conflict = Iterators.single(e.conflicts().iterator());
assertEquals(conflictingValue, conflict.getSinglePropertyValue());
}
tx.success();
}
}
use of org.neo4j.graphdb.Label in project neo4j by neo4j.
the class DatabaseActions method createNode.
// Nodes
public NodeRepresentation createNode(Map<String, Object> properties, Label... labels) throws PropertyValueException {
Node node = graphDb.createNode();
propertySetter.setProperties(node, properties);
if (labels != null) {
for (Label label : labels) {
node.addLabel(label);
}
}
return new NodeRepresentation(node);
}
Aggregations