-
Notifications
You must be signed in to change notification settings - Fork 418
/
object_manager.py
103 lines (79 loc) · 2.89 KB
/
object_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020 Alibaba Group Holding Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from gremlin_python.driver.client import Client
class LibMeta(object):
def __init__(self, key, lib_type, lib_path):
self.key = key
self.type = lib_type
self.lib_path = lib_path
class GraphMeta(object):
def __init__(self, key, object_id, graph_def, schema_path=None):
self.key = key
self.type = "graph"
self.object_id = object_id
self.graph_def = graph_def
self.schema_path = schema_path
class InteractiveQueryManager(object):
def __init__(self, object_id, endpoint=None):
self.type = "gie_manager"
# graph object id in vineyard
self.object_id = object_id
self.endpoint = endpoint
self.client = None
def set_endpoint(self, endpoint):
self.endpoint = endpoint
def __del__(self):
if self.client is not None:
try:
self.client.close()
except Exception:
# TODO(siyuan): throws no event loop exception with tornado 5.1.1
pass
def submit(self, message, bindings=None, request_options=None):
if self.client is None:
if self.endpoint is None:
raise RuntimeError("InteractiveQueryManager's endpoint cannot be None")
self.client = Client(f"ws://{self.endpoint}/gremlin", "g")
return self.client.submit(message, bindings, request_options)
class GremlinResultSet(object):
def __init__(self, key, result_set):
self.key = key
self.type = "result_set"
self.result_set = result_set
class LearningInstanceManager(object):
def __init__(self, object_id):
self.type = "gle_manager"
self.object_id = object_id
class ObjectManager(object):
"""Manage the objects hold by the coordinator."""
def __init__(self):
self._objects = {}
def put(self, key, obj):
self._objects[key] = obj
def get(self, key):
return self._objects.get(key)
def pop(self, key):
return self._objects.pop(key, None)
def keys(self):
return self._objects.keys()
def items(self):
return self._objects.items()
def clear(self):
self._objects.clear()
def __contains__(self, key):
return key in self._objects