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
|
# This file is part of rhizi, a collaborative knowledge graph editor.
# Copyright (C) 2014-2015 Rhizi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/usr/bin/python
import logging
from db_driver import DB_Driver_REST, DB_Driver_Base
from db_op import DB_composed_op
from db_op import DB_op
from neo4j_util import Neo4JException
log = logging.getLogger('rhizi')
class DB_Controller(object):
"""
neo4j DB controller
"""
def __init__(self, rest_api_base_url, db_driver_class=None):
if not db_driver_class:
self.db_driver = DB_Driver_REST(rest_api_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
"""
if isinstance(op, DB_composed_op): # composed DB op
log.debug('exec_composed-op:' + op.name)
for sub_op in op.iter__sub_op():
sub_op_ret = self.exec_op(sub_op) # recursive call
op.post_sub_op_exec_hook(sub_op, sub_op_ret)
op_ret = op.process_result_set()
return op_ret
try: # non-composed DB op
self.db_driver.begin_tx(op)
self.db_driver.exec_query_set(op)
self.db_driver.commit_tx(op)
op_ret = op.process_result_set()
log.debug('exec_op: ' + op.name + ': return value: ' + unicode(op_ret)[:256]) # trim verbose return values
return op_ret
except Neo4JException as e:
log.exception(e) # Neo4JException may be composed of several sub errors, defer to class __str__
raise e
except Exception as e:
# here we watch for IOExecptions, etc - not db errors
# these are returned in the db response itself
log.exception('op: %r' % (op))
raise e
def create_db_op(self, f_work, f_cont):
ret = DB_op(f_work, f_cont)
return ret
|