Skip to content

Commit 01054ed

Browse files
committed
Expose point type with Bolt V2
Introduced Bolt V1 as an extension of Bolt V1 with point type support. Added interfaces to expose point with single multi-dimensional coordinate. These interfaces are now experimental and will most likely change before the next stable release.
1 parent e4da82e commit 01054ed

28 files changed

+794
-117
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal;
20+
21+
import java.util.List;
22+
23+
import org.neo4j.driver.v1.types.Coordinate;
24+
25+
import static java.util.Collections.unmodifiableList;
26+
27+
public class InternalCoordinate implements Coordinate
28+
{
29+
private final List<Double> values;
30+
31+
public InternalCoordinate( List<Double> values )
32+
{
33+
this.values = unmodifiableList( values );
34+
}
35+
36+
@Override
37+
public List<Double> values()
38+
{
39+
return values;
40+
}
41+
42+
@Override
43+
public boolean equals( Object o )
44+
{
45+
if ( this == o )
46+
{
47+
return true;
48+
}
49+
if ( o == null || getClass() != o.getClass() )
50+
{
51+
return false;
52+
}
53+
InternalCoordinate that = (InternalCoordinate) o;
54+
return values.equals( that.values );
55+
}
56+
57+
@Override
58+
public int hashCode()
59+
{
60+
return values.hashCode();
61+
}
62+
63+
@Override
64+
public String toString()
65+
{
66+
return String.format( "Coordinate<%s>", values );
67+
}
68+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal;
20+
21+
import org.neo4j.driver.v1.types.Coordinate;
22+
import org.neo4j.driver.v1.types.Point;
23+
24+
public class InternalPoint implements Point
25+
{
26+
private final long crsTableId;
27+
private final long crsCode;
28+
private final Coordinate coordinate;
29+
30+
public InternalPoint( long crsTableId, long crsCode, Coordinate coordinate )
31+
{
32+
this.crsTableId = crsTableId;
33+
this.crsCode = crsCode;
34+
this.coordinate = coordinate;
35+
}
36+
37+
@Override
38+
public long crsTableId()
39+
{
40+
return crsTableId;
41+
}
42+
43+
@Override
44+
public long crsCode()
45+
{
46+
return crsCode;
47+
}
48+
49+
@Override
50+
public Coordinate coordinate()
51+
{
52+
return coordinate;
53+
}
54+
55+
@Override
56+
public boolean equals( Object o )
57+
{
58+
if ( this == o )
59+
{
60+
return true;
61+
}
62+
if ( o == null || getClass() != o.getClass() )
63+
{
64+
return false;
65+
}
66+
InternalPoint that = (InternalPoint) o;
67+
return crsTableId == that.crsTableId &&
68+
crsCode == that.crsCode &&
69+
coordinate.equals( that.coordinate );
70+
}
71+
72+
@Override
73+
public int hashCode()
74+
{
75+
int result = (int) (crsTableId ^ (crsTableId >>> 32));
76+
result = 31 * result + (int) (crsCode ^ (crsCode >>> 32));
77+
result = 31 * result + coordinate.hashCode();
78+
return result;
79+
}
80+
81+
@Override
82+
public String toString()
83+
{
84+
return String.format( "Point<%s, %s, %s>", crsTableId, crsCode, coordinate );
85+
}
86+
}

driver/src/main/java/org/neo4j/driver/internal/async/BoltProtocolV1Util.java renamed to driver/src/main/java/org/neo4j/driver/internal/async/BoltProtocolUtil.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,43 @@
2222

2323
import static io.netty.buffer.Unpooled.copyInt;
2424
import static io.netty.buffer.Unpooled.unreleasableBuffer;
25+
import static java.lang.Integer.toHexString;
2526

26-
public final class BoltProtocolV1Util
27+
public final class BoltProtocolUtil
2728
{
2829
public static final int HTTP = 1213486160; //== 0x48545450 == "HTTP"
2930

30-
public static final int BOLT_MAGIC_PREAMBLE = 0x6060B017;
3131
public static final int PROTOCOL_VERSION_1 = 1;
32+
public static final int PROTOCOL_VERSION_2 = 2;
33+
34+
public static final int BOLT_MAGIC_PREAMBLE = 0x6060B017;
3235
public static final int NO_PROTOCOL_VERSION = 0;
3336

3437
public static final int CHUNK_HEADER_SIZE_BYTES = 2;
3538

3639
public static final int DEFAULT_MAX_OUTBOUND_CHUNK_SIZE_BYTES = Short.MAX_VALUE / 2;
3740

38-
private static final ByteBuf BOLT_V1_HANDSHAKE_BUF = unreleasableBuffer( copyInt(
41+
private static final ByteBuf HANDSHAKE_BUF = unreleasableBuffer( copyInt(
3942
BOLT_MAGIC_PREAMBLE,
43+
PROTOCOL_VERSION_2,
4044
PROTOCOL_VERSION_1,
4145
NO_PROTOCOL_VERSION,
42-
NO_PROTOCOL_VERSION,
4346
NO_PROTOCOL_VERSION ) ).asReadOnly();
4447

45-
private BoltProtocolV1Util()
48+
private static final String HANDSHAKE_STRING = createHandshakeString();
49+
50+
private BoltProtocolUtil()
4651
{
4752
}
4853

4954
public static ByteBuf handshakeBuf()
5055
{
51-
return BOLT_V1_HANDSHAKE_BUF.duplicate();
56+
return HANDSHAKE_BUF.duplicate();
5257
}
5358

5459
public static String handshakeString()
5560
{
56-
return "[0x6060B017, 1, 0, 0, 0]";
61+
return HANDSHAKE_STRING;
5762
}
5863

5964
public static void writeMessageBoundary( ByteBuf buf )
@@ -70,4 +75,10 @@ public static void writeChunkHeader( ByteBuf buf, int chunkStartIndex, int heade
7075
{
7176
buf.setShort( chunkStartIndex, headerValue );
7277
}
78+
79+
private static String createHandshakeString()
80+
{
81+
ByteBuf buf = handshakeBuf();
82+
return String.format( "[0x%s, %s, %s, %s, %s]", toHexString( buf.readInt() ), buf.readInt(), buf.readInt(), buf.readInt(), buf.readInt() );
83+
}
7384
}

driver/src/main/java/org/neo4j/driver/internal/async/ChannelConnectedListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
3232

3333
import static java.lang.String.format;
34-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.handshakeBuf;
35-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.handshakeString;
34+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.handshakeBuf;
35+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.handshakeString;
3636

3737
public class ChannelConnectedListener implements ChannelFutureListener
3838
{

driver/src/main/java/org/neo4j/driver/internal/async/HandshakeHandler.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@
3131
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
3232
import org.neo4j.driver.internal.messaging.MessageFormat;
3333
import org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1;
34+
import org.neo4j.driver.internal.messaging.PackStreamMessageFormatV2;
3435
import org.neo4j.driver.internal.util.ErrorUtil;
3536
import org.neo4j.driver.v1.Logger;
3637
import org.neo4j.driver.v1.Logging;
3738
import org.neo4j.driver.v1.exceptions.ClientException;
3839
import org.neo4j.driver.v1.exceptions.SecurityException;
3940
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
4041

41-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.HTTP;
42-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.NO_PROTOCOL_VERSION;
43-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.PROTOCOL_VERSION_1;
42+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.HTTP;
43+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.NO_PROTOCOL_VERSION;
44+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.PROTOCOL_VERSION_1;
45+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.PROTOCOL_VERSION_2;
4446

4547
public class HandshakeHandler extends ReplayingDecoder<Void>
4648
{
@@ -121,9 +123,10 @@ protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )
121123
switch ( serverSuggestedVersion )
122124
{
123125
case PROTOCOL_VERSION_1:
124-
MessageFormat messageFormat = new PackStreamMessageFormatV1();
125-
pipelineBuilder.build( messageFormat, pipeline, logging );
126-
handshakeCompletedPromise.setSuccess();
126+
protocolSelected( new PackStreamMessageFormatV1(), pipeline );
127+
break;
128+
case PROTOCOL_VERSION_2:
129+
protocolSelected( new PackStreamMessageFormatV2(), pipeline );
127130
break;
128131
case NO_PROTOCOL_VERSION:
129132
fail( ctx, protocolNoSupportedByServerError() );
@@ -137,6 +140,12 @@ protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )
137140
}
138141
}
139142

143+
private void protocolSelected( MessageFormat messageFormat, ChannelPipeline pipeline )
144+
{
145+
pipelineBuilder.build( messageFormat, pipeline, logging );
146+
handshakeCompletedPromise.setSuccess();
147+
}
148+
140149
private void fail( ChannelHandlerContext ctx, Throwable error )
141150
{
142151
ctx.close().addListener( future -> handshakeCompletedPromise.tryFailure( error ) );

driver/src/main/java/org/neo4j/driver/internal/async/outbound/ChunkAwareByteBufOutput.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
import io.netty.buffer.ByteBuf;
2222

23-
import org.neo4j.driver.internal.async.BoltProtocolV1Util;
23+
import org.neo4j.driver.internal.async.BoltProtocolUtil;
2424
import org.neo4j.driver.internal.packstream.PackOutput;
2525

2626
import static java.util.Objects.requireNonNull;
27-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.CHUNK_HEADER_SIZE_BYTES;
28-
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.DEFAULT_MAX_OUTBOUND_CHUNK_SIZE_BYTES;
27+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.CHUNK_HEADER_SIZE_BYTES;
28+
import static org.neo4j.driver.internal.async.BoltProtocolUtil.DEFAULT_MAX_OUTBOUND_CHUNK_SIZE_BYTES;
2929

3030
public class ChunkAwareByteBufOutput implements PackOutput
3131
{
@@ -138,15 +138,15 @@ private void ensureCanFitInCurrentChunk( int numberOfBytes )
138138
private void startNewChunk( int index )
139139
{
140140
currentChunkStartIndex = index;
141-
BoltProtocolV1Util.writeEmptyChunkHeader( buf );
141+
BoltProtocolUtil.writeEmptyChunkHeader( buf );
142142
currentChunkSize = CHUNK_HEADER_SIZE_BYTES;
143143
}
144144

145145
private void writeChunkSizeHeader()
146146
{
147147
// go to the beginning of the chunk and write the size header
148148
int chunkBodySize = currentChunkSize - CHUNK_HEADER_SIZE_BYTES;
149-
BoltProtocolV1Util.writeChunkHeader( buf, currentChunkStartIndex, chunkBodySize );
149+
BoltProtocolUtil.writeChunkHeader( buf, currentChunkStartIndex, chunkBodySize );
150150
}
151151

152152
private int availableBytesInCurrentChunk()

driver/src/main/java/org/neo4j/driver/internal/async/outbound/OutboundMessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import java.util.List;
2727

28-
import org.neo4j.driver.internal.async.BoltProtocolV1Util;
28+
import org.neo4j.driver.internal.async.BoltProtocolUtil;
2929
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
3030
import org.neo4j.driver.internal.messaging.Message;
3131
import org.neo4j.driver.internal.messaging.MessageFormat;
@@ -95,7 +95,7 @@ protected void encode( ChannelHandlerContext ctx, Message msg, List<Object> out
9595
log.trace( "C: %s", hexDump( messageBuf ) );
9696
}
9797

98-
BoltProtocolV1Util.writeMessageBoundary( messageBuf );
98+
BoltProtocolUtil.writeMessageBoundary( messageBuf );
9999
out.add( messageBuf );
100100
}
101101

driver/src/main/java/org/neo4j/driver/internal/messaging/MessageFormat.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public interface MessageFormat
2727
{
2828
interface Writer
2929
{
30-
Writer write( Message msg ) throws IOException;
30+
void write( Message msg ) throws IOException;
3131
}
3232

3333
interface Reader
@@ -38,6 +38,4 @@ interface Reader
3838
Writer newWriter( PackOutput output, boolean byteArraySupportEnabled );
3939

4040
Reader newReader( PackInput input );
41-
42-
int version();
4341
}

0 commit comments

Comments
 (0)