Skip to content

Commit

Permalink
Move StringValue production one step down
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Aug 23, 2017
1 parent 1be9ef9 commit 1b8d89a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
Expand Up @@ -31,6 +31,7 @@
import org.neo4j.kernel.impl.store.record.Record; import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.util.Bits; import org.neo4j.kernel.impl.util.Bits;
import org.neo4j.string.UTF8; import org.neo4j.string.UTF8;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Value; import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values; import org.neo4j.values.storable.Values;


Expand Down Expand Up @@ -188,18 +189,18 @@ long longValue()
return Double.longBitsToDouble( data[position + 1] ); return Double.longBitsToDouble( data[position + 1] );
} }


String shortStringValue() TextValue shortStringValue()
{ {
assertOfType( SHORT_STRING ); assertOfType( SHORT_STRING );
return LongerShortString.decode( data, position, currentBlocksUsed() ); return LongerShortString.decode( data, position, currentBlocksUsed() );
} }


String stringValue() TextValue stringValue()
{ {
assertOfType( STRING ); assertOfType( STRING );
readFromStore( stringRecordCursor ); readFromStore( stringRecordCursor );
buffer.flip(); buffer.flip();
return UTF8.decode( buffer.array(), 0, buffer.limit() ); return Values.stringValue( UTF8.decode( buffer.array(), 0, buffer.limit() ) );
} }


Value shortArrayValue() Value shortArrayValue()
Expand Down Expand Up @@ -238,9 +239,9 @@ Value value()
case DOUBLE: case DOUBLE:
return Values.doubleValue( doubleValue() ); return Values.doubleValue( doubleValue() );
case SHORT_STRING: case SHORT_STRING:
return Values.stringValue( shortStringValue() ); return shortStringValue();
case STRING: case STRING:
return Values.stringValue( stringValue() ); return stringValue();
case SHORT_ARRAY: case SHORT_ARRAY:
return shortArrayValue(); return shortArrayValue();
case ARRAY: case ARRAY:
Expand Down
Expand Up @@ -26,6 +26,8 @@
import org.neo4j.kernel.impl.util.Bits; import org.neo4j.kernel.impl.util.Bits;
import org.neo4j.string.UTF8; import org.neo4j.string.UTF8;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil; import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;
import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;


/** /**
* Supports encoding alphanumerical and <code>SP . - + , ' : / _</code> * Supports encoding alphanumerical and <code>SP . - + , ' : / _</code>
Expand Down Expand Up @@ -897,17 +899,17 @@ private static void writeHeader( Bits bits, int keyId, int encoding, int stringL
* @param block the value to decode to a short string. * @param block the value to decode to a short string.
* @return the decoded short string * @return the decoded short string
*/ */
public static String decode( PropertyBlock block ) public static TextValue decode( PropertyBlock block )
{ {
return decode( block.getValueBlocks(), 0, block.getValueBlocks().length ); return decode( block.getValueBlocks(), 0, block.getValueBlocks().length );
} }


public static String decode( long[] blocks, int offset, int length ) public static TextValue decode( long[] blocks, int offset, int length )
{ {
long firstLong = blocks[offset]; long firstLong = blocks[offset];
if ( (firstLong & 0xFFFFFF0FFFFFFFFFL) == 0 ) if ( (firstLong & 0xFFFFFF0FFFFFFFFFL) == 0 )
{ {
return ""; return Values.EMPTY_STRING;
} }
// key(24b) + type(4) = 28 // key(24b) + type(4) = 28
int encoding = (int) ((firstLong & 0x1F0000000L) >>> 28); // 5 bits of encoding int encoding = (int) ((firstLong & 0x1F0000000L) >>> 28); // 5 bits of encoding
Expand All @@ -930,7 +932,7 @@ public static String decode( long[] blocks, int offset, int length )
decode( result, blocks, offset, table ); decode( result, blocks, offset, table );


// We know the char array is unshared, so use sharing constructor explicitly // We know the char array is unshared, so use sharing constructor explicitly
return UnsafeUtil.newSharedArrayString( result ); return Values.stringValue( UnsafeUtil.newSharedArrayString( result ) );
} }


private static void decode( char[] result, long[] blocks, int offset, LongerShortString table ) private static void decode( char[] result, long[] blocks, int offset, LongerShortString table )
Expand Down Expand Up @@ -1055,7 +1057,7 @@ private void translateData( Bits bits, byte[] data, int length, final int step )
} }
} }


private static String decodeLatin1( long[] blocks, int offset, int stringLength ) private static TextValue decodeLatin1( long[] blocks, int offset, int stringLength )
{ {
char[] result = new char[stringLength]; char[] result = new char[stringLength];
int block = offset; int block = offset;
Expand All @@ -1071,10 +1073,10 @@ private static String decodeLatin1( long[] blocks, int offset, int stringLength
} }
result[i] = codePoint; result[i] = codePoint;
} }
return UnsafeUtil.newSharedArrayString( result ); return Values.stringValue( UnsafeUtil.newSharedArrayString( result ) );
} }


private static String decodeUTF8( long[] blocks, int offset, int stringLength ) private static TextValue decodeUTF8( long[] blocks, int offset, int stringLength )
{ {
byte[] result = new byte[stringLength]; byte[] result = new byte[stringLength];
int block = offset; int block = offset;
Expand All @@ -1090,7 +1092,7 @@ private static String decodeUTF8( long[] blocks, int offset, int stringLength )
} }
result[i] = codePoint; result[i] = codePoint;
} }
return UTF8.decode( result ); return Values.stringValue( UTF8.decode( result ) );
} }


