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