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/08/09 19:13:02 UTC

[tinkerpop] 04/23: Added Long graphbinary support to 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 0ace314f566cd27905ef24c427b73a96c4bf96a3
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Mon Jul 22 14:14:01 2019 -0400

    Added Long graphbinary support to python
    
    Fixed bug in string serialization that only occurred on python 3.x
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 30 +++++++++++++++++-----
 .../tests/structure/io/test_graphbinaryV1.py       |  5 ++++
 2 files changed, 28 insertions(+), 7 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 9c9ab30..225b399 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
@@ -49,6 +49,7 @@ _deserializers = {}
 
 class DataType(Enum):
     int = 0x01
+    long = 0x02
     string = 0x03
     list = 0x09
 
@@ -146,18 +147,33 @@ class _GraphBinaryTypeIO(object):
         raise NotImplementedError()
 
 
-class IntIO(_GraphBinaryTypeIO):
+class LongIO(_GraphBinaryTypeIO):
 
-    python_type = IntType
-    graphbinary_type = DataType.int
+    python_type = LongType
+    graphbinary_type = DataType.long
+    byte_format = ">q"
 
     @classmethod
     def dictify(cls, n, writer):
-        return cls.as_bytes(cls.graphbinary_type, None, struct.pack(">i", n))
+        if n < -9223372036854775808 or n > 9223372036854775807:
+            raise Exception("TODO: don't forget bigint")
+        else:
+            return cls.as_bytes(cls.graphbinary_type, None, struct.pack(cls.byte_format, n))
 
     @classmethod
-    def objectify(cls, b, reader):
-        return cls.read_int(b)
+    def objectify(cls, buff, reader):
+        return struct.unpack(">q", buff.read(8))[0]
+
+
+class IntIO(LongIO):
+
+    python_type = IntType
+    graphbinary_type = DataType.int
+    byte_format = ">i"
+
+    @classmethod
+    def objectify(cls, buff, reader):
+        return cls.read_int(buff)
 
 
 class StringIO(_GraphBinaryTypeIO):
@@ -171,7 +187,7 @@ class StringIO(_GraphBinaryTypeIO):
 
     @classmethod
     def objectify(cls, b, reader):
-        return b.read(cls.read_int(b))
+        return b.read(cls.read_int(b)).decode("utf-8")
 
 
 class ListIO(_GraphBinaryTypeIO):
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 b95b0e0..766544d 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
@@ -48,6 +48,11 @@ class TestGraphSONWriter(object):
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
 
+    def test_long(self):
+        x = long(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))