public static int calculateNumberOfBlocksUsed( long firstBlock ) public static int calculateNumberOfBlocksUsed( long firstBlock )
Expand Down
Expand Up @@ -168,7 +168,7 @@ private byte[] headOf( byte[] bytes, int length )
@Override @Override
public Value value( PropertyBlock block, PropertyStore store ) public Value value( PropertyBlock block, PropertyStore store )
{ {
return Values.stringValue( LongerShortString.decode( block ) ); return LongerShortString.decode( block );
} }


@Override @Override
Expand Down
Expand Up @@ -47,6 +47,7 @@


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
Expand All @@ -57,7 +58,6 @@
import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Param.param; import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Param.param;
import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Param.paramArg; import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Param.paramArg;
import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Params.params; import static org.neo4j.kernel.impl.api.store.StorePropertyPayloadCursorTest.Params.params;
import static org.neo4j.test.assertion.Assert.assertObjectOrArrayEquals;


@RunWith( Enclosed.class ) @RunWith( Enclosed.class )
public class StorePropertyPayloadCursorTest public class StorePropertyPayloadCursorTest
Expand Down Expand Up @@ -176,6 +176,7 @@ public void shouldUseDynamicStringAndArrayStoresThroughDifferentCursors()
// When // When
assertTrue( cursor.next() ); assertTrue( cursor.next() );
assertNotNull( cursor.stringValue() ); assertNotNull( cursor.stringValue() );
assertNotEquals( Values.NO_VALUE, cursor.stringValue() );


assertTrue( cursor.next() ); assertTrue( cursor.next() );
assertNotNull( cursor.arrayValue() ); assertNotNull( cursor.arrayValue() );
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.List; import java.util.List;


import org.neo4j.kernel.impl.store.record.PropertyBlock; import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.values.storable.Values;


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testRandomStrings() throws Exception
PropertyBlock record = new PropertyBlock(); PropertyBlock record = new PropertyBlock();
if ( LongerShortString.encode( 10, string, record, DEFAULT_PAYLOAD_SIZE ) ) if ( LongerShortString.encode( 10, string, record, DEFAULT_PAYLOAD_SIZE ) )
{ {
assertEquals( string, LongerShortString.decode( record ) ); assertEquals( Values.stringValue( string ), LongerShortString.decode( record ) );
} }
} }
} }
Expand All @@ -104,20 +105,22 @@ public void canEncodeLower() throws Exception
public void canEncodeLowerHex() throws Exception public void canEncodeLowerHex() throws Exception
{ {
assertCanEncodeAndDecodeToSame( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); // sha1hex('') len=40 assertCanEncodeAndDecodeToSame( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); // sha1hex('') len=40
assertCanEncodeAndDecodeToSame( "0123456789" + "abcdefabcd" + "0a0b0c0d0e" + "1a1b1c1d1e" + "f9e8d7c6b5" + "a4f3" ); // len=54 assertCanEncodeAndDecodeToSame(
"0123456789" + "abcdefabcd" + "0a0b0c0d0e" + "1a1b1c1d1e" + "f9e8d7c6b5" + "a4f3" ); // len=54
assertCannotEncode( "da39a3ee5e6b4b0d3255bfef95601890afd80709" + "0123456789" + "abcde" ); // len=55 assertCannotEncode( "da39a3ee5e6b4b0d3255bfef95601890afd80709" + "0123456789" + "abcde" ); // len=55
// test not failing on long illegal hex // test not failing on long illegal hex
assertCannotEncode( "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "x"); assertCannotEncode( "aaaaaaaaaa" + "bbbbbbbbbb" + "cccccccccc" + "dddddddddd" + "eeeeeeeeee" + "x" );
} }


@Test @Test
public void canEncodeUpperHex() throws Exception public void canEncodeUpperHex() throws Exception
{ {
assertCanEncodeAndDecodeToSame( "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709" ); // sha1HEX('') len=40 assertCanEncodeAndDecodeToSame( "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709" ); // sha1HEX('') len=40
assertCanEncodeAndDecodeToSame( "0123456789" + "ABCDEFABCD" + "0A0B0C0D0E" + "1A1B1C1D1E" + "F9E8D7C6B5" + "A4F3" ); // len=54 assertCanEncodeAndDecodeToSame(
"0123456789" + "ABCDEFABCD" + "0A0B0C0D0E" + "1A1B1C1D1E" + "F9E8D7C6B5" + "A4F3" ); // len=54
assertCannotEncode( "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709" + "0123456789" + "ABCDE" ); // len=55 assertCannotEncode( "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709" + "0123456789" + "ABCDE" ); // len=55
// test not failing on long illegal HEX // test not failing on long illegal HEX
assertCannotEncode( "AAAAAAAAAA" + "BBBBBBBBBB" + "CCCCCCCCCC" + "DDDDDDDDDD" + "EEEEEEEEEE" + "X"); assertCannotEncode( "AAAAAAAAAA" + "BBBBBBBBBB" + "CCCCCCCCCC" + "DDDDDDDDDD" + "EEEEEEEEEE" + "X" );
} }


@Test @Test
Expand Down Expand Up @@ -152,7 +155,7 @@ private void assertCanEncodeAndDecodeToSame( String string, int payloadSize )
{ {
PropertyBlock target = new PropertyBlock(); PropertyBlock target = new PropertyBlock();
assertTrue( LongerShortString.encode( 0, string, target, payloadSize ) ); assertTrue( LongerShortString.encode( 0, string, target, payloadSize ) );
assertEquals( string, LongerShortString.decode( target ) ); assertEquals( Values.stringValue( string ), LongerShortString.decode( target ) );
} }


private void assertCannotEncode( String string ) private void assertCannotEncode( String string )
Expand Down

0 comments on commit 1b8d89a

Please sign in to comment.