Skip to content

Commit 45f79f2

Browse files
committed
[~] WIP
1 parent 583018c commit 45f79f2

File tree

4 files changed

+200
-4
lines changed

4 files changed

+200
-4
lines changed

janusgraph_python/driver/serializer.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from gremlin_python.driver.serializer import GraphSONSerializersV3d0
16-
from janusgraph_python.structure.io import graphsonV3d0
15+
from gremlin_python.driver.serializer import GraphSONSerializersV3d0, GraphBinarySerializersV1
16+
from janusgraph_python.structure.io import graphsonV3d0, graphbinaryV1
1717

1818
class JanusGraphSONSerializersV3d0(GraphSONSerializersV3d0):
1919
"""Message serializer for GraphSON 3.0 extended with JanusGraph-specific types"""
2020
def __init__(self):
2121
reader = graphsonV3d0.JanusGraphSONReader()
2222
writer = graphsonV3d0.JanusGraphSONWriter()
23-
super(GraphSONSerializersV3d0, self).__init__(reader, writer)
23+
super(GraphSONSerializersV3d0, self).__init__(reader, writer)
24+
25+
class JanusGraphBinarySerializersV1(GraphBinarySerializersV1):
26+
"""Message serializer for GraphBinary 1.0 extended with JanusGraph-specific types"""
27+
def __init__(self):
28+
reader = graphbinaryV1.JanusGraphBinaryReader()
29+
writer = graphbinaryV1.JanusGraphBinaryWriter()
30+
super().__init__(reader, writer)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Copyright 2023 JanusGraph-Python Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from aenum import Enum
16+
from gremlin_python.statics import ListType
17+
from gremlin_python.structure.io.graphbinaryV1 import (
18+
_GraphBinaryTypeIO, EdgeIO, StringIO, GraphBinaryReader, GraphBinaryWriter,
19+
int32_pack, uint64_pack, int8_pack, _make_packer,
20+
uint8_pack, DataType
21+
)
22+
from gremlin_python.structure.graph import Edge, Vertex
23+
from janusgraph_python.process.traversal import _JanusGraphP, RelationIdentifier
24+
25+
uint16_pack, uint16_unpack = _make_packer('>H')
26+
uint32_pack, uint32_unpack = _make_packer('>I')
27+
28+
class JanusGraphDataType(Enum):
29+
janusgraphp = 0x1002
30+
janusgraphrelationidentifier = 0x1001
31+
32+
class JanusGraphBinaryReader(GraphBinaryReader):
33+
def __init__(self):
34+
# register JanusGraph-specific RelationIdentifier deserializer
35+
deserializer_map = {
36+
#JanusGraphDataType.janusgraphrelationidentifier: JanusGraphRelationIdentifierIO
37+
}
38+
GraphBinaryReader.__init__(self, deserializer_map)
39+
40+
class JanusGraphBinaryWriter(GraphBinaryWriter):
41+
def __init__(self):
42+
# register JanusGraph-specific RelationIdentifier and text-predicate serializer
43+
serializer_map = [
44+
#(RelationIdentifier, JanusGraphRelationIdentifierIO),
45+
(_JanusGraphP, JanusGraphPSerializer)
46+
]
47+
GraphBinaryWriter.__init__(self, serializer_map)
48+
49+
50+
class JanusGraphPSerializer(_GraphBinaryTypeIO):
51+
graphbinary_type = JanusGraphDataType.janusgraphp
52+
python_type = _JanusGraphP
53+
54+
@classmethod
55+
def prefix_bytes(cls, graphbin_type, as_value=False, nullable=True, to_extend=None):
56+
print("HELLOKA")
57+
if to_extend is None:
58+
to_extend = bytearray()
59+
60+
# use the custom type code
61+
if not as_value:
62+
to_extend += uint8_pack(DataType.custom.value)
63+
64+
# for type_info use the custom type code
65+
to_extend += uint32_pack(graphbin_type.value)
66+
67+
if nullable:
68+
to_extend += int8_pack(0)
69+
70+
return to_extend
71+
72+
@classmethod
73+
def dictify(cls, obj, writer, to_extend, as_value=False, nullable=True):
74+
cls.prefix_bytes(cls.graphbinary_type, as_value, nullable, to_extend)
75+
76+
StringIO.dictify(obj.operator, writer, to_extend, True, False)
77+
78+
args = []
79+
if obj.other is None:
80+
if isinstance(obj.value, ListType):
81+
args = obj.value
82+
else:
83+
args.append(obj.value)
84+
else:
85+
args.append(obj.value)
86+
args.append(obj.other)
87+
88+
to_extend.extend(int32_pack(len(args)))
89+
for a in args:
90+
writer.to_dict(a, to_extend)
91+
92+
return to_extend
93+
94+
# class JanusGraphRelationIdentifierIO(_GraphBinaryTypeIO):
95+
# graphbinary_type = JanusGraphDataType.janusgraphrelationidentifier
96+
# python_type = RelationIdentifier
97+
98+
# @classmethod
99+
# def objectify(cls, buff, reader, nullable=True):
100+
# return cls.is_null(buff, reader, cls._read_edge, nullable)
101+
102+
# @classmethod
103+
# def _read_edge(cls, b, r):
104+
# edgeid = r.read_object(b)
105+
# edgelbl = r.to_object(b, DataType.string, False)
106+
# inv = Vertex(r.read_object(b), r.to_object(b, DataType.string, False))
107+
# outv = Vertex(r.read_object(b), r.to_object(b, DataType.string, False))
108+
# b.read(2)
109+
# props = r.read_object(b)
110+
# # null properties are returned as empty lists
111+
# properties = [] if props is None else props
112+
# edge = Edge(edgeid, outv, edgelbl, inv, properties)
113+
# return edge
114+
115+
# @classmethod
116+
# def objectify(cls, l, reader):
117+
# return RelationIdentifier.from_string(l['relationId'])
118+
119+
120+
121+
# @classmethod
122+
# def dictify(cls, relation_identifier, writer):
123+
# out = { "relationId": relation_identifier.string_representation }
124+
# return GraphSONUtil.typed_value("RelationIdentifier", out, "janusgraph")

