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