Skip to content

Commit

Permalink
Refactor bolt module
Browse files Browse the repository at this point in the history
- Remove unused stuff
- Make packing/unpacking symmetric in types
  • Loading branch information
pontusmelke committed Aug 21, 2017
1 parent 805e78e commit 92cb164
Show file tree
Hide file tree
Showing 55 changed files with 1,350 additions and 2,687 deletions.
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.bolt.v1.runtime.Neo4jError; import org.neo4j.bolt.v1.runtime.Neo4jError;
import org.neo4j.bolt.v1.runtime.spi.BoltResult; import org.neo4j.bolt.v1.runtime.spi.BoltResult;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
import org.neo4j.values.AnyValue;
import org.neo4j.values.result.QueryResult; import org.neo4j.values.result.QueryResult;


/** /**
Expand Down Expand Up @@ -139,7 +140,7 @@ public void visit( QueryResult.Record record ) throws Exception
} }


@Override @Override
public void addMetadata( String key, Object value ) public void addMetadata( String key, AnyValue value )
{ {
metadata.put( key, value ); metadata.put( key, value );
} }
Expand Down
Expand Up @@ -63,7 +63,7 @@ public <E extends Exception> void read( BoltRequestMessageHandler<E> handler ) t
{ {
case INIT: case INIT:
String clientName = unpacker.unpackString(); String clientName = unpacker.unpackString();
Map<String,Object> credentials = unpacker.unpackMap(); Map<String,Object> credentials = unpacker.unpackToRawMap();
handler.onInit( clientName, credentials ); handler.onInit( clientName, credentials );
break; break;
case ACK_FAILURE: case ACK_FAILURE:
Expand All @@ -74,7 +74,7 @@ public <E extends Exception> void read( BoltRequestMessageHandler<E> handler ) t
break; break;
case RUN: case RUN:
String statement = unpacker.unpackString(); String statement = unpacker.unpackString();
Map<String,Object> params = unpacker.unpackMap(); Map<String,Object> params = unpacker.unpackToRawMap();
Optional<Neo4jError> error = unpacker.consumeError(); Optional<Neo4jError> error = unpacker.consumeError();
if ( error.isPresent() ) if ( error.isPresent() )
{ {
Expand Down
Expand Up @@ -19,10 +19,9 @@
*/ */
package org.neo4j.bolt.v1.messaging; package org.neo4j.bolt.v1.messaging;


import java.util.Map;

import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.values.result.QueryResult; import org.neo4j.values.result.QueryResult;
import org.neo4j.values.virtual.MapValue;


/** /**
* Interface defining simple handler methods for each defined * Interface defining simple handler methods for each defined
Expand All @@ -32,7 +31,7 @@
*/ */
public interface BoltResponseMessageHandler<E extends Exception> public interface BoltResponseMessageHandler<E extends Exception>
{ {
void onSuccess( Map<String, Object> metadata ) throws E; void onSuccess( MapValue metadata ) throws E;


void onRecord( QueryResult.Record item ) throws E; void onRecord( QueryResult.Record item ) throws E;


Expand Down
Expand Up @@ -20,11 +20,11 @@
package org.neo4j.bolt.v1.messaging; package org.neo4j.bolt.v1.messaging;


import java.io.IOException; import java.io.IOException;
import java.util.Map;


import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.values.AnyValue; import org.neo4j.values.AnyValue;
import org.neo4j.values.result.QueryResult; import org.neo4j.values.result.QueryResult;
import org.neo4j.values.virtual.MapValue;


import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.FAILURE; import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.FAILURE;
import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.IGNORED; import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.IGNORED;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void onRecord( QueryResult.Record item ) throws IOException
} }


@Override @Override
public void onSuccess( Map<String, Object> metadata ) throws IOException public void onSuccess( MapValue metadata ) throws IOException
{ {
packer.packStructHeader( 1, SUCCESS.signature() ); packer.packStructHeader( 1, SUCCESS.signature() );
packer.packRawMap( metadata ); packer.packRawMap( metadata );
Expand Down
Expand Up @@ -29,10 +29,13 @@
import org.neo4j.bolt.v1.runtime.Neo4jError; import org.neo4j.bolt.v1.runtime.Neo4jError;
import org.neo4j.bolt.v1.runtime.spi.BoltResult; import org.neo4j.bolt.v1.runtime.spi.BoltResult;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
import org.neo4j.values.AnyValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.VirtualValues;


class MessageProcessingHandler implements BoltResponseHandler class MessageProcessingHandler implements BoltResponseHandler
{ {
protected final Map<String,Object> metadata = new HashMap<>(); protected final Map<String,AnyValue> metadata = new HashMap<>();


protected final Log log; protected final Log log;
protected final BoltWorker worker; protected final BoltWorker worker;
Expand Down Expand Up @@ -62,7 +65,7 @@ public void onRecords( BoltResult result, boolean pull ) throws Exception
} }


@Override @Override
public void onMetadata( String key, Object value ) public void onMetadata( String key, AnyValue value )
{ {
metadata.put( key, value ); metadata.put( key, value );
} }
Expand Down Expand Up @@ -109,9 +112,9 @@ else if ( error != null )
} }
} }