tests/integration/conftest.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
from testcontainers.core.container import DockerContainer
2323
from gremlin_python.process.anonymous_traversal import traversal
2424
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
25-
from janusgraph_python.driver.serializer import JanusGraphSONSerializersV3d0
25+
from janusgraph_python.driver.serializer import JanusGraphSONSerializersV3d0, JanusGraphBinarySerializersV1
26+
27+
from gremlin_python.driver.serializer import GraphBinarySerializersV1
2628

2729
current_path = pathlib.Path(__file__).parent.resolve()
2830
JANUSGRAPH_REPOSITORY = None
@@ -87,6 +89,27 @@ def graph_connection_graphson(request, graph_container):
8789
container = graph_container(request)
8890
return graph_connection(request, container, JanusGraphSONSerializersV3d0())
8991

92+
@fixture(scope='session')
93+
def graph_connection_graphbinary(request):
94+
connection = DriverRemoteConnection(
95+
f'ws://localhost:8182/gremlin',
96+
'gp_traversal',
97+
message_serializer=JanusGraphBinarySerializersV1(),
98+
#message_serializer=GraphBinarySerializersV1(),
99+
username="jg_user",
100+
password="jg_password"
101+
)
102+
103+
def close_connection():
104+
connection.close()
105+
106+
request.addfinalizer(close_connection)
107+
108+
g = traversal().with_remote(connection)
109+
110+
return g
111+
112+
90113
def graph_connection(request, graph_container, serializer):
91114
"""
92115
Fixture for creating connection with given serializer to the
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2023 JanusGraph-Python Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pytest import fixture, mark
16+
17+
from integration.RelationIdentifier_test import _RelationIdentifierSerializer, _RelationIdentifierDeserializer
18+
from integration.Text_test import _TextTests
19+
from ..conftest import JANUSGRAPH_VERSION_PARAMS
20+
21+
# parametrize all integration tests to run against various JanusGraph versions
22+
pytestmark = mark.parametrize(
23+
"graph_connection_graphbinary", JANUSGRAPH_VERSION_PARAMS, indirect=True
24+
)
25+
26+
# class TestGraphSONRelationIdentifierSerializer(_RelationIdentifierSerializer):
27+
# @fixture(autouse=True)
28+
# def _graph_connection_graphson(self, graph_connection_graphson):
29+
# # setting up 'g' variable so parent class's methods can use it
30+
# self.g = graph_connection_graphson
31+
32+
# class TestGraphSONRelationIdentifierDeserializer(_RelationIdentifierDeserializer):
33+
# @fixture(autouse=True)
34+
# def _graph_connection_graphson(self, graph_connection_graphson):
35+
# # setting up 'g' variable so parent class's methods can use it
36+
# self.g = graph_connection_graphson
37+
38+
class TestGraphBinaryText(_TextTests):
39+
@fixture(autouse=True)
40+
def _graph_connection_graphbinary(self, graph_connection_graphbinary):
41+
# setting up 'g' variable so parent class's methods can use it
42+
self.g = graph_connection_graphbinary

0 commit comments

Comments
 (0)