You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by jo...@apache.org on 2019/10/11 09:47:36 UTC

[tinkerpop] 01/03: TINKERPOP-2305 Introduce Buffer interface

This is an automated email from the ASF dual-hosted git repository.

jorgebg pushed a commit to branch TINKERPOP-2305
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 344b737881f42eaf0e8f6c8e7fd2df526b0b3329
Author: Jorge Bay Gondra <jo...@gmail.com>
AuthorDate: Mon Oct 7 10:40:53 2019 +0200

    TINKERPOP-2305 Introduce Buffer interface
---
 .../tinkerpop/gremlin/structure/io/Buffer.java     | 174 ++++++++++++++++++++
 .../tinkerpop/gremlin/driver/NettyBuffer.java      | 180 +++++++++++++++++++++
 2 files changed, 354 insertions(+)

diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/Buffer.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/Buffer.java
new file mode 100644
index 0000000..3219a90
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/Buffer.java
@@ -0,0 +1,174 @@
+package org.apache.tinkerpop.gremlin.structure.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+/**
+ * Represents an abstract view for one or more primitive byte arrays and NIO buffers.
+ */
+public interface Buffer {
+
+    /**
+     * Returns the reader index of this buffer.
+     */
+    int readerIndex();
+
+    /**
+     * Sets the reader index of this buffer.
+     *
+     * @throws IndexOutOfBoundsException
+     *         if its out of bounds.
+     */
+    Buffer readerIndex(int readerIndex);
+
+    /**
+     * Returns the writer index of this buffer.
+     */
+    int writerIndex();
+
+    /**
+     * Sets the writer index of this buffer.
+     *
+     * @throws IndexOutOfBoundsException
+     *         if its out of bounds.
+     */
+    Buffer writerIndex(int writerIndex);
+
+    /**
+     * Returns the number of bytes (octets) this buffer can contain.
+     */
+    int capacity();
+
+    /**
+     * Gets a boolean and advances the reader index.
+     */
+    boolean readBoolean();
+
+    /**
+     * Gets a byte and advances the reader index.
+     */
+    byte readByte();
+
+    /**
+     * Gets a 16-bit short integer and advances the reader index.
+     */
+    short readShort();
+
+    /**
+     * Gets a 32-bit integer at the current index and advances the reader index.
+     */
+    int readInt();
+
+    /**
+     * Gets a 64-bit integer  and advances the reader index.
+     */
+    long readLong();
+
+    /**
+     * Gets a 32-bit floating point number and advances the reader index.
+     */
+    float readFloat();
+
+    /**
+     * Gets a 64-bit floating point number and advances the reader index.
+     */
+    double readDouble();
+
+    /**
+     * Transfers this buffer's data to the specified destination starting at
+     * the current reader index and advances the reader index.
+     */
+    Buffer readBytes(byte[] destination);
+
+    /**
+     * Transfers this buffer's data to the specified destination starting at
+     * the current reader index and advances the reader index.
+     *
+     * @param destination The destination buffer
+     * @param dstIndex the first index of the destination
+     * @param length   the number of bytes to transfer
+     */
+    Buffer readBytes(byte[] destination, int dstIndex, int length);
+
+    /**
+     * Transfers this buffer's data to the specified destination starting at
+     * the current reader index until the destination's position
+     * reaches its limit, and advances the reader index.
+     */
+    Buffer readBytes(ByteBuffer dst);
+
+    /**
+     * Transfers this buffer's data to the specified stream starting at the
+     * current reader index and advances the index.
+     *
+     * @param length the number of bytes to transfer
+     *
+     * @throws IOException
+     *         if the specified stream threw an exception during I/O
+     */
+    Buffer readBytes(OutputStream out, int length) throws IOException;
+
+    /**
+     * Sets the specified boolean at the current writer index and advances the index.
+     */
+    Buffer writeBoolean(boolean value);
+
+    /**
+     * Sets the specified byte at the current writer index and advances the index.
+     */
+    Buffer writeByte(int value);
+
+    /**
+     * Sets the specified 16-bit short integer at the current writer index and advances the index.
+     */
+    Buffer writeShort(int value);
+
+    /**
+     * Sets the specified 32-bit integer at the current writer index and advances the index.
+     */
+    Buffer writeInt(int value);
+
+    /**
+     * Sets the specified 64-bit long integer at the current writer index and advances the index.
+     */
+    Buffer writeLong(long value);
+
+    /**
+     * Sets the specified 32-bit floating point number at the current writer index and advances the index.
+     */
+    Buffer writeFloat(float value);
+
+    /**
+     * Sets the specified 64-bit floating point number at the current writer index and advances the index.
+     */
+    Buffer writeDouble(double value);
+
+    /**
+     * Transfers the specified source array's data to this buffer starting at the current writer index
+     * and advances the index.
+     */
+    Buffer writeBytes(byte[] src);
+
+    /**
+     * Transfers the specified source array's data to this buffer starting at the current writer index
+     * and advances the index.
+     */
+    Buffer writeBytes(byte[] src, int srcIndex, int length);
+
+    /**
+     * Decreases the reference count by {@code 1} and deallocates this object if the reference count reaches at
+     * {@code 0}.
+     */
+    boolean release();
+
+    /**
+     * Increases the reference count by {@code 1}.
+     */
+    Buffer retain();
+
+    /**
+     * Returns the reference count of this object.
+     */
+    int referenceCount();
+}
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/NettyBuffer.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/NettyBuffer.java
new file mode 100644
index 0000000..40162c0
--- /dev/null
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/NettyBuffer.java
@@ -0,0 +1,180 @@
+package org.apache.tinkerpop.gremlin.driver;
+
+import io.netty.buffer.ByteBuf;
+import org.apache.tinkerpop.gremlin.structure.io.Buffer;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+final class NettyBuffer implements Buffer {
+    private final ByteBuf buffer;
+
+    /**
+     * Creates a new instance.
+     * @param buffer The buffer to wrap.
+     */
+    NettyBuffer(ByteBuf buffer) {
+        if (buffer == null) {
+            throw new IllegalArgumentException("buffer can't be null");
+        }
+
+        this.buffer = buffer;
+    }
+
+    @Override
+    public int readerIndex() {
+        return this.buffer.readerIndex();
+    }
+
+    @Override
+    public Buffer readerIndex(int readerIndex) {
+        this.buffer.readerIndex(readerIndex);
+        return this;
+    }
+
+    @Override
+    public int writerIndex() {
+        return this.buffer.writerIndex();
+    }
+
+    @Override
+    public Buffer writerIndex(int writerIndex) {
+        this.buffer.writerIndex(writerIndex);
+        return this;
+    }
+
+    @Override
+    public int capacity() {
+        return this.buffer.capacity();
+    }
+
+    @Override
+    public boolean readBoolean() {
+        return this.buffer.readBoolean();
+    }
+
+    @Override
+    public byte readByte() {
+        return this.buffer.readByte();
+    }
+
+    @Override
+    public short readShort() {
+        return this.buffer.readShort();
+    }
+
+    @Override
+    public int readInt() {
+        return this.buffer.readInt();
+    }
+
+    @Override
+    public long readLong() {
+        return this.buffer.readLong();
+    }
+
+    @Override
+    public float readFloat() {
+        return this.buffer.readFloat();
+    }
+
+    @Override
+    public double readDouble() {
+        return this.buffer.readDouble();
+    }
+
+    @Override
+    public Buffer readBytes(byte[] destination) {
+        this.buffer.readBytes(destination);
+        return this;
+    }
+
+    @Override
+    public Buffer readBytes(byte[] destination, int dstIndex, int length) {
+        this.buffer.readBytes(destination, dstIndex, length);
+        return this;
+    }
+
+    @Override
+    public Buffer readBytes(ByteBuffer dst) {
+        this.buffer.readBytes(dst);
+        return this;
+    }
+
+    @Override
+    public Buffer readBytes(OutputStream out, int length) throws IOException {
+        this.buffer.readBytes(out, length);
+        return this;
+    }
+
+    @Override
+    public Buffer writeBoolean(boolean value) {
+        this.buffer.writeBoolean(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeByte(int value) {
+        this.buffer.writeByte(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeShort(int value) {
+        this.buffer.writeShort(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeInt(int value) {
+        this.buffer.writeInt(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeLong(long value) {
+        this.buffer.writeLong(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeFloat(float value) {
+        this.buffer.writeFloat(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeDouble(double value) {
+        this.buffer.writeDouble(value);
+        return this;
+    }
+
+    @Override
+    public Buffer writeBytes(byte[] src) {
+        this.buffer.writeBytes(src);
+        return this;
+    }
+
+    @Override
+    public Buffer writeBytes(byte[] src, int srcIndex, int length) {
+        this.buffer.writeBytes(src, srcIndex, length);
+        return this;
+    }
+
+    @Override
+    public boolean release() {
+        return this.buffer.release();
+    }
+
+    @Override
+    public Buffer retain() {
+        this.buffer.retain();
+        return this;
+    }
+
+    @Override
+    public int referenceCount() {
+        return this.buffer.refCnt();
+    }
+}