Skip to content

Commit

Permalink
Add special method for writing byte[]
Browse files Browse the repository at this point in the history
Add a primitive for writing a byte[] in bulk instead of
just one value at a time.
  • Loading branch information
pontusmelke committed Jun 19, 2017
1 parent 1134ae1 commit be0f1c1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static String encode( Value array )
return encoder.result();
}

static class ValueEncoder implements ValueWriter
static class ValueEncoder implements ValueWriter<RuntimeException>
{
StringBuilder builder;

Expand Down Expand Up @@ -167,6 +167,12 @@ public void endArray()

}

@Override
public void writeByteArray( byte[] value )
{
builder.append( 'D' );
}

private char typeChar( ArrayType arrayType )
{
switch ( arrayType )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public boolean equals( double[] x )
}

@Override
public void writeTo( ValueWriter writer )
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
{
PrimitiveArrayWriting.writeTo( writer, value() );
writer.writeByteArray( value() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* Writer of values.
*
* <p>
* Has functionality to write all supported primitives, as well as arrays and different representations of Strings.
*/
public interface ValueWriter<E extends Exception>
Expand Down Expand Up @@ -75,4 +75,6 @@ default void writeString( char[] value ) throws E
void beginArray( int size, ArrayType arrayType ) throws E;

void endArray() throws E;

void writeByteArray( byte[] value ) throws E;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import static org.neo4j.values.BufferValueWriter.SpecialKind.CopyUTF8;
import static org.neo4j.values.BufferValueWriter.SpecialKind.EndArray;
import static org.neo4j.values.BufferValueWriter.SpecialKind.EndUTF8;
import static org.neo4j.values.BufferValueWriter.SpecialKind.WriteByteArray;
import static org.neo4j.values.BufferValueWriter.SpecialKind.WriteCharArray;

public class BufferValueWriter implements ValueWriter<RuntimeException>
{
enum SpecialKind
{
WriteCharArray,
WriteByteArray,
BeginUTF8,
CopyUTF8,
EndUTF8,
Expand Down Expand Up @@ -192,6 +194,12 @@ public void endArray()
buffer.add( Specials.endArray() );
}

@Override
public void writeByteArray( byte[] value ) throws RuntimeException
{
buffer.add( Specials.byteArray( value ) );
}

@SuppressWarnings( "WeakerAccess" )
public static class Specials
{
Expand All @@ -200,6 +208,11 @@ public static Special charArray( char[] value, int offset, int length )
return new Special( WriteCharArray, format( "%d %d %d", Arrays.hashCode( value ), offset, length ) );
}

public static Special byteArray( byte[] value )
{
return new Special( WriteByteArray, Arrays.hashCode( value ) );
}

public static Special beginUTF8( int size )
{
return new Special( BeginUTF8, size );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,11 @@ public void endArray() throws TestException
{
throw new TestException();
}

@Override
public void writeByteArray( byte[] value ) throws TestException
{
throw new TestException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import java.util.Arrays;

import static org.neo4j.values.BufferValueWriter.Specials.beginArray;
import static org.neo4j.values.BufferValueWriter.Specials.byteArray;
import static org.neo4j.values.BufferValueWriter.Specials.endArray;
import static org.neo4j.values.ValueWriter.ArrayType.BOOLEAN;
import static org.neo4j.values.ValueWriter.ArrayType.BYTE;
import static org.neo4j.values.ValueWriter.ArrayType.CHAR;
import static org.neo4j.values.ValueWriter.ArrayType.DOUBLE;
import static org.neo4j.values.ValueWriter.ArrayType.FLOAT;
Expand Down Expand Up @@ -59,43 +59,44 @@ public static Iterable<WriteTest> data()
shouldWrite( NoValue.NO_VALUE, (Object) null ),

// array properties
shouldWrite( new byte[]{1,2,3}, beginArray( 3, BYTE ), (byte)1, (byte)2, (byte)3, endArray() ),
shouldWrite( new short[]{1,2,3}, beginArray( 3, SHORT ), (short)1, (short)2, (short)3, endArray() ),
shouldWrite( new int[]{1,2,3}, beginArray( 3, INT ), 1, 2, 3, endArray() ),
shouldWrite( new long[]{1,2,3}, beginArray( 3, LONG ), 1L, 2L, 3L, endArray() ),
shouldWrite( new float[]{1,2,3}, beginArray( 3, FLOAT ), 1.0f, 2.0f, 3.0f, endArray() ),
shouldWrite( new double[]{1,2,3}, beginArray( 3, DOUBLE ), 1.0, 2.0, 3.0, endArray() ),
shouldWrite( new char[]{'a','b'}, beginArray( 2, CHAR ), 'a', 'b', endArray() ),
shouldWrite( new String[]{"a","b"}, beginArray( 2, STRING ), "a", "b", endArray() ),
shouldWrite( new boolean[]{true, false}, beginArray( 2, BOOLEAN), true, false, endArray() ),
shouldWrite( new byte[]{1, 2, 3}, byteArray( new byte[]{1, 2, 3} ) ),
shouldWrite( new short[]{1, 2, 3}, beginArray( 3, SHORT ), (short) 1, (short) 2, (short) 3,
endArray() ),
shouldWrite( new int[]{1, 2, 3}, beginArray( 3, INT ), 1, 2, 3, endArray() ),
shouldWrite( new long[]{1, 2, 3}, beginArray( 3, LONG ), 1L, 2L, 3L, endArray() ),
shouldWrite( new float[]{1, 2, 3}, beginArray( 3, FLOAT ), 1.0f, 2.0f, 3.0f, endArray() ),
shouldWrite( new double[]{1, 2, 3}, beginArray( 3, DOUBLE ), 1.0, 2.0, 3.0, endArray() ),
shouldWrite( new char[]{'a', 'b'}, beginArray( 2, CHAR ), 'a', 'b', endArray() ),
shouldWrite( new String[]{"a", "b"}, beginArray( 2, STRING ), "a", "b", endArray() ),
shouldWrite( new boolean[]{true, false}, beginArray( 2, BOOLEAN ), true, false, endArray() ),

// lazy array properties
shouldWrite( Values.lazyByteArray( () -> new byte[]{1,2,3} ),
beginArray( 3, BYTE ), (byte)1, (byte)2, (byte) 3, endArray() ),
shouldWrite( Values.lazyByteArray( () -> new byte[]{1, 2, 3} ),
byteArray( new byte[]{1, 2, 3} ) ),

shouldWrite( Values.lazyShortArray( () -> new short[]{1,2,3} ),
beginArray( 3, SHORT ), (short)1, (short)2, (short)3, endArray() ),
shouldWrite( Values.lazyShortArray( () -> new short[]{1, 2, 3} ),
beginArray( 3, SHORT ), (short) 1, (short) 2, (short) 3, endArray() ),

shouldWrite( Values.lazyIntArray( () -> new int[]{1,2,3} ),
shouldWrite( Values.lazyIntArray( () -> new int[]{1, 2, 3} ),
beginArray( 3, INT ), 1, 2, 3, endArray() ),

shouldWrite( Values.lazyLongArray( () -> new long[]{1,2,3} ),
shouldWrite( Values.lazyLongArray( () -> new long[]{1, 2, 3} ),
beginArray( 3, LONG ), 1L, 2L, 3L, endArray() ),

shouldWrite( Values.lazyFloatArray( () -> new float[]{1,2,3} ),
shouldWrite( Values.lazyFloatArray( () -> new float[]{1, 2, 3} ),
beginArray( 3, FLOAT ), 1.0f, 2.0f, 3.0f, endArray() ),

shouldWrite( Values.lazyDoubleArray( () -> new double[]{1,2,3} ),
shouldWrite( Values.lazyDoubleArray( () -> new double[]{1, 2, 3} ),
beginArray( 3, DOUBLE ), 1.0, 2.0, 3.0, endArray() ),

shouldWrite( Values.lazyCharArray( () -> new char[]{'a','b'} ),
shouldWrite( Values.lazyCharArray( () -> new char[]{'a', 'b'} ),
beginArray( 2, CHAR ), 'a', 'b', endArray() ),

shouldWrite( Values.lazyStringArray( () -> new String[]{"a","b"} ),
shouldWrite( Values.lazyStringArray( () -> new String[]{"a", "b"} ),
beginArray( 2, STRING ), "a", "b", endArray() ),

shouldWrite( Values.lazyBooleanArray( () -> new boolean[]{true, false} ),
beginArray( 2, BOOLEAN), true, false, endArray() ),
beginArray( 2, BOOLEAN ), true, false, endArray() ),

// lazy string property
shouldWrite( Values.lazyStringValue( () -> "Finnlines" ), "Finnlines" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import java.util.Arrays;

import org.neo4j.values.AnyValue;
import org.neo4j.values.BufferValueWriter.Specials;

import static org.neo4j.values.BufferValueWriter.Specials.beginArray;
import static org.neo4j.values.BufferValueWriter.Specials.endArray;
import static org.neo4j.values.ValueWriter.ArrayType.BYTE;
import static org.neo4j.values.Values.booleanValue;
import static org.neo4j.values.Values.byteArray;
import static org.neo4j.values.Values.charValue;
Expand Down Expand Up @@ -73,7 +71,7 @@ public static Iterable<WriteTest> data()
),
beginList( 3 ),
false,
beginArray( 3, BYTE ), (byte) 3, (byte) 4, (byte) 5, endArray(),
Specials.byteArray( new byte[]{3, 4, 5} ),
"yo",
endList()
),
Expand Down

0 comments on commit be0f1c1

Please sign in to comment.