Skip to content

Commit

Permalink
Compare UTF8 string values byte by byte
Browse files Browse the repository at this point in the history
  • Loading branch information
henriknyman committed Apr 18, 2018
1 parent 0ab9796 commit dd3a2f6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
Expand Up @@ -35,7 +35,7 @@
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Arrays.copyOf; import static java.util.Arrays.copyOf;
import static org.neo4j.values.storable.StringsLibrary.STRINGS; import static org.neo4j.values.storable.StringsLibrary.STRINGS;
import static org.neo4j.values.storable.UTF8StringValue.codePointByteArrayCompare; import static org.neo4j.values.storable.UTF8StringValue.byteArrayCompare;


abstract class StringLayoutTestUtil extends LayoutTestUtil<StringSchemaKey,NativeSchemaValue> abstract class StringLayoutTestUtil extends LayoutTestUtil<StringSchemaKey,NativeSchemaValue>
{ {
Expand All @@ -53,7 +53,7 @@ IndexQuery rangeQuery( Value from, boolean fromInclusive, Value to, boolean toIn
@Override @Override
int compareIndexedPropertyValue( StringSchemaKey key1, StringSchemaKey key2 ) int compareIndexedPropertyValue( StringSchemaKey key1, StringSchemaKey key2 )
{ {
return codePointByteArrayCompare( return byteArrayCompare(
copyOf( key1.bytes, key1.bytesLength ), copyOf( key1.bytes, key1.bytesLength ),
copyOf( key2.bytes, key2.bytesLength ) ); copyOf( key2.bytes, key2.bytesLength ) );
} }
Expand Down
Expand Up @@ -337,48 +337,32 @@ public int compareTo( TextValue other )
return super.compareTo( other ); return super.compareTo( other );
} }
UTF8StringValue otherUTF8 = (UTF8StringValue) other; UTF8StringValue otherUTF8 = (UTF8StringValue) other;
return codePointByteArrayCompare( bytes, offset, byteLength, otherUTF8.bytes, otherUTF8.offset, otherUTF8.byteLength ); return byteArrayCompare( bytes, offset, byteLength, otherUTF8.bytes, otherUTF8.offset, otherUTF8.byteLength );
} }


public static int codePointByteArrayCompare( byte[] value1, byte[] value2 ) public static int byteArrayCompare( byte[] value1, byte[] value2 )
{ {
return codePointByteArrayCompare( value1, 0, value1.length, value2, 0, value2.length ); return byteArrayCompare( value1, 0, value1.length, value2, 0, value2.length );
} }


public static int codePointByteArrayCompare( byte[] value1, int value1Offset, int value1Length, public static int byteArrayCompare( byte[] value1, int value1Offset, int value1Length,
byte[] value2, int value2Offset, int value2Length ) byte[] value2, int value2Offset, int value2Length )
{ {
int len1 = value1.length; int len1 = value1Length;
int len2 = value2.length; int len2 = value2Length;
int lim = Math.min( len1, len2 ); int lim = Math.min( len1, len2 );
int i = 0; int i = 0;
while ( i < lim ) while ( i < lim )
{ {
byte b = value1[i]; int b1 = ((int) value1[i + value1Offset]) & 0xFF;
int thisCodePoint; int b2 = ((int) value2[i + value2Offset]) & 0xFF;
int thatCodePoint = codePointAt( value2, i ); if ( b1 != b2 )
if ( b >= 0 )
{
i++;
thisCodePoint = b;
}
else
{
int bytesNeeded = 0;
while ( b < 0 )
{
bytesNeeded++;
b = (byte) (b << 1);
}
thisCodePoint = codePoint( value1, b, i, bytesNeeded );
i += bytesNeeded;
}
if ( thisCodePoint != thatCodePoint )
{ {
return thisCodePoint - thatCodePoint; return b1 - b2;
} }
i++;
} }
return numberOfCodePoints( value1, value1Offset, value1Length ) - numberOfCodePoints( value2, value2Offset, value2Length ); return len1 - len2;
} }


@Override @Override
Expand Down

0 comments on commit dd3a2f6

Please sign in to comment.