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

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

commit dda67962bc226826c42c024d67b0106db7ae1146
Author: Jorge Bay Gondra <[email protected]>
AuthorDate: Tue Nov 27 15:59:50 2018 +0100

    Start sample custom type implementation
---
 docs/src/dev/io/graphbinary.asciidoc               |  2 +-
 .../driver/ser/binary/types/sample/SamplePair.java |  8 ++
 .../binary/types/sample/SamplePairSerializer.java  | 14 +++-
 .../sample/{SamplePair.java => SamplePerson.java}  | 30 +++----
 .../types/sample/SamplePersonSerializer.java       | 93 ++++++++++++++++++++++
 5 files changed, 128 insertions(+), 19 deletions(-)

diff --git a/docs/src/dev/io/graphbinary.asciidoc 
b/docs/src/dev/io/graphbinary.asciidoc
index 8462be7..fe8b54f 100644
--- a/docs/src/dev/io/graphbinary.asciidoc
+++ b/docs/src/dev/io/graphbinary.asciidoc
@@ -463,7 +463,7 @@ Type Info: `{name}{custom_type_info}`
 Where:
 
 - `{name}` is a `String` containing the implementation specific text 
identifier of the custom type.
-- `{custom_type_info}` is an optional sequence of bytes representing the 
additional type information, specially useful
+- `{custom_type_info}` is a `ByteBuffer` representing the additional type 
information, specially useful
 for complex custom types.
 
 Value format: `{blob}`
diff --git 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
index 5a729cd..9803b43 100644
--- 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
+++ 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
@@ -18,6 +18,8 @@
  */
 package org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample;
 
+import java.util.Collection;
+
 /**
  * A sample custom data type, trying to demonstrate the possibility to have 
arbitrary complex types and null
  * values without loosing type information.
@@ -28,7 +30,13 @@ class SamplePair<K, V> {
     private final Info info;
 
     class Info {
+        private final Class<?> keyType;
+        private final Collection<Class<?>> keySubTypes;
 
+        Info(Class<?> keyType, Collection<Class<?>> keySubTypes) {
+            this.keyType = keyType;
+            this.keySubTypes = keySubTypes;
+        }
     }
 
     SamplePair(K key, V value, SamplePair.Info info) {
diff --git 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePairSerializer.java
 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePairSerializer.java
index 36db02c..e156217 100644
--- 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePairSerializer.java
+++ 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePairSerializer.java
@@ -31,7 +31,7 @@ import java.nio.charset.StandardCharsets;
 
 class SamplePairSerializer implements TypeSerializer<SamplePair> {
     private final static byte[] dataTypeBuffer = new byte[] { 
DataType.CUSTOM.getCodeByte() };
-    private final byte[] dataTypeNameBuffer = 
"SAMPLE_PAIR".getBytes(StandardCharsets.UTF_8);
+    private final byte[] dataTypeNameBuffer = 
"SAMPLEPAIR".getBytes(StandardCharsets.UTF_8);
 
     @Override
     public SamplePair read(ByteBuf buffer, GraphBinaryReader context) throws 
SerializationException {
@@ -59,6 +59,18 @@ class SamplePairSerializer implements 
TypeSerializer<SamplePair> {
         );
     }
 
+    public ByteBuf writeNull(ByteBufAllocator allocator, Object information, 
GraphBinaryWriter context) {
+        SamplePair.Info info = (SamplePair.Info) information;
+        // Write type code:  "CUSTOM"
+        // Write Type info: "SAMPLEPAIR"
+        // value flag null
+        // 2 fully qualified null values.
+
+        // TODO: How do we get the target serializer / type?
+        //context.writeFullyQualifiedNull(DataType.CUSTOM, )
+        return null;
+    }
+
     @Override
     public ByteBuf writeValue(SamplePair value, ByteBufAllocator allocator, 
GraphBinaryWriter context, boolean nullable) throws SerializationException {
         throw new SerializationException("SamplePairSerializer can't write the 
value without type information");
diff --git 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePerson.java
similarity index 64%
copy from 
gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
copy to 
gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePerson.java
index 5a729cd..2784623 100644
--- 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePair.java
+++ 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePerson.java
@@ -18,30 +18,26 @@
  */
 package org.apache.tinkerpop.gremlin.driver.ser.binary.types.sample;
 
