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

[tinkerpop] 15/40: Added bytecode graphbinary serialization 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 029c494caea5e04a4aa26c248c09ad856f1ed788
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Thu Aug 1 11:30:25 2019 -0400

    Added bytecode graphbinary serialization to python
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 60 +++++++++++++++++++++-
 .../tests/structure/io/test_graphbinaryV1.py       | 12 ++++-
 .../jython/tests/structure/io/test_graphsonV3d0.py |  3 +-
 3 files changed, 71 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 8eb323b..ec8b02d 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
@@ -71,8 +71,8 @@ class DataType(Enum):
     vertex = 0x11
     vertexproperty = 0x12
     barrier = 0x13
-    binding = 0x14      
-    bytecode = 0x15     # todo
+    binding = 0x14
+    bytecode = 0x15
     cardinality = 0x16
     column = 0x17
     direction = 0x18
@@ -588,3 +588,59 @@ class BindingIO(_GraphBinaryTypeIO):
     def objectify(cls, b, reader):
         return Binding(cls.read_string(b), reader.readObject(b))
 
+
+class BytecodeIO(_GraphBinaryTypeIO):
+    python_type = Bytecode
+    graphbinary_type = DataType.bytecode
+    
+    @classmethod
+    def dictify(cls, obj, writer):
+        ba = bytearray([cls.graphbinary_type.value])
+        ba.extend(struct.pack(">i", len(obj.step_instructions)))
+        for inst in obj.step_instructions:
+            inst_name, inst_args = inst[0], inst[1:] if len(inst) > 1 else []
+            ba.extend(cls.string_as_bytes(inst_name))
+            ba.extend(struct.pack(">i", len(inst_args)))
+            for arg in inst_args:
+                ba.extend(writer.writeObject(arg))
+
+        ba.extend(struct.pack(">i", len(obj.source_instructions)))
+        for inst in obj.source_instructions:
+            inst_name, inst_args = inst[0], inst[1:] if len(inst) > 1 else []
+            ba.extend(cls.string_as_bytes(inst_name))
+            ba.extend(struct.pack(">i", len(inst_args)))
+            for arg in inst_args:
+                ba.extend(writer.writeObject(arg))
+                
+        return ba
+    
+    @classmethod
+    def objectify(cls, b, reader):
+        bytecode = Bytecode()
+        
+        step_count = cls.read_int(b)
+        ix = 0
+        while ix < step_count:
+            inst = [cls.read_string(b)]
+            inst_ct = cls.read_int(b)
+            iy = 0
+            while iy < inst_ct:
+                inst.append(reader.readObject(b))
+                iy += 1
+            bytecode.step_instructions.append(inst)
+            ix += 1
+
+        source_count = cls.read_int(b)
+        ix = 0
+        while ix < source_count:
+            inst = [cls.read_string(b)]
+            inst_ct = cls.read_int(b)
+            iy = 0
+            while iy < inst_ct:
+                inst.append(reader.readObject(b))
+                iy += 1
+            bytecode.source_instructions.append(inst)
+            ix += 1
+
+        return bytecode
+
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 f3ee2c8..c9ce87e 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
@@ -31,7 +31,7 @@ import six
 from gremlin_python.statics import timestamp, long
 from gremlin_python.structure.graph import Vertex, Edge, Property, VertexProperty, Graph, Path
 from gremlin_python.structure.io.graphbinaryV1 import GraphBinaryWriter, GraphBinaryReader, DataType
-from gremlin_python.process.traversal import P, Barrier, Binding
+from gremlin_python.process.traversal import P, Barrier, Binding, Bytecode
 from gremlin_python.process.strategies import SubgraphStrategy
 from gremlin_python.process.graph_traversal import __
 
@@ -159,3 +159,13 @@ class TestGraphSONWriter(object):
         x = Binding("name", "marko")
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
+
+    def test_bytecode(self):
+        x = Bytecode()
+        x.source_instructions.append(["withStrategies", "SubgraphStrategy"])
+        x.step_instructions.append(["V", 1, 2, 3])
+        x.step_instructions.append(["out"])
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
+
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
index 2b9331f..7cc8d68 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphsonV3d0.py
@@ -409,7 +409,8 @@ class TestGraphSONWriter(object):
         assert result == json.loads(self.graphson_writer.writeObject(P.within(1)))
 
     def test_strategies(self):
-        # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the Gremlin traversal machine (yet)
+        # we have a proxy model for now given that we don't want to have to have g:XXX all registered on the 
+        # Gremlin traversal machine (yet)
         assert {"@type": "g:SubgraphStrategy", "@value": {}} == json.loads(
             self.graphson_writer.writeObject(SubgraphStrategy))
         assert {"@type": "g:SubgraphStrategy", "@value": {