Skip to content

Commit

Permalink
Remove map( Map<String, AnyValue) replace with builder
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 22, 2018
1 parent d5ba055 commit cb78d2a
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 214 deletions.
Expand Up @@ -157,7 +157,7 @@ public void visit( QueryResult.Record record ) throws Exception
@Override
public void addMetadata( String key, AnyValue value )
{
metadata.put( key, value );
metadata.add( key, value );
}

} );
Expand Down
Expand Up @@ -21,9 +21,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.neo4j.bolt.runtime.BoltConnection;
Expand All @@ -35,14 +33,14 @@
import org.neo4j.logging.Log;
import org.neo4j.values.AnyValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.VirtualValues;
import org.neo4j.values.virtual.MapValueBuilder;

class MessageProcessingHandler implements BoltResponseHandler
{
// Errors that are expected when the client disconnects mid-operation
private static final Set<Status> CLIENT_MID_OP_DISCONNECT_ERRORS = new HashSet<>( Arrays.asList(
Status.Transaction.Terminated, Status.Transaction.LockClientStopped ) );
protected final Map<String,AnyValue> metadata = new HashMap<>();
protected final MapValueBuilder metadata = new MapValueBuilder( );

protected final Log log;
protected final BoltConnection connection;
Expand Down Expand Up @@ -72,7 +70,7 @@ public void onRecords( BoltResult result, boolean pull ) throws Exception
@Override
public void onMetadata( String key, AnyValue value )
{
metadata.put( key, value );
metadata.add( key, value );
}

@Override
Expand Down Expand Up @@ -118,7 +116,7 @@ else if ( error != null )

MapValue getMetadata()
{
return VirtualValues.map( metadata );
return metadata.build();
}

private void clearState()
Expand Down
Expand Up @@ -26,9 +26,7 @@
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.bolt.messaging.StructType;
import org.neo4j.bolt.v1.packstream.PackInput;
Expand All @@ -44,6 +42,7 @@
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.MapValueBuilder;
import org.neo4j.values.virtual.NodeValue;
import org.neo4j.values.virtual.RelationshipValue;
import org.neo4j.values.virtual.VirtualValues;
Expand Down Expand Up @@ -541,10 +540,10 @@ public MapValue unpackMap() throws IOException
{
return VirtualValues.EMPTY_MAP;
}
Map<String,AnyValue> map;
MapValueBuilder map;
if ( size == UNKNOWN_SIZE )
{
map = new HashMap<>();
map = new MapValueBuilder( );
boolean more = true;
while ( more )
{
Expand All @@ -560,7 +559,7 @@ public MapValue unpackMap() throws IOException
case STRING:
key = unpackString();
val = unpack();
if ( map.put( key, val ) != null )
if ( map.add( key, val ) != null )
{
throw new BoltIOException( Status.Request.Invalid, "Duplicate map key `" + key + "`." );
}
Expand All @@ -574,7 +573,7 @@ public MapValue unpackMap() throws IOException
}
else
{
map = new HashMap<>( size, 1 );
map = new MapValueBuilder( size );
for ( int i = 0; i < size; i++ )
{
PackType keyType = peekNextType();
Expand All @@ -591,13 +590,13 @@ public MapValue unpackMap() throws IOException
}

AnyValue val = unpack();
if ( map.put( key, val ) != null )
if ( map.add( key, val ) != null )
{
throw new BoltIOException( Status.Request.Invalid, "Duplicate map key `" + key + "`." );
}
}
}
return VirtualValues.map( map );
return map.build();
}
}
}
Expand Up @@ -21,11 +21,8 @@

import java.time.Clock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.neo4j.bolt.v1.messaging.BoltIOException;
import org.neo4j.bolt.v1.runtime.spi.BoltResult;
import org.neo4j.cypher.result.QueryResult;
import org.neo4j.graphdb.ExecutionPlanDescription;
Expand All @@ -36,6 +33,7 @@
import org.neo4j.values.AnyValue;
import org.neo4j.values.storable.Values;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.MapValueBuilder;
import org.neo4j.values.virtual.VirtualValues;

import static org.neo4j.values.storable.Values.intValue;
Expand Down Expand Up @@ -101,26 +99,26 @@ public void accept( final Visitor visitor ) throws Exception

private MapValue queryStats( QueryStatistics queryStatistics )
{
Map<String,AnyValue> result = new HashMap<>();
addIfNonZero( result, "nodes-created", queryStatistics.getNodesCreated() );
addIfNonZero( result, "nodes-deleted", queryStatistics.getNodesDeleted() );
addIfNonZero( result, "relationships-created", queryStatistics.getRelationshipsCreated() );
addIfNonZero( result, "relationships-deleted", queryStatistics.getRelationshipsDeleted() );
addIfNonZero( result, "properties-set", queryStatistics.getPropertiesSet() );
addIfNonZero( result, "labels-added", queryStatistics.getLabelsAdded() );
addIfNonZero( result, "labels-removed", queryStatistics.getLabelsRemoved() );
addIfNonZero( result, "indexes-added", queryStatistics.getIndexesAdded() );
addIfNonZero( result, "indexes-removed", queryStatistics.getIndexesRemoved() );
addIfNonZero( result, "constraints-added", queryStatistics.getConstraintsAdded() );
addIfNonZero( result, "constraints-removed", queryStatistics.getConstraintsRemoved() );
return VirtualValues.map( result );
MapValueBuilder builder = new MapValueBuilder();
addIfNonZero( builder, "nodes-created", queryStatistics.getNodesCreated() );
addIfNonZero( builder, "nodes-deleted", queryStatistics.getNodesDeleted() );
addIfNonZero( builder, "relationships-created", queryStatistics.getRelationshipsCreated() );
addIfNonZero( builder, "relationships-deleted", queryStatistics.getRelationshipsDeleted() );
addIfNonZero( builder, "properties-set", queryStatistics.getPropertiesSet() );
addIfNonZero( builder, "labels-added", queryStatistics.getLabelsAdded() );
addIfNonZero( builder, "labels-removed", queryStatistics.getLabelsRemoved() );
addIfNonZero( builder, "indexes-added", queryStatistics.getIndexesAdded() );
addIfNonZero( builder, "indexes-removed", queryStatistics.getIndexesRemoved() );
addIfNonZero( builder, "constraints-added", queryStatistics.getConstraintsAdded() );
addIfNonZero( builder, "constraints-removed", queryStatistics.getConstraintsRemoved() );
return builder.build();
}

