diff --git a/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/PrimitiveSortedArraySet.java b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/PrimitiveSortedArraySet.java new file mode 100644 index 0000000000000..c1efbf4418601 --- /dev/null +++ b/community/primitive-collections/src/main/java/org/neo4j/collection/primitive/PrimitiveSortedArraySet.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2002-2017 "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 . + */ +package org.neo4j.collection.primitive; + +import java.util.Arrays; + +public class PrimitiveSortedArraySet +{ + /** + * Merges two sets of integers represented as sorted arrays. + * + * @param lhs + * a set of integers, represented as a sorted array. + * @param rhs + * a set of integers, represented as a sorted array. + * @return a set of integers, represented as a sorted array. + */ + public static int[] mergeSortedSet( int[] lhs, int[] rhs ) + { + if ( lhs.length < rhs.length ) + { + return mergeSortedSet( rhs, lhs ); + } + int[] merged = null; + int m = 0; + for ( int l = 0, r = 0; l < lhs.length && r < rhs.length; ) + { + while ( l < lhs.length && lhs[l] < rhs[r] ) + { + if ( merged != null ) + { + merged[m++] = lhs[l]; + } + l++; + } + if ( l == lhs.length ) + { + if ( merged == null ) + { + merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r ); + m = l; + } + System.arraycopy( rhs, r, merged, m, rhs.length - r ); + m += rhs.length - r; + break; + } + if ( lhs[l] > rhs[r] ) + { + if ( merged == null ) + { + merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r ); + m = l; + } + merged[m++] = rhs[r++]; + } + else // i.e. ( lhs[l] == rhs[r] ) + { + if ( merged != null ) + { + merged[m++] = lhs[l]; + } + l++; + r++; + } + } + if ( merged == null ) + { + return lhs; + } + if ( merged.length < m ) + { + merged = Arrays.copyOf( merged, m ); + } + return merged; + } +} diff --git a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcerMergeTest.java b/community/primitive-collections/src/test/java/org/neo4j/collection/primitive/PrimitiveSortedArraySetTest.java similarity index 84% rename from enterprise/kernel/src/test/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcerMergeTest.java rename to community/primitive-collections/src/test/java/org/neo4j/collection/primitive/PrimitiveSortedArraySetTest.java index 82a6ff76318ab..e84dbdddbef2e 100644 --- a/enterprise/kernel/src/test/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcerMergeTest.java +++ b/community/primitive-collections/src/test/java/org/neo4j/collection/primitive/PrimitiveSortedArraySetTest.java @@ -5,19 +5,19 @@ * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. + * 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 Affero General Public License for more details. + * GNU General Public License for more details. * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ -package org.neo4j.kernel.impl.enterprise; +package org.neo4j.collection.primitive; import java.util.Arrays; @@ -27,10 +27,10 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertSame; -import static org.neo4j.kernel.impl.enterprise.PropertyExistenceEnforcer.merge; +import static org.neo4j.collection.primitive.PrimitiveSortedArraySet.mergeSortedSet; @RunWith( Parameterized.class ) -public class PropertyExistenceEnforcerMergeTest +public class PrimitiveSortedArraySetTest { @Parameterized.Parameters( name = "{0}" ) public static Iterable parameters() @@ -53,7 +53,7 @@ public static Iterable parameters() private final int[] rhs; private final int[] expected; - public PropertyExistenceEnforcerMergeTest( Input input ) + public PrimitiveSortedArraySetTest( Input input ) { this.lhs = input.lhs; this.rhs = input.rhs; @@ -63,7 +63,7 @@ public PropertyExistenceEnforcerMergeTest( Input input ) @Test public void testMerge() throws Exception { - int[] actual = merge( lhs, rhs ); + int[] actual = mergeSortedSet( lhs, rhs ); if ( lhs == expected || rhs == expected ) { assertSame( expected, actual ); diff --git a/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java b/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java index a48161f763d96..f2e21fe8ceaae 100644 --- a/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java +++ b/enterprise/kernel/src/main/java/org/neo4j/kernel/impl/enterprise/PropertyExistenceEnforcer.java @@ -50,6 +50,7 @@ import static java.lang.String.format; import static java.util.Collections.emptyList; +import static org.neo4j.collection.primitive.PrimitiveSortedArraySet.mergeSortedSet; import static org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException.Phase.VALIDATION; class PropertyExistenceEnforcer @@ -84,7 +85,7 @@ private static void update( PrimitiveIntObjectMap map, int key, int[] val int[] current = map.get( key ); if ( current != null ) { - values = merge( current, values ); + values = mergeSortedSet( current, values ); } map.put( key, values ); } @@ -373,74 +374,6 @@ private void failRelationship( long id, int relationshipType, int propertyKey ) relationshipType, propertyKey ) ); } - /** - * Merges two sets of integers represented as sorted arrays. - * - * @param lhs - * a set of integers, represented as a sorted array. - * @param rhs - * a set of integers, represented as a sorted array. - * @return a set of integers, represented as a sorted array. - */ - static int[] merge( int[] lhs, int[] rhs ) - { - if ( lhs.length < rhs.length ) - { - return merge( rhs, lhs ); - } - int[] merged = null; - int m = 0; - for ( int l = 0, r = 0; l < lhs.length && r < rhs.length; ) - { - while ( l < lhs.length && lhs[l] < rhs[r] ) - { - if ( merged != null ) - { - merged[m++] = lhs[l]; - } - l++; - } - if ( l == lhs.length ) - { - if ( merged == null ) - { - merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r ); - m = l; - } - System.arraycopy( rhs, r, merged, m, rhs.length - r ); - m += rhs.length - r; - break; - } - if ( lhs[l] > rhs[r] ) - { - if ( merged == null ) - { - merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r ); - m = l; - } - merged[m++] = rhs[r++]; - } - else // i.e. ( lhs[l] == rhs[r] ) - { - if ( merged != null ) - { - merged[m++] = lhs[l]; - } - l++; - r++; - } - } - if ( merged == null ) - { - return lhs; - } - if ( merged.length < m ) - { - merged = Arrays.copyOf( merged, m ); - } - return merged; - } - private boolean contains( int[] list, int value ) { for ( int x : list )