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