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