<T extends PropertyContainer> PropertyEntryImpl<T> fetchExpectedPropertyEntry( Map<T, Map<String, PropertyEntryImpl<T>>> map, PropertyEntry<T> entry ) { T entity = entry.entity(); boolean hasEntity = map.containsKey( entity ); if ( ignoreAdditionalData && !hasEntity ) { return null; } assertTrue( "Unexpected entity " + entry, hasEntity ); Map<String, PropertyEntryImpl<T>> innerMap = map.get( entity ); PropertyEntryImpl<T> expectedEntry = innerMap.remove( entry.key() ); if ( expectedEntry == null && ignoreAdditionalData ) { return null; } assertNotNull( "Unexpected property entry " + entry, expectedEntry ); if ( innerMap.isEmpty() ) { map.remove( entity ); } return expectedEntry; } }
void compareToAssigned( PropertyEntry<T> entry ) { basicCompareTo( entry ); assertEqualsMaybeNull( entry.value(), value(), entry.entity(), entry.key() ); }
@Test public 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, genericMap( "theKey", prevValue ) ); // When Iterable<PropertyEntry<Relationship>> propertyEntries = snapshot().removedRelationshipProperties(); // Then PropertyEntry<Relationship> entry = single( propertyEntries ); assertThat( entry.key(), equalTo( "theKey" ) ); assertThat( entry.previouslyCommitedValue(), equalTo( "prevValue" ) ); assertThat( entry.entity().getId(), equalTo( 1L ) ); }
@Test public 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, NO_LABELS, genericMap( "theKey", prevValue ) ); // When Iterable<PropertyEntry<Node>> propertyEntries = snapshot().assignedNodeProperties(); // Then PropertyEntry<Node> entry = single( propertyEntries ); assertThat( entry.key(), equalTo( "theKey" ) ); assertThat( entry.value(), equalTo( "newValue" ) ); assertThat( entry.previouslyCommitedValue(), equalTo( "prevValue" ) ); assertThat( entry.entity().getId(), equalTo( 1L ) ); }
@Test public void shouldListAddedRelationshipProperties() throws Exception { // Given Value prevValue = Values.of( "prevValue" ); int propertyKeyId = ops.propertyKeyTokenHolder().getOrCreateId( "theKey" ); state.relationshipDoReplaceProperty( 1L, propertyKeyId, prevValue, Values.of( "newValue" ) ); ops.withRelationship( 1, 0, 0, 0, genericMap( "theKey", prevValue ) ); // When Iterable<PropertyEntry<Relationship>> propertyEntries = snapshot().assignedRelationshipProperties(); // Then PropertyEntry<Relationship> entry = single( propertyEntries ); assertThat( entry.key(), equalTo( "theKey" ) ); assertThat( entry.value(), equalTo( "newValue" ) ); assertThat( entry.previouslyCommitedValue(), equalTo( "prevValue" ) ); assertThat( entry.entity().getId(), equalTo( 1L ) ); }
@Test public void shouldListRemovedNodeProperties() throws Exception { // Given int propertyKeyId = ops.propertyKeyTokenHolder().getOrCreateId( "theKey" ); Value prevValue = Values.of( "prevValue" ); state.nodeDoRemoveProperty( 1L, propertyKeyId ); ops.withNode( 1, NO_LABELS, genericMap( "theKey", prevValue ) ); // When Iterable<PropertyEntry<Node>> propertyEntries = snapshot().removedNodeProperties(); // Then PropertyEntry<Node> entry = single( propertyEntries ); assertThat( entry.key(), equalTo( "theKey" ) ); assertThat( entry.previouslyCommitedValue(), equalTo( "prevValue" ) ); assertThat( entry.entity().getId(), equalTo( 1L ) ); }
private static <T extends PropertyContainer> Map<String,List<Map<String,Object>>> aggregatePropertyKeys(Iterable<PropertyEntry<T>> entries, boolean nodes, boolean removed) { if (!entries.iterator().hasNext()) return Collections.emptyMap(); Map<String,List<Map<String,Object>>> result = new HashMap<>(); String entityType = nodes ? "node" : "relationship"; for (PropertyEntry<T> entry : entries) { result.compute(entry.key(), (k, v) -> { if (v == null) v = new ArrayList<>(100); Map<String, Object> map = map("key", k, entityType, entry.entity(), "old", entry.previouslyCommitedValue()); if (!removed) map.put("new", entry.value()); v.add(map); return v; }); } return result; } private static Map<String,List<Node>> aggregateLabels(Iterable<LabelEntry> labelEntries) {
private void iterateNodePropertyChange(Stream<PropertyEntry<Node>> stream, boolean propertyRemoved, IndexFunction<Index<Node>, Node, String, Object, Object> function) { stream.forEach(nodePropertyEntry -> { final Node entity = nodePropertyEntry.entity(); final String key = nodePropertyEntry.key(); final Object value = propertyRemoved ? null : nodePropertyEntry.value(); entity.getLabels().forEach(label -> { final String labelName = label.name(); final Map<String, Collection<Index<Node>>> propertyIndexMap = indexesByLabelAndProperty.get(labelName); if (propertyIndexMap!=null) { final Collection<Index<Node>> indices = propertyIndexMap.get(key); if (indices!= null) { for (Index<Node> index : indices) { String indexKey = labelName + "." + key; function.apply(index, entity, indexKey, value, nodePropertyEntry.previouslyCommitedValue()); } } } }); }); }
iterateNodePropertyChange(stream(data.removedNodeProperties()).filter(nodePropertyEntry -> !data.isDeleted(nodePropertyEntry.entity())), true, (index, node, key, value, oldValue) -> indexUpdate(state, aVoid -> { index.remove(node, key); index.remove(node, FreeTextSearch.KEY);
private boolean hasLabel(PropertyEntry<Node> propEntry) { return hasLabel(propEntry.entity()); }
@Override public Collection<BulkableAction> beforeCommit(TransactionData transactionData) throws Exception { Map<IndexId, BulkableAction> actions = new HashMap<>(1000); for (Node node : transactionData.createdNodes()) { if (hasLabel(node)) actions.putAll(indexRequests(node)); } for (LabelEntry labelEntry : transactionData.assignedLabels()) { if (hasLabel(labelEntry)) { if (transactionData.isDeleted(labelEntry.node())) { actions.putAll(deleteRequests(labelEntry.node())); } else { actions.putAll(indexRequests(labelEntry.node())); } } } for (LabelEntry labelEntry : transactionData.removedLabels()) { if (hasLabel(labelEntry)) actions.putAll(deleteRequests(labelEntry.node(), labelEntry.label())); } for (PropertyEntry<Node> propEntry : transactionData.assignedNodeProperties()) { if (hasLabel(propEntry)) actions.putAll(indexRequests(propEntry.entity())); } for (PropertyEntry<Node> propEntry : transactionData.removedNodeProperties()) { if (!transactionData.isDeleted(propEntry.entity()) && hasLabel(propEntry)) actions.putAll(updateRequests(propEntry.entity())); } return actions.isEmpty() ? Collections.<BulkableAction>emptyList() : actions.values(); }
private void initializeChanged() { initializeCreated(); initializeDeleted(); if (changed == null) { changed = new HashMap<>(); for (PropertyEntry<T> propertyEntry : assignedProperties()) { if (hasNotActuallyChanged(propertyEntry)) { continue; } T candidate = propertyEntry.entity(); if (!hasBeenCreated(candidate) && !changed.containsKey(id(candidate))) { Change<T> change = new Change<>(oldSnapshot(candidate), newSnapshot(candidate)); changed.put(id(candidate), change); } } } }
protected void initializeChanged() { initializeCreated(); initializeDeleted(); if (changed == null) { changed = new HashMap<>(); for (PropertyEntry<T> propertyEntry : assignedProperties()) { if (hasNotActuallyChanged(propertyEntry)) { continue; } T candidate = propertyEntry.entity(); if (!hasBeenCreated(candidate)) { registerChange(candidate); } } for (PropertyEntry<T> propertyEntry : removedProperties()) { T candidate = propertyEntry.entity(); if (!hasBeenDeleted(candidate)) { registerChange(candidate); } } doInitializeChanged(); } }