-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Do not store (unused) property changes data in tx state"
This reverts commit c87d945.
- Loading branch information
Andrei Koval
committed
Mar 20, 2018
1 parent
90072aa
commit e2a5f5d
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
community/kernel/src/main/java/org/neo4j/kernel/impl/api/state/PropertyChanges.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.kernel.impl.api.state; | ||
|
||
import java.util.Map; | ||
|
||
import org.neo4j.kernel.impl.util.VersionedHashMap; | ||
import org.neo4j.kernel.impl.util.collection.CollectionsFactory; | ||
import org.neo4j.kernel.impl.util.diffsets.PrimitiveLongDiffSets; | ||
import org.neo4j.storageengine.api.txstate.PrimitiveLongReadableDiffSets; | ||
|
||
/** | ||
* Indexes entities by what property and value has been modified on them. | ||
*/ | ||
public class PropertyChanges | ||
{ | ||
private final CollectionsFactory collectionsFactory; | ||
|
||
private VersionedHashMap<Integer, Map<Object,PrimitiveLongDiffSets>> changes; | ||
|
||
public PropertyChanges( CollectionsFactory collectionsFactory ) | ||
{ | ||
this.collectionsFactory = collectionsFactory; | ||
} | ||
|
||
public PrimitiveLongReadableDiffSets changesForProperty( int propertyKeyId, Object value ) | ||
{ | ||
if ( changes != null ) | ||
{ | ||
Map<Object,PrimitiveLongDiffSets> keyChanges = changes.get( propertyKeyId ); | ||
if ( keyChanges != null ) | ||
{ | ||
PrimitiveLongDiffSets valueChanges = keyChanges.get( value ); | ||
if ( valueChanges != null ) | ||
{ | ||
return valueChanges; | ||
} | ||
} | ||
} | ||
return PrimitiveLongReadableDiffSets.EMPTY; | ||
} | ||
|
||
public void changeProperty( long entityId, int propertyKeyId, Object oldValue, Object newValue ) | ||
{ | ||
Map<Object, PrimitiveLongDiffSets> keyChanges = keyChanges( propertyKeyId ); | ||
valueChanges( newValue, keyChanges ).add( entityId ); | ||
valueChanges( oldValue, keyChanges ).remove( entityId ); | ||
} | ||
|
||
public void addProperty( long entityId, int propertyKeyId, Object value ) | ||
{ | ||
valueChanges( value, keyChanges( propertyKeyId ) ).add( entityId ); | ||
} | ||
|
||
public void removeProperty( long entityId, int propertyKeyId, Object oldValue ) | ||
{ | ||
valueChanges( oldValue, keyChanges( propertyKeyId ) ).remove( entityId ); | ||
} | ||
|
||
private Map<Object, PrimitiveLongDiffSets> keyChanges( int propertyKeyId ) | ||
{ | ||
if ( changes == null ) | ||
{ | ||
changes = new VersionedHashMap<>(); | ||
} | ||
|
||
return changes.computeIfAbsent( propertyKeyId, k -> new VersionedHashMap<>() ); | ||
} | ||
|
||
private PrimitiveLongDiffSets valueChanges( Object newValue, Map<Object, PrimitiveLongDiffSets> keyChanges ) | ||
{ | ||
return keyChanges.computeIfAbsent( newValue, k -> collectionsFactory.newLongDiffSets() ); | ||
} | ||
|
||
public void release() | ||
{ | ||
for ( final Map<Object, PrimitiveLongDiffSets> map : changes.values() ) | ||
{ | ||
map.values().forEach( PrimitiveLongDiffSets::close ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
community/kernel/src/test/java/org/neo4j/kernel/impl/api/state/PropertyChangesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2002-2018 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.kernel.impl.api.state; | ||
|
||
import org.hamcrest.Matcher; | ||
import org.junit.Test; | ||
|
||
import org.neo4j.collection.primitive.PrimitiveLongSet; | ||
import org.neo4j.kernel.impl.util.collection.OnHeapCollectionsFactory; | ||
import org.neo4j.kernel.impl.util.diffsets.PrimitiveLongDiffSets; | ||
import org.neo4j.storageengine.api.txstate.PrimitiveLongReadableDiffSets; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.neo4j.collection.primitive.PrimitiveLongCollections.emptySet; | ||
import static org.neo4j.collection.primitive.PrimitiveLongCollections.setOf; | ||
|
||
public class PropertyChangesTest | ||
{ | ||
@Test | ||
public void shouldListChanges() | ||
{ | ||
// Given | ||
PropertyChanges changes = new PropertyChanges( OnHeapCollectionsFactory.INSTANCE ); | ||
changes.changeProperty( 1L, 2, "from", "to" ); | ||
changes.addProperty( 1L, 3, "from" ); | ||
changes.removeProperty( 2L, 2, "to" ); | ||
|
||
// When & Then | ||
assertThat( changes.changesForProperty( 2, "to" ), isDiffSets( setOf( 1L ), setOf( 2L ) ) ); | ||
|
||
assertThat( changes.changesForProperty( 3, "from" ), isDiffSets( setOf( 1L ), emptySet() ) ); | ||
|
||
assertThat( changes.changesForProperty( 2, "from" ), isDiffSets( emptySet(), setOf( 1L ) ) ); | ||
} | ||
|
||
@SuppressWarnings( "unchecked" ) | ||
private Matcher<? super PrimitiveLongReadableDiffSets> isDiffSets( PrimitiveLongSet added, PrimitiveLongSet removed ) | ||
{ | ||
return (Matcher) equalTo( new PrimitiveLongDiffSets( added, removed, OnHeapCollectionsFactory.INSTANCE ) ); | ||
} | ||
} |