Skip to content

Commit cb7647b

Browse files
Condense Packer a bit.
Trying to be smarter than the GraalVM AoT compiler, but failed. Looks nicer nevertheless.
1 parent 8f4973f commit cb7647b

File tree

1 file changed

+42
-61
lines changed

1 file changed

+42
-61
lines changed

driver/src/main/java/org/neo4j/driver/internal/packstream/PackStream.java

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.nio.charset.Charset;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.function.Function;
26+
import java.util.function.Supplier;
2527

2628
import static java.lang.Integer.toHexString;
2729
import static java.lang.String.format;
@@ -409,17 +411,7 @@ public Unpacker( PackInput in )
409411

410412
public long unpackStructHeader() throws IOException
411413
{
412-
final byte markerByte = in.readByte();
413-
final byte markerHighNibble = (byte) (markerByte & 0xF0);
414-
final byte markerLowNibble = (byte) (markerByte & 0x0F);
415-
416-
if ( markerHighNibble == TINY_STRUCT ) { return markerLowNibble; }
417-
switch(markerByte)
418-
{
419-
case STRUCT_8: return unpackUINT8();
420-
case STRUCT_16: return unpackUINT16();
421-
default: throw new Unexpected( "Expected a struct, but got: " + toHexString( markerByte ));
422-
}
414+
return unpackHeaderImpl( TINY_STRUCT, markerByteAsHexString -> "Expected a struct, but got: " + markerByteAsHexString );
423415
}
424416

425417
public byte unpackStructSignature() throws IOException
@@ -429,33 +421,32 @@ public byte unpackStructSignature() throws IOException
429421

430422
public long unpackListHeader() throws IOException
431423
{
432-
final byte markerByte = in.readByte();
433-
final byte markerHighNibble = (byte) (markerByte & 0xF0);
434-
final byte markerLowNibble = (byte) (markerByte & 0x0F);
435-
436-
if ( markerHighNibble == TINY_LIST ) { return markerLowNibble; }
437-
switch(markerByte)
438-
{
439-
case LIST_8: return unpackUINT8();
440-
case LIST_16: return unpackUINT16();
441-
case LIST_32: return unpackUINT32();
442-
default: throw new Unexpected( "Expected a list, but got: " + toHexString( markerByte & 0xFF ));
443-
}
424+
return unpackHeaderImpl( TINY_LIST, markerByteAsHexString -> "Expected a list, but got: " + markerByteAsHexString );
444425
}
445426

446427
public long unpackMapHeader() throws IOException
447428
{
429+
return unpackHeaderImpl( TINY_MAP, markerByteAsHexString -> "Expected a map, but got: " + markerByteAsHexString );
430+
}
431+
432+
private long unpackHeaderImpl( byte possibleTinyType, Function<String, String> exceptionmessageSupplier ) throws IOException {
433+
448434
final byte markerByte = in.readByte();
449435
final byte markerHighNibble = (byte) (markerByte & 0xF0);
450-
final byte markerLowNibble = (byte) (markerByte & 0x0F);
436+
final byte markerLowNibble = (byte) (markerByte & 0x0F);
451437

452-
if ( markerHighNibble == TINY_MAP ) { return markerLowNibble; }
438+
if ( markerHighNibble == possibleTinyType ) { return markerLowNibble; }
453439
switch(markerByte)
454440
{
455-
case MAP_8: return unpackUINT8();
456-
case MAP_16: return unpackUINT16();
457-
case MAP_32: return unpackUINT32();
458-
default: throw new Unexpected( "Expected a map, but got: " + toHexString( markerByte ));
441+
case STRUCT_8:
442+
case MAP_8:
443+
case LIST_8: return unpackUINT8();
444+
case STRUCT_16:
445+
case MAP_16:
446+
case LIST_16: return unpackUINT16();
447+
case MAP_32:
448+
case LIST_32: return unpackUINT32();
449+
default: throw new Unexpected( exceptionmessageSupplier.apply( toHexString( markerByte ) ) );
459450
}
460451
}
461452

@@ -485,24 +476,31 @@ public double unpackDouble() throws IOException
485476

486477
public byte[] unpackBytes() throws IOException
487478
{
488-
final byte markerByte = in.readByte();
479+
return unpackBytesImpl(in.readByte(), markerByteAsHexString -> "Expected bytes, but got: 0x" + markerByteAsHexString );
480+
}
481+
482+
private byte[] unpackBytesImpl( final byte markerByte, Function<String, String> exceptionMessageSupplier ) throws IOException
483+
{
489484
switch(markerByte)
490485
{
491-
case BYTES_8: return unpackRawBytes( unpackUINT8() );
492-
case BYTES_16: return unpackRawBytes( unpackUINT16() );
493-
case BYTES_32:
494-
{
495-
long size = unpackUINT32();
496-
if ( size <= Integer.MAX_VALUE )
497-
{
498-
return unpackRawBytes( (int) size );
499-
}
500-
else
486+
case BYTES_8:
487+
case STRING_8: return unpackRawBytes( unpackUINT8() );
488+
case BYTES_16:
489+
case STRING_16: return unpackRawBytes( unpackUINT16() );
490+
case BYTES_32:
491+
case STRING_32:
501492
{
502-
throw new Overflow( "BYTES_32 too long for Java" );
493+
long size = unpackUINT32();
494+
if ( size <= Integer.MAX_VALUE )
495+
{
496+
return unpackRawBytes( (int) size );
497+
}
498+
else
499+
{
500+
throw new Overflow( markerByte == BYTES_32 ? "BYTES_32 too long for Java" : "STRING_32 too long for Java" );
501+
}
503502
}
504-
}
505-
default: throw new Unexpected( "Expected bytes, but got: 0x" + toHexString( markerByte & 0xFF ));
503+
default: throw new Unexpected( exceptionMessageSupplier.apply( toHexString( markerByte & 0xFF ) ));
506504
}
507505
}
508506

@@ -540,24 +538,7 @@ private byte[] unpackUtf8(byte markerByte) throws IOException
540538
final byte markerLowNibble = (byte) (markerByte & 0x0F);
541539

542540
if ( markerHighNibble == TINY_STRING ) { return unpackRawBytes( markerLowNibble ); }
543-
switch(markerByte)
544-
{
545-
case STRING_8: return unpackRawBytes( unpackUINT8() );
546-
case STRING_16: return unpackRawBytes( unpackUINT16() );
547-
case STRING_32:
548-
{
549-
long size = unpackUINT32();
550-
if ( size <= Integer.MAX_VALUE )
551-
{
552-
return unpackRawBytes( (int) size );
553-
}
554-
else
555-
{
556-
throw new Overflow( "STRING_32 too long for Java" );
557-
}
558-
}
559-
default: throw new Unexpected( "Expected a string, but got: 0x" + toHexString( markerByte & 0xFF ));
560-
}
541+
return unpackBytesImpl(markerByte, markerByteAsHexString -> "Expected a string, but got: 0x" + markerByteAsHexString );
561542
}
562543

563544
public boolean unpackBoolean() throws IOException

0 commit comments

Comments
 (0)