+import java.util.Date;
+
 /**
- * A sample custom data type, trying to demonstrate the possibility to have 
arbitrary complex types and null
- * values without loosing type information.
+ * A sample custom data type containing few properties.
  */
-class SamplePair<K, V> {
-    private final K key;
-    private final V value;
-    private final Info info;
-
-    class Info {
+class SamplePerson {
+    private final String name;
+    private final Date birthDate;
 
-    }
+    SamplePerson(String name, Date birthDate) {
 
-    SamplePair(K key, V value, SamplePair.Info info) {
-        this.key = key;
-        this.value = value;
-        this.info = info;
+        this.name = name;
+        this.birthDate = birthDate;
     }
 
-    public K getKey() {
-        return key;
+    public String getName() {
+        return name;
     }
 
-    public V getValue() {
-        return value;
+    public Date getBirthDate() {
+        return birthDate;
     }
 }
diff --git 
a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePersonSerializer.java
 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePersonSerializer.java
new file mode 100644
index 0000000..f4384ef
--- /dev/null
+++ 
b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ser/binary/types/sample/SamplePersonSerializer.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.apache.tinkerpop.gremlin.driver.ser.binary.types.sample;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.buffer.Unpooled;
+import org.apache.tinkerpop.gremlin.driver.ser.SerializationException;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.DataType;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.GraphBinaryReader;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.GraphBinaryWriter;
+import org.apache.tinkerpop.gremlin.driver.ser.binary.TypeSerializer;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
+
+/**
+ * A sample custom type serializer.
+ */
+class SamplePersonSerializer implements TypeSerializer<SamplePerson> {
+    private final static byte[] dataTypeBuffer = new byte[] { 
DataType.CUSTOM.getCodeByte() };
+    private final byte[] dataTypeNameBuffer = 
"SAMPLEPERSON".getBytes(StandardCharsets.UTF_8);
+    private final byte[] typeInfoBuffer = new byte[] { 0, 0, 0, 0 };
+
+    @Override
+    public SamplePerson read(ByteBuf buffer, GraphBinaryReader context) throws 
SerializationException {
+        // {custom type info}, {value_flag} and {value}
+        // No custom_type_info
+        assert buffer.readInt() == 0;
+
+        final byte valueFlag = buffer.readByte();
+        if ((valueFlag & 1) == 1) {
+            return null;
+        }
+
+        return new SamplePerson(
+                context.readValue(buffer, String.class, false), 
context.readValue(buffer, Date.class,false));
+    }
+
+    @Override
+    public SamplePerson readValue(ByteBuf buffer, GraphBinaryReader context, 
boolean nullable) throws SerializationException {
+        throw new SerializationException("SamplePersonSerializer can not read 
a value without type information");
+    }
+
+    @Override
+    public ByteBuf write(SamplePerson value, ByteBufAllocator allocator, 
GraphBinaryWriter context) throws SerializationException {
+        if (value == null) {
+            return allocator.compositeBuffer(4).addComponents(true,
+                    Unpooled.wrappedBuffer(dataTypeBuffer),
+                    Unpooled.wrappedBuffer(dataTypeNameBuffer),
+                    // No custom_type_info
+                    Unpooled.wrappedBuffer(typeInfoBuffer),
+                    // Value_flag null set
+                    context.getValueFlagNull()
+            );
+        }
+
+        ByteBuf valueBuffer = allocator.compositeBuffer(2).addComponents(true,
+                context.writeValue(value.getName(), allocator, false),
+                context.writeValue(value.getBirthDate(), allocator, false));
+
+        return allocator.compositeBuffer(6).addComponents(true,
+                Unpooled.wrappedBuffer(dataTypeBuffer),
+                Unpooled.wrappedBuffer(dataTypeNameBuffer),
+                // No custom_type_info
+                Unpooled.wrappedBuffer(typeInfoBuffer),
+                // value_flag empty
+                context.getValueFlagNone(),
+                allocator.buffer(4).writeInt(valueBuffer.readableBytes()),
+                valueBuffer);
+    }
+
+    @Override
+    public ByteBuf writeValue(SamplePerson value, ByteBufAllocator allocator, 
GraphBinaryWriter context, boolean nullable) throws SerializationException {
+        throw new SerializationException("SamplePersonSerializer can not write 
a value without type information");
+    }
+}

Reply via email to