Skip to content

Expose point type with Bolt V2 #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal;

import java.util.Objects;

import org.neo4j.driver.v1.types.Point2D;

public class InternalPoint2D implements Point2D
{
private final long srid;
private final double x;
private final double y;

public InternalPoint2D( long srid, double x, double y )
{
this.srid = srid;
this.x = x;
this.y = y;
}

@Override
public long srid()
{
return srid;
}

@Override
public double x()
{
return x;
}

@Override
public double y()
{
return y;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InternalPoint2D that = (InternalPoint2D) o;
return srid == that.srid &&
Double.compare( that.x, x ) == 0 &&
Double.compare( that.y, y ) == 0;
}

@Override
public int hashCode()
{
return Objects.hash( srid, x, y );
}

@Override
public String toString()
{
return "Point2D{" +
"srid=" + srid +
", x=" + x +
", y=" + y +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.neo4j.driver.internal;

import java.util.Objects;

import org.neo4j.driver.v1.types.Point3D;

public class InternalPoint3D implements Point3D
{
private final long srid;
private final double x;
private final double y;
private final double z;

public InternalPoint3D( long srid, double x, double y, double z )
{
this.srid = srid;
this.x = x;
this.y = y;
this.z = z;
}

@Override
public long srid()
{
return srid;
}

@Override
public double x()
{
return x;
}

@Override
public double y()
{
return y;
}

@Override
public double z()
{
return z;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
InternalPoint3D that = (InternalPoint3D) o;
return srid == that.srid &&
Double.compare( that.x, x ) == 0 &&
Double.compare( that.y, y ) == 0 &&
Double.compare( that.z, z ) == 0;
}

@Override
public int hashCode()
{
return Objects.hash( srid, x, y, z );
}

@Override
public String toString()
{
return "Point3D{" +
"srid=" + srid +
", x=" + x +
", y=" + y +
", z=" + z +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,43 @@

import static io.netty.buffer.Unpooled.copyInt;
import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static java.lang.Integer.toHexString;

public final class BoltProtocolV1Util
public final class BoltProtocolUtil
{
public static final int HTTP = 1213486160; //== 0x48545450 == "HTTP"

public static final int BOLT_MAGIC_PREAMBLE = 0x6060B017;
public static final int PROTOCOL_VERSION_1 = 1;
public static final int PROTOCOL_VERSION_2 = 2;

public static final int BOLT_MAGIC_PREAMBLE = 0x6060B017;
public static final int NO_PROTOCOL_VERSION = 0;

public static final int CHUNK_HEADER_SIZE_BYTES = 2;

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

private static final ByteBuf BOLT_V1_HANDSHAKE_BUF = unreleasableBuffer( copyInt(
private static final ByteBuf HANDSHAKE_BUF = unreleasableBuffer( copyInt(
BOLT_MAGIC_PREAMBLE,
PROTOCOL_VERSION_2,
PROTOCOL_VERSION_1,
NO_PROTOCOL_VERSION,
NO_PROTOCOL_VERSION,
NO_PROTOCOL_VERSION ) ).asReadOnly();

private BoltProtocolV1Util()
private static final String HANDSHAKE_STRING = createHandshakeString();

private BoltProtocolUtil()
{
}

public static ByteBuf handshakeBuf()
{
return BOLT_V1_HANDSHAKE_BUF.duplicate();
return HANDSHAKE_BUF.duplicate();
}

public static String handshakeString()
{
return "[0x6060B017, 1, 0, 0, 0]";
return HANDSHAKE_STRING;
}

public static void writeMessageBoundary( ByteBuf buf )
Expand All @@ -70,4 +75,10 @@ public static void writeChunkHeader( ByteBuf buf, int chunkStartIndex, int heade
{
buf.setShort( chunkStartIndex, headerValue );
}

private static String createHandshakeString()
{
ByteBuf buf = handshakeBuf();
return String.format( "[0x%s, %s, %s, %s, %s]", toHexString( buf.readInt() ), buf.readInt(), buf.readInt(), buf.readInt(), buf.readInt() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;

import static java.lang.String.format;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.handshakeBuf;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.handshakeString;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.handshakeBuf;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.handshakeString;

public class ChannelConnectedListener implements ChannelFutureListener
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
import org.neo4j.driver.internal.logging.ChannelActivityLogger;
import org.neo4j.driver.internal.messaging.MessageFormat;
import org.neo4j.driver.internal.messaging.PackStreamMessageFormatV1;
import org.neo4j.driver.internal.messaging.PackStreamMessageFormatV2;
import org.neo4j.driver.internal.util.ErrorUtil;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.exceptions.ClientException;
import org.neo4j.driver.v1.exceptions.SecurityException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;

import static org.neo4j.driver.internal.async.BoltProtocolV1Util.HTTP;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.NO_PROTOCOL_VERSION;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.PROTOCOL_VERSION_1;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.HTTP;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.NO_PROTOCOL_VERSION;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.PROTOCOL_VERSION_1;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.PROTOCOL_VERSION_2;

public class HandshakeHandler extends ReplayingDecoder<Void>
{
Expand Down Expand Up @@ -121,9 +123,10 @@ protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )
switch ( serverSuggestedVersion )
{
case PROTOCOL_VERSION_1:
MessageFormat messageFormat = new PackStreamMessageFormatV1();
pipelineBuilder.build( messageFormat, pipeline, logging );
handshakeCompletedPromise.setSuccess();
protocolSelected( new PackStreamMessageFormatV1(), pipeline );
break;
case PROTOCOL_VERSION_2:
protocolSelected( new PackStreamMessageFormatV2(), pipeline );
break;
case NO_PROTOCOL_VERSION:
fail( ctx, protocolNoSupportedByServerError() );
Expand All @@ -137,6 +140,12 @@ protected void decode( ChannelHandlerContext ctx, ByteBuf in, List<Object> out )
}
}

private void protocolSelected( MessageFormat messageFormat, ChannelPipeline pipeline )
{
pipelineBuilder.build( messageFormat, pipeline, logging );
handshakeCompletedPromise.setSuccess();
}

private void fail( ChannelHandlerContext ctx, Throwable error )
{
ctx.close().addListener( future -> handshakeCompletedPromise.tryFailure( error ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ public double readDouble()
}

@Override
public PackInput readBytes( byte[] into, int offset, int toRead )
public void readBytes( byte[] into, int offset, int toRead )
{
buf.readBytes( into, offset, toRead );
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import io.netty.buffer.ByteBuf;

import org.neo4j.driver.internal.async.BoltProtocolV1Util;
import org.neo4j.driver.internal.async.BoltProtocolUtil;
import org.neo4j.driver.internal.packstream.PackOutput;

import static java.util.Objects.requireNonNull;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.CHUNK_HEADER_SIZE_BYTES;
import static org.neo4j.driver.internal.async.BoltProtocolV1Util.DEFAULT_MAX_OUTBOUND_CHUNK_SIZE_BYTES;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.CHUNK_HEADER_SIZE_BYTES;
import static org.neo4j.driver.internal.async.BoltProtocolUtil.DEFAULT_MAX_OUTBOUND_CHUNK_SIZE_BYTES;

public class ChunkAwareByteBufOutput implements PackOutput
{
Expand Down Expand Up @@ -138,15 +138,15 @@ private void ensureCanFitInCurrentChunk( int numberOfBytes )
private void startNewChunk( int index )
{
currentChunkStartIndex = index;
BoltProtocolV1Util.writeEmptyChunkHeader( buf );
BoltProtocolUtil.writeEmptyChunkHeader( buf );
currentChunkSize = CHUNK_HEADER_SIZE_BYTES;
}

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

private int availableBytesInCurrentChunk()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.List;

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

BoltProtocolV1Util.writeMessageBoundary( messageBuf );
BoltProtocolUtil.writeMessageBoundary( messageBuf );
out.add( messageBuf );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface MessageFormat
{
interface Writer
{
Writer write( Message msg ) throws IOException;
void write( Message msg ) throws IOException;
}

interface Reader
Expand All @@ -38,6 +38,4 @@ interface Reader
Writer newWriter( PackOutput output, boolean byteArraySupportEnabled );

Reader newReader( PackInput input );

int version();
}
Loading