author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 09 Oct 2009 15:53:02 +0200 | |
branch | stable |
changeset 3626 | 017869a514c3 |
parent 3553 | 76b4a177a018 |
child 3589 | a5432f99f2d9 |
child 3986 | cc29eddf51ad |
permissions | -rw-r--r-- |
0 | 1 |
"""Repository users' and internal' sessions. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1880
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1880
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
import threading |
|
12 |
from time import time |
|
13 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
14 |
from logilab.common.deprecation import deprecated |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
15 |
from rql.nodes import VariableRef, Function, ETYPE_PYOBJ_MAP, etype_from_pyobj |
0 | 16 |
from yams import BASE_TYPES |
17 |
||
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
18 |
from cubicweb import RequestSessionMixIn, Binary, UnknownEid |
0 | 19 |
from cubicweb.dbapi import ConnectionProperties |
940
15dcdc863965
fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
746
diff
changeset
|
20 |
from cubicweb.utils import make_uid |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
21 |
from cubicweb.rqlrewrite import RQLRewriter |
0 | 22 |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
985
diff
changeset
|
23 |
ETYPE_PYOBJ_MAP[Binary] = 'Bytes' |
0 | 24 |
|
25 |
def is_final(rqlst, variable, args): |
|
26 |
# try to find if this is a final var or not |
|
27 |
for select in rqlst.children: |
|
28 |
for sol in select.solutions: |
|
29 |
etype = variable.get_type(sol, args) |
|
30 |
if etype is None: |
|
31 |
continue |
|
32 |
if etype in BASE_TYPES: |
|
33 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
34 |
return False |
0 | 35 |
|
36 |
def _make_description(selected, args, solution): |
|
37 |
"""return a description for a result set""" |
|
38 |
description = [] |
|
39 |
for term in selected: |
|
40 |
description.append(term.get_type(solution, args)) |
|
41 |
return description |
|
42 |
||
43 |
||
44 |
class Session(RequestSessionMixIn): |
|
45 |
"""tie session id, user, connections pool and other session data all |
|
46 |
together |
|
47 |
""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
48 |
|
0 | 49 |
def __init__(self, user, repo, cnxprops=None, _id=None): |
50 |
super(Session, self).__init__(repo.vreg) |
|
51 |
self.id = _id or make_uid(user.login.encode('UTF8')) |
|
52 |
cnxprops = cnxprops or ConnectionProperties('inmemory') |
|
53 |
self.user = user |
|
54 |
self.repo = repo |
|
55 |
self.cnxtype = cnxprops.cnxtype |
|
56 |
self.creation = time() |
|
57 |
self.timestamp = self.creation |
|
58 |
self.is_internal_session = False |
|
59 |
self.is_super_session = False |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
60 |
self.default_mode = 'read' |
0 | 61 |
# short cut to querier .execute method |
62 |
self._execute = repo.querier.execute |
|
63 |
# shared data, used to communicate extra information between the client |
|
64 |
# and the rql server |
|
65 |
self.data = {} |
|
66 |
# i18n initialization |
|
67 |
self.set_language(cnxprops.lang) |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
68 |
# internals |
0 | 69 |
self._threaddata = threading.local() |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
70 |
self._threads_in_transaction = set() |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
71 |
self._closed = False |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
72 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
73 |
def __str__(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
74 |
return '<%ssession %s (%s 0x%x)>' % (self.cnxtype, self.user.login, |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
75 |
self.id, id(self)) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
76 |
|
2630
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
77 |
@property |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
78 |
def schema(self): |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
79 |
return self.repo.schema |
b3f997bc8c26
R [session] property for quick access to repo.schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2623
diff
changeset
|
80 |
|
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
81 |
def hijack_user(self, user): |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
82 |
"""return a fake request/session using specified user""" |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
83 |
session = Session(user, self.repo) |
3161
005f745315b0
[notification] should get actual session here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3112
diff
changeset
|
84 |
session._threaddata = self.actual_session()._threaddata |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
85 |
return session |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
86 |
|
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
87 |
def _super_call(self, __cb, *args, **kwargs): |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
88 |
if self.is_super_session: |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
89 |
__cb(self, *args, **kwargs) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
90 |
return |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
91 |
self.is_super_session = True |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
92 |
try: |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
93 |
__cb(self, *args, **kwargs) |
2604
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
94 |
finally: |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
95 |
self.is_super_session = False |
6b55a2a81fd8
[R repo session] add_relation method use in hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2603
diff
changeset
|
96 |
|
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
97 |
def add_relation(self, fromeid, rtype, toeid): |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
98 |
"""provide direct access to the repository method to add a relation. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
99 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
100 |
This is equivalent to the following rql query: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
101 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
102 |
SET X rtype Y WHERE X eid fromeid, T eid toeid |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
103 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
104 |
without read security check but also all the burden of rql execution. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
105 |
You may use this in hooks when you know both eids of the relation you |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
106 |
want to add. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
107 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
108 |
if self.vreg.schema[rtype].inlined: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
109 |
entity = self.entity_from_eid(fromeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
110 |
entity[rtype] = toeid |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
111 |
self._super_call(self.repo.glob_update_entity, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
112 |
entity, set((rtype,))) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
113 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
114 |
self._super_call(self.repo.glob_add_relation, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
115 |
fromeid, rtype, toeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
116 |
|
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
117 |
def delete_relation(self, fromeid, rtype, toeid): |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
118 |
"""provide direct access to the repository method to delete a relation. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
119 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
120 |
This is equivalent to the following rql query: |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
121 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
122 |
DELETE X rtype Y WHERE X eid fromeid, T eid toeid |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
123 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
124 |
without read security check but also all the burden of rql execution. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
125 |
You may use this in hooks when you know both eids of the relation you |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
126 |
want to delete. |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
127 |
""" |
3197
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
128 |
if self.vreg.schema[rtype].inlined: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
129 |
entity = self.entity_from_eid(fromeid) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
130 |
entity[rtype] = None |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
131 |
self._super_call(self.repo.glob_update_entity, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
132 |
entity, set((rtype,))) |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
133 |
else: |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
134 |
self._super_call(self.repo.glob_delete_relation, |
b27d19c0db1c
[repo] take care of inlined relation in session.[add|delete]_relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3196
diff
changeset
|
135 |
fromeid, rtype, toeid) |
3112
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
136 |
|
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
137 |
# relations cache handling ################################################# |
873202e181bb
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3074
diff
changeset
|
138 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
139 |
def update_rel_cache_add(self, subject, rtype, object, symetric=False): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
140 |
self._update_entity_rel_cache_add(subject, rtype, 'subject', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
141 |
if symetric: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
142 |
self._update_entity_rel_cache_add(object, rtype, 'subject', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
143 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
144 |
self._update_entity_rel_cache_add(object, rtype, 'object', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
145 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
146 |
def update_rel_cache_del(self, subject, rtype, object, symetric=False): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
147 |
self._update_entity_rel_cache_del(subject, rtype, 'subject', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
148 |
if symetric: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
149 |
self._update_entity_rel_cache_del(object, rtype, 'object', object) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
150 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
151 |
self._update_entity_rel_cache_del(object, rtype, 'object', subject) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
152 |
|
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
153 |
def _update_entity_rel_cache_add(self, eid, rtype, role, targeteid): |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
154 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
155 |
entity = self.entity_cache(eid) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
156 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
157 |
return |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
158 |
rcache = entity.relation_cached(rtype, role) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
159 |
if rcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
160 |
rset, entities = rcache |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
161 |
rset = rset.copy() |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
162 |
entities = list(entities) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
163 |
rset.rows.append([targeteid]) |
2781
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
164 |
if not isinstance(rset.description, list): # else description not set |
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
165 |
rset.description = list(rset.description) |
4e1ad9d6a3e7
[session cache] always append to description, turn it into a list if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2764
diff
changeset
|
166 |
rset.description.append([self.describe(targeteid)[0]]) |
3196
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
167 |
targetentity = self.entity_from_eid(targeteid) |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
168 |
if targetentity.rset is None: |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
169 |
targetentity.rset = rset |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
170 |
targetentity.row = rset.rowcount |
77936fa67ae6
[repo] set entity.rset & co when needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3161
diff
changeset
|
171 |
targetentity.col = 0 |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
172 |
rset.rowcount += 1 |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
173 |
entities.append(targetentity) |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
174 |
entity._related_cache['%s_%s' % (rtype, role)] = (rset, tuple(entities)) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
175 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
176 |
def _update_entity_rel_cache_del(self, eid, rtype, role, targeteid): |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
177 |
try: |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
178 |
entity = self.entity_cache(eid) |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
179 |
except KeyError: |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
180 |
return |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
181 |
rcache = entity.relation_cached(rtype, role) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
182 |
if rcache is not None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
183 |
rset, entities = rcache |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
184 |
for idx, row in enumerate(rset.rows): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
185 |
if row[0] == targeteid: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
186 |
break |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
187 |
else: |
3074
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
188 |
# this may occurs if the cache has been filed by a hook |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
189 |
# after the database update |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
190 |
self.debug('cache inconsistency for %s %s %s %s', eid, rtype, |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
191 |
role, targeteid) |
141cfaac6b97
don't fail when trying to update relation cache for relation deletion, this may occurs regularly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2874
diff
changeset
|
192 |
return |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
193 |
rset = rset.copy() |
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
194 |
entities = list(entities) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
195 |
del rset.rows[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
196 |
if isinstance(rset.description, list): # else description not set |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
197 |
del rset.description[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
198 |
del entities[idx] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
199 |
rset.rowcount -= 1 |
3553
76b4a177a018
[session cache] ensure we're not modifying cached rset/entities, they may be used elsewhere
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3275
diff
changeset
|
200 |
entity._related_cache['%s_%s' % (rtype, role)] = (rset, tuple(entities)) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
201 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
202 |
# resource accessors ###################################################### |
0 | 203 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
204 |
def actual_session(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
205 |
"""return the original parent session if any, else self""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
206 |
return self |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
207 |
|
2623
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
208 |
def system_sql(self, sql, args=None, rollback_on_failure=True): |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
209 |
"""return a sql cursor on the system database""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
210 |
if not sql.split(None, 1)[0].upper() == 'SELECT': |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
211 |
self.mode = 'write' |
2623
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
212 |
return self.pool.source('system').doexec(self, sql, args, |
e1f34b77290b
oops, should have been in a earlier commit (give rollback_on_failure to doexec)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
213 |
rollback=rollback_on_failure) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
214 |
|
0 | 215 |
def set_language(self, language): |
216 |
"""i18n configuration for translation""" |
|
217 |
vreg = self.vreg |
|
218 |
language = language or self.user.property_value('ui.language') |
|
219 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
220 |
gettext, pgettext = vreg.config.translations[language] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
221 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
222 |
self.pgettext = pgettext |
0 | 223 |
except KeyError: |
224 |
language = vreg.property_value('ui.language') |
|
225 |
try: |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
226 |
gettext, pgettext = vreg.config.translations[language] |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
227 |
self._ = self.__ = gettext |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
228 |
self.pgettext = pgettext |
0 | 229 |
except KeyError: |
230 |
self._ = self.__ = unicode |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3240
diff
changeset
|
231 |
self.pgettext = lambda x,y: y |
0 | 232 |
self.lang = language |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
233 |
|
0 | 234 |
def change_property(self, prop, value): |
235 |
assert prop == 'lang' # this is the only one changeable property for now |
|
236 |
self.set_language(value) |
|
237 |
||
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
238 |
# connection management ################################################### |
0 | 239 |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
240 |
def keep_pool_mode(self, mode): |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
241 |
"""set pool_mode, e.g. how the session will keep its pool: |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
242 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
243 |
* if mode == 'write', the pool is freed after each ready query, but kept |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
244 |
until the transaction's end (eg commit or rollback) when a write query |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
245 |
is detected (eg INSERT/SET/DELETE queries) |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
246 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
247 |
* if mode == 'transaction', the pool is only freed after the |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
248 |
transaction's end |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
249 |
|
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
250 |
notice that a repository has a limited set of pools, and a session has to |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
251 |
wait for a free pool to run any rql query (unless it already has a pool |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
252 |
set). |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
253 |
""" |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
254 |
assert mode in ('transaction', 'write') |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
255 |
if mode == 'transaction': |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
256 |
self.default_mode = 'transaction' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
257 |
else: # mode == 'write' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
258 |
self.default_mode = 'read' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
259 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
260 |
def get_mode(self): |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
261 |
return getattr(self._threaddata, 'mode', self.default_mode) |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
262 |
def set_mode(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
263 |
self._threaddata.mode = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
264 |
mode = property(get_mode, set_mode, |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
265 |
doc='transaction mode (read/write/transaction), resetted to' |
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
266 |
' default_mode on commit / rollback') |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
267 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
268 |
def get_commit_state(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
269 |
return getattr(self._threaddata, 'commit_state', None) |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
270 |
def set_commit_state(self, value): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
271 |
self._threaddata.commit_state = value |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
272 |
commit_state = property(get_commit_state, set_commit_state) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
273 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
274 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
275 |
def pool(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
276 |
"""connections pool, set according to transaction mode for each query""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
277 |
return getattr(self._threaddata, 'pool', None) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
278 |
|
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
279 |
def set_pool(self, checkclosed=True): |
0 | 280 |
"""the session need a pool to execute some queries""" |
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
281 |
if checkclosed and self._closed: |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
282 |
raise Exception('try to set pool on a closed session') |
0 | 283 |
if self.pool is None: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
284 |
# get pool first to avoid race-condition |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
285 |
self._threaddata.pool = pool = self.repo._get_pool() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
286 |
try: |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
287 |
pool.pool_set() |
0 | 288 |
except: |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
289 |
self._threaddata.pool = None |
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2200
diff
changeset
|
290 |
self.repo._free_pool(pool) |
0 | 291 |
raise |
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
292 |
self._threads_in_transaction.add(threading.currentThread()) |
0 | 293 |
return self._threaddata.pool |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
294 |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
295 |
def reset_pool(self, ignoremode=False): |
2319
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
296 |
"""the session is no longer using its pool, at least for some time""" |
0 | 297 |
# pool may be none if no operation has been done since last commit |
298 |
# or rollback |
|
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
299 |
if self.pool is not None and (ignoremode or self.mode == 'read'): |
0 | 300 |
# even in read mode, we must release the current transaction |
2055 | 301 |
pool = self.pool |
2319
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
302 |
try: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
303 |
self._threads_in_transaction.remove(threading.currentThread()) |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
304 |
except KeyError: |
654decb099e3
typo, no error if thread isn't in running set
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2306
diff
changeset
|
305 |
pass |
2063
fe4278b50388
fix [re]set_pool prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2055
diff
changeset
|
306 |
pool.pool_reset() |
0 | 307 |
self._threaddata.pool = None |
2054
277e8d3b1154
fix potential race-condition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
308 |
# free pool once everything is done to avoid race-condition |
2055 | 309 |
self.repo._free_pool(pool) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
310 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
311 |
def _touch(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
312 |
"""update latest session usage timestamp and reset mode to read""" |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
313 |
self.timestamp = time() |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
314 |
self.local_perm_cache.clear() |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
315 |
self._threaddata.mode = self.default_mode |
0 | 316 |
|
317 |
# shared data handling ################################################### |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
318 |
|
0 | 319 |
def get_shared_data(self, key, default=None, pop=False): |
320 |
"""return value associated to `key` in session data""" |
|
321 |
if pop: |
|
322 |
return self.data.pop(key, default) |
|
323 |
else: |
|
324 |
return self.data.get(key, default) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
325 |
|
0 | 326 |
def set_shared_data(self, key, value, querydata=False): |
327 |
"""set value associated to `key` in session data""" |
|
328 |
if querydata: |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
329 |
self.transaction_data[key] = value |
0 | 330 |
else: |
331 |
self.data[key] = value |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
332 |
|
0 | 333 |
# request interface ####################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
334 |
|
0 | 335 |
def set_entity_cache(self, entity): |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
336 |
# XXX session level caching may be a pb with multiple repository |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
337 |
# instances, but 1. this is probably not the only one :$ and 2. it |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
338 |
# may be an acceptable risk. Anyway we could activate it or not |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
339 |
# according to a configuration option |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
340 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
341 |
self.transaction_data['ecache'].setdefault(entity.eid, entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
342 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
343 |
self.transaction_data['ecache'] = ecache = {} |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
344 |
ecache[entity.eid] = entity |
0 | 345 |
|
346 |
def entity_cache(self, eid): |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
347 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
348 |
return self.transaction_data['ecache'][eid] |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
349 |
except: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
350 |
raise |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
351 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
352 |
def cached_entities(self): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
353 |
return self.transaction_data.get('ecache', {}).values() |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
354 |
|
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
355 |
def drop_entity_cache(self, eid=None): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
356 |
if eid is None: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
357 |
self.transaction_data.pop('ecache', None) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
358 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
359 |
del self.transaction_data['ecache'][eid] |
0 | 360 |
|
361 |
def base_url(self): |
|
2466
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
362 |
url = self.repo.config['base-url'] |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
363 |
if not url: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
364 |
try: |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
365 |
url = self.repo.config.default_base_url() |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
366 |
except AttributeError: # default_base_url() might not be available |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
367 |
self.warning('missing base-url definition in server config') |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
368 |
url = u'' |
c4ccfd38a542
[server] take a chance to grab base-url from all-in-one config if not specified explicitly
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2319
diff
changeset
|
369 |
return url |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
370 |
|
0 | 371 |
def from_controller(self): |
372 |
"""return the id (string) of the controller issuing the request (no |
|
373 |
sense here, always return 'view') |
|
374 |
""" |
|
375 |
return 'view' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
376 |
|
0 | 377 |
def source_defs(self): |
378 |
return self.repo.source_defs() |
|
379 |
||
380 |
def describe(self, eid): |
|
381 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
382 |
return self.repo.type_and_source_from_eid(eid, self) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
383 |
|
0 | 384 |
# db-api like interface ################################################### |
385 |
||
386 |
def source_from_eid(self, eid): |
|
387 |
"""return the source where the entity with id <eid> is located""" |
|
388 |
return self.repo.source_from_eid(eid, self) |
|
389 |
||
390 |
def decorate_rset(self, rset, propagate=False): |
|
391 |
rset.vreg = self.vreg |
|
392 |
rset.req = propagate and self or self.actual_session() |
|
393 |
return rset |
|
394 |
||
395 |
@property |
|
396 |
def super_session(self): |
|
397 |
try: |
|
398 |
csession = self._threaddata.childsession |
|
399 |
except AttributeError: |
|
2874
acdd8d8c2cff
[session] use isinstance instead of testing value of is_super_session since we hack it in the add_relation method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2781
diff
changeset
|
400 |
if isinstance(self, (ChildSession, InternalSession)): |
0 | 401 |
csession = self |
402 |
else: |
|
403 |
csession = ChildSession(self) |
|
404 |
self._threaddata.childsession = csession |
|
405 |
# need shared pool set |
|
2764
55579275a091
[session pool] dont check closed session when calling set_pool from super_session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
406 |
self.set_pool(checkclosed=False) |
0 | 407 |
return csession |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
408 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2630
diff
changeset
|
409 |
def unsafe_execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
0 | 410 |
propagate=False): |
411 |
"""like .execute but with security checking disabled (this method is |
|
412 |
internal to the server, it's not part of the db-api) |
|
413 |
||
414 |
if `propagate` is true, the super_session will be attached to the result |
|
415 |
set instead of the parent session, hence further query done through |
|
416 |
entities fetched from this result set will bypass security as well |
|
417 |
""" |
|
418 |
return self.super_session.execute(rql, kwargs, eid_key, build_descr, |
|
419 |
propagate) |
|
420 |
||
421 |
@property |
|
422 |
def cursor(self): |
|
423 |
"""return a rql cursor""" |
|
424 |
return self |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
425 |
|
0 | 426 |
def execute(self, rql, kwargs=None, eid_key=None, build_descr=True, |
427 |
propagate=False): |
|
428 |
"""db-api like method directly linked to the querier execute method |
|
429 |
||
430 |
Becare that unlike actual cursor.execute, `build_descr` default to |
|
431 |
false |
|
432 |
""" |
|
433 |
rset = self._execute(self, rql, kwargs, eid_key, build_descr) |
|
434 |
return self.decorate_rset(rset, propagate) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
435 |
|
0 | 436 |
def commit(self, reset_pool=True): |
437 |
"""commit the current session's transaction""" |
|
438 |
if self.pool is None: |
|
439 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
440 |
self.transaction_data.clear() |
0 | 441 |
self._touch() |
2200
25bb65dc4559
test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2190
diff
changeset
|
442 |
self.debug('commit session %s done (no db activity)', self.id) |
0 | 443 |
return |
444 |
if self.commit_state: |
|
445 |
return |
|
446 |
# on rollback, an operation should have the following state |
|
447 |
# information: |
|
448 |
# - processed by the precommit/commit event or not |
|
449 |
# - if processed, is it the failed operation |
|
450 |
try: |
|
451 |
for trstate in ('precommit', 'commit'): |
|
452 |
processed = [] |
|
453 |
self.commit_state = trstate |
|
454 |
try: |
|
455 |
while self.pending_operations: |
|
456 |
operation = self.pending_operations.pop(0) |
|
457 |
operation.processed = trstate |
|
458 |
processed.append(operation) |
|
459 |
operation.handle_event('%s_event' % trstate) |
|
460 |
self.pending_operations[:] = processed |
|
461 |
self.debug('%s session %s done', trstate, self.id) |
|
462 |
except: |
|
463 |
self.exception('error while %sing', trstate) |
|
464 |
operation.failed = True |
|
465 |
for operation in processed: |
|
466 |
operation.handle_event('revert%s_event' % trstate) |
|
467 |
self.rollback(reset_pool) |
|
468 |
raise |
|
469 |
self.pool.commit() |
|
470 |
finally: |
|
471 |
self._touch() |
|
472 |
self.commit_state = None |
|
473 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
474 |
self.transaction_data.clear() |
0 | 475 |
if reset_pool: |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
476 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
477 |
|
0 | 478 |
def rollback(self, reset_pool=True): |
479 |
"""rollback the current session's transaction""" |
|
480 |
if self.pool is None: |
|
481 |
assert not self.pending_operations |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
482 |
self.transaction_data.clear() |
0 | 483 |
self._touch() |
2200
25bb65dc4559
test fixes, all server tests ok, except unittest_migractions (due to inter-tests-side-effects...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2190
diff
changeset
|
484 |
self.debug('rollback session %s done (no db activity)', self.id) |
0 | 485 |
return |
486 |
try: |
|
487 |
while self.pending_operations: |
|
488 |
try: |
|
489 |
operation = self.pending_operations.pop(0) |
|
490 |
operation.handle_event('rollback_event') |
|
491 |
except: |
|
492 |
self.critical('rollback error', exc_info=sys.exc_info()) |
|
493 |
continue |
|
494 |
self.pool.rollback() |
|
2188
3a57c8173290
* deprecate session.entity method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
495 |
self.debug('rollback for session %s done', self.id) |
0 | 496 |
finally: |
497 |
self._touch() |
|
498 |
self.pending_operations[:] = [] |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
499 |
self.transaction_data.clear() |
0 | 500 |
if reset_pool: |
2570
80a996bb536d
[repo session] ability to ask session to keep it's pool set even when only read queries are done, necessary at least during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2319
diff
changeset
|
501 |
self.reset_pool(ignoremode=True) |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
502 |
|
0 | 503 |
def close(self): |
504 |
"""do not close pool on session close, since they are shared now""" |
|
1880
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
505 |
self._closed = True |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
506 |
# copy since _threads_in_transaction maybe modified while waiting |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
507 |
for thread in self._threads_in_transaction.copy(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
508 |
if thread is threading.currentThread(): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
509 |
continue |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
510 |
self.info('waiting for thread %s', thread) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
511 |
# do this loop/break instead of a simple join(10) in case thread is |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
512 |
# the main thread (in which case it will be removed from |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
513 |
# self._threads_in_transaction but still be alive...) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
514 |
for i in xrange(10): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
515 |
thread.join(1) |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
516 |
if not (thread.isAlive() and |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
517 |
thread in self._threads_in_transaction): |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
518 |
break |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
519 |
else: |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
520 |
self.error('thread %s still alive after 10 seconds, will close ' |
293fe4b49e28
two in one: #343320: Logging out while deleting a CWUser blocks the cw server / #342692: ensure transaction state when Ctrl-C or other stop signal is received
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1660
diff
changeset
|
521 |
'session anyway', thread) |
0 | 522 |
self.rollback() |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
523 |
|
0 | 524 |
# transaction data/operations management ################################## |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
525 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
526 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
527 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
528 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
529 |
return self._threaddata.transaction_data |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
530 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
531 |
self._threaddata.transaction_data = {} |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
532 |
return self._threaddata.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
533 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
534 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
535 |
def pending_operations(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
536 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
537 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
538 |
except AttributeError: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
539 |
self._threaddata.pending_operations = [] |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
540 |
return self._threaddata.pending_operations |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
541 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
542 |
|
0 | 543 |
def add_operation(self, operation, index=None): |
544 |
"""add an observer""" |
|
545 |
assert self.commit_state != 'commit' |
|
546 |
if index is not None: |
|
547 |
self.pending_operations.insert(index, operation) |
|
548 |
else: |
|
549 |
self.pending_operations.append(operation) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
550 |
|
0 | 551 |
# querier helpers ######################################################### |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
552 |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
553 |
@property |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
554 |
def rql_rewriter(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
555 |
try: |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
556 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
557 |
except AttributeError: |
3240
8604a15995d1
refactor so that rql rewriter may be used outside the server. Enhance it to be usable for RRQLExpression as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
558 |
self._threaddata._rewriter = RQLRewriter(self) |
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
559 |
return self._threaddata._rewriter |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
560 |
|
0 | 561 |
def build_description(self, rqlst, args, result): |
562 |
"""build a description for a given result""" |
|
563 |
if len(rqlst.children) == 1 and len(rqlst.children[0].solutions) == 1: |
|
564 |
# easy, all lines are identical |
|
565 |
selected = rqlst.children[0].selection |
|
566 |
solution = rqlst.children[0].solutions[0] |
|
567 |
description = _make_description(selected, args, solution) |
|
568 |
return [tuple(description)] * len(result) |
|
569 |
# hard, delegate the work :o) |
|
570 |
return self.manual_build_descr(rqlst, args, result) |
|
571 |
||
572 |
def manual_build_descr(self, rqlst, args, result): |
|
573 |
"""build a description for a given result by analysing each row |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
574 |
|
0 | 575 |
XXX could probably be done more efficiently during execution of query |
576 |
""" |
|
577 |
# not so easy, looks for variable which changes from one solution |
|
578 |
# to another |
|
579 |
unstables = rqlst.get_variable_variables() |
|
580 |
basedescription = [] |
|
581 |
todetermine = [] |
|
582 |
selected = rqlst.children[0].selection # sample selection |
|
583 |
for i, term in enumerate(selected): |
|
584 |
if isinstance(term, Function) and term.descr().rtype is not None: |
|
585 |
basedescription.append(term.get_type(term.descr().rtype, args)) |
|
586 |
continue |
|
587 |
for vref in term.get_nodes(VariableRef): |
|
588 |
if vref.name in unstables: |
|
589 |
basedescription.append(None) |
|
590 |
todetermine.append( (i, is_final(rqlst, vref.variable, args)) ) |
|
591 |
break |
|
592 |
else: |
|
593 |
# sample etype |
|
594 |
etype = rqlst.children[0].solutions[0] |
|
595 |
basedescription.append(term.get_type(etype, args)) |
|
596 |
if not todetermine: |
|
597 |
return [tuple(basedescription)] * len(result) |
|
598 |
return self._build_descr(result, basedescription, todetermine) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
599 |
|
0 | 600 |
def _build_descr(self, result, basedescription, todetermine): |
601 |
description = [] |
|
602 |
etype_from_eid = self.describe |
|
603 |
for row in result: |
|
604 |
row_descr = basedescription |
|
605 |
for index, isfinal in todetermine: |
|
606 |
value = row[index] |
|
607 |
if value is None: |
|
608 |
# None value inserted by an outer join, no type |
|
609 |
row_descr[index] = None |
|
610 |
continue |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
611 |
if isfinal: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
612 |
row_descr[index] = etype_from_pyobj(value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
613 |
else: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
614 |
try: |
1169
52058e8a3af9
somewhat handle corrupted database when manually building a database
sylvain.thenault@logilab.fr
parents:
974
diff
changeset
|
615 |
row_descr[index] = etype_from_eid(value)[0] |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
616 |
except UnknownEid: |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
617 |
self.critical('wrong eid %s in repository, should check database' % value) |
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
618 |
row_descr[index] = row[index] = None |
0 | 619 |
description.append(tuple(row_descr)) |
620 |
return description |
|
621 |
||
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
622 |
@deprecated("use vreg['etypes'].etype_class(etype)") |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
623 |
def etype_class(self, etype): |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
624 |
"""return an entity class for the given entity type""" |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
625 |
return self.vreg['etypes'].etype_class(etype) |
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2647
diff
changeset
|
626 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
627 |
@deprecated('use direct access to session.transaction_data') |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
628 |
def query_data(self, key, default=None, setdefault=False, pop=False): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
629 |
if setdefault: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
630 |
assert not pop |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
631 |
return self.transaction_data.setdefault(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
632 |
if pop: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
633 |
return self.transaction_data.pop(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
634 |
else: |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
635 |
return self.transaction_data.get(key, default) |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
636 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2466
diff
changeset
|
637 |
@deprecated('use entity_from_eid(eid, etype=None)') |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
638 |
def entity(self, eid): |
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
639 |
"""return a result set for the given eid""" |
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
640 |
return self.entity_from_eid(eid) |
2182
488099333160
mark session.entity method as obsolete
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2100
diff
changeset
|
641 |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
642 |
|
0 | 643 |
class ChildSession(Session): |
644 |
"""child (or internal) session are used to hijack the security system |
|
645 |
""" |
|
646 |
cnxtype = 'inmemory' |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
647 |
|
0 | 648 |
def __init__(self, parent_session): |
649 |
self.id = None |
|
650 |
self.is_internal_session = False |
|
651 |
self.is_super_session = True |
|
652 |
# session which has created this one |
|
653 |
self.parent_session = parent_session |
|
654 |
self.user = InternalManager() |
|
655 |
self.repo = parent_session.repo |
|
656 |
self.vreg = parent_session.vreg |
|
657 |
self.data = parent_session.data |
|
658 |
self.encoding = parent_session.encoding |
|
659 |
self.lang = parent_session.lang |
|
660 |
self._ = self.__ = parent_session._ |
|
661 |
# short cut to querier .execute method |
|
662 |
self._execute = self.repo.querier.execute |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
663 |
|
0 | 664 |
@property |
665 |
def super_session(self): |
|
666 |
return self |
|
667 |
||
668 |
def get_mode(self): |
|
669 |
return self.parent_session.mode |
|
670 |
def set_mode(self, value): |
|
671 |
self.parent_session.set_mode(value) |
|
672 |
mode = property(get_mode, set_mode) |
|
673 |
||
674 |
def get_commit_state(self): |
|
675 |
return self.parent_session.commit_state |
|
676 |
def set_commit_state(self, value): |
|
677 |
self.parent_session.set_commit_state(value) |
|
678 |
commit_state = property(get_commit_state, set_commit_state) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
679 |
|
0 | 680 |
@property |
681 |
def pool(self): |
|
682 |
return self.parent_session.pool |
|
683 |
@property |
|
684 |
def pending_operations(self): |
|
685 |
return self.parent_session.pending_operations |
|
686 |
@property |
|
2100
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
687 |
def transaction_data(self): |
89b825cdec74
simplify transaction data api, reorganize code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2063
diff
changeset
|
688 |
return self.parent_session.transaction_data |
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
689 |
|
0 | 690 |
def set_pool(self): |
691 |
"""the session need a pool to execute some queries""" |
|
692 |
self.parent_session.set_pool() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
693 |
|
0 | 694 |
def reset_pool(self): |
695 |
"""the session has no longer using its pool, at least for some time |
|
696 |
""" |
|
697 |
self.parent_session.reset_pool() |
|
698 |
||
699 |
def actual_session(self): |
|
700 |
"""return the original parent session if any, else self""" |
|
701 |
return self.parent_session |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
702 |
|
0 | 703 |
def commit(self, reset_pool=True): |
704 |
"""commit the current session's transaction""" |
|
705 |
self.parent_session.commit(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
706 |
|
0 | 707 |
def rollback(self, reset_pool=True): |
708 |
"""rollback the current session's transaction""" |
|
709 |
self.parent_session.rollback(reset_pool) |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
710 |
|
0 | 711 |
def close(self): |
712 |
"""do not close pool on session close, since they are shared now""" |
|
713 |
self.rollback() |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
714 |
|
0 | 715 |
def user_data(self): |
716 |
"""returns a dictionnary with this user's information""" |
|
717 |
return self.parent_session.user_data() |
|
718 |
||
719 |
||
720 |
class InternalSession(Session): |
|
721 |
"""special session created internaly by the repository""" |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
722 |
|
0 | 723 |
def __init__(self, repo, cnxprops=None): |
724 |
super(InternalSession, self).__init__(_IMANAGER, repo, cnxprops, |
|
725 |
_id='internal') |
|
726 |
self.cnxtype = 'inmemory' |
|
727 |
self.is_internal_session = True |
|
728 |
self.is_super_session = True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
729 |
|
0 | 730 |
@property |
731 |
def super_session(self): |
|
732 |
return self |
|
733 |
||
734 |
||
735 |
class InternalManager(object): |
|
736 |
"""a manager user with all access rights used internally for task such as |
|
737 |
bootstrapping the repository or creating regular users according to |
|
738 |
repository content |
|
739 |
""" |
|
740 |
def __init__(self): |
|
741 |
self.eid = -1 |
|
742 |
self.login = u'__internal_manager__' |
|
743 |
self.properties = {} |
|
744 |
||
745 |
def matching_groups(self, groups): |
|
746 |
return 1 |
|
747 |
||
748 |
def is_in_group(self, group): |
|
749 |
return True |
|
750 |
||
751 |
def owns(self, eid): |
|
752 |
return True |
|
1660
d1030dd9730b
delete-trailing-whitespaces, missing import
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
753 |
|
0 | 754 |
def has_permission(self, pname, contexteid=None): |
755 |
return True |
|
756 |
||
757 |
def property_value(self, key): |
|
758 |
if key == 'ui.language': |
|
759 |
return 'en' |
|
760 |
return None |
|
761 |
||
1132 | 762 |
_IMANAGER = InternalManager() |
0 | 763 |
|
764 |
from logging import getLogger |
|
765 |
from cubicweb import set_log_methods |
|
766 |
set_log_methods(Session, getLogger('cubicweb.session')) |