This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
__init__.py
executable file
·298 lines (248 loc) · 11.4 KB
/
__init__.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/python2.6
# Copyright 2020 Google LLC
#
# 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.
"""
Client for Processing MQL queries.
This class exposes two main services, read() and write(), which perform
mql reads and mql writes. Both take any varenv params as keyword args.
(There are no envelopes here).
See http://www.freebase.com/docs/web_services for more information
"""
__author__ = "[email protected] (Tyler Pirtle)"
import collections
import copy
import logging
import time
from mql import error as mql_error
from mql.graph import TcpGraphConnector
from mql.hijson import HighQuery
from mql.lojson import LowQuery
class InvalidGraphAddr(Exception):
pass
class MQLService(object):
"""Entry point for making MQL requests to the graph.
see google3/metaweb/freebase/api, which provides
the stubby interface for mql queries. It should be
the only user of this library,
"""
dollar_keys = [
"user", "privileged", "lang", "permission", "authority", "attribution"
]
MQLResult = collections.namedtuple("MQLResult", "result cost dateline cursor")
def _fix_varenv(self, env):
"""Make a copy of self.varenv, update it with env."""
dollared_env = dict([
("$" + k, v) for k, v in env.items() if k in self.dollar_keys
])
not_dollared = dict([
(k, v) for k, v in env.items() if k not in self.dollar_keys
])
if "as_of_time" in not_dollared:
not_dollared["asof"] = not_dollared["as_of_time"]
del not_dollared["as_of_time"]
# externally, it's "debug_token", pymql internal it's "tid",
# in graphd it's the "id" field. legacy stuff.
if "debug_token" in not_dollared:
not_dollared["tid"] = not_dollared["debug_token"]
del not_dollared["debug_token"]
varenv = copy.deepcopy(self.varenv)
varenv.update(dollared_env)
varenv.update(not_dollared)
# convert 'deadline' to an absolute
# unix epoch deadline and set the var
deadline = varenv.get("deadline")
if deadline:
varenv["epoch_deadline"] = time.time() + deadline
return varenv
def __init__(self, connector=None, graphd_addrs=None):
"""Initialize a MQLService with a connector."""
self.varenv = {}
if connector is not None:
self.gc = connector
elif graphd_addrs:
addr_list = list(self._parse_graphaddr(graphd_addrs))
self.gc = TcpGraphConnector(addr_list)
else:
raise Exception("Must supply an address list or connector")
self.gc.open()
low_querier = LowQuery(self.gc)
self.high_querier = HighQuery(low_querier)
def _parse_graphaddr(self, addrs):
for g in addrs:
if isinstance(g, str):
addr = g.split(":")
if len(addr) < 2:
logging.warn("graph addr [%s] is malformed (missing :port)", g)
continue
yield (addr[0], int(addr[1]))
elif isinstance(g, tuple): # better be a tuple.
yield g
else:
raise InvalidGraphAddr(g)
def get_cost(self):
return self.gc.totalcost
def reset_costs(self):
self.gc.reset_cost()
self.high_querier.reset_cost()
def read(self, query, **varenv):
"""Initiate a read of the specified query.
Args:
query: dict/json obj, mql query
varenv: dict/json obj, options, key/vals: as_of_time (optional)
timestamp string e.g. "2013-01-01T00:00:00.0000" or less precise.
graph responses will be as though the query were made at this time.
cursor (optional) None/True or string returned from a previous query
(query with a "limit": n directive) which allows a paging mechanism
for the database to provide the next set of n results. To request
the first cursor use True. deadline (optional) float, timeout in
seconds for this request, feeds into epoch_deadline escape
(optional) boolean, default True (in effect) turns on cgi escaping
of string values. lang (optional) string, lang id, default
"/lang/en" project_id (optional) string, the project id that the
request should use quota from. query_timeout_tu int or None, if
provided, each resulting graph query will be cpu user-time
constrained by this number of ms. Think
of it as limiting the work done by the db. Note: a mql query can
result in an arbitrarily large number of graph queries, so even a
small value here could result in a lot of work done.
uniqueness_failure (optional)
string: 'hard' or 'soft', default 'hard'. If a query constraint is
null or {}, 'soft' won't complain if a list is returned
write_dateline string, which must be a valid dateline returned by a
previous mql query. In proper practice, this should be a dateline
returned by a mqlwrite, thus the name 'write_dateline'. This
dateline is passed to the graph db replica and requires that the
replica poll until it is caught up to the dateline that you provide
(the dateline represents the primitive index count, i.e. the hex
value of the latest guid + 1). It has the effect of ensuring the
replica is up to date with the users last update to the database. If
the replica is not up to date, it polls, until it gets there or
times out (lagging graphs could timeout) The assumption is that the
user only needs a level of freshness up to the last write that they
did.
So, the basic pattern is: use the write_dateline they provide for all
reads, until they do a write and then
return them a new dateline. see: go/graphd-dateline debug_token
(optional)
string: unique string to aid in debugging requests
DEPRECATED: normalize, extended
Returns:
response: json object, query result
cost: dict, of various cost key/val pairs
dateline: string, see description of write_dateline above
not sure how this, the latest dateline received, is
being used. frapi doesn't pass it on. In proper use
the user should only need a new dateline when doing
a mqlwrite.
cursor: string, a cursor to be used in subsequent paging
queries.
Raises: various exceptions
"""
self.reset_costs()
env = self._fix_varenv(varenv)
if env.get("cursor"):
query = sort_query_keys(query)
logging.debug("pymql.read.start env: %s query: %s", env, query)
r = self.high_querier.read(query, env)
cost = self.get_cost()
logging.debug("pymql.read.end env: %s cost: %s", env, cost.items())
result = self.MQLResult(r, cost, env.get("dateline"), env.get("cursor"))
return result
def write(self, query, **varenv):
"""Initiate a write of the specified query using the GraphConnector.
Args:
query: dict/json obj, mql query
varenv: dict/json obj, options, key/vals: attribution (optional) string,
id of freebase attribution object This will be written as the
attribution link for primitives written authority (optional) object,
Allows requests to be made with the attribution of the current user, but
using the permissions of the user specified by this param. Another
dangerous one; improper exposure to the outside world could result in
unwanted escalation of privileges. deadline (optional) float, timeout in
seconds for this request, feeds into epoch_deadline escape (optional)
boolean, default True (in effect) turns on cgi escaping of string
values. lang (optional) string, lang id, default "/lang/en" permission
(optional) string, id of freebase permission object This param should
only be used for certain low-level operations by members of the
Freebase/Metaweb team. privileged (optional) object, this object when
passed as the privileged field enables you to pass another user id as
the authority field the write will still be attributed to 'user', but
'authority' permissions will be checked in addition to 'user'
permissions. project_id (optional) string, the project id that the
request should use quota from. query_timeout_tu int or None, if
provided, all graph queries will be cpu user-time constrained by this
number of ms. Think
of it as limiting the work done by the db. Note: a mql query can
result in an arbitrarily large number of graph queries, so even a
small value here could result in a lot of work done. user (required)
string, freebase user id e.g. "/user/brendan" write_dateline string,
see description in read method, datelines have the same effect on
writes, they ensure the db replica you are talking to is up to date
with the user's last write state before doing the write. This is
necessary since a many graph reads happen as part of a write i.e.
the write may require data from a previous write in order to
complete correctly. see go/graphd-dateline debug_token (optional)
string: unique string to aid in debugging requests
DEPRECATED: normalize, extended
Returns:
response: json object, query result
cost: dict, of various cost key/val pairs
dateline: string, see description of write_dateline above
cursor: string, a cursor to be used in subsequent paging
queries.
Raises: various exceptions
"""
self.reset_costs()
env = self._fix_varenv(varenv)
if not "$user" in env:
raise mql_error.MQLAccessError(
None, "You need to specify a user to write with.")
logging.debug("pymql.write.start env: %s query: %s", env, query)
r = self.high_querier.write(query, env)
cost = self.get_cost()
logging.debug("pymql.write.end env: %s cost: %s", env, cost.items())
result = self.MQLResult(r, cost, env.get("write_dateline"),
env.get("cursor"))
return result
def normalize(self, query):
"""Normalize the specified query. TODO(rtp) What does this actually do?"""
self.reset_costs()
r = self.read(query, normalize_only=True)
result = self.MQLResult(r.result, r.cost, r.dateline, r.cursor)
return result
def sort_query_keys(part):
"""sort keys in place.
We do this to every mqlread with a cursor because
graphd relies on GQL query string order to
maintain the state of the cursor.
This calls itself recursively sorting keys
in any ply of the query that is a dict
Args:
part: any ply of your dict
Returns:
an OrderedDict where all keys have been sorted
in every ply of the query.
"""
if isinstance(part, list):
new_d = []
for item in part:
new_d.append(sort_query_keys(item))
return new_d
elif isinstance(part, dict):
new_d = collections.OrderedDict()
for k in sorted(part.keys()):
new_d[k] = sort_query_keys(part[k])
return new_d
else:
return part