Map<String,Object> getMetadata() MapValue getMetadata()
{ {
return metadata; return VirtualValues.map( metadata );
} }


private void clearState() private void clearState()
Expand Down
Expand Up @@ -32,18 +32,25 @@
import org.neo4j.bolt.v1.packstream.PackType; import org.neo4j.bolt.v1.packstream.PackType;
import org.neo4j.bolt.v1.runtime.Neo4jError; import org.neo4j.bolt.v1.runtime.Neo4jError;
import org.neo4j.collection.primitive.PrimitiveLongIntKeyValueArray; import org.neo4j.collection.primitive.PrimitiveLongIntKeyValueArray;
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.api.exceptions.Status;
import org.neo4j.values.AnyValue; import org.neo4j.values.AnyValue;
import org.neo4j.values.AnyValueWriter; import org.neo4j.values.AnyValueWriter;
import org.neo4j.values.AnyValues; import org.neo4j.values.BaseToObjectValueWriter;
import org.neo4j.values.storable.TextArray; import org.neo4j.values.storable.TextArray;
import org.neo4j.values.storable.TextValue; import org.neo4j.values.storable.TextValue;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.CoordinateReferenceSystem; import org.neo4j.values.virtual.CoordinateReferenceSystem;
import org.neo4j.values.virtual.EdgeValue; import org.neo4j.values.virtual.EdgeValue;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.MapValue; import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.NodeValue; import org.neo4j.values.virtual.NodeValue;
import org.neo4j.values.virtual.VirtualValues;


import static org.neo4j.bolt.v1.packstream.PackStream.UNKNOWN_SIZE; import static org.neo4j.bolt.v1.packstream.PackStream.UNKNOWN_SIZE;
import static org.neo4j.values.storable.Values.byteArray;


