|
| 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") |
0 commit comments