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