Skip to content

Commit e9ccc38

Browse files
authored
Merge pull request #461 from lutovich/1.6-point
Expose point type with Bolt V2
2 parents 5fcb617 + 454ed6f commit e9ccc38

38 files changed

+1231
-264
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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.Objects;
22+
23+
import org.neo4j.driver.v1.types.Point2D;
24+
25+
public class InternalPoint2D implements Point2D
26+
{
27+
private final long srid;
28+
private final double x;
29+
private final double y;
30+
31+
public InternalPoint2D( long srid, double x, double y )
32+
{
33+
this.srid = srid;
34+
this.x = x;
35+
this.y = y;
36+
}
37+
38+
@Override
39+
public long srid()
40+
{
41+
return srid;
42+
}
43+
44+
@Override
45+
public double x()
46+
{
47+
return x;
48+
}
49+
50+
@Override
51+
public double y()
52+
{
53+
return y;
54+
}
55+
56+
@Override
57+
public boolean equals( Object o )
58+
{
59+
if ( this == o )
60+
{
61+
return true;
62+
}
63+
if ( o == null || getClass() != o.getClass() )
64+
{
65+
return false;
66+
}
67+
InternalPoint2D that = (InternalPoint2D) o;
68+
return srid == that.srid &&
69+
Double.compare( that.x, x ) == 0 &&
70+
Double.compare( that.y, y ) == 0;
71+
}
72+
73+
@Override
74+
public int hashCode()
75+
{
76+
return Objects.hash( srid, x, y );
77+
}
78+
79+
@Override
80+
public String toString()
81+
{
82+
return "Point2D{" +
83+
"srid=" + srid +
84+
", x=" + x +
85+
", y=" + y +
86+
'}';
87+
}
88+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.Objects;
22+
23+
import org.neo4j.driver.v1.types.Point3D;
24+
25+
public class InternalPoint3D implements Point3D
26+
{
27+
private final long srid;
28+
private final double x;
29+
private final double y;
30+
private final double z;
31+
32+
public InternalPoint3D( long srid, double x, double y, double z )
33+
{
34+
this.srid = srid;
35+
this.x = x;
36+
this.y = y;
37+
this.z = z;
38+
}
39+
40+
@Override
41+
public long srid()
42+
{
43+
return srid;
44+
}
45+
46+
@Override
47+
public double x()
48+
{
49+
return x;
50+
}
51+
52+
@Override
53+
public double y()
54+
{
55+
return y;
56+
}
57+
58+
@Override
59+
public double z()
60+
{
61+
return z;
62+
}
63+
64+
@Override
65+
public boolean equals( Object o )
66+
{
67+
if ( this == o )
68+
{
69+
return true;
70+
}
71+
if ( o == null || getClass() != o.getClass() )
72+
{
73+
return false;
74+
}
75+
InternalPoint3D that = (InternalPoint3D) o;
76+
return srid == that.srid &&
77+
Double.compare( that.x, x ) == 0 &&
78+
Double.compare( that.y, y ) == 0 &&
79+
Double.compare( that.z, z ) == 0;
80+
}
81+
82+
@Override
83+
public int hashCode()
84+
{
85+
return Objects.hash( srid, x, y, z );
86+
}
87+
88+
@Override
89+
public String toString()
90+
{
91+
return "Point3D{" +
92+
"srid=" + srid +
93+
", x=" + x +
94+
", y=" + y +
95+
", z=" + z +
96+
'}';
97+
}
98+
}

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/inbound/ByteBufInput.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public double readDouble()
7070
}
7171

7272
@Override
73-
public PackInput readBytes( byte[] into, int offset, int toRead )
73+
public void readBytes( byte[] into, int offset, int toRead )
7474
{
7575
buf.readBytes( into, offset, toRead );
76-
return this;
7776
}
7877

7978
@Override

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)