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