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:20 UTC

[tinkerpop] 10/40: Added uuid/edge to python graphbinary derser

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 9a7f3a81a8c2ba47e2675ef8525225ad41b2dc4b
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Wed Jul 31 13:19:05 2019 -0400

    Added uuid/edge to python graphbinary derser
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 56 +++++++++++++++++++++-
 .../tests/structure/io/test_graphbinaryV1.py       | 10 ++++
 2 files changed, 65 insertions(+), 1 deletion(-)

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 3d7d7da..76dfaee 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
@@ -60,6 +60,8 @@ class DataType(Enum):
     list = 0x09
     map = 0x0a
     set = 0x0b
+    uuid = 0x0c
+    edge = 0x0d
 
 
 class GraphBinaryTypeType(type):
@@ -141,10 +143,18 @@ class _GraphBinaryTypeIO(object):
         return ba
 
     @classmethod
+    def string_as_bytes(cls, s):
+        return cls.as_bytes(None, len(s), s.encode("utf-8"))
+
+    @classmethod
     def read_int(cls, buff):
         return struct.unpack(">i", buff.read(4))[0]
 
     @classmethod
+    def read_string(cls, buff):
+        return buff.read(cls.read_int(buff)).decode("utf-8")
+
+    @classmethod
     def unmangleKeyword(cls, symbol):
         return cls.symbolMap.get(symbol, symbol)
 
@@ -288,7 +298,7 @@ class StringIO(_GraphBinaryTypeIO):
 
     @classmethod
     def objectify(cls, b, reader):
-        return b.read(cls.read_int(b)).decode("utf-8")
+        return cls.read_string(b)
 
 
 class ListIO(_GraphBinaryTypeIO):
@@ -350,3 +360,47 @@ class MapIO(_GraphBinaryTypeIO):
             size = size - 1
 
         return the_dict
+
+
+class UuidIO(_GraphBinaryTypeIO):
+
+    python_type = uuid.UUID
+    graphbinary_type = DataType.uuid
+
+    @classmethod
+    def dictify(cls, obj, writer):
+        ba = bytearray([cls.graphbinary_type.value])
+        ba.extend(obj.bytes)
+        return ba
+
+    @classmethod
+    def objectify(cls, b, reader):
+        return uuid.UUID(bytes=b.read(16))
+
+
+class EdgeIO(_GraphBinaryTypeIO):
+
+    python_type = Edge
+    graphbinary_type = DataType.edge
+
+    @classmethod
+    def dictify(cls, obj, writer):
+        ba = bytearray([cls.graphbinary_type.value])
+        ba.extend(writer.writeObject(obj.id))
+        ba.extend(cls.string_as_bytes(obj.label))
+        ba.extend(writer.writeObject(obj.inV.id))
+        ba.extend(cls.string_as_bytes(obj.inV.label))
+        ba.extend(writer.writeObject(obj.outV.id))
+        ba.extend(cls.string_as_bytes(obj.outV.label))
+        ba.extend([0xfe])
+        ba.extend([0xfe])
+        return ba
+
+    @classmethod
+    def objectify(cls, b, reader):
+        edgeid = reader.readObject(b)
+        edgelbl = cls.read_string(b)
+        edge = Edge(edgeid, Vertex(reader.readObject(b), cls.read_string(b)),
+                    edgelbl, Vertex(reader.readObject(b), cls.read_string(b)))
+        b.read(2)
+        return edge
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 54acb55..f287c01 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
@@ -119,3 +119,13 @@ class TestGraphSONWriter(object):
              987: ["go", "deep", {"here": "!"}]}
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
+        
+    def test_uuid(self):
+        x = uuid.UUID("41d2e28a-20a4-4ab0-b379-d810dede3786")
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
+    def test_edge(self):
+        x = Edge(123, Vertex(1, 'person'), "developed", Vertex(10, "software"))
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output