You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/09/04 13:22:13 UTC

[tinkerpop] 03/40: Added int graphbinary serialization for python

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

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

commit 897874256ef90345e380ecca0ed806708dfd591b
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Mon Jul 22 13:51:55 2019 -0400

    Added int graphbinary serialization for python
    
    Included tests for heterogenous lists now that we have two primitives that work.
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 25 ++++++++++++++++++----
 .../tests/structure/io/test_graphbinaryV1.py       | 10 +++++++++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
index 5e06da4..9c9ab30 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
@@ -48,6 +48,7 @@ _deserializers = {}
 
 
 class DataType(Enum):
+    int = 0x01
     string = 0x03
     list = 0x09
 
@@ -131,6 +132,10 @@ class _GraphBinaryTypeIO(object):
         return ba
 
     @classmethod
+    def read_int(cls, buff):
+        return struct.unpack(">i", buff.read(4))[0]
+
+    @classmethod
     def unmangleKeyword(cls, symbol):
         return cls.symbolMap.get(symbol, symbol)
 
@@ -141,6 +146,20 @@ class _GraphBinaryTypeIO(object):
         raise NotImplementedError()
 
 
+class IntIO(_GraphBinaryTypeIO):
+
+    python_type = IntType
+    graphbinary_type = DataType.int
+
+    @classmethod
+    def dictify(cls, n, writer):
+        return cls.as_bytes(cls.graphbinary_type, None, struct.pack(">i", n))
+
+    @classmethod
+    def objectify(cls, b, reader):
+        return cls.read_int(b)
+
+
 class StringIO(_GraphBinaryTypeIO):
 
     python_type = str
@@ -152,8 +171,7 @@ class StringIO(_GraphBinaryTypeIO):
 
     @classmethod
     def objectify(cls, b, reader):
-        size = struct.unpack(">i", b.read(4))[0]
-        return b.read(size)
+        return b.read(cls.read_int(b))
 
 
 class ListIO(_GraphBinaryTypeIO):
@@ -171,8 +189,7 @@ class ListIO(_GraphBinaryTypeIO):
 
     @classmethod
     def objectify(cls, buff, reader):
-        x = buff.read(4)
-        size = struct.unpack(">i", x)[0]
+        size = cls.read_int(buff)
         the_list = []
         while size > 0:
             the_list.append(reader.readObject(buff))
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
index 1fd2a19..b95b0e0 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
@@ -43,6 +43,11 @@ class TestGraphSONWriter(object):
     graphbinary_writer = GraphBinaryWriter()
     graphbinary_reader = GraphBinaryReader()
 
+    def test_int(self):
+        x = 100
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
     def test_string(self):
         x = "serialize this!"
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
@@ -52,3 +57,8 @@ class TestGraphSONWriter(object):
         x = ["serialize this!", "serialize that!", "stop telling me what to serialize"]
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
+
+    def test_heterogeneous_list(self):
+        x = ["serialize this!", 0, "serialize that!", 1, "stop telling me what to serialize", 2]
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output