use of org.neo4j.graphdb.event.PropertyEntry in project neo4j by neo4j.
the class TxStateTransactionDataViewTest method shouldListAddedRelationshipProperties.
@Test
public void shouldListAddedRelationshipProperties() throws Exception {
// Given
int propertyKeyId = 1;
DefinedProperty prevProp = stringProperty(propertyKeyId, "prevValue");
state.relationshipDoReplaceProperty(1L, prevProp, stringProperty(propertyKeyId, "newValue"));
when(ops.propertyKeyGetName(propertyKeyId)).thenReturn("theKey");
long propertyId = 40L;
when(storeStatement.acquireSingleRelationshipCursor(1)).thenReturn(asRelationshipCursor(1, 0, 0, 0, propertyId));
when(storeStatement.acquireSinglePropertyCursor(propertyId, propertyKeyId, NO_LOCK)).thenReturn(asPropertyCursor(prevProp));
// When
Iterable<PropertyEntry<Relationship>> propertyEntries = snapshot().assignedRelationshipProperties();
// Then
PropertyEntry<Relationship> entry = single(propertyEntries);
assertThat(entry.key(), equalTo("theKey"));
assertThat(entry.value(), equalTo((Object) "newValue"));
assertThat(entry.previouslyCommitedValue(), equalTo((Object) "prevValue"));
assertThat(entry.entity().getId(), equalTo(1L));
}
use of org.neo4j.graphdb.event.PropertyEntry in project neo4j-apoc-procedures by neo4j-contrib.
the class Trigger method nodesByLabel.
@UserFunction
@Description("function to filter labelEntries by label, to be used within a trigger statement with {assignedLabels}, {removedLabels}, {assigned/removedNodeProperties}")
public List<Node> nodesByLabel(@Name("labelEntries") Object entries, @Name("label") String labelString) {
if (!(entries instanceof Map))
return Collections.emptyList();
Map map = (Map) entries;
if (map.isEmpty())
return Collections.emptyList();
Object result = ((Map) entries).get(labelString);
if (result instanceof List)
return (List<Node>) result;
Object anEntry = map.values().iterator().next();
if (anEntry instanceof List) {
List list = (List) anEntry;
if (!list.isEmpty()) {
if (list.get(0) instanceof Map) {
Set<Node> nodeSet = new HashSet<>(100);
Label label = labelString == null ? null : Label.label(labelString);
for (List<Map<String, Object>> entry : (Collection<List<Map<String, Object>>>) map.values()) {
for (Map<String, Object> propertyEntry : entry) {
Object node = propertyEntry.get("node");
if (node instanceof Node && (label == null || ((Node) node).hasLabel(label))) {
nodeSet.add((Node) node);
}
}
}
if (!nodeSet.isEmpty())
return new SetBackedList<>(nodeSet);
} else if (list.get(0) instanceof Node) {
if (labelString == null) {
Set<Node> nodeSet = new HashSet<>(map.size() * list.size());
map.values().forEach((l) -> nodeSet.addAll((Collection<Node>) l));
return new SetBackedList<>(nodeSet);
}
}
}
}
return Collections.emptyList();
}
use of org.neo4j.graphdb.event.PropertyEntry in project neo4j by neo4j.
the class TxStateTransactionDataViewTest method shouldListAddedNodePropertiesProperties.
@Test
void shouldListAddedNodePropertiesProperties() throws Exception {
// Given
int propertyKeyId = ops.propertyKeyTokenHolder().getOrCreateId("theKey");
Value prevValue = Values.of("prevValue");
state.nodeDoChangeProperty(1L, propertyKeyId, Values.of("newValue"));
ops.withNode(1).properties("theKey", prevValue);
// When
Iterable<PropertyEntry<Node>> propertyEntries = snapshot().assignedNodeProperties();
// Then
PropertyEntry<Node> entry = single(propertyEntries);
assertThat(entry.key()).isEqualTo("theKey");
assertThat(entry.value()).isEqualTo("newValue");
assertThat(entry.previouslyCommittedValue()).isEqualTo("prevValue");
assertThat(entry.entity().getId()).isEqualTo(1L);
}
use of org.neo4j.graphdb.event.PropertyEntry in project neo4j by neo4j.
the class TxStateTransactionDataViewTest method shouldListRemovedRelationshipProperties.
@Test
void shouldListRemovedRelationshipProperties() throws Exception {
// Given
int propertyKeyId = ops.propertyKeyTokenHolder().getOrCreateId("theKey");
Value prevValue = Values.of("prevValue");
state.relationshipDoRemoveProperty(1L, propertyKeyId);
ops.withRelationship(1, 0, 0, 0).properties("theKey", prevValue);
// When
Iterable<PropertyEntry<Relationship>> propertyEntries = snapshot().removedRelationshipProperties();
// Then
PropertyEntry<Relationship> entry = single(propertyEntries);
assertThat(entry.key()).isEqualTo("theKey");
assertThat(entry.previouslyCommittedValue()).isEqualTo("prevValue");
assertThat(entry.entity().getId()).isEqualTo(1L);
}
use of org.neo4j.graphdb.event.PropertyEntry in project neo4j by neo4j.
the class TestTransactionEvents method shouldProvideTheCorrectRelationshipData.
@Test
void shouldProvideTheCorrectRelationshipData() {
// create a rel type so the next type id is non zero
try (Transaction tx = db.beginTx()) {
tx.createNode().createRelationshipTo(tx.createNode(), withName("TYPE"));
}
RelationshipType livesIn = withName("LIVES_IN");
long relId;
try (Transaction tx = db.beginTx()) {
Node person = tx.createNode(label("Person"));
Node city = tx.createNode(label("City"));
Relationship rel = person.createRelationshipTo(city, livesIn);
rel.setProperty("since", 2009);
relId = rel.getId();
tx.commit();
}
final Set<String> changedRelationships = new HashSet<>();
dbms.registerTransactionEventListener(DEFAULT_DATABASE_NAME, new TransactionEventListenerAdapter<Void>() {
@Override
public Void beforeCommit(TransactionData data, Transaction transaction, GraphDatabaseService databaseService) {
for (PropertyEntry<Relationship> entry : data.assignedRelationshipProperties()) {
changedRelationships.add(entry.entity().getType().name());
}
return null;
}
});
try (Transaction tx = db.beginTx()) {
Relationship rel = tx.getRelationshipById(relId);
rel.setProperty("since", 2010);
tx.commit();
}
assertEquals(1, changedRelationships.size());
assertTrue(changedRelationships.contains(livesIn.name()), livesIn + " not in " + changedRelationships);
}
Aggregations