Skip to content

Commit

Permalink
Preserve utf8 from Bolt
Browse files Browse the repository at this point in the history
There is no need to directly convert string values coming from
Bolt to `java.lang.String` directly, we can preserve it as a
`UTF8StringValue` until we actually need the value. Currently
this will not give much in performance except for queries like
`RETURN 'hello world'`, since most string operations currently
requires an actual string instance. However going forward we can
be smarter about preserving the `UTF8StringValue`.
  • Loading branch information
pontusmelke committed Oct 19, 2017
1 parent 2b82af3 commit 1b4ab9f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
import org.neo4j.bolt.v1.packstream.PackType;
import org.neo4j.bolt.v1.runtime.Neo4jError;
import org.neo4j.collection.primitive.PrimitiveLongIntKeyValueArray;
import org.neo4j.kernel.impl.util.BaseToObjectValueWriter;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.util.BaseToObjectValueWriter;
import org.neo4j.values.AnyValue;
import org.neo4j.values.AnyValueWriter;
import org.neo4j.values.storable.TextArray;
Expand Down Expand Up @@ -427,7 +427,7 @@ public AnyValue unpack() throws IOException
case BYTES:
return byteArray( unpackBytes() );
case STRING:
return Values.stringValue( unpackString() );
return Values.utf8Value( unpackUTF8() );
case INTEGER:
return Values.longValue( unpackLong() );
case FLOAT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.StringValue;
import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
Expand All @@ -47,6 +48,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.neo4j.bolt.v1.messaging.example.Edges.ALICE_KNOWS_BOB;
import static org.neo4j.bolt.v1.messaging.example.Nodes.ALICE;
import static org.neo4j.bolt.v1.messaging.example.Paths.ALL_PATHS;
Expand Down Expand Up @@ -252,6 +254,7 @@ public void shouldPackUtf8() throws IOException

// When
AnyValue unpacked = unpacked( output.bytes() );
assertThat( unpacked, is( instanceOf( StringValue.UTF8StringValue.class ) ));

// Then
assertThat( unpacked, equalTo( textValue ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.values.storable;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static java.lang.String.format;

Expand Down Expand Up @@ -120,11 +121,11 @@ public int length()
* Just as a normal StringValue but is backed by a byte array and does string
* serialization lazily.
*
* TODO in this implementation most operation will actually load the string
* such as hashCode, length, equals etc. These could be implemented using
* the byte array directly
* TODO in this implementation most operations will actually load the string
* such as hashCode, length. These could be implemented using
* the byte array directly in later optimizations
*/
static final class UTF8StringValue extends StringValue
public static final class UTF8StringValue extends StringValue
{
private volatile String value;
private final byte[] bytes;
Expand All @@ -145,6 +146,19 @@ public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
writer.writeUTF8( bytes, offset, length );
}

@Override
public boolean equals( Value value )
{
if ( value instanceof UTF8StringValue )
{
return Arrays.equals( bytes, ((UTF8StringValue) value).bytes );
}
else
{
return super.equals( value );
}
}

@Override
String value()
{
Expand Down

0 comments on commit 1b4ab9f

Please sign in to comment.