private void addIfNonZero( Map<String,AnyValue> map, String name, int count )
private void addIfNonZero( MapValueBuilder builder, String name, int count )
{
if ( count > 0 )
{
map.put( name, intValue( count ) );
builder.add( name, intValue( count ) );
}
}

Expand Down Expand Up @@ -150,26 +148,28 @@ private static class NotificationConverter
public static AnyValue convert( Iterable<Notification> notifications )
{
List<AnyValue> out = new ArrayList<>();
MapValueBuilder builder = new MapValueBuilder();
for ( Notification notification : notifications )
{
Map<String,AnyValue> notificationMap = new HashMap<>( 4 );
notificationMap.put( "code", stringValue( notification.getCode() ) );
notificationMap.put( "title", stringValue( notification.getTitle() ) );
notificationMap.put( "description", stringValue( notification.getDescription() ) );
notificationMap.put( "severity", stringValue( notification.getSeverity().toString() ) );

builder.add( "code", stringValue( notification.getCode() ) );
builder.add( "title", stringValue( notification.getTitle() ) );
builder.add( "description", stringValue( notification.getDescription() ) );
builder.add( "severity", stringValue( notification.getSeverity().toString() ) );

InputPosition pos = notification.getPosition(); // position is optional
if ( !pos.equals( InputPosition.empty ) )
boolean includePosition = !pos.equals( InputPosition.empty );
if ( includePosition )
{
// only add the position if it is not empty
Map<String,AnyValue> posMap = new HashMap<>( 3 );
posMap.put( "offset", intValue( pos.getOffset() ) );
posMap.put( "line", intValue( pos.getLine() ) );
posMap.put( "column", intValue( pos.getColumn() ) );
notificationMap.put( "position", VirtualValues.map( posMap ) );
builder.add( "position", VirtualValues.map( new String[]{"offset", "line", "columne"},
new AnyValue[]{
intValue( pos.getOffset() ),
intValue( pos.getLine() ),
intValue( pos.getColumn() ) } ) );
}

out.add( VirtualValues.map( notificationMap ) );
out.add( builder.build() );
}
return VirtualValues.fromList( out );
}
Expand Down
Expand Up @@ -19,16 +19,15 @@
*/
package org.neo4j.bolt.v1.runtime;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.neo4j.graphdb.ExecutionPlanDescription;
import org.neo4j.kernel.impl.util.ValueUtils;
import org.neo4j.values.AnyValue;
import org.neo4j.values.virtual.ListValue;
import org.neo4j.values.virtual.MapValue;
import org.neo4j.values.virtual.MapValueBuilder;
import org.neo4j.values.virtual.VirtualValues;

import static org.neo4j.values.storable.Values.doubleValue;
Expand All @@ -44,21 +43,21 @@ private ExecutionPlanConverter()

public static MapValue convert( ExecutionPlanDescription plan )
{
Map<String,AnyValue> out = new HashMap<>();
out.put( "operatorType", stringValue( plan.getName() ) );
out.put( "args", ValueUtils.asMapValue( plan.getArguments() ) );
out.put( "identifiers", ValueUtils.asListValue( plan.getIdentifiers() ) );
out.put( "children", children( plan ) );
MapValueBuilder out = new MapValueBuilder( );
out.add( "operatorType", stringValue( plan.getName() ) );
out.add( "args", ValueUtils.asMapValue( plan.getArguments() ) );
out.add( "identifiers", ValueUtils.asListValue( plan.getIdentifiers() ) );
out.add( "children", children( plan ) );
if ( plan.hasProfilerStatistics() )
{
ExecutionPlanDescription.ProfilerStatistics profile = plan.getProfilerStatistics();
out.put( "dbHits", longValue( profile.getDbHits() ) );
out.put( "pageCacheHits", longValue( profile.getPageCacheHits() ) );
out.put( "pageCacheMisses", longValue( profile.getPageCacheMisses() ) );
out.put( "pageCacheHitRatio", doubleValue( profile.getPageCacheHitRatio() ) );
out.put( "rows", longValue( profile.getRows() ) );
out.add( "dbHits", longValue( profile.getDbHits() ) );
out.add( "pageCacheHits", longValue( profile.getPageCacheHits() ) );
out.add( "pageCacheMisses", longValue( profile.getPageCacheMisses() ) );
out.add( "pageCacheHitRatio", doubleValue( profile.getPageCacheHitRatio() ) );
out.add( "rows", longValue( profile.getRows() ) );
}
return VirtualValues.map( out );
return out.build();
}

private static ListValue children( ExecutionPlanDescription plan )
Expand Down

0 comments on commit cb78d2a

Please sign in to comment.