author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 Aug 2009 09:25:26 +0200 | |
changeset 2901 | 8f4d495c94c2 |
parent 2893 | 5989ce0707bc |
child 2968 | 0e3460341023 |
permissions | -rw-r--r-- |
0 | 1 |
"""Defines the central class for the CubicWeb RQL server: the repository. |
2 |
||
3 |
The repository is an abstraction allowing execution of rql queries against |
|
4 |
data sources. Most of the work is actually done in helper classes. The |
|
5 |
repository mainly: |
|
6 |
||
7 |
* brings these classes all together to provide a single access |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
8 |
point to a cubicweb instance. |
0 | 9 |
* handles session management |
10 |
* provides method for pyro registration, to call if pyro is enabled |
|
11 |
||
12 |
||
13 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
14 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 15 |
: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:
1954
diff
changeset
|
16 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 17 |
""" |
18 |
__docformat__ = "restructuredtext en" |
|
19 |
||
20 |
import sys |
|
21 |
import Queue |
|
22 |
from os.path import join, exists |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
636
diff
changeset
|
23 |
from datetime import datetime |
0 | 24 |
from time import time, localtime, strftime |
25 |
||
26 |
from logilab.common.decorators import cached |
|
27 |
||
28 |
from yams import BadSchemaDefinition |
|
29 |
from rql import RQLSyntaxError |
|
30 |
||
2709
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
31 |
from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, CW_EVENT_MANAGER, |
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
32 |
UnknownEid, AuthenticationError, ExecutionError, |
0 | 33 |
ETypeNotSupportedBySources, RTypeNotSupportedBySources, |
34 |
BadConnectionId, Unauthorized, ValidationError, |
|
2709
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
35 |
typed_eid) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
36 |
from cubicweb import cwvreg, schema, server |
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
37 |
from cubicweb.server import utils, hook, pool, querier, sources |
0 | 38 |
from cubicweb.server.session import Session, InternalSession |
39 |
||
40 |
||
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
41 |
class CleanupEidTypeCacheOp(hook.SingleLastOperation): |
0 | 42 |
"""on rollback of a insert query or commit of delete query, we have to |
43 |
clear repository's cache from no more valid entries |
|
44 |
||
45 |
NOTE: querier's rqlst/solutions cache may have been polluted too with |
|
46 |
queries such as Any X WHERE X eid 32 if 32 has been rollbacked however |
|
47 |
generated queries are unpredictable and analysing all the cache probably |
|
48 |
too expensive. Notice that there is no pb when using args to specify eids |
|
49 |
instead of giving them into the rql string. |
|
50 |
""" |
|
51 |
||
52 |
def commit_event(self): |
|
53 |
"""the observed connections pool has been rollbacked, |
|
54 |
remove inserted eid from repository type/source cache |
|
55 |
""" |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
try: |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
self.repo.clear_caches(self.session.transaction_data['pendingeids']) |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
58 |
except KeyError: |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
59 |
pass |
1482 | 60 |
|
0 | 61 |
def rollback_event(self): |
62 |
"""the observed connections pool has been rollbacked, |
|
63 |
remove inserted eid from repository type/source cache |
|
64 |
""" |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
try: |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
66 |
self.repo.clear_caches(self.session.transaction_data['neweids']) |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
67 |
except KeyError: |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
68 |
pass |
0 | 69 |
|
70 |
||
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
71 |
class FTIndexEntityOp(hook.LateOperation): |
0 | 72 |
"""operation to delay entity full text indexation to commit |
73 |
||
74 |
since fti indexing may trigger discovery of other entities, it should be |
|
75 |
triggered on precommit, not commit, and this should be done after other |
|
76 |
precommit operation which may add relations to the entity |
|
77 |
""" |
|
78 |
||
79 |
def precommit_event(self): |
|
80 |
session = self.session |
|
81 |
entity = self.entity |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
if entity.eid in session.transaction_data.get('pendingeids', ()): |
0 | 83 |
return # entity added and deleted in the same transaction |
84 |
session.repo.system_source.fti_unindex_entity(session, entity.eid) |
|
85 |
for container in entity.fti_containers(): |
|
86 |
session.repo.index_entity(session, container) |
|
1482 | 87 |
|
0 | 88 |
def commit_event(self): |
89 |
pass |
|
90 |
||
91 |
def del_existing_rel_if_needed(session, eidfrom, rtype, eidto): |
|
92 |
"""delete existing relation when adding a new one if card is 1 or ? |
|
93 |
||
94 |
have to be done once the new relation has been inserted to avoid having |
|
95 |
an entity without a relation for some time |
|
96 |
||
97 |
this kind of behaviour has to be done in the repository so we don't have |
|
98 |
hooks order hazardness |
|
99 |
""" |
|
100 |
# skip delete queries (only?) if session is an internal session. This is |
|
101 |
# hooks responsability to ensure they do not violate relation's cardinality |
|
102 |
if session.is_super_session: |
|
103 |
return |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
104 |
card = session.schema_rproperty(rtype, eidfrom, eidto, 'cardinality') |
0 | 105 |
# one may be tented to check for neweids but this may cause more than one |
106 |
# relation even with '1?' cardinality if thoses relations are added in the |
|
107 |
# same transaction where the entity is being created. This never occurs from |
|
108 |
# the web interface but may occurs during test or dbapi connection (though |
|
109 |
# not expected for this). So: don't do it, we pretend to ensure repository |
|
110 |
# consistency. |
|
111 |
if card[0] in '1?': |
|
112 |
rschema = session.repo.schema.rschema(rtype) |
|
113 |
if not rschema.inlined: |
|
1320 | 114 |
session.unsafe_execute( |
115 |
'DELETE X %s Y WHERE X eid %%(x)s, NOT Y eid %%(y)s' % rtype, |
|
116 |
{'x': eidfrom, 'y': eidto}, 'x') |
|
0 | 117 |
if card[1] in '1?': |
1320 | 118 |
session.unsafe_execute( |
119 |
'DELETE X %s Y WHERE NOT X eid %%(x)s, Y eid %%(y)s' % rtype, |
|
120 |
{'x': eidfrom, 'y': eidto}, 'y') |
|
0 | 121 |
|
1482 | 122 |
|
0 | 123 |
class Repository(object): |
124 |
"""a repository provides access to a set of persistent storages for |
|
125 |
entities and relations |
|
126 |
||
127 |
XXX protect pyro access |
|
128 |
""" |
|
1482 | 129 |
|
0 | 130 |
def __init__(self, config, vreg=None, debug=False): |
131 |
self.config = config |
|
132 |
if vreg is None: |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
133 |
vreg = cwvreg.CubicWebVRegistry(config, debug) |
0 | 134 |
self.vreg = vreg |
135 |
self.pyro_registered = False |
|
136 |
self.info('starting repository from %s', self.config.apphome) |
|
137 |
# dictionary of opened sessions |
|
138 |
self._sessions = {} |
|
139 |
# list of functions to be called at regular interval |
|
140 |
self._looping_tasks = [] |
|
141 |
# list of running threads |
|
142 |
self._running_threads = [] |
|
143 |
# initial schema, should be build or replaced latter |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
144 |
self.schema = schema.CubicWebSchema(config.appid) |
0 | 145 |
# querier helper, need to be created after sources initialization |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
146 |
self.querier = querier.QuerierHelper(self, self.schema) |
1187 | 147 |
# should we reindex in changes? |
1217 | 148 |
self.do_fti = not config['delay-full-text-indexation'] |
0 | 149 |
# sources |
150 |
self.sources = [] |
|
151 |
self.sources_by_uri = {} |
|
152 |
# FIXME: store additional sources info in the system database ? |
|
153 |
# FIXME: sources should be ordered (add_entity priority) |
|
154 |
for uri, source_config in config.sources().items(): |
|
155 |
if uri == 'admin': |
|
156 |
# not an actual source |
|
1482 | 157 |
continue |
0 | 158 |
source = self.get_source(uri, source_config) |
159 |
self.sources_by_uri[uri] = source |
|
160 |
self.sources.append(source) |
|
161 |
self.system_source = self.sources_by_uri['system'] |
|
162 |
# ensure system source is the first one |
|
163 |
self.sources.remove(self.system_source) |
|
164 |
self.sources.insert(0, self.system_source) |
|
165 |
# cache eid -> type / source |
|
166 |
self._type_source_cache = {} |
|
167 |
# cache (extid, source uri) -> eid |
|
168 |
self._extid_cache = {} |
|
169 |
# open some connections pools |
|
170 |
self._available_pools = Queue.Queue() |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
171 |
self._available_pools.put_nowait(pool.ConnectionsPool(self.sources)) |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
172 |
if config.read_instance_schema: |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
173 |
# normal start: load the instance schema from the database |
0 | 174 |
self.fill_schema() |
175 |
elif config.bootstrap_schema: |
|
176 |
# usually during repository creation |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
177 |
self.warning("set fs instance'schema as bootstrap schema") |
0 | 178 |
config.bootstrap_cubes() |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
179 |
self.set_schema(self.config.load_schema(), resetvreg=False) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
180 |
# need to load the Any and CWUser entity types |
0 | 181 |
self.vreg.schema = self.schema |
182 |
etdirectory = join(CW_SOFTWARE_ROOT, 'entities') |
|
1317 | 183 |
self.vreg.init_registration([etdirectory]) |
1316
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
184 |
self.vreg.load_file(join(etdirectory, '__init__.py'), |
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
185 |
'cubicweb.entities.__init__') |
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
186 |
self.vreg.load_file(join(etdirectory, 'authobjs.py'), |
6d71d38822ee
introduce init_registration method and call it in repo initialization
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
187 |
'cubicweb.entities.authobjs') |
0 | 188 |
else: |
189 |
# test start: use the file system schema (quicker) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
190 |
self.warning("set fs instance'schema") |
0 | 191 |
config.bootstrap_cubes() |
192 |
self.set_schema(self.config.load_schema()) |
|
193 |
if not config.creating: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
194 |
if 'CWProperty' in self.schema: |
0 | 195 |
self.vreg.init_properties(self.properties()) |
196 |
# call source's init method to complete their initialisation if |
|
197 |
# needed (for instance looking for persistent configuration using an |
|
198 |
# internal session, which is not possible until pools have been |
|
199 |
# initialized) |
|
200 |
for source in self.sources: |
|
201 |
source.init() |
|
202 |
else: |
|
203 |
# call init_creating so for instance native source can configurate |
|
204 |
# tsearch according to postgres version |
|
205 |
for source in self.sources: |
|
206 |
source.init_creating() |
|
207 |
# close initialization pool and reopen fresh ones for proper |
|
208 |
# initialization now that we know cubes |
|
1482 | 209 |
self._get_pool().close(True) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
210 |
# list of available pools (we can't iterated on Queue instance) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
211 |
self.pools = [] |
0 | 212 |
for i in xrange(config['connections-pool-size']): |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
213 |
self.pools.append(pool.ConnectionsPool(self.sources)) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
214 |
self._available_pools.put_nowait(self.pools[-1]) |
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
215 |
self._shutting_down = False |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
216 |
self.hm = vreg['hooks'] |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
217 |
if not (config.creating or config.repairing): |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
218 |
# call instance level initialisation hooks |
2153
d42d1eaefcdd
call server_startup hook once pools have been initialized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
219 |
self.hm.call_hooks('server_startup', repo=self) |
d42d1eaefcdd
call server_startup hook once pools have been initialized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
220 |
# register a task to cleanup expired session |
d42d1eaefcdd
call server_startup hook once pools have been initialized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
221 |
self.looping_task(self.config['session-time']/3., |
d42d1eaefcdd
call server_startup hook once pools have been initialized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2101
diff
changeset
|
222 |
self.clean_sessions) |
1482 | 223 |
|
0 | 224 |
# internals ############################################################### |
225 |
||
226 |
def get_source(self, uri, source_config): |
|
227 |
source_config['uri'] = uri |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
228 |
return sources.get_source(source_config, self.schema, self) |
1482 | 229 |
|
0 | 230 |
def set_schema(self, schema, resetvreg=True): |
231 |
schema.rebuild_infered_relations() |
|
232 |
self.info('set schema %s %#x', schema.name, id(schema)) |
|
233 |
self.debug(', '.join(sorted(str(e) for e in schema.entities()))) |
|
234 |
self.querier.set_schema(schema) |
|
235 |
for source in self.sources: |
|
236 |
source.set_schema(schema) |
|
237 |
self.schema = schema |
|
238 |
if resetvreg: |
|
2881
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
239 |
if self.config._cubes is None: |
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
240 |
self.config.init_cubes(self.get_cubes()) |
0 | 241 |
# full reload of all appobjects |
242 |
self.vreg.reset() |
|
243 |
self.vreg.set_schema(schema) |
|
244 |
||
245 |
def fill_schema(self): |
|
246 |
"""lod schema from the repository""" |
|
247 |
from cubicweb.server.schemaserial import deserialize_schema |
|
248 |
self.info('loading schema from the repository') |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
249 |
appschema = schema.CubicWebSchema(self.config.appid) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
250 |
self.set_schema(self.config.load_bootstrap_schema(), resetvreg=False) |
0 | 251 |
self.debug('deserializing db schema into %s %#x', appschema.name, id(appschema)) |
252 |
session = self.internal_session() |
|
253 |
try: |
|
254 |
try: |
|
255 |
deserialize_schema(appschema, session) |
|
256 |
except BadSchemaDefinition: |
|
257 |
raise |
|
258 |
except Exception, ex: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
259 |
import traceback |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
260 |
traceback.print_exc() |
1482 | 261 |
raise Exception('Is the database initialised ? (cause: %s)' % |
0 | 262 |
(ex.args and ex.args[0].strip() or 'unknown')), \ |
263 |
None, sys.exc_info()[-1] |
|
264 |
finally: |
|
265 |
session.close() |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
266 |
self.set_schema(appschema) |
1482 | 267 |
|
0 | 268 |
def start_looping_tasks(self): |
269 |
assert isinstance(self._looping_tasks, list), 'already started' |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
270 |
for i, (interval, func, args) in enumerate(self._looping_tasks): |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
271 |
self._looping_tasks[i] = task = utils.LoopTask(interval, func, args) |
0 | 272 |
self.info('starting task %s with interval %.2fs', task.name, |
273 |
interval) |
|
274 |
task.start() |
|
275 |
# ensure no tasks will be further added |
|
276 |
self._looping_tasks = tuple(self._looping_tasks) |
|
277 |
||
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
278 |
def looping_task(self, interval, func, *args): |
0 | 279 |
"""register a function to be called every `interval` seconds. |
1482 | 280 |
|
0 | 281 |
looping tasks can only be registered during repository initialization, |
282 |
once done this method will fail. |
|
283 |
""" |
|
284 |
try: |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
285 |
self._looping_tasks.append( (interval, func, args) ) |
0 | 286 |
except AttributeError: |
287 |
raise RuntimeError("can't add looping task once the repository is started") |
|
288 |
||
289 |
def threaded_task(self, func): |
|
290 |
"""start function in a separated thread""" |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
291 |
t = utils.RepoThread(func, self._running_threads) |
0 | 292 |
t.start() |
1482 | 293 |
|
0 | 294 |
#@locked |
295 |
def _get_pool(self): |
|
296 |
try: |
|
297 |
return self._available_pools.get(True, timeout=5) |
|
298 |
except Queue.Empty: |
|
299 |
raise Exception('no pool available after 5 secs, probably either a ' |
|
300 |
'bug in code (to many uncommited/rollbacked ' |
|
301 |
'connections) or to much load on the server (in ' |
|
302 |
'which case you can try to set a bigger ' |
|
303 |
'connections pools size)') |
|
1482 | 304 |
|
0 | 305 |
def _free_pool(self, pool): |
306 |
self._available_pools.put_nowait(pool) |
|
307 |
||
308 |
def pinfo(self): |
|
309 |
# XXX: session.pool is accessed from a local storage, would be interesting |
|
310 |
# to see if there is a pool set in any thread specific data) |
|
311 |
import threading |
|
312 |
return '%s: %s (%s)' % (self._available_pools.qsize(), |
|
313 |
','.join(session.user.login for session in self._sessions.values() |
|
314 |
if session.pool), |
|
315 |
threading.currentThread()) |
|
316 |
def shutdown(self): |
|
317 |
"""called on server stop event to properly close opened sessions and |
|
318 |
connections |
|
319 |
""" |
|
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
320 |
self._shutting_down = True |
0 | 321 |
if isinstance(self._looping_tasks, tuple): # if tasks have been started |
322 |
for looptask in self._looping_tasks: |
|
323 |
self.info('canceling task %s...', looptask.name) |
|
324 |
looptask.cancel() |
|
325 |
looptask.join() |
|
326 |
self.info('task %s finished', looptask.name) |
|
327 |
for thread in self._running_threads: |
|
328 |
self.info('waiting thread %s...', thread.name) |
|
329 |
thread.join() |
|
330 |
self.info('thread %s finished', thread.name) |
|
331 |
self.hm.call_hooks('server_shutdown', repo=self) |
|
332 |
self.close_sessions() |
|
333 |
while not self._available_pools.empty(): |
|
334 |
pool = self._available_pools.get_nowait() |
|
335 |
try: |
|
336 |
pool.close(True) |
|
337 |
except: |
|
338 |
self.exception('error while closing %s' % pool) |
|
339 |
continue |
|
340 |
if self.pyro_registered: |
|
341 |
pyro_unregister(self.config) |
|
342 |
hits, misses = self.querier.cache_hit, self.querier.cache_miss |
|
343 |
try: |
|
344 |
self.info('rqlt st cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
345 |
(hits * 100) / (hits + misses)) |
|
346 |
hits, misses = self.system_source.cache_hit, self.system_source.cache_miss |
|
347 |
self.info('sql cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
348 |
(hits * 100) / (hits + misses)) |
|
349 |
nocache = self.system_source.no_cache |
|
350 |
self.info('sql cache usage: %s/%s (%s%%)', hits+ misses, nocache, |
|
351 |
((hits + misses) * 100) / (hits + misses + nocache)) |
|
352 |
except ZeroDivisionError: |
|
353 |
pass |
|
1482 | 354 |
|
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
355 |
def _login_from_email(self, login): |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
356 |
session = self.internal_session() |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
357 |
try: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
358 |
rset = session.execute('Any L WHERE U login L, U primary_email M, ' |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
359 |
'M address %(login)s', {'login': login}) |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
360 |
if rset.rowcount == 1: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
361 |
login = rset[0][0] |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
362 |
finally: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
363 |
session.close() |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
364 |
return login |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
365 |
|
0 | 366 |
def authenticate_user(self, session, login, password): |
367 |
"""validate login / password, raise AuthenticationError on failure |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
368 |
return associated CWUser instance on success |
0 | 369 |
""" |
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
370 |
if self.vreg.config['allow-email-login'] and '@' in login: |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2245
diff
changeset
|
371 |
login = self._login_from_email(login) |
0 | 372 |
for source in self.sources: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
373 |
if source.support_entity('CWUser'): |
0 | 374 |
try: |
375 |
eid = source.authenticate(session, login, password) |
|
376 |
break |
|
377 |
except AuthenticationError: |
|
378 |
continue |
|
379 |
else: |
|
380 |
raise AuthenticationError('authentication failed with all sources') |
|
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
381 |
cwuser = self._build_user(session, eid) |
0 | 382 |
if self.config.consider_user_state and \ |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
383 |
not cwuser.state in cwuser.AUTHENTICABLE_STATES: |
0 | 384 |
raise AuthenticationError('user is not in authenticable state') |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
385 |
return cwuser |
0 | 386 |
|
387 |
def _build_user(self, session, eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
388 |
"""return a CWUser entity for user with the given eid""" |
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
|
389 |
cls = self.vreg['etypes'].etype_class('CWUser') |
0 | 390 |
rql = cls.fetch_rql(session.user, ['X eid %(x)s']) |
391 |
rset = session.execute(rql, {'x': eid}, 'x') |
|
392 |
assert len(rset) == 1, rset |
|
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
393 |
cwuser = rset.get_entity(0, 0) |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
394 |
# pylint: disable-msg=W0104 |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
395 |
# prefetch / cache cwuser's groups and properties. This is especially |
0 | 396 |
# useful for internal sessions to avoid security insertions |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
397 |
cwuser.groups |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
398 |
cwuser.properties |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
399 |
return cwuser |
1482 | 400 |
|
0 | 401 |
# public (dbapi) interface ################################################ |
1482 | 402 |
|
0 | 403 |
def get_schema(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
404 |
"""return the instance schema. This is a public method, not |
0 | 405 |
requiring a session id |
406 |
""" |
|
407 |
try: |
|
408 |
# necessary to support pickling used by pyro |
|
409 |
self.schema.__hashmode__ = 'pickle' |
|
410 |
return self.schema |
|
411 |
finally: |
|
412 |
self.schema.__hashmode__ = None |
|
413 |
||
414 |
def get_cubes(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
415 |
"""return the list of cubes used by this instance. This is a |
0 | 416 |
public method, not requiring a session id. |
417 |
""" |
|
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
418 |
versions = self.get_versions(not (self.config.creating |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
419 |
or self.config.repairing)) |
0 | 420 |
cubes = list(versions) |
421 |
cubes.remove('cubicweb') |
|
422 |
return cubes |
|
423 |
||
424 |
@cached |
|
425 |
def get_versions(self, checkversions=False): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
426 |
"""return the a dictionary containing cubes used by this instance |
0 | 427 |
as key with their version as value, including cubicweb version. This is a |
428 |
public method, not requiring a session id. |
|
429 |
""" |
|
430 |
from logilab.common.changelog import Version |
|
431 |
vcconf = {} |
|
432 |
session = self.internal_session() |
|
433 |
try: |
|
434 |
for pk, version in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
435 |
'Any K,V WHERE P is CWProperty, P value V, P pkey K, ' |
0 | 436 |
'P pkey ~="system.version.%"', build_descr=False): |
437 |
cube = pk.split('.')[-1] |
|
438 |
# XXX cubicweb migration |
|
439 |
if cube in CW_MIGRATION_MAP: |
|
440 |
cube = CW_MIGRATION_MAP[cube] |
|
441 |
version = Version(version) |
|
442 |
vcconf[cube] = version |
|
443 |
if checkversions: |
|
444 |
if cube != 'cubicweb': |
|
445 |
fsversion = self.config.cube_version(cube) |
|
446 |
else: |
|
447 |
fsversion = self.config.cubicweb_version() |
|
448 |
if version < fsversion: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
449 |
msg = ('instance has %s version %s but %s ' |
0 | 450 |
'is installed. Run "cubicweb-ctl upgrade".') |
451 |
raise ExecutionError(msg % (cube, version, fsversion)) |
|
452 |
finally: |
|
453 |
session.close() |
|
454 |
return vcconf |
|
1482 | 455 |
|
0 | 456 |
@cached |
457 |
def source_defs(self): |
|
458 |
sources = self.config.sources().copy() |
|
459 |
# remove manager information |
|
460 |
sources.pop('admin', None) |
|
461 |
# remove sensitive information |
|
462 |
for uri, sourcedef in sources.iteritems(): |
|
463 |
sourcedef = sourcedef.copy() |
|
464 |
self.sources_by_uri[uri].remove_sensitive_information(sourcedef) |
|
465 |
sources[uri] = sourcedef |
|
466 |
return sources |
|
467 |
||
468 |
def properties(self): |
|
469 |
"""return a result set containing system wide properties""" |
|
470 |
session = self.internal_session() |
|
471 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
472 |
return session.execute('Any K,V WHERE P is CWProperty,' |
0 | 473 |
'P pkey K, P value V, NOT P for_user U', |
474 |
build_descr=False) |
|
475 |
finally: |
|
476 |
session.close() |
|
477 |
||
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
478 |
def register_user(self, login, password, email=None, **kwargs): |
0 | 479 |
"""check a user with the given login exists, if not create it with the |
480 |
given password. This method is designed to be used for anonymous |
|
481 |
registration on public web site. |
|
482 |
""" |
|
1664
03ebeccf9f1d
add XXX before 2 calls to self.repo.internal_session() on the web interface side
Florent <florent@secondweb.fr>
parents:
1482
diff
changeset
|
483 |
# XXX should not be called from web interface |
0 | 484 |
session = self.internal_session() |
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
485 |
# for consistency, keep same error as unique check hook (although not required) |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
486 |
errmsg = session._('the value "%s" is already used, use another one') |
0 | 487 |
try: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
488 |
if (session.execute('CWUser X WHERE X login %(login)s', {'login': login}) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
489 |
or session.execute('CWUser X WHERE X use_email C, C address %(login)s', |
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
490 |
{'login': login})): |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
491 |
raise ValidationError(None, {'login': errmsg % login}) |
0 | 492 |
# we have to create the user |
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
|
493 |
user = self.vreg['etypes'].etype_class('CWUser')(session, None) |
0 | 494 |
if isinstance(password, unicode): |
495 |
# password should *always* be utf8 encoded |
|
496 |
password = password.encode('UTF8') |
|
497 |
kwargs['login'] = login |
|
498 |
kwargs['upassword'] = password |
|
499 |
user.update(kwargs) |
|
500 |
self.glob_add_entity(session, user) |
|
501 |
session.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
|
502 |
{'x': user.eid}) |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
503 |
if email or '@' in login: |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
504 |
d = {'login': login, 'email': email or login} |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
505 |
if session.execute('EmailAddress X WHERE X address %(email)s', d): |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
506 |
raise ValidationError(None, {'address': errmsg % d['email']}) |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
507 |
session.execute('INSERT EmailAddress X: X address %(email)s, ' |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
508 |
'U primary_email X, U use_email X WHERE U login %(login)s', d) |
0 | 509 |
session.commit() |
510 |
finally: |
|
511 |
session.close() |
|
594
76218d42d21f
return success or not on creation of user
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
479
diff
changeset
|
512 |
return True |
1482 | 513 |
|
0 | 514 |
def connect(self, login, password, cnxprops=None): |
515 |
"""open a connection for a given user |
|
516 |
||
517 |
base_url may be needed to send mails |
|
518 |
cnxtype indicate if this is a pyro connection or a in-memory connection |
|
1482 | 519 |
|
0 | 520 |
raise `AuthenticationError` if the authentication failed |
521 |
raise `ConnectionError` if we can't open a connection |
|
522 |
""" |
|
523 |
# use an internal connection |
|
524 |
session = self.internal_session() |
|
525 |
# try to get a user object |
|
526 |
try: |
|
527 |
user = self.authenticate_user(session, login, password) |
|
528 |
finally: |
|
529 |
session.close() |
|
530 |
session = Session(user, self, cnxprops) |
|
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2839
diff
changeset
|
531 |
user._cw = user.rset.req = session |
0 | 532 |
user.clear_related_cache() |
533 |
self._sessions[session.id] = session |
|
534 |
self.info('opened %s', session) |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
535 |
self.hm.call_hooks('session_open', session) |
0 | 536 |
# commit session at this point in case write operation has been done |
537 |
# during `session_open` hooks |
|
538 |
session.commit() |
|
539 |
return session.id |
|
540 |
||
541 |
def execute(self, sessionid, rqlstring, args=None, eid_key=None, build_descr=True): |
|
542 |
"""execute a RQL query |
|
543 |
||
544 |
* rqlstring should be an unicode string or a plain ascii string |
|
545 |
* args the optional parameters used in the query |
|
546 |
* build_descr is a flag indicating if the description should be |
|
547 |
built on select queries |
|
548 |
""" |
|
549 |
session = self._get_session(sessionid, setpool=True) |
|
550 |
try: |
|
551 |
try: |
|
552 |
return self.querier.execute(session, rqlstring, args, eid_key, |
|
553 |
build_descr) |
|
554 |
except (Unauthorized, RQLSyntaxError): |
|
555 |
raise |
|
556 |
except ValidationError, ex: |
|
557 |
# need ValidationError normalization here so error may pass |
|
558 |
# through pyro |
|
559 |
if hasattr(ex.entity, 'eid'): |
|
560 |
ex.entity = ex.entity.eid # error raised by yams |
|
561 |
args = list(ex.args) |
|
562 |
args[0] = ex.entity |
|
563 |
ex.args = tuple(args) |
|
564 |
raise |
|
565 |
except: |
|
566 |
# FIXME: check error to catch internal errors |
|
567 |
self.exception('unexpected error') |
|
568 |
raise |
|
569 |
finally: |
|
570 |
session.reset_pool() |
|
1482 | 571 |
|
0 | 572 |
def describe(self, sessionid, eid): |
573 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
574 |
session = self._get_session(sessionid, setpool=True) |
|
575 |
try: |
|
576 |
return self.type_and_source_from_eid(eid, session) |
|
577 |
finally: |
|
578 |
session.reset_pool() |
|
579 |
||
580 |
def check_session(self, sessionid): |
|
581 |
"""raise `BadSessionId` if the connection is no more valid""" |
|
582 |
self._get_session(sessionid, setpool=False) |
|
583 |
||
584 |
def get_shared_data(self, sessionid, key, default=None, pop=False): |
|
585 |
"""return the session's data dictionary""" |
|
586 |
session = self._get_session(sessionid, setpool=False) |
|
587 |
return session.get_shared_data(key, default, pop) |
|
588 |
||
589 |
def set_shared_data(self, sessionid, key, value, querydata=False): |
|
590 |
"""set value associated to `key` in shared data |
|
591 |
||
592 |
if `querydata` is true, the value will be added to the repository |
|
593 |
session's query data which are cleared on commit/rollback of the current |
|
594 |
transaction, and won't be available through the connexion, only on the |
|
595 |
repository side. |
|
596 |
""" |
|
597 |
session = self._get_session(sessionid, setpool=False) |
|
598 |
session.set_shared_data(key, value, querydata) |
|
599 |
||
600 |
def commit(self, sessionid): |
|
601 |
"""commit transaction for the session with the given id""" |
|
602 |
self.debug('begin commit for session %s', sessionid) |
|
603 |
try: |
|
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:
1664
diff
changeset
|
604 |
self._get_session(sessionid).commit() |
1482 | 605 |
except (ValidationError, Unauthorized): |
0 | 606 |
raise |
607 |
except: |
|
608 |
self.exception('unexpected error') |
|
609 |
raise |
|
1482 | 610 |
|
0 | 611 |
def rollback(self, sessionid): |
612 |
"""commit transaction for the session with the given id""" |
|
613 |
self.debug('begin rollback for session %s', sessionid) |
|
614 |
try: |
|
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:
1664
diff
changeset
|
615 |
self._get_session(sessionid).rollback() |
0 | 616 |
except: |
617 |
self.exception('unexpected error') |
|
618 |
raise |
|
619 |
||
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
620 |
def close(self, sessionid, checkshuttingdown=True): |
0 | 621 |
"""close the session with the given id""" |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
622 |
session = self._get_session(sessionid, setpool=True, |
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
623 |
checkshuttingdown=checkshuttingdown) |
0 | 624 |
# operation uncommited before close are rollbacked before hook is called |
625 |
session.rollback() |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
626 |
self.hm.call_hooks('session_close', session) |
0 | 627 |
# commit session at this point in case write operation has been done |
628 |
# during `session_close` hooks |
|
629 |
session.commit() |
|
630 |
session.close() |
|
631 |
del self._sessions[sessionid] |
|
632 |
self.info('closed session %s for user %s', sessionid, session.user.login) |
|
1482 | 633 |
|
0 | 634 |
def user_info(self, sessionid, props=None): |
635 |
"""this method should be used by client to: |
|
636 |
* check session id validity |
|
637 |
* update user information on each user's request (i.e. groups and |
|
638 |
custom properties) |
|
639 |
""" |
|
640 |
session = self._get_session(sessionid, setpool=False) |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
641 |
if props is not None: |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
642 |
self.set_session_props(sessionid, props) |
0 | 643 |
user = session.user |
644 |
return user.eid, user.login, user.groups, user.properties |
|
1482 | 645 |
|
2245
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
646 |
def set_session_props(self, sessionid, props): |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
647 |
"""this method should be used by client to: |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
648 |
* check session id validity |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
649 |
* update user information on each user's request (i.e. groups and |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
650 |
custom properties) |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
651 |
""" |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
652 |
session = self._get_session(sessionid, setpool=False) |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
653 |
# update session properties |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
654 |
for prop, value in props.items(): |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
655 |
session.change_property(prop, value) |
7463e1a748dd
new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2192
diff
changeset
|
656 |
|
0 | 657 |
# public (inter-repository) interface ##################################### |
1482 | 658 |
|
0 | 659 |
def entities_modified_since(self, etypes, mtime): |
660 |
"""function designed to be called from an external repository which |
|
661 |
is using this one as a rql source for synchronization, and return a |
|
662 |
3-uple containing : |
|
663 |
* the local date |
|
664 |
* list of (etype, eid) of entities of the given types which have been |
|
665 |
modified since the given timestamp (actually entities whose full text |
|
666 |
index content has changed) |
|
667 |
* list of (etype, eid) of entities of the given types which have been |
|
668 |
deleted since the given timestamp |
|
669 |
""" |
|
670 |
session = self.internal_session() |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
636
diff
changeset
|
671 |
updatetime = datetime.now() |
0 | 672 |
try: |
673 |
modentities, delentities = self.system_source.modified_entities( |
|
674 |
session, etypes, mtime) |
|
675 |
return updatetime, modentities, delentities |
|
676 |
finally: |
|
677 |
session.close() |
|
678 |
||
679 |
# session handling ######################################################## |
|
1482 | 680 |
|
0 | 681 |
def close_sessions(self): |
682 |
"""close every opened sessions""" |
|
683 |
for sessionid in self._sessions.keys(): |
|
684 |
try: |
|
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
685 |
self.close(sessionid, checkshuttingdown=False) |
0 | 686 |
except: |
687 |
self.exception('error while closing session %s' % sessionid) |
|
688 |
||
689 |
def clean_sessions(self): |
|
690 |
"""close sessions not used since an amount of time specified in the |
|
691 |
configuration |
|
692 |
""" |
|
693 |
mintime = time() - self.config['session-time'] |
|
694 |
self.debug('cleaning session unused since %s', |
|
695 |
strftime('%T', localtime(mintime))) |
|
696 |
nbclosed = 0 |
|
697 |
for session in self._sessions.values(): |
|
698 |
if session.timestamp < mintime: |
|
699 |
self.close(session.id) |
|
700 |
nbclosed += 1 |
|
701 |
return nbclosed |
|
1482 | 702 |
|
0 | 703 |
def internal_session(self, cnxprops=None): |
704 |
"""return a dbapi like connection/cursor using internal user which |
|
705 |
have every rights on the repository. You'll *have to* commit/rollback |
|
706 |
or close (rollback implicitly) the session once the job's done, else |
|
707 |
you'll leak connections pool up to the time where no more pool is |
|
708 |
available, causing irremediable freeze... |
|
709 |
""" |
|
710 |
session = InternalSession(self, cnxprops) |
|
711 |
session.set_pool() |
|
712 |
return session |
|
1482 | 713 |
|
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
714 |
def _get_session(self, sessionid, setpool=False, checkshuttingdown=True): |
0 | 715 |
"""return the user associated to the given session identifier""" |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
716 |
if checkshuttingdown and self._shutting_down: |
1883
011e13d74cfc
shuting -> shutting
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1880
diff
changeset
|
717 |
raise Exception('Repository is shutting down') |
0 | 718 |
try: |
719 |
session = self._sessions[sessionid] |
|
720 |
except KeyError: |
|
721 |
raise BadConnectionId('No such session %s' % sessionid) |
|
722 |
if setpool: |
|
723 |
session.set_pool() |
|
724 |
return session |
|
725 |
||
726 |
# data sources handling ################################################### |
|
727 |
# * correspondance between eid and (type, source) |
|
728 |
# * correspondance between eid and local id (i.e. specific to a given source) |
|
729 |
# * searchable text indexes |
|
1482 | 730 |
|
0 | 731 |
def type_and_source_from_eid(self, eid, session=None): |
732 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
733 |
try: |
|
734 |
eid = typed_eid(eid) |
|
735 |
except ValueError: |
|
736 |
raise UnknownEid(eid) |
|
737 |
try: |
|
738 |
return self._type_source_cache[eid] |
|
739 |
except KeyError: |
|
740 |
if session is None: |
|
741 |
session = self.internal_session() |
|
742 |
reset_pool = True |
|
743 |
else: |
|
744 |
reset_pool = False |
|
745 |
try: |
|
746 |
etype, uri, extid = self.system_source.eid_type_source(session, |
|
747 |
eid) |
|
748 |
finally: |
|
749 |
if reset_pool: |
|
750 |
session.reset_pool() |
|
751 |
self._type_source_cache[eid] = (etype, uri, extid) |
|
752 |
if uri != 'system': |
|
753 |
self._extid_cache[(extid, uri)] = eid |
|
754 |
return etype, uri, extid |
|
755 |
||
756 |
def clear_caches(self, eids): |
|
757 |
etcache = self._type_source_cache |
|
758 |
extidcache = self._extid_cache |
|
759 |
rqlcache = self.querier._rql_cache |
|
760 |
for eid in eids: |
|
761 |
try: |
|
762 |
etype, uri, extid = etcache.pop(typed_eid(eid)) # may be a string in some cases |
|
763 |
rqlcache.pop('%s X WHERE X eid %s' % (etype, eid), None) |
|
764 |
extidcache.pop((extid, uri), None) |
|
765 |
except KeyError: |
|
766 |
etype = None |
|
767 |
rqlcache.pop('Any X WHERE X eid %s' % eid, None) |
|
768 |
for source in self.sources: |
|
769 |
source.clear_eid_cache(eid, etype) |
|
1482 | 770 |
|
0 | 771 |
def type_from_eid(self, eid, session=None): |
772 |
"""return the type of the entity with id <eid>""" |
|
773 |
return self.type_and_source_from_eid(eid, session)[0] |
|
1482 | 774 |
|
0 | 775 |
def source_from_eid(self, eid, session=None): |
776 |
"""return the source for the given entity's eid""" |
|
777 |
return self.sources_by_uri[self.type_and_source_from_eid(eid, session)[1]] |
|
1482 | 778 |
|
0 | 779 |
def eid2extid(self, source, eid, session=None): |
780 |
"""get local id from an eid""" |
|
781 |
etype, uri, extid = self.type_and_source_from_eid(eid, session) |
|
782 |
if source.uri != uri: |
|
783 |
# eid not from the given source |
|
784 |
raise UnknownEid(eid) |
|
785 |
return extid |
|
786 |
||
1954 | 787 |
def extid2eid(self, source, extid, etype, session=None, insert=True, |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
788 |
recreate=False): |
0 | 789 |
"""get eid from a local id. An eid is attributed if no record is found""" |
1954 | 790 |
cachekey = (extid, source.uri) |
0 | 791 |
try: |
792 |
return self._extid_cache[cachekey] |
|
793 |
except KeyError: |
|
794 |
pass |
|
795 |
reset_pool = False |
|
796 |
if session is None: |
|
797 |
session = self.internal_session() |
|
798 |
reset_pool = True |
|
1954 | 799 |
eid = self.system_source.extid2eid(session, source, extid) |
0 | 800 |
if eid is not None: |
801 |
self._extid_cache[cachekey] = eid |
|
1954 | 802 |
self._type_source_cache[eid] = (etype, source.uri, extid) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
803 |
if recreate: |
1954 | 804 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
805 |
entity._cw_recreating = True |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
806 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
807 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
808 |
# XXX add fti op ? |
1954 | 809 |
source.after_entity_insertion(session, extid, entity) |
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1228
diff
changeset
|
810 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
811 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 812 |
if reset_pool: |
813 |
session.reset_pool() |
|
814 |
return eid |
|
815 |
if not insert: |
|
816 |
return |
|
1954 | 817 |
# no link between extid and eid, create one using an internal session |
0 | 818 |
# since the current session user may not have required permissions to |
819 |
# do necessary stuff and we don't want to commit user session. |
|
820 |
# |
|
450
5e14ea0e81c8
a note for later
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
341
diff
changeset
|
821 |
# Moreover, even if session is already an internal session but is |
0 | 822 |
# processing a commit, we have to use another one |
823 |
if not session.is_internal_session: |
|
824 |
session = self.internal_session() |
|
825 |
reset_pool = True |
|
826 |
try: |
|
827 |
eid = self.system_source.create_eid(session) |
|
828 |
self._extid_cache[cachekey] = eid |
|
1954 | 829 |
self._type_source_cache[eid] = (etype, source.uri, extid) |
830 |
entity = source.before_entity_insertion(session, extid, etype, eid) |
|
0 | 831 |
if source.should_call_hooks: |
2893
5989ce0707bc
set edited_attributes before calling hooks for entities from external sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2881
diff
changeset
|
832 |
entity.edited_attributes = set(entity) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
833 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
450
5e14ea0e81c8
a note for later
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
341
diff
changeset
|
834 |
# XXX call add_info with complete=False ? |
1954 | 835 |
self.add_info(session, entity, source, extid) |
836 |
source.after_entity_insertion(session, extid, entity) |
|
0 | 837 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
838 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 839 |
else: |
840 |
# minimal meta-data |
|
841 |
session.execute('SET X is E WHERE X eid %(x)s, E name %(name)s', |
|
842 |
{'x': entity.eid, 'name': entity.id}, 'x') |
|
843 |
session.commit(reset_pool) |
|
844 |
return eid |
|
845 |
except: |
|
846 |
session.rollback(reset_pool) |
|
847 |
raise |
|
1482 | 848 |
|
0 | 849 |
def add_info(self, session, entity, source, extid=None, complete=True): |
850 |
"""add type and source info for an eid into the system table, |
|
851 |
and index the entity with the full text index |
|
852 |
""" |
|
853 |
# begin by inserting eid/type/source/extid into the entities table |
|
854 |
self.system_source.add_info(session, entity, source, extid) |
|
855 |
if complete: |
|
856 |
entity.complete(entity.e_schema.indexable_attributes()) |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
857 |
new = session.transaction_data.setdefault('neweids', set()) |
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
858 |
new.add(entity.eid) |
0 | 859 |
# now we can update the full text index |
1160
77bf88f01fcc
new delay-full-text-indexation configuration option
sylvain.thenault@logilab.fr
parents:
594
diff
changeset
|
860 |
if self.do_fti: |
77bf88f01fcc
new delay-full-text-indexation configuration option
sylvain.thenault@logilab.fr
parents:
594
diff
changeset
|
861 |
FTIndexEntityOp(session, entity=entity) |
0 | 862 |
CleanupEidTypeCacheOp(session) |
1482 | 863 |
|
0 | 864 |
def delete_info(self, session, eid): |
865 |
self._prepare_delete_info(session, eid) |
|
866 |
self._delete_info(session, eid) |
|
1482 | 867 |
|
0 | 868 |
def _prepare_delete_info(self, session, eid): |
869 |
"""prepare the repository for deletion of an entity: |
|
870 |
* update the fti |
|
871 |
* mark eid as being deleted in session info |
|
872 |
* setup cache update operation |
|
873 |
""" |
|
874 |
self.system_source.fti_unindex_entity(session, eid) |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
875 |
pending = session.transaction_data.setdefault('pendingeids', set()) |
0 | 876 |
pending.add(eid) |
877 |
CleanupEidTypeCacheOp(session) |
|
1482 | 878 |
|
0 | 879 |
def _delete_info(self, session, eid): |
880 |
"""delete system information on deletion of an entity: |
|
881 |
* delete all relations on this entity |
|
882 |
* transfer record from the entities table to the deleted_entities table |
|
883 |
""" |
|
884 |
etype, uri, extid = self.type_and_source_from_eid(eid, session) |
|
885 |
self._clear_eid_relations(session, etype, eid) |
|
886 |
self.system_source.delete_info(session, eid, etype, uri, extid) |
|
1482 | 887 |
|
0 | 888 |
def _clear_eid_relations(self, session, etype, eid): |
889 |
"""when a entity is deleted, build and execute rql query to delete all |
|
890 |
its relations |
|
891 |
""" |
|
892 |
rql = [] |
|
893 |
eschema = self.schema.eschema(etype) |
|
2641
9c33d98a074e
R [schema hooks] big refactoring / reorganization for clearer code, a few fixes on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2631
diff
changeset
|
894 |
pendingrtypes = session.transaction_data.get('pendingrtypes', ()) |
0 | 895 |
for rschema, targetschemas, x in eschema.relation_definitions(): |
896 |
rtype = rschema.type |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
897 |
if rtype in schema.VIRTUAL_RTYPES or rtype in pendingrtypes: |
0 | 898 |
continue |
899 |
var = '%s%s' % (rtype.upper(), x.upper()) |
|
900 |
if x == 'subject': |
|
901 |
# don't skip inlined relation so they are regularly |
|
902 |
# deleted and so hooks are correctly called |
|
2731
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
903 |
selection = 'X %s %s' % (rtype, var) |
0 | 904 |
else: |
2731
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
905 |
selection = '%s %s X' % (var, rtype) |
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
906 |
rql = 'DELETE %s WHERE X eid %%(x)s' % selection |
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
907 |
# unsafe_execute since we suppose that if user can delete the entity, |
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
908 |
# he can delete all its relations without security checking |
815b93271338
delete relations one by one to avoid too complex rql analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2709
diff
changeset
|
909 |
session.unsafe_execute(rql, {'x': eid}, 'x', build_descr=False) |
0 | 910 |
|
911 |
def index_entity(self, session, entity): |
|
912 |
"""full text index a modified entity""" |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
913 |
alreadydone = session.transaction_data.setdefault('indexedeids', set()) |
0 | 914 |
if entity.eid in alreadydone: |
915 |
self.info('skipping reindexation of %s, already done', entity.eid) |
|
916 |
return |
|
917 |
alreadydone.add(entity.eid) |
|
918 |
self.system_source.fti_index_entity(session, entity) |
|
1482 | 919 |
|
0 | 920 |
def locate_relation_source(self, session, subject, rtype, object): |
921 |
subjsource = self.source_from_eid(subject, session) |
|
922 |
objsource = self.source_from_eid(object, session) |
|
923 |
if not (subjsource is objsource and subjsource.support_relation(rtype, 1)): |
|
924 |
source = self.system_source |
|
925 |
if not source.support_relation(rtype, 1): |
|
926 |
raise RTypeNotSupportedBySources(rtype) |
|
927 |
else: |
|
928 |
source = subjsource |
|
929 |
return source |
|
1482 | 930 |
|
0 | 931 |
def locate_etype_source(self, etype): |
932 |
for source in self.sources: |
|
933 |
if source.support_entity(etype, 1): |
|
934 |
return source |
|
935 |
else: |
|
936 |
raise ETypeNotSupportedBySources(etype) |
|
1482 | 937 |
|
0 | 938 |
def glob_add_entity(self, session, entity): |
939 |
"""add an entity to the repository |
|
1482 | 940 |
|
0 | 941 |
the entity eid should originaly be None and a unique eid is assigned to |
942 |
the entity instance |
|
943 |
""" |
|
944 |
entity = entity.pre_add_hook() |
|
945 |
eschema = entity.e_schema |
|
946 |
etype = str(eschema) |
|
947 |
source = self.locate_etype_source(etype) |
|
948 |
# attribute an eid to the entity before calling hooks |
|
949 |
entity.set_eid(self.system_source.create_eid(session)) |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
950 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
951 |
print 'ADD entity', etype, entity.eid, dict(entity) |
0 | 952 |
entity._is_saved = False # entity has an eid but is not yet saved |
953 |
relations = [] |
|
954 |
# if inlined relations are specified, fill entity's related cache to |
|
955 |
# avoid unnecessary queries |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
956 |
entity.edited_attributes = set(entity) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
957 |
for attr in entity.edited_attributes: |
0 | 958 |
rschema = eschema.subject_relation(attr) |
959 |
if not rschema.is_final(): # inlined relation |
|
960 |
relations.append((attr, entity[attr])) |
|
961 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
962 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
0 | 963 |
entity.set_defaults() |
964 |
entity.check(creation=True) |
|
965 |
source.add_entity(session, entity) |
|
966 |
if source.uri != 'system': |
|
967 |
extid = source.get_extid(entity) |
|
968 |
self._extid_cache[(str(extid), source.uri)] = entity.eid |
|
969 |
else: |
|
970 |
extid = None |
|
971 |
self.add_info(session, entity, source, extid, complete=False) |
|
972 |
entity._is_saved = True # entity has an eid and is saved |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
973 |
# prefill entity relation caches |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
974 |
session.set_entity_cache(entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
975 |
for rschema in eschema.subject_relations(): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
976 |
rtype = str(rschema) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
977 |
if rtype in schema.VIRTUAL_RTYPES: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
978 |
continue |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
979 |
if rschema.is_final(): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
980 |
entity.setdefault(rtype, None) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
981 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
982 |
entity.set_related_cache(rtype, 'subject', session.empty_rset()) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
983 |
for rschema in eschema.object_relations(): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
984 |
rtype = str(rschema) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
985 |
if rtype in schema.VIRTUAL_RTYPES: |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
986 |
continue |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
987 |
entity.set_related_cache(rtype, 'object', session.empty_rset()) |
2756
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
988 |
# set inline relation cache before call to after_add_entity |
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
989 |
for attr, value in relations: |
2c94606eefc0
[server caching] should set cache before call to after_add_entity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2731
diff
changeset
|
990 |
session.update_rel_cache_add(entity.eid, attr, value) |
0 | 991 |
# trigger after_add_entity after after_add_relation |
992 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
993 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 994 |
# call hooks for inlined relations |
995 |
for attr, value in relations: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
996 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
997 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
998 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
999 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1000 |
return entity.eid |
1482 | 1001 |
|
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1002 |
def glob_update_entity(self, session, entity, edited_attributes): |
0 | 1003 |
"""replace an entity in the repository |
1004 |
the type and the eid of an entity must not be changed |
|
1005 |
""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1006 |
etype = str(entity.e_schema) |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1007 |
if server.DEBUG & server.DBG_REPO: |
2667
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1008 |
print 'UPDATE entity', etype, entity.eid, \ |
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1009 |
dict(entity), edited_attributes |
c8aa82538d8e
[repo] explicitly specifies edited attributes to glob_update_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2665
diff
changeset
|
1010 |
entity.edited_attributes = edited_attributes |
0 | 1011 |
entity.check() |
1012 |
eschema = entity.e_schema |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1013 |
session.set_entity_cache(entity) |
0 | 1014 |
only_inline_rels, need_fti_update = True, False |
1015 |
relations = [] |
|
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1016 |
for attr in edited_attributes: |
0 | 1017 |
if attr == 'eid': |
1018 |
continue |
|
1019 |
rschema = eschema.subject_relation(attr) |
|
1020 |
if rschema.is_final(): |
|
1021 |
if eschema.rproperty(attr, 'fulltextindexed'): |
|
1022 |
need_fti_update = True |
|
1023 |
only_inline_rels = False |
|
1024 |
else: |
|
1025 |
# inlined relation |
|
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1026 |
previous_value = entity.related(attr) or None |
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1027 |
if previous_value is not None: |
0 | 1028 |
previous_value = previous_value[0][0] # got a result set |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1029 |
if previous_value == entity[attr]: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1030 |
previous_value = None |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1031 |
else: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1032 |
self.hm.call_hooks('before_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1033 |
eidfrom=entity.eid, rtype=attr, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1034 |
eidto=previous_value) |
0 | 1035 |
relations.append((attr, entity[attr], previous_value)) |
1036 |
source = self.source_from_eid(entity.eid, session) |
|
1037 |
if source.should_call_hooks: |
|
1038 |
# call hooks for inlined relations |
|
1039 |
for attr, value, _ in relations: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1040 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1041 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1042 |
if not only_inline_rels: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1043 |
self.hm.call_hooks('before_update_entity', session, entity=entity) |
0 | 1044 |
source.update_entity(session, entity) |
1045 |
if not only_inline_rels: |
|
1160
77bf88f01fcc
new delay-full-text-indexation configuration option
sylvain.thenault@logilab.fr
parents:
594
diff
changeset
|
1046 |
if need_fti_update and self.do_fti: |
0 | 1047 |
# reindex the entity only if this query is updating at least |
1048 |
# one indexable attribute |
|
1049 |
FTIndexEntityOp(session, entity=entity) |
|
1050 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1051 |
self.hm.call_hooks('after_update_entity', session, entity=entity) |
0 | 1052 |
if source.should_call_hooks: |
1053 |
for attr, value, prevvalue in relations: |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1054 |
# if the relation is already cached, update existant cache |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1055 |
relcache = entity.relation_cached(attr, 'subject') |
2873
51bcd8e8f65c
[repo] misc cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2854
diff
changeset
|
1056 |
if prevvalue is not None: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1057 |
self.hm.call_hooks('after_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1058 |
eidfrom=entity.eid, rtype=attr, eidto=prevvalue) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1059 |
if relcache 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:
2641
diff
changeset
|
1060 |
session.update_rel_cache_del(entity.eid, attr, prevvalue) |
0 | 1061 |
del_existing_rel_if_needed(session, entity.eid, attr, value) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1062 |
if relcache 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:
2641
diff
changeset
|
1063 |
session.update_rel_cache_add(entity.eid, attr, value) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1064 |
else: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1065 |
entity.set_related_cache(attr, 'subject', |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1066 |
session.eid_rset(value)) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1067 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1068 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1069 |
|
1070 |
def glob_delete_entity(self, session, eid): |
|
1071 |
"""delete an entity and all related entities from the repository""" |
|
1072 |
# call delete_info before hooks |
|
1073 |
self._prepare_delete_info(session, eid) |
|
1074 |
etype, uri, extid = self.type_and_source_from_eid(eid, session) |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1075 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1076 |
print 'DELETE entity', etype, 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:
2641
diff
changeset
|
1077 |
if eid == 937: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1078 |
server.DEBUG |= (server.DBG_SQL | server.DBG_RQL | server.DBG_MORE) |
0 | 1079 |
source = self.sources_by_uri[uri] |
1080 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1081 |
entity = session.entity_from_eid(eid) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1082 |
self.hm.call_hooks('before_delete_entity', session, entity=entity) |
0 | 1083 |
self._delete_info(session, eid) |
1084 |
source.delete_entity(session, etype, eid) |
|
1085 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1086 |
self.hm.call_hooks('after_delete_entity', session, entity=entity) |
0 | 1087 |
# don't clear cache here this is done in a hook on commit |
1482 | 1088 |
|
0 | 1089 |
def glob_add_relation(self, session, subject, rtype, object): |
1090 |
"""add a relation to the repository""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1091 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1092 |
print 'ADD relation', subject, rtype, object |
0 | 1093 |
source = self.locate_relation_source(session, subject, rtype, object) |
1094 |
if source.should_call_hooks: |
|
1095 |
del_existing_rel_if_needed(session, subject, rtype, object) |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1096 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1097 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1098 |
source.add_relation(session, subject, rtype, object) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1099 |
rschema = self.schema.rschema(rtype) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1100 |
session.update_rel_cache_add(subject, rtype, object, rschema.symetric) |
0 | 1101 |
if source.should_call_hooks: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1102 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1103 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1104 |
|
1105 |
def glob_delete_relation(self, session, subject, rtype, object): |
|
1106 |
"""delete a relation from the repository""" |
|
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1107 |
if server.DEBUG & server.DBG_REPO: |
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1108 |
print 'DELETE relation', subject, rtype, object |
0 | 1109 |
source = self.locate_relation_source(session, subject, rtype, object) |
1110 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1111 |
self.hm.call_hooks('before_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1112 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1113 |
source.delete_relation(session, subject, rtype, object) |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1114 |
rschema = self.schema.rschema(rtype) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1115 |
session.update_rel_cache_del(subject, rtype, object, rschema.symetric) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2641
diff
changeset
|
1116 |
if rschema.symetric: |
0 | 1117 |
# on symetric relation, we can't now in which sense it's |
1118 |
# stored so try to delete both |
|
1119 |
source.delete_relation(session, object, rtype, subject) |
|
1120 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1121 |
self.hm.call_hooks('after_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1122 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1123 |
|
1124 |
||
1125 |
# pyro handling ########################################################### |
|
1482 | 1126 |
|
0 | 1127 |
def pyro_register(self, host=''): |
1128 |
"""register the repository as a pyro object""" |
|
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1129 |
from logilab.common.pyro_ext import register_object |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1130 |
appid = self.config['pyro-id'] or self.config.appid |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1131 |
daemon = register_object(self, appid, self.config['pyro-ns-group'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1132 |
self.config['pyro-host'], |
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1133 |
self.config['pyro-ns-host']) |
0 | 1134 |
msg = 'repository registered as a pyro object using group %s and id %s' |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1135 |
self.info(msg, self.config['pyro-ns-group'], appid) |
0 | 1136 |
self.pyro_registered = True |
1137 |
return daemon |
|
1482 | 1138 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1139 |
# multi-sources planner helpers ########################################### |
1482 | 1140 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1141 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1142 |
def rel_type_sources(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1143 |
return [source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1144 |
if source.support_relation(rtype) |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1145 |
or rtype in source.dont_cross_relations] |
1482 | 1146 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1147 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1148 |
def can_cross_relation(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1149 |
return [source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1150 |
if source.support_relation(rtype) |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1151 |
and rtype in source.cross_relations] |
1482 | 1152 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1153 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1154 |
def is_multi_sources_relation(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1155 |
return any(source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1156 |
if not source is self.system_source |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1157 |
and source.support_relation(rtype)) |
1482 | 1158 |
|
0 | 1159 |
|
1160 |
def pyro_unregister(config): |
|
1161 |
"""unregister the repository from the pyro name server""" |
|
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1162 |
from logilab.common.pyro_ext import ns_unregister |
0 | 1163 |
appid = config['pyro-id'] or config.appid |
2665
0c6281487f90
[pyro] use lgc.pyro_ext, simplify pyro related options
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
1164 |
ns_unregister(appid, config['pyro-ns-group'], config['pyro-ns-host']) |
0 | 1165 |
|
1166 |
||
1167 |
from logging import getLogger |
|
1168 |
from cubicweb import set_log_methods |
|
1169 |
set_log_methods(Repository, getLogger('cubicweb.repository')) |