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