Search in sources :

Example 6 with PropertyEntry

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));
}
Also used : DefinedProperty(org.neo4j.kernel.api.properties.DefinedProperty) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Relationship(org.neo4j.graphdb.Relationship) Test(org.junit.Test)

Example 7 with PropertyEntry

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();
}
Also used : ApocConfiguration(apoc.ApocConfiguration) java.util(java.util) Log(org.neo4j.logging.Log) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) org.neo4j.procedure(org.neo4j.procedure) Iterators(org.neo4j.helpers.collection.Iterators) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TransactionData(org.neo4j.graphdb.event.TransactionData) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) LabelEntry(org.neo4j.graphdb.event.LabelEntry) Stream(java.util.stream.Stream) Description(apoc.Description) org.neo4j.graphdb(org.neo4j.graphdb) GraphProperties(org.neo4j.kernel.impl.core.GraphProperties) SetBackedList(apoc.coll.SetBackedList) Util(apoc.util.Util) Util.map(apoc.util.Util.map) NodeManager(org.neo4j.kernel.impl.core.NodeManager) SetBackedList(apoc.coll.SetBackedList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Description(apoc.Description)

Example 8 with PropertyEntry

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);
}
Also used : PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Node(org.neo4j.graphdb.Node) Value(org.neo4j.values.storable.Value) Test(org.junit.jupiter.api.Test)

Example 9 with PropertyEntry

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);
}
Also used : PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Relationship(org.neo4j.graphdb.Relationship) Value(org.neo4j.values.storable.Value) Test(org.junit.jupiter.api.Test)

Example 10 with PropertyEntry

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);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) RelationshipType(org.neo4j.graphdb.RelationshipType) Transaction(org.neo4j.graphdb.Transaction) PropertyEntry(org.neo4j.graphdb.event.PropertyEntry) Relationship(org.neo4j.graphdb.Relationship) TransactionData(org.neo4j.graphdb.event.TransactionData) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyEntry (org.neo4j.graphdb.event.PropertyEntry)10 Test (org.junit.jupiter.api.Test)5 Node (org.neo4j.graphdb.Node)5 Relationship (org.neo4j.graphdb.Relationship)5 Test (org.junit.Test)4 DefinedProperty (org.neo4j.kernel.api.properties.DefinedProperty)4 Value (org.neo4j.values.storable.Value)4 TransactionData (org.neo4j.graphdb.event.TransactionData)2 ApocConfiguration (apoc.ApocConfiguration)1 Description (apoc.Description)1 SetBackedList (apoc.coll.SetBackedList)1 Util (apoc.util.Util)1 Util.map (apoc.util.Util.map)1 java.util (java.util)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Stream (java.util.stream.Stream)1 org.neo4j.graphdb (org.neo4j.graphdb)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1