/** /**
* Extended PackStream packer and unpacker classes for working * Extended PackStream packer and unpacker classes for working
Expand All @@ -52,7 +59,7 @@
public class Neo4jPack public class Neo4jPack
{ {
private static final List<Object> EMPTY_LIST = new ArrayList<>(); private static final List<Object> EMPTY_LIST = new ArrayList<>();
private static final Map<String,Object> EMPTY_MAP = new HashMap<>(); private static final Map<String,AnyValue> EMPTY_MAP = new HashMap<>();


public static final byte NODE = 'N'; public static final byte NODE = 'N';
public static final byte RELATIONSHIP = 'R'; public static final byte RELATIONSHIP = 'R';
Expand All @@ -79,26 +86,10 @@ public void pack( AnyValue value ) throws IOException
value.writeTo( this ); value.writeTo( this );
} }


//TODO this should go away when we have cypher providing us with values public void packRawMap( MapValue map ) throws IOException
public void pack( Object value ) throws IOException
{
try
{
pack( AnyValues.of( value ) );
}
catch ( IllegalArgumentException e )
{
error = new Error( Status.Request.Invalid,
"Unpackable value " + value + " of type " + value.getClass().getName() );
packNull();
}
}

//TODO this needs to go away as well since it is relying on pack(Object)
public void packRawMap( Map<String,Object> map ) throws IOException
{ {
packMapHeader( map.size() ); packMapHeader( map.size() );
for ( Map.Entry<String,Object> entry : map.entrySet() ) for ( Map.Entry<String,AnyValue> entry : map.entrySet() )
{ {
pack( entry.getKey() ); pack( entry.getKey() );
pack( entry.getValue() ); pack( entry.getValue() );
Expand Down Expand Up @@ -440,25 +431,25 @@ public Unpacker( PackInput input )
super( input ); super( input );
} }


public Object unpack() throws IOException public AnyValue unpack() throws IOException
{ {
PackType valType = peekNextType(); PackType valType = peekNextType();
switch ( valType ) switch ( valType )
{ {
case BYTES: case BYTES:
return unpackBytes(); return byteArray( unpackBytes() );
case STRING: case STRING:
return unpackString(); return Values.stringValue( unpackString() );
case INTEGER: case INTEGER:
return unpackLong(); return Values.longValue( unpackLong() );
case FLOAT: case FLOAT:
return unpackDouble(); return Values.doubleValue( unpackDouble() );
case BOOLEAN: case BOOLEAN:
return unpackBoolean(); return Values.booleanValue( unpackBoolean() );
case NULL: case NULL:
// still need to move past the null value // still need to move past the null value
unpackNull(); unpackNull();
return null; return Values.NO_VALUE;
case LIST: case LIST:
{ {
return unpackList(); return unpackList();
Expand Down Expand Up @@ -505,14 +496,14 @@ public Object unpack() throws IOException
} }
} }


List<Object> unpackList() throws IOException ListValue unpackList() throws IOException
{ {
int size = (int) unpackListHeader(); int size = (int) unpackListHeader();
if ( size == 0 ) if ( size == 0 )
{ {
return EMPTY_LIST; return VirtualValues.EMPTY_LIST;
} }
ArrayList<Object> list; ArrayList<AnyValue> list;
if ( size == UNKNOWN_SIZE ) if ( size == UNKNOWN_SIZE )
{ {
list = new ArrayList<>(); list = new ArrayList<>();
Expand All @@ -539,17 +530,17 @@ List<Object> unpackList() throws IOException
list.add( unpack() ); list.add( unpack() );
} }
} }
return list; return VirtualValues.list( list.toArray( new AnyValue[list.size()] ) );
} }


public Map<String,Object> unpackMap() throws IOException public MapValue unpackMap() throws IOException
{ {
int size = (int) unpackMapHeader(); int size = (int) unpackMapHeader();
if ( size == 0 ) if ( size == 0 )
{ {
return EMPTY_MAP; return VirtualValues.EMPTY_MAP;
} }
Map<String,Object> map; Map<String,AnyValue> map;
if ( size == UNKNOWN_SIZE ) if ( size == UNKNOWN_SIZE )
{ {
map = new HashMap<>(); map = new HashMap<>();
Expand All @@ -558,7 +549,7 @@ public Map<String,Object> unpackMap() throws IOException
{ {
PackType keyType = peekNextType(); PackType keyType = peekNextType();
String key; String key;
Object val; AnyValue val;
switch ( keyType ) switch ( keyType )
{ {
case END_OF_STREAM: case END_OF_STREAM:
Expand Down Expand Up @@ -608,13 +599,26 @@ public Map<String,Object> unpackMap() throws IOException
throw new PackStream.PackStreamException( "Bad key type: " + type ); throw new PackStream.PackStreamException( "Bad key type: " + type );
} }


Object val = unpack(); AnyValue val = unpack();
if ( map.put( key, val ) != null ) if ( map.put( key, val ) != null )
{ {
errors.add( Neo4jError.from( Status.Request.Invalid, "Duplicate map key `" + key + "`." ) ); errors.add( Neo4jError.from( Status.Request.Invalid, "Duplicate map key `" + key + "`." ) );
} }
} }
} }
return VirtualValues.map( map );
}

public Map<String,Object> unpackToRawMap() throws IOException
{
MapValue mapValue = unpackMap();
HashMap<String,Object> map = new HashMap<>( mapValue.size() );
for ( Map.Entry<String,AnyValue> entry : mapValue.entrySet() )
{
UnpackerWriter unpackerWriter = new UnpackerWriter();
entry.getValue().writeTo( unpackerWriter );
map.put( entry.getKey(), unpackerWriter.value() );
}
return map; return map;
} }


Expand Down Expand Up @@ -658,4 +662,32 @@ String msg()
private Neo4jPack() private Neo4jPack()
{ {
} }

private static class UnpackerWriter extends BaseToObjectValueWriter<RuntimeException>
{

@Override
protected Node newNodeProxyById( long id )
{
throw new UnsupportedOperationException( "Cannot unpack nodes" );
}

@Override
protected Relationship newRelationshipProxyById( long id )
{
throw new UnsupportedOperationException( "Cannot unpack relationships" );
}

@Override
protected Point newGeographicPoint( double longitude, double latitude, String name, int code, String href )
{
throw new UnsupportedOperationException( "Cannot unpack points" );
}

@Override
protected Point newCartesianPoint( double x, double y, String name, int code, String href )
{
throw new UnsupportedOperationException( "Cannot unpack points" );
}
}
} }

0 comments on commit 92cb164

Please sign in to comment.