use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.
the class NodeEntity method getProperty.
@Override
public Object getProperty(String key, Object defaultValue) {
if (null == key) {
throw new IllegalArgumentException("(null) property key is not allowed");
}
KernelTransaction transaction = internalTransaction.kernelTransaction();
NodeCursor nodes = transaction.ambientNodeCursor();
PropertyCursor properties = transaction.ambientPropertyCursor();
int propertyKey = transaction.tokenRead().propertyKey(key);
if (propertyKey == TokenRead.NO_TOKEN) {
return defaultValue;
}
singleNode(transaction, nodes);
nodes.properties(properties);
return properties.seekProperty(propertyKey) ? properties.propertyValue().asObjectCopy() : defaultValue;
}
use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.
the class ConstraintTestBase method shouldCheckUniquenessWhenAddingLabel.
@Test
void shouldCheckUniquenessWhenAddingLabel() throws Exception {
// GIVEN
long nodeConflicting, nodeNotConflicting;
addConstraints("FOO", "prop");
try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
Node conflict = tx.createNode();
conflict.setProperty("prop", 1337);
nodeConflicting = conflict.getId();
Node ok = tx.createNode();
ok.setProperty("prop", 42);
nodeNotConflicting = ok.getId();
// Existing node
Node existing = tx.createNode();
existing.addLabel(Label.label("FOO"));
existing.setProperty("prop", 1337);
tx.commit();
}
int label;
try (KernelTransaction tx = beginTransaction()) {
label = tx.tokenWrite().labelGetOrCreateForName("FOO");
// This is ok, since it will satisfy constraint
assertTrue(tx.dataWrite().nodeAddLabel(nodeNotConflicting, label));
try {
tx.dataWrite().nodeAddLabel(nodeConflicting, label);
fail();
} catch (ConstraintValidationException e) {
// ignore
}
tx.commit();
}
// Verify
try (KernelTransaction tx = beginTransaction();
NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext())) {
// Node without conflict
tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
assertTrue(nodeCursor.next());
assertTrue(nodeCursor.labels().contains(label));
// Node with conflict
tx.dataRead().singleNode(nodeConflicting, nodeCursor);
assertTrue(nodeCursor.next());
assertFalse(nodeCursor.labels().contains(label));
}
}
use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.
the class ConstraintTestBase method shouldCheckUniquenessWhenAddingProperties.
@Test
void shouldCheckUniquenessWhenAddingProperties() throws Exception {
// GIVEN
long nodeConflicting, nodeNotConflicting;
addConstraints("FOO", "prop");
try (org.neo4j.graphdb.Transaction tx = graphDb.beginTx()) {
Node conflict = tx.createNode();
conflict.addLabel(Label.label("FOO"));
nodeConflicting = conflict.getId();
Node ok = tx.createNode();
ok.addLabel(Label.label("BAR"));
nodeNotConflicting = ok.getId();
// Existing node
Node existing = tx.createNode();
existing.addLabel(Label.label("FOO"));
existing.setProperty("prop", 1337);
tx.commit();
}
int property;
try (KernelTransaction tx = beginTransaction()) {
property = tx.tokenWrite().propertyKeyGetOrCreateForName("prop");
// This is ok, since it will satisfy constraint
tx.dataWrite().nodeSetProperty(nodeNotConflicting, property, intValue(1337));
try {
tx.dataWrite().nodeSetProperty(nodeConflicting, property, intValue(1337));
fail();
} catch (ConstraintValidationException e) {
// ignore
}
tx.commit();
}
// Verify
try (KernelTransaction tx = beginTransaction();
NodeCursor nodeCursor = tx.cursors().allocateNodeCursor(tx.cursorContext());
PropertyCursor propertyCursor = tx.cursors().allocatePropertyCursor(tx.cursorContext(), tx.memoryTracker())) {
// Node without conflict
tx.dataRead().singleNode(nodeNotConflicting, nodeCursor);
assertTrue(nodeCursor.next());
nodeCursor.properties(propertyCursor);
assertTrue(hasKey(propertyCursor, property));
// Node with conflict
tx.dataRead().singleNode(nodeConflicting, nodeCursor);
assertTrue(nodeCursor.next());
nodeCursor.properties(propertyCursor);
assertFalse(hasKey(propertyCursor, property));
}
}
use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.
the class PropertyCursorTestBase method assertAccessSingleNodeProperty.
private void assertAccessSingleNodeProperty(long nodeId, Object expectedValue, ValueGroup expectedValueType) {
// given
try (NodeCursor node = cursors.allocateNodeCursor(NULL);
PropertyCursor props = cursors.allocatePropertyCursor(NULL, INSTANCE)) {
// when
read.singleNode(nodeId, node);
assertTrue(node.next(), "node by reference");
assertTrue(hasProperties(node, props), "has properties");
node.properties(props);
assertTrue(props.next(), "has properties by direct method");
assertEquals(expectedValue, props.propertyValue(), "correct value");
assertEquals(expectedValueType, props.propertyType(), "correct value type ");
assertFalse(props.next(), "single property");
read.nodeProperties(node.nodeReference(), node.propertiesReference(), props);
assertTrue(props.next(), "has properties via property ref");
assertEquals(expectedValue, props.propertyValue(), "correct value");
assertFalse(props.next(), "single property");
}
}
use of org.neo4j.internal.kernel.api.NodeCursor in project neo4j by neo4j.
the class PropertyCursorTestBase method shouldAccessAllNodeProperties.
@Test
void shouldAccessAllNodeProperties() {
// given
try (NodeCursor node = cursors.allocateNodeCursor(NULL);
PropertyCursor props = cursors.allocatePropertyCursor(NULL, INSTANCE)) {
// when
read.singleNode(allPropsNodeId, node);
assertTrue(node.next(), "node by reference");
assertTrue(hasProperties(node, props), "has properties");
node.properties(props);
Set<Object> values = new HashSet<>();
while (props.next()) {
values.add(props.propertyValue().asObject());
}
assertTrue(values.contains((byte) 13), BYTE_PROP);
assertTrue(values.contains((short) 13), SHORT_PROP);
assertTrue(values.contains(13), INT_PROP);
assertTrue(values.contains(13L), INLINE_LONG_PROP);
assertTrue(values.contains(Long.MAX_VALUE), LONG_PROP);
assertTrue(values.contains(13.0f), FLOAT_PROP);
assertTrue(values.contains(13.0), DOUBLE_PROP);
assertTrue(values.contains(true), TRUE_PROP);
assertTrue(values.contains(false), FALSE_PROP);
assertTrue(values.contains('x'), CHAR_PROP);
assertTrue(values.contains(""), EMPTY_STRING_PROP);
assertTrue(values.contains("hello"), SHORT_STRING_PROP);
assertTrue(values.contains(chinese), UTF_8_PROP);
if (supportsBigProperties()) {
assertTrue(values.contains(LONG_STRING), LONG_STRING_PROP);
assertThat(values).as(SMALL_ARRAY_PROP).contains(new int[] { 1, 2, 3, 4 });
assertThat(values).as(BIG_ARRAY_PROP).contains(LONG_STRING);
}
assertTrue(values.contains(pointValue), POINT_PROP);
assertTrue(values.contains(dateValue.asObject()), DATE_PROP);
int expected = supportsBigProperties() ? 18 : 15;
assertEquals(expected, values.size(), "number of values");
}
}
Aggregations