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
|
#!/usr/bin/python
import os
import json
import re
import logging
import traceback
import urllib2
import neo4j_util as dbu
log = logging.getLogger('rhizi')
class DB_op(object):
"""
tx wrapped DB operation possibly composing multiple DB queries
"""
def __init__(self):
self.s_id = 0 # statement id counter
self.id_to_statement_map = {} # zero based id to statement map
self.tx_id = None
self.tx_commit_url = None # cached from response to tx begin
def parse_tx_id(self, tx_commit_url):
m = re.search('/(?P<id>\d+)/commit$', tx_commit_url)
id_str = m.group('id')
self.tx_id = int(id_str)
def add_statement(self, cypher_query, params={}):
"""
add a DB query language statement
@return: statement id
"""
ret = self.s_id
self.id_to_statement_map[self.s_id] = dbu.statement_to_REST_form(cypher_query, params)
self.s_id = self.s_id + 1
return ret
def __iter__(self):
"""
iterate over (statement_index, statement, statement_result)
note: statement_index is zero based
TODO: support statement_result
"""
i = 0
for s in self.statement_set:
yield (i, s, None)
i = i + 1
def extract_single_query_response_data(self, q, data):
"""
DB op can issue complex sets of quries all at once - this helper method
assists in parsing response data from a single query.
"""
ret = []
r_0 = data['results'][0]
for row in r_0['data']:
ret.append(row['row'][0])
return ret
def on_success(self, data):
pass
def on_error(self):
pass
class DBO_add_node_set(DB_op):
def __init__(self, node_map, input_to_DB_property_map=lambda _: _):
"""
DB op: add node set
@param node_map: node-type to node list map
@input_to_DB_property_map: optional function which takes a map of input properties and returns a map of DB properties - use to map input schemas to DB schemas
"""
super(DBO_add_node_set, self).__init__()
for k, v in node_map.iteritems(): # do some type sanity checking
assert isinstance(k, basestring)
assert isinstance(v, list)
self.node_map = node_map
for type, n_set in self.node_map.items():
q = "create (n:{0} {{prop_dict}}) return id(n)".format(type)
for n_prop_dict in n_set:
p = {'prop_dict' : input_to_DB_property_map(n_prop_dict)}
self.add_statement(q, p)
def on_success(self, data):
# [!] fragile - parse results
# sample input: dict: {u'errors': [], u'results': [{u'data': [{u'row': [20]}], u'columns': [u'id(n)']}]}
id_set = []
for r in data['results']:
columns = r['columns']
for k in r['data']:
nid = k['row'][0]
id_set.append(nid)
log.debug('node-set added: ids: ' + str(id_set))
return id_set
class DBO_load_node_set_by_DB_id(DB_op):
def __init__(self, id_set):
"""
load a set of nodes whose DB id is in id_set
@return: loaded node set or an empty set if no match was found
"""
super(DBO_load_node_set_by_DB_id, self).__init__()
q = "match (n) where id(n) in {id_set} return n"
self.add_statement(q, { 'id_set': id_set})
def on_success(self, data):
log.debug('loaded node set: ' + str(data))
return self.extract_single_query_response_data(self.statement_set[0], data)
class DBO_load_node_set(DB_op):
def __init__(self, filter_type=None, filter_attr_map=None):
"""
load a set of nodes according to filter_attr_map
@param filter_attr_map: is a filter_key to filter_value_set map of
attributes to match against, eg.:
{ 'id':[0,1], 'color: ['red','blue'] }
@param filter_type: node type filter
@return: loaded node set or an empty set if no match was found
"""
filter_str = dbu.where_clause_from_filter_attr_map()
super(DBO_load_node_set, self).__init__()
q = "match (n) {0} return n".format(filter_str)
self.add_statement(q, params=filter_attr_map)
def on_success(self, data):
log.debug('loaded node set: ' + str(data))
return self.extract_single_query_response_data(self.statement_set[0], data)
class DBO_load_node_set_by_id_attribute(DBO_load_node_set):
def __init__(self, id_set):
"""
convenience op: load a set of nodes by their 'id' attribute != DB node id
"""
assert isinstance(id_set, list)
super(DBO_load_node_set_by_id_attribute, self).__init__({'id': id_set})
class DBO_load_link_id_set(DB_op):
def __init__(self, filter_type=None, filter_attr_map=None):
"""
load a set of link ids
@param filter_type: link type filter
@param filter_attr_map: is a filter_key to filter_value_set map of
attributes to match link properties against
@return: a set of loaded link ids
"""
filter_str = dbu.where_clause_from_filter_attr_map()
class DB_Driver_Base():
pass
class DB_Driver_REST(DB_Driver_Base):
def __init__(self, db_base_url):
self.tx_base_url = db_base_url + '/db/data/transaction'
def begin_tx(self, op):
tx_open_url = self.tx_base_url
try:
#
# [!] neo4j seems picky about receiving an additional empty statement list
#
data = data = dbu.statement_set_to_REST_form([])
ret = dbu.post_neo4j(tx_open_url, data)
tx_commit_url = ret['commit']
op.parse_tx_id(tx_commit_url)
log.debug('tx-open: id: {0}, commit-url: {1}'.format(op.tx_id, tx_commit_url))
except Exception as e:
raise Exception('failed to open transaction:' + e.message)
def exex_op_statements(self, op):
tx_url = "{0}/{1}".format(self.tx_base_url, op.tx_id)
statement_set = dbu.statement_set_to_REST_form(op.statement_set)
try:
ret = dbu.post_neo4j(tx_url, statement_set)
self.log_committed_queries(statement_set)
return ret
except Exception as e:
raise Exception('failed exec op statements: err: {0}, url: {1}'.format(e.message, tx_url))
def commit_tx(self, op):
tx_commit_url = "{0}/{1}/commit".format(self.tx_base_url, op.tx_id)
try:
#
# [!] neo4j seems picky about receiving an additional empty statement list
#
data = dbu.statement_set_to_REST_form([])
ret = dbu.post(tx_commit_url, data)
log.debug('tx-commit: id: {0}, commit-url: {1}'.format(op.tx_id, tx_commit_url))
return ret
except Exception as e:
raise Exception('failed to commit transaction:' + e.message)
def log_committed_queries(self, statement_set):
for sp_dict in statement_set['statements']:
log.debug('\tq: {0}'.format(sp_dict['statement']))
class DB_Driver_Embedded(DB_Driver_Base):
def __init__(self, db_base_url):
self.tx_base_url = db_base_url + '/db/data/transaction'
from org.rhizi.db.neo4j.util import EmbeddedNeo4j
self.edb = EmbeddedNeo4j.createDb()
self.edb.createDb()
def begin_tx(self, op):
pass
def exex_op_statements(self, op):
s_set = op.statement_set
self.edb.executeCypherQury()
def commit_tx(self, op):
pass
def log_committed_queries(self, statement_set):
for sp_dict in statement_set['statements']:
log.debug('\tq: {0}'.format(sp_dict['statement']))
class DB_Controller:
"""
neo4j DB controller
"""
def __init__(self, config, db_driver_class=None):
self.config = config
if not db_driver_class:
self.db_driver = DB_Driver_REST(self.config.db_base_url)
else:
self.db_driver = db_driver_class()
assert isinstance(self.db_driver, DB_Driver_Base)
def exec_op(self, op):
"""
execute operation within a DB transaction
"""
try:
self.db_driver.begin_tx(op)
ret_tx = self.db_driver.exex_op_statements(op)
ret_commit = self.db_driver.commit_tx(op)
return op.on_success(ret_tx)
except Exception as e:
log.error(e.message)
log.error(traceback.print_exc())
op.on_error()
def create_db_op(self, f_work, f_cont):
ret = DB_op(f_work, f_cont)
return ret
def exec_cypher_query(self, q):
"""
@deprecated: use transaction based api
"""
# call post and not dbu.post_neo4j to avoid response key errors
dbu.post(self.config.db_base_url + '/db/data/cypher', {"query" : q})
|