author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 16 Feb 2012 14:15:37 +0100 | |
changeset 8239 | c6cdd060212e |
parent 8236 | cc70da744f43 |
parent 8238 | 087bb529035c |
child 8268 | c9babe49c1c1 |
permissions | -rw-r--r-- |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5376
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Defines the central class for the CubicWeb RQL server: the repository. |
19 |
||
20 |
The repository is an abstraction allowing execution of rql queries against |
|
21 |
data sources. Most of the work is actually done in helper classes. The |
|
22 |
repository mainly: |
|
23 |
||
24 |
* 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
|
25 |
point to a cubicweb instance. |
0 | 26 |
* handles session management |
27 |
* provides method for pyro registration, to call if pyro is enabled |
|
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
28 |
""" |
0 | 29 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
30 |
from __future__ import with_statement |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
31 |
|
0 | 32 |
__docformat__ = "restructuredtext en" |
33 |
||
34 |
import sys |
|
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
35 |
import threading |
0 | 36 |
import Queue |
7507
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
37 |
from warnings import warn |
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
38 |
from itertools import chain |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4714
diff
changeset
|
39 |
from os.path import join |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
636
diff
changeset
|
40 |
from datetime import datetime |
0 | 41 |
from time import time, localtime, strftime |
42 |
||
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
43 |
from logilab.common.decorators import cached, clear_cache |
3757 | 44 |
from logilab.common.compat import any |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
45 |
from logilab.common import flatten |
0 | 46 |
|
47 |
from yams import BadSchemaDefinition |
|
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
48 |
from yams.schema import role_name |
0 | 49 |
from rql import RQLSyntaxError |
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
50 |
from rql.utils import rqlvar_maker |
0 | 51 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
52 |
from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, QueryError, |
2709
6ee816eb9f25
[hooksmanager] hooks should be reseted after vreg reload
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
53 |
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
|
54 |
ETypeNotSupportedBySources, MultiSourcesError, |
0 | 55 |
BadConnectionId, Unauthorized, ValidationError, |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
56 |
RepositoryError, UniqueTogetherError, typed_eid, onevent) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
57 |
from cubicweb import cwvreg, schema, server |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7550
diff
changeset
|
58 |
from cubicweb.server import ShuttingDown, utils, hook, pool, querier, sources |
5079
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
59 |
from cubicweb.server.session import Session, InternalSession, InternalManager, \ |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
60 |
security_enabled |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
61 |
from cubicweb.server.ssplanner import EditedEntity |
0 | 62 |
|
7780
a1d5365fefc1
[cubification] moved CWPermission handling into a localperms cube. closes #1914011
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7723
diff
changeset
|
63 |
NO_CACHE_RELATIONS = set( [('owned_by', 'object'), |
7373
e5e6ef56cfb5
[repo cache] there are some relations we don't want to cache, they may cause memory leak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7305
diff
changeset
|
64 |
('created_by', 'object'), |
e5e6ef56cfb5
[repo cache] there are some relations we don't want to cache, they may cause memory leak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7305
diff
changeset
|
65 |
('cw_source', 'object'), |
e5e6ef56cfb5
[repo cache] there are some relations we don't want to cache, they may cause memory leak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7305
diff
changeset
|
66 |
]) |
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
67 |
|
6467
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
68 |
def prefill_entity_caches(entity, relations): |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
69 |
session = entity._cw |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
70 |
# prefill entity relation caches |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
71 |
for rschema in entity.e_schema.subject_relations(): |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
72 |
rtype = str(rschema) |
7373
e5e6ef56cfb5
[repo cache] there are some relations we don't want to cache, they may cause memory leak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7305
diff
changeset
|
73 |
if rtype in schema.VIRTUAL_RTYPES or (rtype, 'subject') in NO_CACHE_RELATIONS: |
6467
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
74 |
continue |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
75 |
if rschema.final: |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
76 |
entity.cw_attr_cache.setdefault(rtype, None) |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
77 |
else: |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
78 |
entity.cw_set_relation_cache(rtype, 'subject', |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
79 |
session.empty_rset()) |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
80 |
for rschema in entity.e_schema.object_relations(): |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
81 |
rtype = str(rschema) |
7373
e5e6ef56cfb5
[repo cache] there are some relations we don't want to cache, they may cause memory leak
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7305
diff
changeset
|
82 |
if rtype in schema.VIRTUAL_RTYPES or (rtype, 'object') in NO_CACHE_RELATIONS: |
6467
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
83 |
continue |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
84 |
entity.cw_set_relation_cache(rtype, 'object', session.empty_rset()) |
869813c2dd40
[entity, repo] move prefill_entity_cache function back to repository due to usage of del_existing_rel_if_needed function which is also defined there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6466
diff
changeset
|
85 |
|
0 | 86 |
def del_existing_rel_if_needed(session, eidfrom, rtype, eidto): |
87 |
"""delete existing relation when adding a new one if card is 1 or ? |
|
88 |
||
89 |
have to be done once the new relation has been inserted to avoid having |
|
90 |
an entity without a relation for some time |
|
91 |
||
92 |
this kind of behaviour has to be done in the repository so we don't have |
|
93 |
hooks order hazardness |
|
94 |
""" |
|
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
95 |
# skip that if integrity explicitly disabled |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
96 |
if not session.is_hook_category_activated('activeintegrity'): |
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
|
97 |
return |
7502
e7190f7e850e
[session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
98 |
rdef = session.rtype_eids_rdef(rtype, eidfrom, eidto) |
e7190f7e850e
[session] deprecates schema_rproperty in favor of more optimized rtype_eids_rdef which return the rdef (so reusable to gather other data)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
99 |
card = rdef.cardinality |
0 | 100 |
# one may be tented to check for neweids but this may cause more than one |
101 |
# relation even with '1?' cardinality if thoses relations are added in the |
|
102 |
# same transaction where the entity is being created. This never occurs from |
|
103 |
# the web interface but may occurs during test or dbapi connection (though |
|
104 |
# not expected for this). So: don't do it, we pretend to ensure repository |
|
105 |
# 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
|
106 |
# |
6189 | 107 |
# notes: |
108 |
# * inlined relations will be implicitly deleted for the subject entity |
|
109 |
# * we don't want read permissions to be applied but we want delete |
|
110 |
# permission to be checked |
|
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
111 |
if card[0] in '1?': |
6189 | 112 |
with security_enabled(session, read=False): |
113 |
session.execute('DELETE X %s Y WHERE X eid %%(x)s, ' |
|
114 |
'NOT Y eid %%(y)s' % rtype, |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
115 |
{'x': eidfrom, 'y': eidto}) |
0 | 116 |
if card[1] in '1?': |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
117 |
with security_enabled(session, read=False): |
5711
ea2538a3c622
[repo] fix typo
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5641
diff
changeset
|
118 |
session.execute('DELETE X %s Y WHERE Y eid %%(y)s, ' |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
119 |
'NOT X eid %%(x)s' % rtype, |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
120 |
{'x': eidfrom, 'y': eidto}) |
1482 | 121 |
|
3694 | 122 |
|
8211
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
123 |
class NullEventBus(object): |
8214
ce9556358dbd
[event bus] event has been renamed to publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8211
diff
changeset
|
124 |
def publish(self, msg): |
8211
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
125 |
pass |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
126 |
|
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
127 |
def add_subscription(self, topic, callback): |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
128 |
pass |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
129 |
|
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
130 |
def start(self): |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
131 |
pass |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
132 |
|
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
133 |
def stop(self): |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
134 |
pass |
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
135 |
|
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
136 |
|
0 | 137 |
class Repository(object): |
138 |
"""a repository provides access to a set of persistent storages for |
|
139 |
entities and relations |
|
140 |
||
141 |
XXX protect pyro access |
|
142 |
""" |
|
1482 | 143 |
|
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
144 |
def __init__(self, config, vreg=None): |
0 | 145 |
self.config = config |
146 |
if vreg is None: |
|
8190
2a3c1b787688
[vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8133
diff
changeset
|
147 |
vreg = cwvreg.CWRegistryStore(config) |
0 | 148 |
self.vreg = vreg |
149 |
self.pyro_registered = False |
|
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
150 |
self.pyro_uri = None |
8211
543e1579ba0d
[repo] Add a publish/subscribe mechanism for inter-instance communication using zmq
Julien Cristau <julien.cristau@logilab.fr>, Quentin Roquefort <quentin@kpsule.me>
parents:
8190
diff
changeset
|
151 |
self.app_instances_bus = NullEventBus() |
0 | 152 |
self.info('starting repository from %s', self.config.apphome) |
153 |
# dictionary of opened sessions |
|
154 |
self._sessions = {} |
|
155 |
# list of functions to be called at regular interval |
|
156 |
self._looping_tasks = [] |
|
157 |
# list of running threads |
|
158 |
self._running_threads = [] |
|
159 |
# initial schema, should be build or replaced latter |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
160 |
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
|
161 |
self.vreg.schema = self.schema # until actual schema is loaded... |
5906
d40ced753291
[repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5813
diff
changeset
|
162 |
# shutdown flag |
d40ced753291
[repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5813
diff
changeset
|
163 |
self.shutting_down = False |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
164 |
# sources (additional sources info in the system database) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
165 |
self.system_source = self.get_source('native', 'system', |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
166 |
config.sources()['system']) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
167 |
self.sources = [self.system_source] |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
168 |
self.sources_by_uri = {'system': self.system_source} |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
169 |
# querier helper, need to be created after sources initialization |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
170 |
self.querier = querier.QuerierHelper(self, self.schema) |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
171 |
# cache eid -> (type, physical source, extid, actual source) |
0 | 172 |
self._type_source_cache = {} |
173 |
# cache (extid, source uri) -> eid |
|
174 |
self._extid_cache = {} |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
175 |
# open some connections set |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
176 |
if config.init_cnxset_pool: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
177 |
self.init_cnxset_pool() |
5079
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
178 |
@onevent('after-registry-reload', self) |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
179 |
def fix_user_classes(self): |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
180 |
usercls = self.vreg['etypes'].etype_class('CWUser') |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
181 |
for session in self._sessions.values(): |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
182 |
if not isinstance(session.user, InternalManager): |
e646047f80cb
[reload] on registry reloading, we should change class of users of opened session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5073
diff
changeset
|
183 |
session.user.__class__ = usercls |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
184 |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
185 |
def init_cnxset_pool(self): |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
186 |
config = self.config |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
187 |
self._cnxsets_pool = Queue.Queue() |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
188 |
self._cnxsets_pool.put_nowait(pool.ConnectionsSet(self.sources)) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
189 |
if config.quick_start: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
190 |
# quick start, usually only to get a minimal repository to get cubes |
5084
d8f491cb046c
[repo] we want a hooks manager and hooks initialized event on quick start (we trigger events such as server_backup / server_restore for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5079
diff
changeset
|
191 |
# information (eg dump/restore/...) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
192 |
config._cubes = () |
5084
d8f491cb046c
[repo] we want a hooks manager and hooks initialized event on quick start (we trigger events such as server_backup / server_restore for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5079
diff
changeset
|
193 |
# only load hooks and entity classes in the registry |
7305
1f97c05cfbab
[repository quick start] should modify class attribute to get this actually working (used by a classmethod)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7238
diff
changeset
|
194 |
config.__class__.cube_appobject_path = set(('hooks', 'entities')) |
1f97c05cfbab
[repository quick start] should modify class attribute to get this actually working (used by a classmethod)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7238
diff
changeset
|
195 |
config.__class__.cubicweb_appobject_path = set(('hooks', 'entities')) |
5084
d8f491cb046c
[repo] we want a hooks manager and hooks initialized event on quick start (we trigger events such as server_backup / server_restore for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5079
diff
changeset
|
196 |
self.set_schema(config.load_schema()) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
197 |
config['connections-pool-size'] = 1 |
5084
d8f491cb046c
[repo] we want a hooks manager and hooks initialized event on quick start (we trigger events such as server_backup / server_restore for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5079
diff
changeset
|
198 |
# will be reinitialized later from cubes found in the database |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
199 |
config._cubes = None |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
200 |
elif config.creating: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
201 |
# repository creation |
0 | 202 |
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
|
203 |
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
|
204 |
# need to load the Any and CWUser entity types |
0 | 205 |
etdirectory = join(CW_SOFTWARE_ROOT, 'entities') |
1317 | 206 |
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
|
207 |
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
|
208 |
self.vreg.load_file(join(etdirectory, '%s.py' % modname), |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
209 |
'cubicweb.entities.%s' % modname) |
5447
f29dafba250a
[vreg repo] shouldn't call init_registration in on hook registry bootstrap, already done before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5442
diff
changeset
|
210 |
hooksdirectory = join(CW_SOFTWARE_ROOT, 'hooks') |
f29dafba250a
[vreg repo] shouldn't call init_registration in on hook registry bootstrap, already done before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5442
diff
changeset
|
211 |
self.vreg.load_file(join(hooksdirectory, 'metadata.py'), |
f29dafba250a
[vreg repo] shouldn't call init_registration in on hook registry bootstrap, already done before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5442
diff
changeset
|
212 |
'cubicweb.hooks.metadata') |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
213 |
elif config.read_instance_schema: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
214 |
# normal start: load the instance schema from the database |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
215 |
self.fill_schema() |
0 | 216 |
else: |
217 |
# 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
|
218 |
self.warning("set fs instance'schema") |
0 | 219 |
config.bootstrap_cubes() |
2961 | 220 |
self.set_schema(config.load_schema()) |
0 | 221 |
if not config.creating: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
222 |
self.init_sources_from_database() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
223 |
if 'CWProperty' in self.schema: |
0 | 224 |
self.vreg.init_properties(self.properties()) |
225 |
else: |
|
6128 | 226 |
# call init_creating so that for instance native source can |
227 |
# configurate tsearch according to postgres version |
|
0 | 228 |
for source in self.sources: |
229 |
source.init_creating() |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
230 |
# close initialization connetions set and reopen fresh ones for proper |
0 | 231 |
# initialization now that we know cubes |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
232 |
self._get_cnxset().close(True) |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
233 |
# list of available_cnxsets (we can't iterate on Queue instance) |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
234 |
self.cnxsets = [] |
0 | 235 |
for i in xrange(config['connections-pool-size']): |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
236 |
self.cnxsets.append(pool.ConnectionsSet(self.sources)) |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
237 |
self._cnxsets_pool.put_nowait(self.cnxsets[-1]) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
238 |
if config.quick_start: |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
239 |
config.init_cubes(self.get_cubes()) |
5093
8d073d2e089d
[optimization] improve massive write performance by optimizing hooks selection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5090
diff
changeset
|
240 |
self.hm = hook.HooksManager(self.vreg) |
1482 | 241 |
|
0 | 242 |
# internals ############################################################### |
243 |
||
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
244 |
def init_sources_from_database(self): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
245 |
self.sources_by_eid = {} |
6664
7b6acf314a7a
[repo] don't attempt to read sources from database on quick start. Fix crash after restoring a pre 3.10 database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6629
diff
changeset
|
246 |
if self.config.quick_start \ |
7b6acf314a7a
[repo] don't attempt to read sources from database on quick start. Fix crash after restoring a pre 3.10 database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6629
diff
changeset
|
247 |
or not 'CWSource' in self.schema: # # 3.10 migration |
7550
183a61d1bab9
[3.10] fix typo and error for pre 3.10 migration w/ postgres: we *must* call init_creating else the fti isn't correctly initialized, which may lead to silently rollbacked transaction
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7513
diff
changeset
|
248 |
self.system_source.init_creating() |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
249 |
return |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
250 |
session = self.internal_session() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
251 |
try: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
252 |
# FIXME: sources should be ordered (add_entity priority) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
253 |
for sourceent in session.execute( |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
254 |
'Any S, SN, SA, SC WHERE S is_instance_of CWSource, ' |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
255 |
'S name SN, S type SA, S config SC').entities(): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
256 |
if sourceent.name == 'system': |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
257 |
self.system_source.eid = sourceent.eid |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
258 |
self.sources_by_eid[sourceent.eid] = self.system_source |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
259 |
self.system_source.init(True, sourceent) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
260 |
continue |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
261 |
self.add_source(sourceent, add_to_cnxsets=False) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
262 |
finally: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
263 |
session.close() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
264 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
265 |
def _clear_planning_caches(self): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
266 |
for cache in ('source_defs', 'is_multi_sources_relation', |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
267 |
'can_cross_relation', 'rel_type_sources'): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
268 |
clear_cache(self, cache) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
269 |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
270 |
def add_source(self, sourceent, add_to_cnxsets=True): |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
271 |
source = self.get_source(sourceent.type, sourceent.name, |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6941
diff
changeset
|
272 |
sourceent.host_config, sourceent.eid) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
273 |
self.sources_by_eid[sourceent.eid] = source |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
274 |
self.sources_by_uri[sourceent.name] = source |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
275 |
if self.config.source_enabled(source): |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
276 |
# call source's init method to complete their initialisation if |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
277 |
# needed (for instance looking for persistent configuration using an |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
278 |
# internal session, which is not possible until connections sets have been |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
279 |
# initialized) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
280 |
source.init(True, sourceent) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
281 |
if not source.copy_based_source: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
282 |
self.sources.append(source) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
283 |
self.querier.set_planner() |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
284 |
if add_to_cnxsets: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
285 |
for cnxset in self.cnxsets: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
286 |
cnxset.add_source(source) |
6724
24bf6f181d0e
[pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
287 |
else: |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
288 |
source.init(False, sourceent) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
289 |
self._clear_planning_caches() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
290 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
291 |
def remove_source(self, uri): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
292 |
source = self.sources_by_uri.pop(uri) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
293 |
del self.sources_by_eid[source.eid] |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
294 |
if self.config.source_enabled(source) and not source.copy_based_source: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
295 |
self.sources.remove(source) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
296 |
self.querier.set_planner() |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
297 |
for cnxset in self.cnxsets: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
298 |
cnxset.remove_source(source) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
299 |
self._clear_planning_caches() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
300 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6941
diff
changeset
|
301 |
def get_source(self, type, uri, source_config, eid=None): |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
302 |
# set uri and type in source config so it's available through |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
303 |
# source_defs() |
0 | 304 |
source_config['uri'] = uri |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
305 |
source_config['type'] = type |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6941
diff
changeset
|
306 |
return sources.get_source(type, source_config, self, eid) |
1482 | 307 |
|
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
|
308 |
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
|
309 |
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
|
310 |
schema.rebuild_infered_relations() |
0 | 311 |
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
|
312 |
if resetvreg: |
2881
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
313 |
if self.config._cubes is None: |
d1a5b77e42bc
fix set_schema/init_cubes order
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2880
diff
changeset
|
314 |
self.config.init_cubes(self.get_cubes()) |
5447
f29dafba250a
[vreg repo] shouldn't call init_registration in on hook registry bootstrap, already done before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5442
diff
changeset
|
315 |
# trigger full reload of all appobjects |
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
|
316 |
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
|
317 |
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
|
318 |
self.vreg._set_schema(schema) |
0 | 319 |
self.querier.set_schema(schema) |
6126
aca6a2c357fd
[repository] enabled sources refactoring: to avoid error, we should always have all known source in repo.sources_by_uri and only enabled ones in repo.sources, so we still have access to the definition of temporarily disabled sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
320 |
# don't use self.sources, we may want to give schema even to disabled |
aca6a2c357fd
[repository] enabled sources refactoring: to avoid error, we should always have all known source in repo.sources_by_uri and only enabled ones in repo.sources, so we still have access to the definition of temporarily disabled sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
321 |
# sources |
aca6a2c357fd
[repository] enabled sources refactoring: to avoid error, we should always have all known source in repo.sources_by_uri and only enabled ones in repo.sources, so we still have access to the definition of temporarily disabled sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6061
diff
changeset
|
322 |
for source in self.sources_by_uri.values(): |
0 | 323 |
source.set_schema(schema) |
324 |
self.schema = schema |
|
325 |
||
326 |
def fill_schema(self): |
|
327 |
"""lod schema from the repository""" |
|
328 |
from cubicweb.server.schemaserial import deserialize_schema |
|
329 |
self.info('loading schema from the repository') |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
330 |
appschema = schema.CubicWebSchema(self.config.appid) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
331 |
self.set_schema(self.config.load_bootstrap_schema(), resetvreg=False) |
0 | 332 |
self.debug('deserializing db schema into %s %#x', appschema.name, id(appschema)) |
333 |
session = self.internal_session() |
|
334 |
try: |
|
335 |
try: |
|
336 |
deserialize_schema(appschema, session) |
|
337 |
except BadSchemaDefinition: |
|
338 |
raise |
|
339 |
except Exception, ex: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
340 |
import traceback |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
341 |
traceback.print_exc() |
1482 | 342 |
raise Exception('Is the database initialised ? (cause: %s)' % |
0 | 343 |
(ex.args and ex.args[0].strip() or 'unknown')), \ |
344 |
None, sys.exc_info()[-1] |
|
345 |
finally: |
|
346 |
session.close() |
|
347 |
self.set_schema(appschema) |
|
1482 | 348 |
|
0 | 349 |
def start_looping_tasks(self): |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
350 |
if not (self.config.creating or self.config.repairing |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
351 |
or self.config.quick_start): |
4958
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
352 |
# call instance level initialisation hooks |
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
353 |
self.hm.call_hooks('server_startup', repo=self) |
665eacdd8c50
[repo] call server_startup event in start_looping_task the repo is fully started (registered in pyro for instance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
354 |
# register a task to cleanup expired session |
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
355 |
self.cleanup_session_time = self.config['cleanup-session-time'] or 60 * 60 * 24 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
356 |
assert self.cleanup_session_time > 0 |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
357 |
cleanup_session_interval = min(60*60, self.cleanup_session_time / 3) |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
358 |
self.looping_task(cleanup_session_interval, self.clean_sessions) |
0 | 359 |
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
|
360 |
for i, (interval, func, args) in enumerate(self._looping_tasks): |
7823
5f145462e041
[repo] ensure we don't restart a task while the repo is shutting down (closes #1942736)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7815
diff
changeset
|
361 |
self._looping_tasks[i] = task = utils.LoopTask(self, interval, func, args) |
0 | 362 |
self.info('starting task %s with interval %.2fs', task.name, |
363 |
interval) |
|
364 |
task.start() |
|
365 |
# ensure no tasks will be further added |
|
366 |
self._looping_tasks = tuple(self._looping_tasks) |
|
367 |
||
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
368 |
def looping_task(self, interval, func, *args): |
0 | 369 |
"""register a function to be called every `interval` seconds. |
1482 | 370 |
|
0 | 371 |
looping tasks can only be registered during repository initialization, |
372 |
once done this method will fail. |
|
373 |
""" |
|
374 |
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
|
375 |
self._looping_tasks.append( (interval, func, args) ) |
0 | 376 |
except AttributeError: |
377 |
raise RuntimeError("can't add looping task once the repository is started") |
|
378 |
||
379 |
def threaded_task(self, func): |
|
380 |
"""start function in a separated thread""" |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
381 |
t = utils.RepoThread(func, self._running_threads) |
0 | 382 |
t.start() |
1482 | 383 |
|
0 | 384 |
#@locked |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
385 |
def _get_cnxset(self): |
0 | 386 |
try: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
387 |
return self._cnxsets_pool.get(True, timeout=5) |
0 | 388 |
except Queue.Empty: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
389 |
raise Exception('no connections set available after 5 secs, probably either a ' |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
390 |
'bug in code (too many uncommited/rollbacked ' |
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
391 |
'connections) or too much load on the server (in ' |
0 | 392 |
'which case you can try to set a bigger ' |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
393 |
'connections pool size)') |
1482 | 394 |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
395 |
def _free_cnxset(self, cnxset): |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
396 |
self._cnxsets_pool.put_nowait(cnxset) |
0 | 397 |
|
398 |
def pinfo(self): |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
399 |
# XXX: session.cnxset is accessed from a local storage, would be interesting |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
400 |
# to see if there is a cnxset set in any thread specific data) |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
401 |
return '%s: %s (%s)' % (self._cnxsets_pool.qsize(), |
0 | 402 |
','.join(session.user.login for session in self._sessions.values() |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
403 |
if session.cnxset), |
0 | 404 |
threading.currentThread()) |
405 |
def shutdown(self): |
|
406 |
"""called on server stop event to properly close opened sessions and |
|
407 |
connections |
|
408 |
""" |
|
5906
d40ced753291
[repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5813
diff
changeset
|
409 |
assert not self.shutting_down, 'already shutting down' |
d40ced753291
[repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5813
diff
changeset
|
410 |
self.shutting_down = True |
5749
b4393b681f7a
[repo] on repository shutdown, we've to close the new eid creation connection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5747
diff
changeset
|
411 |
self.system_source.shutdown() |
0 | 412 |
if isinstance(self._looping_tasks, tuple): # if tasks have been started |
413 |
for looptask in self._looping_tasks: |
|
414 |
self.info('canceling task %s...', looptask.name) |
|
415 |
looptask.cancel() |
|
416 |
looptask.join() |
|
417 |
self.info('task %s finished', looptask.name) |
|
418 |
for thread in self._running_threads: |
|
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5292
diff
changeset
|
419 |
self.info('waiting thread %s...', thread.getName()) |
0 | 420 |
thread.join() |
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5292
diff
changeset
|
421 |
self.info('thread %s finished', thread.getName()) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
422 |
if not (self.config.creating or self.config.repairing |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
423 |
or self.config.quick_start): |
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
|
424 |
self.hm.call_hooks('server_shutdown', repo=self) |
0 | 425 |
self.close_sessions() |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
426 |
while not self._cnxsets_pool.empty(): |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
427 |
cnxset = self._cnxsets_pool.get_nowait() |
0 | 428 |
try: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
429 |
cnxset.close(True) |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7782
diff
changeset
|
430 |
except Exception: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
431 |
self.exception('error while closing %s' % cnxset) |
0 | 432 |
continue |
433 |
if self.pyro_registered: |
|
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
434 |
if self._use_pyrons(): |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
435 |
pyro_unregister(self.config) |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
436 |
self.pyro_uri = None |
0 | 437 |
hits, misses = self.querier.cache_hit, self.querier.cache_miss |
438 |
try: |
|
5747
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
439 |
self.info('rql st cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
0 | 440 |
(hits * 100) / (hits + misses)) |
441 |
hits, misses = self.system_source.cache_hit, self.system_source.cache_miss |
|
442 |
self.info('sql cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
443 |
(hits * 100) / (hits + misses)) |
|
444 |
nocache = self.system_source.no_cache |
|
445 |
self.info('sql cache usage: %s/%s (%s%%)', hits+ misses, nocache, |
|
446 |
((hits + misses) * 100) / (hits + misses + nocache)) |
|
447 |
except ZeroDivisionError: |
|
448 |
pass |
|
1482 | 449 |
|
6849
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
450 |
def check_auth_info(self, session, login, authinfo): |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
451 |
"""validate authentication, raise AuthenticationError on failure, return |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
452 |
associated CWUser's eid on success. |
0 | 453 |
""" |
8229
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
454 |
# iter on sources_by_uri then check enabled source since sources doesn't |
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
455 |
# contain copy based sources |
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
456 |
for source in self.sources_by_uri.itervalues(): |
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
457 |
if self.config.source_enabled(source) and source.support_entity('CWUser'): |
0 | 458 |
try: |
6849
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
459 |
return source.authenticate(session, login, **authinfo) |
0 | 460 |
except AuthenticationError: |
461 |
continue |
|
462 |
else: |
|
463 |
raise AuthenticationError('authentication failed with all sources') |
|
6849
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
464 |
|
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
465 |
def authenticate_user(self, session, login, **authinfo): |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
466 |
"""validate login / password, raise AuthenticationError on failure |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
467 |
return associated CWUser instance on success |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
468 |
""" |
5a0c2cfc19bf
[repository auth] cleanup email login by turning it into a proper repo-side authentication plugin
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6822
diff
changeset
|
469 |
eid = self.check_auth_info(session, login, authinfo) |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
470 |
cwuser = self._build_user(session, eid) |
0 | 471 |
if self.config.consider_user_state and \ |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5447
diff
changeset
|
472 |
not cwuser.cw_adapt_to('IWorkflowable').state in cwuser.AUTHENTICABLE_STATES: |
0 | 473 |
raise AuthenticationError('user is not in authenticable state') |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
474 |
return cwuser |
0 | 475 |
|
476 |
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
|
477 |
"""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
|
478 |
cls = self.vreg['etypes'].etype_class('CWUser') |
7794
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7780
diff
changeset
|
479 |
st = cls.fetch_rqlst(session.user, ordermethod=None) |
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7780
diff
changeset
|
480 |
st.add_eid_restriction(st.get_variable('X'), 'x', 'Substitute') |
aed065b97f12
refactor entity fetch_rql method to use a RQL syntax tree instead of RQL strings ; closes #1585650
Florent Cayré <florent.cayre@gmail.com>
parents:
7780
diff
changeset
|
481 |
rset = session.execute(st.as_string(), {'x': eid}) |
0 | 482 |
assert len(rset) == 1, rset |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
483 |
cwuser = rset.get_entity(0, 0) |
6491
ee9a10b6620e
pylint option update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6467
diff
changeset
|
484 |
# pylint: disable=W0104 |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
485 |
# prefetch / cache cwuser's groups and properties. This is especially |
0 | 486 |
# useful for internal sessions to avoid security insertions |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
487 |
cwuser.groups |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
488 |
cwuser.properties |
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
489 |
return cwuser |
1482 | 490 |
|
0 | 491 |
# public (dbapi) interface ################################################ |
1482 | 492 |
|
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
493 |
def stats(self): # XXX restrict to managers session? |
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
494 |
"""Return a dictionary containing some statistics about the repository |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
495 |
resources usage. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
496 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
497 |
This is a public method, not requiring a session id. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
498 |
""" |
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
499 |
results = {} |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
500 |
querier = self.querier |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
501 |
source = self.system_source |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
502 |
for size, maxsize, hits, misses, title in ( |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
503 |
(len(querier._rql_cache), self.config['rql-cache-size'], |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
504 |
querier.cache_hit, querier.cache_miss, 'rqlt_st'), |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
505 |
(len(source._cache), self.config['rql-cache-size'], |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
506 |
source.cache_hit, source.cache_miss, 'sql'), |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
507 |
): |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
508 |
results['%s_cache_size' % title] = '%s / %s' % (size, maxsize) |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
509 |
results['%s_cache_hit' % title] = hits |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
510 |
results['%s_cache_miss' % title] = misses |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
511 |
results['%s_cache_hit_percent' % title] = (hits * 100) / (hits + misses) |
8224
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
512 |
results['type_source_cache_size'] = len(self._type_source_cache) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
513 |
results['extid_cache_size'] = len(self._extid_cache) |
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
514 |
results['sql_no_cache'] = self.system_source.no_cache |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
515 |
results['nb_open_sessions'] = len(self._sessions) |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
516 |
results['nb_active_threads'] = threading.activeCount() |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
517 |
results['looping_tasks'] = ', '.join(str(t) for t in self._looping_tasks) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
518 |
results['available_cnxsets'] = self._cnxsets_pool.qsize() |
5606
61b28589d33f
merge back to stable some changes made on site for a customer.
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
diff
changeset
|
519 |
results['threads'] = ', '.join(sorted(str(t) for t in threading.enumerate())) |
5587
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
520 |
return results |
72679e450f6d
[web] dont attempt to update last login time on ldap users, avoiding spurious tb in logs (closes #914464)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
521 |
|
8224
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
522 |
def gc_stats(self, nmax=20): |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
523 |
"""Return a dictionary containing some statistics about the repository |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
524 |
memory usage. |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
525 |
|
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
526 |
This is a public method, not requiring a session id. |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
527 |
|
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
528 |
nmax is the max number of (most) referenced object returned as |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
529 |
the 'referenced' result |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
530 |
""" |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
531 |
|
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
532 |
from cubicweb._gcdebug import gc_info |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
533 |
from cubicweb.appobject import AppObject |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
534 |
from cubicweb.rset import ResultSet |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
535 |
from cubicweb.dbapi import Connection, Cursor |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
536 |
from cubicweb.web.request import CubicWebRequestBase |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
537 |
from rql.stmts import Union |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
538 |
|
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
539 |
lookupclasses = (AppObject, |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
540 |
Union, ResultSet, |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
541 |
Connection, Cursor, |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
542 |
CubicWebRequestBase) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
543 |
try: |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
544 |
from cubicweb.server.session import Session, InternalSession |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
545 |
lookupclasses += (InternalSession, Session) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
546 |
except ImportError: |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
547 |
pass # no server part installed |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
548 |
|
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
549 |
results = {} |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
550 |
counters, ocounters, garbage = gc_info(lookupclasses, |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
551 |
viewreferrersclasses=()) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
552 |
values = sorted(counters.iteritems(), key=lambda x: x[1], reverse=True) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
553 |
results['lookupclasses'] = values |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
554 |
values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax] |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
555 |
results['referenced'] = values |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
556 |
results['unreachable'] = len(garbage) |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
557 |
return results |
e35d4d4f7eb3
[repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents:
8133
diff
changeset
|
558 |
|
0 | 559 |
def get_schema(self): |
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
560 |
"""Return the instance schema. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
561 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
562 |
This is a public method, not requiring a session id. |
0 | 563 |
""" |
7723
badfd5524ab6
[repo] Stop setting hashmode on schema
Julien Cristau <julien.cristau@logilab.fr>
parents:
7716
diff
changeset
|
564 |
return self.schema |
0 | 565 |
|
566 |
def get_cubes(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
567 |
"""Return the list of cubes used by this instance. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
568 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
569 |
This is a public method, not requiring a session id. |
0 | 570 |
""" |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
571 |
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
|
572 |
or self.config.repairing |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
573 |
or self.config.quick_start |
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
|
574 |
or self.config.mode == 'test')) |
0 | 575 |
cubes = list(versions) |
576 |
cubes.remove('cubicweb') |
|
577 |
return cubes |
|
578 |
||
6308
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
579 |
def get_option_value(self, option, foreid=None): |
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
580 |
"""Return the value for `option` in the configuration. If `foreid` is |
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
581 |
specified, the actual repository to which this entity belongs is |
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
582 |
derefenced and the option value retrieved from it. |
6257
7eb5f1aed728
[repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6256
diff
changeset
|
583 |
|
7eb5f1aed728
[repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6256
diff
changeset
|
584 |
This is a public method, not requiring a session id. |
7eb5f1aed728
[repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6256
diff
changeset
|
585 |
""" |
7eb5f1aed728
[repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6256
diff
changeset
|
586 |
# XXX we may want to check we don't give sensible information |
8074
a7f76e322659
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8051
diff
changeset
|
587 |
# XXX the only cube using 'foreid', apycot, stop used this, we probably |
a7f76e322659
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8051
diff
changeset
|
588 |
# want to drop this argument |
6308
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
589 |
if foreid is None: |
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
590 |
return self.config[option] |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
591 |
_, sourceuri, extid, _ = self.type_and_source_from_eid(foreid) |
6308
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
592 |
if sourceuri == 'system': |
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
593 |
return self.config[option] |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
594 |
cnxset = self._get_cnxset() |
6308
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
595 |
try: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
596 |
cnx = cnxset.connection(sourceuri) |
7064
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
597 |
# needed to check connection is valid and usable by the current |
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
598 |
# thread |
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
599 |
newcnx = self.sources_by_uri[sourceuri].check_connection(cnx) |
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
600 |
if newcnx is not None: |
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
601 |
cnx = newcnx |
b8ce2266d026
[repo] fix multi-threads bug with get_option_value: pyro proxy can't be used from a thread to another without caution (closes #1535206)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
602 |
return cnx.get_option_value(option, extid) |
6308
c151c730a1ac
[dbapi] get_option_value now has a foreid argument telling the option should be dereferenced to the entity's actual repository (necessary for apycot/local_cache handling)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6257
diff
changeset
|
603 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
604 |
self._free_cnxset(cnxset) |
6257
7eb5f1aed728
[repo] new method on repo+dbapi.Connection to get a value from repository's configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6256
diff
changeset
|
605 |
|
0 | 606 |
@cached |
607 |
def get_versions(self, checkversions=False): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
608 |
"""Return the a dictionary containing cubes used by this instance |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
609 |
as key with their version as value, including cubicweb version. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
610 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
611 |
This is a public method, not requiring a session id. |
0 | 612 |
""" |
613 |
from logilab.common.changelog import Version |
|
614 |
vcconf = {} |
|
615 |
session = self.internal_session() |
|
616 |
try: |
|
617 |
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
|
618 |
'Any K,V WHERE P is CWProperty, P value V, P pkey K, ' |
0 | 619 |
'P pkey ~="system.version.%"', build_descr=False): |
620 |
cube = pk.split('.')[-1] |
|
621 |
# XXX cubicweb migration |
|
622 |
if cube in CW_MIGRATION_MAP: |
|
623 |
cube = CW_MIGRATION_MAP[cube] |
|
624 |
version = Version(version) |
|
625 |
vcconf[cube] = version |
|
626 |
if checkversions: |
|
627 |
if cube != 'cubicweb': |
|
628 |
fsversion = self.config.cube_version(cube) |
|
629 |
else: |
|
630 |
fsversion = self.config.cubicweb_version() |
|
631 |
if version < fsversion: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
632 |
msg = ('instance has %s version %s but %s ' |
0 | 633 |
'is installed. Run "cubicweb-ctl upgrade".') |
634 |
raise ExecutionError(msg % (cube, version, fsversion)) |
|
635 |
finally: |
|
636 |
session.close() |
|
637 |
return vcconf |
|
1482 | 638 |
|
0 | 639 |
@cached |
640 |
def source_defs(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
641 |
"""Return the a dictionary containing source uris as value and a |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
642 |
dictionary describing each source as value. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
643 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
644 |
This is a public method, not requiring a session id. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
645 |
""" |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
646 |
sources = {} |
0 | 647 |
# remove sensitive information |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
648 |
for uri, source in self.sources_by_uri.iteritems(): |
6722
3341521d857b
[repo source] rename attribute so it's much easier to grasp its role
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6674
diff
changeset
|
649 |
sources[uri] = source.public_config |
0 | 650 |
return sources |
651 |
||
652 |
def properties(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
653 |
"""Return a result set containing system wide properties. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
654 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
655 |
This is a public method, not requiring a session id. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
656 |
""" |
0 | 657 |
session = self.internal_session() |
658 |
try: |
|
5747
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
659 |
# don't use session.execute, we don't want rset.req set |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
660 |
return self.querier.execute(session, 'Any K,V WHERE P is CWProperty,' |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
661 |
'P pkey K, P value V, NOT P for_user U', |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
662 |
build_descr=False) |
0 | 663 |
finally: |
664 |
session.close() |
|
665 |
||
5032 | 666 |
# XXX protect this method: anonymous should be allowed and registration |
667 |
# plugged |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
668 |
def register_user(self, login, password, email=None, **kwargs): |
0 | 669 |
"""check a user with the given login exists, if not create it with the |
670 |
given password. This method is designed to be used for anonymous |
|
671 |
registration on public web site. |
|
672 |
""" |
|
673 |
session = self.internal_session() |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
674 |
# 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
|
675 |
errmsg = session._('the value "%s" is already used, use another one') |
0 | 676 |
try: |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
677 |
if (session.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
678 |
build_descr=False) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
679 |
or session.execute('CWUser X WHERE X use_email C, C address %(login)s', |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
680 |
{'login': login}, build_descr=False)): |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
681 |
qname = role_name('login', 'subject') |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
682 |
raise ValidationError(None, {qname: errmsg % login}) |
0 | 683 |
# we have to create the user |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
684 |
user = self.vreg['etypes'].etype_class('CWUser')(session) |
0 | 685 |
if isinstance(password, unicode): |
686 |
# password should *always* be utf8 encoded |
|
687 |
password = password.encode('UTF8') |
|
688 |
kwargs['login'] = login |
|
689 |
kwargs['upassword'] = password |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
690 |
self.glob_add_entity(session, EditedEntity(user, **kwargs)) |
0 | 691 |
session.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
692 |
{'x': user.eid}) |
|
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
693 |
if email or '@' in login: |
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
694 |
d = {'login': login, 'email': email or login} |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
695 |
if session.execute('EmailAddress X WHERE X address %(email)s', d, |
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
696 |
build_descr=False): |
5030
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
697 |
qname = role_name('address', 'subject') |
5238d9a8dfee
[form] put qualified name on validation error, should fix #784299
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
698 |
raise ValidationError(None, {qname: errmsg % d['email']}) |
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
699 |
session.execute('INSERT EmailAddress X: X address %(email)s, ' |
5032 | 700 |
'U primary_email X, U use_email X ' |
5073
a9697325cffa
[repo] don't need rset description on those queries
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5072
diff
changeset
|
701 |
'WHERE U login %(login)s', d, build_descr=False) |
0 | 702 |
session.commit() |
703 |
finally: |
|
704 |
session.close() |
|
594
76218d42d21f
return success or not on creation of user
Arthur Lutz <arthur.lutz@logilab.fr>
parents:
479
diff
changeset
|
705 |
return True |
1482 | 706 |
|
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
707 |
def find_users(self, fetch_attrs, **query_attrs): |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
708 |
"""yield user attributes for cwusers matching the given query_attrs |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
709 |
(the result set cannot survive this method call) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
710 |
|
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
711 |
This can be used by low-privileges account (anonymous comes to |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
712 |
mind). |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
713 |
|
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
714 |
`fetch_attrs`: tuple of attributes to be fetched |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
715 |
`query_attrs`: dict of attr/values to restrict the query |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
716 |
""" |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
717 |
assert query_attrs |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
718 |
if not hasattr(self, '_cwuser_attrs'): |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
719 |
cwuser = self.schema['CWUser'] |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
720 |
self._cwuser_attrs = set(str(rschema) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
721 |
for rschema, _eschema in cwuser.attribute_definitions() |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
722 |
if not rschema.meta) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
723 |
cwuserattrs = self._cwuser_attrs |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
724 |
for k in chain(fetch_attrs, query_attrs.iterkeys()): |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
725 |
if k not in cwuserattrs: |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
726 |
raise Exception('bad input for find_user') |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
727 |
session = self.internal_session() |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
728 |
try: |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
729 |
varmaker = rqlvar_maker() |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
730 |
vars = [(attr, varmaker.next()) for attr in fetch_attrs] |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
731 |
rql = 'Any %s WHERE X is CWUser, ' % ','.join(var[1] for var in vars) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
732 |
rql += ','.join('X %s %s' % (var[0], var[1]) for var in vars) + ',' |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
733 |
rset = session.execute(rql + ','.join('X %s %%(%s)s' % (attr, attr) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
734 |
for attr in query_attrs.iterkeys()), |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
735 |
query_attrs) |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
736 |
return rset.rows |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
737 |
finally: |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
738 |
session.close() |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
739 |
|
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
|
740 |
def connect(self, login, **kwargs): |
0 | 741 |
"""open a connection for a given user |
742 |
||
743 |
base_url may be needed to send mails |
|
744 |
cnxtype indicate if this is a pyro connection or a in-memory connection |
|
1482 | 745 |
|
0 | 746 |
raise `AuthenticationError` if the authentication failed |
747 |
raise `ConnectionError` if we can't open a connection |
|
748 |
""" |
|
749 |
# use an internal connection |
|
750 |
session = self.internal_session() |
|
751 |
# try to get a user object |
|
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3890
diff
changeset
|
752 |
cnxprops = kwargs.pop('cnxprops', None) |
0 | 753 |
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
|
754 |
user = self.authenticate_user(session, login, **kwargs) |
0 | 755 |
finally: |
756 |
session.close() |
|
757 |
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
|
758 |
user._cw = user.cw_rset.req = session |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
759 |
user.cw_clear_relation_cache() |
0 | 760 |
self._sessions[session.id] = session |
5638
cea831403e72
log the user name on session opening for easier debugging
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5603
diff
changeset
|
761 |
self.info('opened session %s for user %s', session.id, login) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
762 |
self.hm.call_hooks('session_open', session) |
0 | 763 |
# commit session at this point in case write operation has been done |
764 |
# during `session_open` hooks |
|
765 |
session.commit() |
|
766 |
return session.id |
|
767 |
||
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
768 |
def execute(self, sessionid, rqlstring, args=None, build_descr=True, |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
769 |
txid=None): |
0 | 770 |
"""execute a RQL query |
771 |
||
772 |
* rqlstring should be an unicode string or a plain ascii string |
|
773 |
* args the optional parameters used in the query |
|
774 |
* build_descr is a flag indicating if the description should be |
|
775 |
built on select queries |
|
776 |
""" |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
777 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
0 | 778 |
try: |
779 |
try: |
|
5747
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
780 |
rset = self.querier.execute(session, rqlstring, args, |
0 | 781 |
build_descr) |
5747
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
782 |
# NOTE: the web front will (re)build it when needed |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
783 |
# e.g in facets |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
784 |
# Zeroed to avoid useless overhead with pyro |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
785 |
rset._rqlst = None |
d6ac0cd30fde
[rset] do not filter rsets with __getstate__, ensure whatever flies with pyro has no .req attribute, also set the ._rqlst to None since it will be reconstructed later on demand
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5711
diff
changeset
|
786 |
return rset |
0 | 787 |
except (Unauthorized, RQLSyntaxError): |
788 |
raise |
|
789 |
except ValidationError, ex: |
|
790 |
# need ValidationError normalization here so error may pass |
|
791 |
# through pyro |
|
792 |
if hasattr(ex.entity, 'eid'): |
|
793 |
ex.entity = ex.entity.eid # error raised by yams |
|
794 |
args = list(ex.args) |
|
795 |
args[0] = ex.entity |
|
796 |
ex.args = tuple(args) |
|
797 |
raise |
|
7616
8837e4733888
[repository] catch Exception instead of everything
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
798 |
except Exception: |
0 | 799 |
# FIXME: check error to catch internal errors |
5167
529861a73ec8
log RQL query in case of unexpected failure (priceless when porting to new database)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5115
diff
changeset
|
800 |
self.exception('unexpected error while executing %s with %s', rqlstring, args) |
0 | 801 |
raise |
802 |
finally: |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
803 |
session.free_cnxset() |
1482 | 804 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
805 |
def describe(self, sessionid, eid, txid=None): |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
806 |
"""return a tuple `(type, physical source uri, extid, actual source |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
807 |
uri)` for the entity of the given `eid` |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
808 |
""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
809 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
0 | 810 |
try: |
811 |
return self.type_and_source_from_eid(eid, session) |
|
812 |
finally: |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
813 |
session.free_cnxset() |
0 | 814 |
|
815 |
def check_session(self, sessionid): |
|
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
816 |
"""raise `BadConnectionId` if the connection is no more valid, else |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
817 |
return its latest activity timestamp. |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
818 |
""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
819 |
return self._get_session(sessionid, setcnxset=False).timestamp |
0 | 820 |
|
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
821 |
def get_shared_data(self, sessionid, key, default=None, pop=False, txdata=False): |
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
822 |
"""return value associated to key in the session's data dictionary or |
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
823 |
session's transaction's data if `txdata` is true. |
0 | 824 |
|
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8224
diff
changeset
|
825 |
If pop is True, value will be removed from the dictionary. |
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
826 |
|
8238
087bb529035c
[spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8224
diff
changeset
|
827 |
If key isn't defined in the dictionary, value specified by the |
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
828 |
`default` argument will be returned. |
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
829 |
""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
830 |
session = self._get_session(sessionid, setcnxset=False) |
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
831 |
return session.get_shared_data(key, default, pop, txdata) |
0 | 832 |
|
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
833 |
def set_shared_data(self, sessionid, key, value, txdata=False): |
0 | 834 |
"""set value associated to `key` in shared data |
835 |
||
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
836 |
if `txdata` is true, the value will be added to the repository session's |
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
837 |
transaction's data which are cleared on commit/rollback of the current |
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
838 |
transaction. |
0 | 839 |
""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
840 |
session = self._get_session(sessionid, setcnxset=False) |
6013
8ca424bc393b
[dbapi] cleanup shared data api: let access to transaction from dbapi, we can write it after all... Also, querydata is better named txdata
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6012
diff
changeset
|
841 |
session.set_shared_data(key, value, txdata) |
0 | 842 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
843 |
def commit(self, sessionid, txid=None): |
0 | 844 |
"""commit transaction for the session with the given id""" |
845 |
self.debug('begin commit for session %s', sessionid) |
|
846 |
try: |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
847 |
session = self._get_session(sessionid) |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
848 |
session.set_tx_data(txid) |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
849 |
return session.commit() |
1482 | 850 |
except (ValidationError, Unauthorized): |
0 | 851 |
raise |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7782
diff
changeset
|
852 |
except Exception: |
0 | 853 |
self.exception('unexpected error') |
854 |
raise |
|
1482 | 855 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
856 |
def rollback(self, sessionid, txid=None): |
0 | 857 |
"""commit transaction for the session with the given id""" |
858 |
self.debug('begin rollback for session %s', sessionid) |
|
859 |
try: |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
860 |
session = self._get_session(sessionid) |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
861 |
session.set_tx_data(txid) |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
862 |
session.rollback() |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7782
diff
changeset
|
863 |
except Exception: |
0 | 864 |
self.exception('unexpected error') |
865 |
raise |
|
866 |
||
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
867 |
def close(self, sessionid, txid=None, checkshuttingdown=True): |
0 | 868 |
"""close the session with the given id""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
869 |
session = self._get_session(sessionid, setcnxset=True, txid=txid, |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
870 |
checkshuttingdown=checkshuttingdown) |
0 | 871 |
# operation uncommited before close are rollbacked before hook is called |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
872 |
session.rollback(free_cnxset=False) |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
873 |
self.hm.call_hooks('session_close', session) |
0 | 874 |
# commit session at this point in case write operation has been done |
875 |
# during `session_close` hooks |
|
876 |
session.commit() |
|
877 |
session.close() |
|
878 |
del self._sessions[sessionid] |
|
879 |
self.info('closed session %s for user %s', sessionid, session.user.login) |
|
1482 | 880 |
|
0 | 881 |
def user_info(self, sessionid, props=None): |
882 |
"""this method should be used by client to: |
|
883 |
* check session id validity |
|
884 |
* update user information on each user's request (i.e. groups and |
|
885 |
custom properties) |
|
886 |
""" |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
887 |
session = self._get_session(sessionid, setcnxset=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
|
888 |
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
|
889 |
self.set_session_props(sessionid, props) |
0 | 890 |
user = session.user |
891 |
return user.eid, user.login, user.groups, user.properties |
|
1482 | 892 |
|
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
|
893 |
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
|
894 |
"""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
|
895 |
* 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
|
896 |
* 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
|
897 |
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
|
898 |
""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
899 |
session = self._get_session(sessionid, setcnxset=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
|
900 |
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
|
901 |
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
|
902 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
903 |
def undoable_transactions(self, sessionid, ueid=None, txid=None, |
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
904 |
**actionfilters): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
905 |
"""See :class:`cubicweb.dbapi.Connection.undoable_transactions`""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
906 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
907 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
908 |
return self.system_source.undoable_transactions(session, ueid, |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
909 |
**actionfilters) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
910 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
911 |
session.free_cnxset() |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
912 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
913 |
def transaction_info(self, sessionid, txuuid, txid=None): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
914 |
"""See :class:`cubicweb.dbapi.Connection.transaction_info`""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
915 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
916 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
917 |
return self.system_source.tx_info(session, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
918 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
919 |
session.free_cnxset() |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
920 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
921 |
def transaction_actions(self, sessionid, txuuid, public=True, txid=None): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
922 |
"""See :class:`cubicweb.dbapi.Connection.transaction_actions`""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
923 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
924 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
925 |
return self.system_source.tx_actions(session, txuuid, public) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
926 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
927 |
session.free_cnxset() |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
928 |
|
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
929 |
def undo_transaction(self, sessionid, txuuid, txid=None): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
930 |
"""See :class:`cubicweb.dbapi.Connection.undo_transaction`""" |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
931 |
session = self._get_session(sessionid, setcnxset=True, txid=txid) |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
932 |
try: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
933 |
return self.system_source.undo_transaction(session, txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
934 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
935 |
session.free_cnxset() |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
936 |
|
0 | 937 |
# public (inter-repository) interface ##################################### |
1482 | 938 |
|
0 | 939 |
def entities_modified_since(self, etypes, mtime): |
940 |
"""function designed to be called from an external repository which |
|
941 |
is using this one as a rql source for synchronization, and return a |
|
942 |
3-uple containing : |
|
943 |
* the local date |
|
944 |
* list of (etype, eid) of entities of the given types which have been |
|
945 |
modified since the given timestamp (actually entities whose full text |
|
946 |
index content has changed) |
|
947 |
* list of (etype, eid) of entities of the given types which have been |
|
948 |
deleted since the given timestamp |
|
949 |
""" |
|
950 |
session = self.internal_session() |
|
7922
d307c3817782
[repository] use utcnow instead of now for some internal timetamps (closes #1988458)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7898
diff
changeset
|
951 |
updatetime = datetime.utcnow() |
0 | 952 |
try: |
953 |
modentities, delentities = self.system_source.modified_entities( |
|
954 |
session, etypes, mtime) |
|
955 |
return updatetime, modentities, delentities |
|
956 |
finally: |
|
957 |
session.close() |
|
958 |
||
959 |
# session handling ######################################################## |
|
1482 | 960 |
|
0 | 961 |
def close_sessions(self): |
962 |
"""close every opened sessions""" |
|
963 |
for sessionid in self._sessions.keys(): |
|
964 |
try: |
|
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
965 |
self.close(sessionid, checkshuttingdown=False) |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7782
diff
changeset
|
966 |
except Exception: # XXX BaseException? |
0 | 967 |
self.exception('error while closing session %s' % sessionid) |
968 |
||
969 |
def clean_sessions(self): |
|
970 |
"""close sessions not used since an amount of time specified in the |
|
971 |
configuration |
|
972 |
""" |
|
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
973 |
mintime = time() - self.cleanup_session_time |
0 | 974 |
self.debug('cleaning session unused since %s', |
975 |
strftime('%T', localtime(mintime))) |
|
976 |
nbclosed = 0 |
|
977 |
for session in self._sessions.values(): |
|
978 |
if session.timestamp < mintime: |
|
979 |
self.close(session.id) |
|
980 |
nbclosed += 1 |
|
981 |
return nbclosed |
|
1482 | 982 |
|
7706
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
983 |
def internal_session(self, cnxprops=None, safe=False): |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
984 |
"""return a dbapi like connection/cursor using internal user which have |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
985 |
every rights on the repository. The `safe` argument is a boolean flag |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
986 |
telling if integrity hooks should be activated or not. |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
987 |
|
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
988 |
*YOU HAVE TO* commit/rollback or close (rollback implicitly) the |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
989 |
session once the job's done, else you'll leak connections set up to the |
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
990 |
time where no one is available, causing irremediable freeze... |
0 | 991 |
""" |
7706
359bc86d2827
[session] safe internal sessions don't deactivate integrity hooks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7698
diff
changeset
|
992 |
session = InternalSession(self, cnxprops, safe) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
993 |
session.set_cnxset() |
0 | 994 |
return session |
1482 | 995 |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
996 |
def _get_session(self, sessionid, setcnxset=False, txid=None, |
5813
0b250d72fcfa
[transaction w/ separated web/repo processes] the dbapi should explicitly specify a transaction id to avoid confusion when web server / repository run in separated processes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5808
diff
changeset
|
997 |
checkshuttingdown=True): |
0 | 998 |
"""return the user associated to the given session identifier""" |
5906
d40ced753291
[repository] fix so that when repository is shutting down, internal session in transaction are interrupted
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5813
diff
changeset
|
999 |
if checkshuttingdown and self.shutting_down: |
7573
c8f8762c986d
[repo, looping task] raise a custom exception when repository is shuting down, avoid looping task to be restarted in such case. Closes #1021276
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7550
diff
changeset
|
1000 |
raise ShuttingDown('Repository is shutting down') |
0 | 1001 |
try: |
1002 |
session = self._sessions[sessionid] |
|
1003 |
except KeyError: |
|
1004 |
raise BadConnectionId('No such session %s' % sessionid) |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1005 |
if setcnxset: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1006 |
session.set_tx_data(txid) # must be done before set_cnxset |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1007 |
session.set_cnxset() |
0 | 1008 |
return session |
1009 |
||
1010 |
# data sources handling ################################################### |
|
1011 |
# * correspondance between eid and (type, source) |
|
1012 |
# * correspondance between eid and local id (i.e. specific to a given source) |
|
1482 | 1013 |
|
0 | 1014 |
def type_and_source_from_eid(self, eid, session=None): |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1015 |
"""return a tuple `(type, physical source uri, extid, actual source |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1016 |
uri)` for the entity of the given `eid` |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1017 |
""" |
0 | 1018 |
try: |
1019 |
eid = typed_eid(eid) |
|
1020 |
except ValueError: |
|
1021 |
raise UnknownEid(eid) |
|
1022 |
try: |
|
1023 |
return self._type_source_cache[eid] |
|
1024 |
except KeyError: |
|
1025 |
if session is None: |
|
1026 |
session = self.internal_session() |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1027 |
free_cnxset = True |
0 | 1028 |
else: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1029 |
free_cnxset = False |
0 | 1030 |
try: |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1031 |
etype, uri, extid, auri = self.system_source.eid_type_source( |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1032 |
session, eid) |
0 | 1033 |
finally: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1034 |
if free_cnxset: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1035 |
session.free_cnxset() |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1036 |
self._type_source_cache[eid] = (etype, uri, extid, auri) |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1037 |
if uri != 'system': |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1038 |
self._extid_cache[(extid, uri)] = eid |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1039 |
return etype, uri, extid, auri |
0 | 1040 |
|
1041 |
def clear_caches(self, eids): |
|
1042 |
etcache = self._type_source_cache |
|
1043 |
extidcache = self._extid_cache |
|
1044 |
rqlcache = self.querier._rql_cache |
|
1045 |
for eid in eids: |
|
1046 |
try: |
|
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1047 |
etype, uri, extid, auri = etcache.pop(typed_eid(eid)) # may be a string in some cases |
8133
b0a70092946a
[repo cache] fix cache clearing for an eid: keys are now always a tuple
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8074
diff
changeset
|
1048 |
rqlcache.pop( ('%s X WHERE X eid %s' % (etype, eid),), None) |
0 | 1049 |
extidcache.pop((extid, uri), None) |
1050 |
except KeyError: |
|
1051 |
etype = None |
|
8133
b0a70092946a
[repo cache] fix cache clearing for an eid: keys are now always a tuple
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8074
diff
changeset
|
1052 |
rqlcache.pop( ('Any X WHERE X eid %s' % eid,), None) |
0 | 1053 |
for source in self.sources: |
1054 |
source.clear_eid_cache(eid, etype) |
|
1482 | 1055 |
|
0 | 1056 |
def type_from_eid(self, eid, session=None): |
1057 |
"""return the type of the entity with id <eid>""" |
|
1058 |
return self.type_and_source_from_eid(eid, session)[0] |
|
1482 | 1059 |
|
0 | 1060 |
def source_from_eid(self, eid, session=None): |
1061 |
"""return the source for the given entity's eid""" |
|
1062 |
return self.sources_by_uri[self.type_and_source_from_eid(eid, session)[1]] |
|
1482 | 1063 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1064 |
def querier_cache_key(self, session, rql, args, eidkeys): |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1065 |
cachekey = [rql] |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1066 |
for key in sorted(eidkeys): |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1067 |
try: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1068 |
etype = self.type_from_eid(args[key], session) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1069 |
except KeyError: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1070 |
raise QueryError('bad cache key %s (no value)' % key) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1071 |
except TypeError: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1072 |
raise QueryError('bad cache key %s (value: %r)' % ( |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1073 |
key, args[key])) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1074 |
cachekey.append(etype) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1075 |
# ensure eid is correctly typed in args |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1076 |
args[key] = typed_eid(args[key]) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1077 |
return tuple(cachekey) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
1078 |
|
0 | 1079 |
def eid2extid(self, source, eid, session=None): |
1080 |
"""get local id from an eid""" |
|
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1081 |
etype, uri, extid, _ = self.type_and_source_from_eid(eid, session) |
0 | 1082 |
if source.uri != uri: |
1083 |
# eid not from the given source |
|
1084 |
raise UnknownEid(eid) |
|
1085 |
return extid |
|
1086 |
||
1954 | 1087 |
def extid2eid(self, source, extid, etype, session=None, insert=True, |
7708
45be3a9debe6
[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7706
diff
changeset
|
1088 |
complete=True, commit=True, sourceparams=None): |
7399
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1089 |
"""Return eid from a local id. If the eid is a negative integer, that |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1090 |
means the entity is known but has been copied back to the system source |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1091 |
hence should be ignored. |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1092 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1093 |
If no record is found, ie the entity is not known yet: |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1094 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1095 |
1. an eid is attributed |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1096 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1097 |
2. the source's :meth:`before_entity_insertion` method is called to |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1098 |
build the entity instance |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1099 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1100 |
3. unless source's :attr:`should_call_hooks` tell otherwise, |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1101 |
'before_add_entity' hooks are called |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1102 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1103 |
4. record is added into the system source |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1104 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1105 |
5. the source's :meth:`after_entity_insertion` method is called to |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1106 |
complete building of the entity instance |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1107 |
|
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1108 |
6. unless source's :attr:`should_call_hooks` tell otherwise, |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1109 |
'before_add_entity' hooks are called |
972ed1843bd8
[multi-sources] support for moving an entity from an external source (closes #343818)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1110 |
""" |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1111 |
uri = 'system' if source.copy_based_source else source.uri |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1112 |
cachekey = (extid, uri) |
0 | 1113 |
try: |
1114 |
return self._extid_cache[cachekey] |
|
1115 |
except KeyError: |
|
1116 |
pass |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1117 |
free_cnxset = False |
0 | 1118 |
if session is None: |
1119 |
session = self.internal_session() |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1120 |
free_cnxset = True |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1121 |
eid = self.system_source.extid2eid(session, uri, extid) |
0 | 1122 |
if eid is not None: |
1123 |
self._extid_cache[cachekey] = eid |
|
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1124 |
self._type_source_cache[eid] = (etype, uri, extid, source.uri) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1125 |
if free_cnxset: |
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1126 |
session.free_cnxset() |
0 | 1127 |
return eid |
1128 |
if not insert: |
|
1129 |
return |
|
1954 | 1130 |
# no link between extid and eid, create one using an internal session |
0 | 1131 |
# since the current session user may not have required permissions to |
1132 |
# do necessary stuff and we don't want to commit user session. |
|
1133 |
# |
|
450
5e14ea0e81c8
a note for later
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
341
diff
changeset
|
1134 |
# Moreover, even if session is already an internal session but is |
0 | 1135 |
# processing a commit, we have to use another one |
1136 |
if not session.is_internal_session: |
|
1137 |
session = self.internal_session() |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1138 |
free_cnxset = True |
0 | 1139 |
try: |
1140 |
eid = self.system_source.create_eid(session) |
|
1141 |
self._extid_cache[cachekey] = eid |
|
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1142 |
self._type_source_cache[eid] = (etype, uri, extid, source.uri) |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1143 |
entity = source.before_entity_insertion( |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1144 |
session, extid, etype, eid, sourceparams) |
0 | 1145 |
if source.should_call_hooks: |
7950
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1146 |
# get back a copy of operation for later restore if necessary, |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1147 |
# see below |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1148 |
pending_operations = session.pending_operations[:] |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1149 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
7698
1c7411535c2d
[datafeed / fti] rather control a 'complete' parameter than setting empty attribute values
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7622
diff
changeset
|
1150 |
self.add_info(session, entity, source, extid, complete=complete) |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1151 |
source.after_entity_insertion(session, extid, entity, sourceparams) |
0 | 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_add_entity', session, entity=entity) |
7716
c893150b2394
[repo ms] commit/rollback session if it has been created in the method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7708
diff
changeset
|
1154 |
if commit or free_cnxset: |
7708
45be3a9debe6
[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7706
diff
changeset
|
1155 |
session.commit(free_cnxset) |
0 | 1156 |
return eid |
7708
45be3a9debe6
[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7706
diff
changeset
|
1157 |
except Exception: |
7716
c893150b2394
[repo ms] commit/rollback session if it has been created in the method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7708
diff
changeset
|
1158 |
if commit or free_cnxset: |
7708
45be3a9debe6
[datafeed] for datafeed source, we don't want commit in extid2eid but explicitly handled by the source. Also, we should use 'safe' internal session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7706
diff
changeset
|
1159 |
session.rollback(free_cnxset) |
7950
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1160 |
else: |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1161 |
# XXX do some cleanup manually so that the transaction has a |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1162 |
# chance to be commited, with simply this entity discarded |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1163 |
self._extid_cache.pop(cachekey, None) |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1164 |
self._type_source_cache.pop(eid, None) |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1165 |
if 'entity' in locals(): |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1166 |
hook.CleanupDeletedEidsCacheOp.get_instance(session).add_data(entity.eid) |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1167 |
self.system_source.delete_info_multi(session, [entity], uri) |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1168 |
if source.should_call_hooks: |
99ae8c883ad3
[ms repository] when an exception is raised during extid2eid and no rollback is done, some manual cleanups have to be done (closes #1993420)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7931
diff
changeset
|
1169 |
session._threaddata.pending_operations = pending_operations |
0 | 1170 |
raise |
1482 | 1171 |
|
0 | 1172 |
def add_info(self, session, entity, source, extid=None, complete=True): |
1173 |
"""add type and source info for an eid into the system table, |
|
1174 |
and index the entity with the full text index |
|
1175 |
""" |
|
1176 |
# begin by inserting eid/type/source/extid into the entities table |
|
6426
541659c39f6a
[hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6390
diff
changeset
|
1177 |
hook.CleanupNewEidsCacheOp.get_instance(session).add_data(entity.eid) |
4806
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4756
diff
changeset
|
1178 |
self.system_source.add_info(session, entity, source, extid, complete) |
1482 | 1179 |
|
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1180 |
def delete_info(self, session, entity, sourceuri, scleanup=None): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1181 |
"""called by external source when some entity known by the system source |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1182 |
has been deleted in the external source |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1183 |
""" |
5067
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
1184 |
# mark eid as being deleted in session info and setup cache update |
adc2122eed03
[repo] more efficient eid cache operations handling based on set_operation; refactor
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5043
diff
changeset
|
1185 |
# operation |
6426
541659c39f6a
[hook/operation] nicer api to achieve same result as set_operation, as described in #1253630
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6390
diff
changeset
|
1186 |
hook.CleanupDeletedEidsCacheOp.get_instance(session).add_data(entity.eid) |
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1187 |
self._delete_info(session, entity, sourceuri, scleanup) |
1482 | 1188 |
|
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1189 |
def _delete_info(self, session, entity, sourceuri, scleanup=None): |
0 | 1190 |
"""delete system information on deletion of an entity: |
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1191 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1192 |
* delete all remaining relations from/to this entity |
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1193 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1194 |
* call delete info on the system source which will transfer record from |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1195 |
the entities table to the deleted_entities table |
7884
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1196 |
|
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1197 |
When scleanup is specified, it's expected to be the source's eid, in |
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1198 |
which case we'll specify the target's relation source so that this |
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1199 |
source is ignored. E.g. we want to delete relations stored locally, as |
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1200 |
the deletion information comes from the external source, it's its |
35d2e2f4e10a
[repo] cleanup [_]delete_info prototype: extid argument isn't used
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7823
diff
changeset
|
1201 |
responsability to have cleaned-up its own relations. |
0 | 1202 |
""" |
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
|
1203 |
pendingrtypes = session.transaction_data.get('pendingrtypes', ()) |
7887
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1204 |
if scleanup is not None: |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1205 |
source = self.sources_by_eid[scleanup] |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1206 |
# delete remaining relations: if user can delete the entity, he can |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1207 |
# delete all its relations without security checking |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1208 |
with security_enabled(session, read=False, write=False): |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1209 |
eid = entity.eid |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1210 |
for rschema, _, role in entity.e_schema.relation_definitions(): |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1211 |
rtype = rschema.type |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1212 |
if rtype in schema.VIRTUAL_RTYPES or rtype in pendingrtypes: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1213 |
continue |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1214 |
if role == 'subject': |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1215 |
# don't skip inlined relation so they are regularly |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1216 |
# deleted and so hooks are correctly called |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1217 |
rql = 'DELETE X %s Y WHERE X eid %%(x)s' % rtype |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1218 |
else: |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1219 |
rql = 'DELETE Y %s X WHERE X eid %%(x)s' % rtype |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1220 |
if scleanup is not None: |
7887
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1221 |
# if the relation can't be crossed, nothing to cleanup (we |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1222 |
# would get a BadRQLQuery from the multi-sources planner). |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1223 |
# This may still leave some junk if the mapping has changed |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1224 |
# at some point, but one can still run db-check to catch |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1225 |
# those |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1226 |
if not source in self.can_cross_relation(rtype): |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1227 |
continue |
6626
5c20a005bddc
[pyro source] when cleaning relations of an external entity being cleaned up, only delete local relationsd
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6491
diff
changeset
|
1228 |
# source cleaning: only delete relations stored locally |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1229 |
# (here, scleanup |
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1230 |
rql += ', NOT (Y cw_source S, S eid %(seid)s)' |
6674
fd9c76196bf7
[repository] don't crash when cascading delete a relation, this may let the database in an inconsistent state anyway, so simply log the error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6664
diff
changeset
|
1231 |
try: |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1232 |
session.execute(rql, {'x': eid, 'seid': scleanup}, |
6674
fd9c76196bf7
[repository] don't crash when cascading delete a relation, this may let the database in an inconsistent state anyway, so simply log the error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6664
diff
changeset
|
1233 |
build_descr=False) |
7616
8837e4733888
[repository] catch Exception instead of everything
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
1234 |
except Exception: |
7885
9454b7ef5ae4
[ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7884
diff
changeset
|
1235 |
if self.config.mode == 'test': |
9454b7ef5ae4
[ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7884
diff
changeset
|
1236 |
raise |
6674
fd9c76196bf7
[repository] don't crash when cascading delete a relation, this may let the database in an inconsistent state anyway, so simply log the error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6664
diff
changeset
|
1237 |
self.exception('error while cascading delete for entity %s ' |
fd9c76196bf7
[repository] don't crash when cascading delete a relation, this may let the database in an inconsistent state anyway, so simply log the error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6664
diff
changeset
|
1238 |
'from %s. RQL: %s', entity, sourceuri, rql) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1239 |
self.system_source.delete_info_multi(session, [entity], sourceuri) |
0 | 1240 |
|
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1241 |
def _delete_info_multi(self, session, entities, sourceuri, scleanup=None): |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1242 |
"""same as _delete_info but accepts a list of entities with |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1243 |
the same etype and belinging to the same source. |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1244 |
""" |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1245 |
pendingrtypes = session.transaction_data.get('pendingrtypes', ()) |
7887
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1246 |
if scleanup is not None: |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1247 |
source = self.sources_by_eid[scleanup] |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1248 |
# delete remaining relations: if user can delete the entity, he can |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1249 |
# delete all its relations without security checking |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1250 |
with security_enabled(session, read=False, write=False): |
7895 | 1251 |
in_eids = ','.join([str(_e.eid) for _e in entities]) |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1252 |
for rschema, _, role in entities[0].e_schema.relation_definitions(): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1253 |
rtype = rschema.type |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1254 |
if rtype in schema.VIRTUAL_RTYPES or rtype in pendingrtypes: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1255 |
continue |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1256 |
if role == 'subject': |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1257 |
# don't skip inlined relation so they are regularly |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1258 |
# deleted and so hooks are correctly called |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1259 |
rql = 'DELETE X %s Y WHERE X eid IN (%s)' % (rtype, in_eids) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1260 |
else: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1261 |
rql = 'DELETE Y %s X WHERE X eid IN (%s)' % (rtype, in_eids) |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1262 |
if scleanup is not None: |
7887
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1263 |
# if the relation can't be crossed, nothing to cleanup (we |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1264 |
# would get a BadRQLQuery from the multi-sources planner). |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1265 |
# This may still leave some junk if the mapping has changed |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1266 |
# at some point, but one can still run db-check to catch |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1267 |
# those |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1268 |
if not source in self.can_cross_relation(rtype): |
42a0b7398d31
[repository] when deleting relations of a deleted external entity, we should skip non-crossable relation (closes #1973680)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7885
diff
changeset
|
1269 |
continue |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1270 |
# source cleaning: only delete relations stored locally |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1271 |
rql += ', NOT (Y cw_source S, S eid %(seid)s)' |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1272 |
try: |
6941
9ed02daa7dbb
[repo, ms] optimise external source deletion by using source's eid instead of name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
1273 |
session.execute(rql, {'seid': scleanup}, build_descr=False) |
8051
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1274 |
except ValidationError: |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1275 |
raise |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1276 |
except Unauthorized: |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1277 |
self.exception('Unauthorized exception while cascading delete for entity %s ' |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1278 |
'from %s. RQL: %s.\nThis should not happen since security is disabled here.', |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1279 |
entities, sourceuri, rql) |
41398fa1a90f
[rql, integrity] let ValidationError and Unauthorized pass during composite deletion (closes #2070042)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7950
diff
changeset
|
1280 |
raise |
7616
8837e4733888
[repository] catch Exception instead of everything
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7573
diff
changeset
|
1281 |
except Exception: |
7885
9454b7ef5ae4
[ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7884
diff
changeset
|
1282 |
if self.config.mode == 'test': |
9454b7ef5ae4
[ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7884
diff
changeset
|
1283 |
raise |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1284 |
self.exception('error while cascading delete for entity %s ' |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1285 |
'from %s. RQL: %s', entities, sourceuri, rql) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1286 |
self.system_source.delete_info_multi(session, entities, sourceuri) |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1287 |
|
0 | 1288 |
def locate_relation_source(self, session, subject, rtype, object): |
1289 |
subjsource = self.source_from_eid(subject, session) |
|
1290 |
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
|
1291 |
if not subjsource is objsource: |
0 | 1292 |
source = self.system_source |
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4899
diff
changeset
|
1293 |
if not (subjsource.may_cross_relation(rtype) |
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
1294 |
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
|
1295 |
raise MultiSourcesError( |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
1296 |
"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
|
1297 |
% rtype) |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
1298 |
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
|
1299 |
source = self.system_source |
0 | 1300 |
else: |
1301 |
source = subjsource |
|
3042
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
1302 |
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
|
1303 |
raise MultiSourcesError( |
d2455badf7fb
[multi-sources] enhance relation'source detection to avoid inconsistency
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2967
diff
changeset
|
1304 |
"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
|
1305 |
% (source.uri, rtype)) |
0 | 1306 |
return source |
1482 | 1307 |
|
0 | 1308 |
def locate_etype_source(self, etype): |
1309 |
for source in self.sources: |
|
1310 |
if source.support_entity(etype, 1): |
|
1311 |
return source |
|
1312 |
else: |
|
1313 |
raise ETypeNotSupportedBySources(etype) |
|
1482 | 1314 |
|
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1315 |
def init_entity_caches(self, session, entity, source): |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1316 |
"""add entity to session entities cache and repo's extid cache. |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1317 |
Return entity's ext id if the source isn't the system source. |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1318 |
""" |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1319 |
session.set_entity_cache(entity) |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1320 |
suri = source.uri |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1321 |
if suri == 'system': |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1322 |
extid = None |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1323 |
else: |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1324 |
if source.copy_based_source: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
1325 |
suri = 'system' |
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1326 |
extid = source.get_extid(entity) |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1327 |
self._extid_cache[(str(extid), suri)] = entity.eid |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1328 |
self._type_source_cache[entity.eid] = (entity.__regid__, suri, extid, |
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1329 |
source.uri) |
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1330 |
return extid |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1331 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1332 |
def glob_add_entity(self, session, edited): |
0 | 1333 |
"""add an entity to the repository |
1482 | 1334 |
|
0 | 1335 |
the entity eid should originaly be None and a unique eid is assigned to |
1336 |
the entity instance |
|
1337 |
""" |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1338 |
entity = edited.entity |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
1339 |
entity._cw_is_saved = False # entity has an eid but is not yet saved |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1340 |
# init edited_attributes before calling before_add_entity hooks |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1341 |
entity.cw_edited = edited |
0 | 1342 |
eschema = entity.e_schema |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1343 |
source = self.locate_etype_source(entity.__regid__) |
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1344 |
# allocate an eid to the entity before calling hooks |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
1345 |
entity.eid = self.system_source.create_eid(session) |
5068
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1346 |
# set caches asap |
10c3422d7419
[repo] on add entity, set cache as soon as possible + fill type/source cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5067
diff
changeset
|
1347 |
extid = self.init_entity_caches(session, entity, source) |
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1348 |
if server.DEBUG & server.DBG_REPO: |
6466
43d71dd8a8ec
cleanup and 3.10 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6465
diff
changeset
|
1349 |
print 'ADD entity', self, entity.__regid__, entity.eid, edited |
0 | 1350 |
relations = [] |
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1351 |
prefill_entity_caches(entity, relations) |
2929
51cdfe069e10
fix edited_attributes handling when adding entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2875
diff
changeset
|
1352 |
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
|
1353 |
self.hm.call_hooks('before_add_entity', session, entity=entity) |
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1354 |
activintegrity = session.is_hook_category_activated('activeintegrity') |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1355 |
for attr in edited.iterkeys(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3606
diff
changeset
|
1356 |
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
|
1357 |
if not rschema.final: # inlined relation |
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1358 |
value = edited[attr] |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1359 |
relations.append((attr, value)) |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1360 |
session.update_rel_cache_add(entity.eid, attr, value) |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1361 |
rdef = session.rtype_eids_rdef(attr, entity.eid, value) |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1362 |
if rdef.cardinality[1] in '1?' and activintegrity: |
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1363 |
with security_enabled(session, read=False): |
7507
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1364 |
session.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr, |
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
1365 |
{'x': entity.eid, 'y': value}) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1366 |
edited.set_defaults() |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1367 |
if session.is_hook_category_activated('integrity'): |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1368 |
edited.check(creation=True) |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1369 |
try: |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1370 |
source.add_entity(session, entity) |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1371 |
except UniqueTogetherError, exc: |
6465
6401a9d0b5aa
[architecture] introduce adapter to ease transformation of errors before display to the end user. Use it for UniqueTogetherError first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6464
diff
changeset
|
1372 |
userhdlr = session.vreg['adapters'].select( |
6401a9d0b5aa
[architecture] introduce adapter to ease transformation of errors before display to the end user. Use it for UniqueTogetherError first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6464
diff
changeset
|
1373 |
'IUserFriendlyError', session, entity=entity, exc=exc) |
6401a9d0b5aa
[architecture] introduce adapter to ease transformation of errors before display to the end user. Use it for UniqueTogetherError first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6464
diff
changeset
|
1374 |
userhdlr.raise_user_exception() |
0 | 1375 |
self.add_info(session, entity, source, extid, complete=False) |
6368
f907cc7f2875
[repo] properly mark object as saved once added to its source (code much probably gone during a merge...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6333
diff
changeset
|
1376 |
edited.saved = entity._cw_is_saved = True |
0 | 1377 |
# trigger after_add_entity after after_add_relation |
1378 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1379 |
self.hm.call_hooks('after_add_entity', session, entity=entity) |
0 | 1380 |
# call hooks for inlined relations |
1381 |
for attr, value in relations: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1382 |
self.hm.call_hooks('before_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1383 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1384 |
self.hm.call_hooks('after_add_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1385 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 1386 |
return entity.eid |
1482 | 1387 |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1388 |
def glob_update_entity(self, session, edited): |
0 | 1389 |
"""replace an entity in the repository |
1390 |
the type and the eid of an entity must not be changed |
|
1391 |
""" |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1392 |
entity = edited.entity |
2600
6cd6c5d11b45
[F repo debugging] log repo event on DBG_REPO debug level
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
1393 |
if server.DEBUG & server.DBG_REPO: |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
1394 |
print 'UPDATE entity', entity.__regid__, entity.eid, \ |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1395 |
entity.cw_attr_cache, edited |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1396 |
hm = self.hm |
0 | 1397 |
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
|
1398 |
session.set_entity_cache(entity) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1399 |
orig_edited = getattr(entity, 'cw_edited', None) |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1400 |
entity.cw_edited = edited |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1401 |
try: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1402 |
only_inline_rels, need_fti_update = True, False |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1403 |
relations = [] |
5292
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1404 |
source = self.source_from_eid(entity.eid, session) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1405 |
for attr in list(edited): |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1406 |
if attr == 'eid': |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1407 |
continue |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1408 |
rschema = eschema.subjrels[attr] |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1409 |
if rschema.final: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1410 |
if getattr(eschema.rdef(attr), 'fulltextindexed', False): |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1411 |
need_fti_update = True |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1412 |
only_inline_rels = False |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1413 |
else: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1414 |
# inlined relation |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1415 |
previous_value = entity.related(attr) or None |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1416 |
if previous_value is not None: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1417 |
previous_value = previous_value[0][0] # got a result set |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1418 |
if previous_value == entity.cw_attr_cache[attr]: |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1419 |
previous_value = None |
5292
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1420 |
elif source.should_call_hooks: |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1421 |
hm.call_hooks('before_delete_relation', session, |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1422 |
eidfrom=entity.eid, rtype=attr, |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1423 |
eidto=previous_value) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1424 |
relations.append((attr, edited[attr], previous_value)) |
5292
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1425 |
if source.should_call_hooks: |
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1426 |
# call hooks for inlined relations |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1427 |
for attr, value, _t in relations: |
5292
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1428 |
hm.call_hooks('before_add_relation', session, |
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1429 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1430 |
if not only_inline_rels: |
8b496574b65c
[repo] fix more indentation pb in repo.glob_update_entity, causing duplicated inline relation hooks call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5187
diff
changeset
|
1431 |
hm.call_hooks('before_update_entity', session, entity=entity) |
5808
2de32c0c293b
[repo] call entity.check() once before_update hooks has been called, to let them a chance to transform data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5749
diff
changeset
|
1432 |
if session.is_hook_category_activated('integrity'): |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1433 |
edited.check() |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1434 |
try: |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1435 |
source.update_entity(session, entity) |
6225 | 1436 |
edited.saved = True |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1437 |
except UniqueTogetherError, exc: |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1438 |
etype, rtypes = exc.args |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1439 |
problems = {} |
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1440 |
for col in rtypes: |
6225 | 1441 |
problems[col] = session._('violates unique_together constraints (%s)') % (','.join(rtypes)) |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
1442 |
raise ValidationError(entity.eid, problems) |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1443 |
self.system_source.update_info(session, entity, need_fti_update) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1444 |
if source.should_call_hooks: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1445 |
if not only_inline_rels: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1446 |
hm.call_hooks('after_update_entity', session, entity=entity) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1447 |
for attr, value, prevvalue in relations: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1448 |
# if the relation is already cached, update existant cache |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
1449 |
relcache = entity.cw_relation_cached(attr, 'subject') |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1450 |
if prevvalue is not None: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1451 |
hm.call_hooks('after_delete_relation', session, |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1452 |
eidfrom=entity.eid, rtype=attr, eidto=prevvalue) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1453 |
if relcache is not None: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1454 |
session.update_rel_cache_del(entity.eid, attr, prevvalue) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1455 |
del_existing_rel_if_needed(session, entity.eid, attr, value) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1456 |
if relcache is not None: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1457 |
session.update_rel_cache_add(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
|
1458 |
else: |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
1459 |
entity.cw_set_relation_cache(attr, 'subject', |
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
1460 |
session.eid_rset(value)) |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1461 |
hm.call_hooks('after_add_relation', session, |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1462 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1463 |
finally: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1464 |
if orig_edited is not None: |
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1465 |
entity.cw_edited = orig_edited |
0 | 1466 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1467 |
|
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1468 |
def glob_delete_entities(self, session, eids): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1469 |
"""delete a list of entities and all related entities from the repository""" |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1470 |
# mark eids as being deleted in session info and setup cache update |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1471 |
# operation (register pending eids before actual deletion to avoid |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1472 |
# multiple call to glob_delete_entities) |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1473 |
op = hook.CleanupDeletedEidsCacheOp.get_instance(session) |
7507
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1474 |
if not isinstance(eids, (set, frozenset)): |
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1475 |
warn('[3.13] eids should be given as a set', DeprecationWarning, |
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1476 |
stacklevel=2) |
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1477 |
eids = frozenset(eids) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1478 |
eids = eids - op._container |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1479 |
op._container |= eids |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1480 |
data_by_etype_source = {} # values are ([list of eids], |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1481 |
# [list of extid], |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1482 |
# [list of entities]) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1483 |
# |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1484 |
# WARNING: the way this dictionary is populated is heavily optimized |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1485 |
# and does not use setdefault on purpose. Unless a new release |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1486 |
# of the Python interpreter advertises large perf improvements |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1487 |
# in setdefault, this should not be changed without profiling. |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1488 |
|
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1489 |
for eid in eids: |
7543
570522300e22
[ms, entity metas] add 'actual source' to entities table / base entity metadata cache. Closes #1767090
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7514
diff
changeset
|
1490 |
etype, sourceuri, extid, _ = self.type_and_source_from_eid(eid, session) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1491 |
# XXX should cache entity's cw_metainformation |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1492 |
entity = session.entity_from_eid(eid, etype) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1493 |
try: |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1494 |
data_by_etype_source[(etype, sourceuri)].append(entity) |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1495 |
except KeyError: |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1496 |
data_by_etype_source[(etype, sourceuri)] = [entity] |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1497 |
for (etype, sourceuri), entities in data_by_etype_source.iteritems(): |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1498 |
if server.DEBUG & server.DBG_REPO: |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1499 |
print 'DELETE entities', etype, [entity.eid for entity in entities] |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1500 |
source = self.sources_by_uri[sourceuri] |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1501 |
if source.should_call_hooks: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1502 |
self.hm.call_hooks('before_delete_entity', session, entities=entities) |
7898
405017c79b45
[ms, test] fix source deletion pb, detected by test since 7885:9454b7ef5ae4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7895
diff
changeset
|
1503 |
if session.deleted_in_transaction(source.eid): |
405017c79b45
[ms, test] fix source deletion pb, detected by test since 7885:9454b7ef5ae4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7895
diff
changeset
|
1504 |
# source is being deleted, think to give scleanup argument |
405017c79b45
[ms, test] fix source deletion pb, detected by test since 7885:9454b7ef5ae4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7895
diff
changeset
|
1505 |
self._delete_info_multi(session, entities, sourceuri, scleanup=source.eid) |
405017c79b45
[ms, test] fix source deletion pb, detected by test since 7885:9454b7ef5ae4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7895
diff
changeset
|
1506 |
else: |
405017c79b45
[ms, test] fix source deletion pb, detected by test since 7885:9454b7ef5ae4
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7895
diff
changeset
|
1507 |
self._delete_info_multi(session, entities, sourceuri) |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1508 |
source.delete_entities(session, entities) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1509 |
if source.should_call_hooks: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1510 |
self.hm.call_hooks('after_delete_entity', session, entities=entities) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1511 |
# don't clear cache here, it is done in a hook on commit |
1482 | 1512 |
|
0 | 1513 |
def glob_add_relation(self, session, subject, rtype, object): |
1514 |
"""add a relation to the repository""" |
|
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1515 |
self.glob_add_relations(session, {rtype: [(subject, object)]}) |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1516 |
|
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1517 |
def glob_add_relations(self, session, relations): |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1518 |
"""add several relations to the repository |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1519 |
|
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1520 |
relations is a dictionary rtype: [(subj_eid, obj_eid), ...] |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1521 |
""" |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1522 |
sources = {} |
7513
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1523 |
subjects_by_types = {} |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1524 |
objects_by_types = {} |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1525 |
activintegrity = session.is_hook_category_activated('activeintegrity') |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1526 |
for rtype, eids_subj_obj in relations.iteritems(): |
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1527 |
if server.DEBUG & server.DBG_REPO: |
7513
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1528 |
for subjeid, objeid in relations: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1529 |
print 'ADD relation', subjeid, rtype, objeid |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1530 |
for subjeid, objeid in eids_subj_obj: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1531 |
source = self.locate_relation_source(session, subjeid, rtype, objeid) |
7238
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1532 |
if source not in sources: |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1533 |
relations_by_rtype = {} |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1534 |
sources[source] = relations_by_rtype |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1535 |
else: |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1536 |
relations_by_rtype = sources[source] |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1537 |
if rtype in relations_by_rtype: |
7513
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1538 |
relations_by_rtype[rtype].append((subjeid, objeid)) |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1539 |
else: |
7513
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1540 |
relations_by_rtype[rtype] = [(subjeid, objeid)] |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1541 |
if not activintegrity: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1542 |
continue |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1543 |
# take care to relation of cardinality '?1', as all eids will |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1544 |
# be inserted later, we've remove duplicated eids since they |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1545 |
# won't be catched by `del_existing_rel_if_needed` |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1546 |
rdef = session.rtype_eids_rdef(rtype, subjeid, objeid) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1547 |
card = rdef.cardinality |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1548 |
if card[0] in '?1': |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1549 |
with security_enabled(session, read=False): |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1550 |
session.execute('DELETE X %s Y WHERE X eid %%(x)s, ' |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1551 |
'NOT Y eid %%(y)s' % rtype, |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1552 |
{'x': subjeid, 'y': objeid}) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1553 |
subjects = subjects_by_types.setdefault(rdef, {}) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1554 |
if subjeid in subjects: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1555 |
del relations_by_rtype[rtype][subjects[subjeid]] |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1556 |
subjects[subjeid] = len(relations_by_rtype[rtype]) - 1 |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1557 |
continue |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1558 |
subjects[subjeid] = len(relations_by_rtype[rtype]) - 1 |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1559 |
if card[1] in '?1': |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1560 |
with security_enabled(session, read=False): |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1561 |
session.execute('DELETE X %s Y WHERE Y eid %%(y)s, ' |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1562 |
'NOT X eid %%(x)s' % rtype, |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1563 |
{'x': subjeid, 'y': objeid}) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1564 |
objects = objects_by_types.setdefault(rdef, {}) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1565 |
if objeid in objects: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1566 |
del relations_by_rtype[rtype][objects[objeid]] |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1567 |
objects[objeid] = len(relations_by_rtype[rtype]) |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1568 |
continue |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1569 |
objects[objeid] = len(relations_by_rtype[rtype]) |
7238
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1570 |
for source, relations_by_rtype in sources.iteritems(): |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1571 |
if source.should_call_hooks: |
7238
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1572 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1573 |
self.hm.call_hooks('before_add_relation', session, |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1574 |
rtype=rtype, eids_from_to=source_relations) |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1575 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1576 |
source.add_relations(session, rtype, source_relations) |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1577 |
rschema = self.schema.rschema(rtype) |
7513
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1578 |
for subjeid, objeid in source_relations: |
8f4422391e5a
[repo integrity] test and fix glob add relation where several entities are added at once for a relation of 1? cardinality
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7507
diff
changeset
|
1579 |
session.update_rel_cache_add(subjeid, rtype, objeid, rschema.symmetric) |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1580 |
if source.should_call_hooks: |
7238
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1581 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1582 |
self.hm.call_hooks('after_add_relation', session, |
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1583 |
rtype=rtype, eids_from_to=source_relations) |
0 | 1584 |
|
1585 |
def glob_delete_relation(self, session, subject, rtype, object): |
|
1586 |
"""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
|
1587 |
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
|
1588 |
print 'DELETE relation', subject, rtype, object |
0 | 1589 |
source = self.locate_relation_source(session, subject, rtype, object) |
1590 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1591 |
self.hm.call_hooks('before_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1592 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1593 |
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
|
1594 |
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
|
1595 |
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
|
1596 |
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
|
1597 |
# on symmetric relation, we can't now in which sense it's |
0 | 1598 |
# stored so try to delete both |
1599 |
source.delete_relation(session, object, rtype, subject) |
|
1600 |
if source.should_call_hooks: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1601 |
self.hm.call_hooks('after_delete_relation', session, |
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
1602 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1603 |
|
1604 |
||
1605 |
# pyro handling ########################################################### |
|
1482 | 1606 |
|
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1607 |
@property |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1608 |
@cached |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1609 |
def pyro_appid(self): |
6061
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1610 |
from logilab.common import pyro_ext as pyro |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1611 |
config = self.config |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1612 |
appid = '%s.%s' % pyro.ns_group_and_id( |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1613 |
config['pyro-instance-id'] or config.appid, |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1614 |
config['pyro-ns-group']) |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1615 |
# ensure config['pyro-instance-id'] is a full qualified pyro name |
7cb29eab1c9d
[pyro] instance id cleanup, ensure config['pyro-instance-id'] is a fully qualified pyro id on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
1616 |
config['pyro-instance-id'] = appid |
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1617 |
return appid |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1618 |
|
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1619 |
def _use_pyrons(self): |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1620 |
"""return True if the pyro-ns-host is set to something else |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1621 |
than NO_PYRONS, meaning we want to go through a pyro |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1622 |
nameserver""" |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1623 |
return self.config['pyro-ns-host'] != 'NO_PYRONS' |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1624 |
|
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1625 |
def pyro_register(self, host=''): |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1626 |
"""register the repository as a pyro object""" |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1627 |
from logilab.common import pyro_ext as pyro |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1628 |
daemon = pyro.register_object(self, self.pyro_appid, |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1629 |
daemonhost=self.config['pyro-host'], |
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1630 |
nshost=self.config['pyro-ns-host'], |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1631 |
use_pyrons=self._use_pyrons()) |
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1632 |
self.info('repository registered as a pyro object %s', self.pyro_appid) |
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1633 |
self.pyro_uri = pyro.get_object_uri(self.pyro_appid) |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1634 |
self.info('pyro uri is: %s', self.pyro_uri) |
0 | 1635 |
self.pyro_registered = True |
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1636 |
# register a looping task to regularly ensure we're still registered |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1637 |
# into the pyro name server |
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1638 |
if self._use_pyrons(): |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1639 |
self.looping_task(60*10, self._ensure_pyro_ns) |
0 | 1640 |
return daemon |
1482 | 1641 |
|
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1642 |
def _ensure_pyro_ns(self): |
7134
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1643 |
if not self._use_pyrons(): |
01544b6d98fa
allow CW to act as a Pyro Server without registering the server to a Pyro Nameserver (closes #1528533)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7085
diff
changeset
|
1644 |
return |
6801
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1645 |
from logilab.common import pyro_ext as pyro |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1646 |
pyro.ns_reregister(self.pyro_appid, nshost=self.config['pyro-ns-host']) |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1647 |
self.info('repository re-registered as a pyro object %s', |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1648 |
self.pyro_appid) |
33952695295b
[repository, pyro] ensure we're still properly registered into pyro name server in a looping task (closes #1336002)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
1649 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1650 |
# multi-sources planner helpers ########################################### |
1482 | 1651 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1652 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1653 |
def rel_type_sources(self, rtype): |
6128 | 1654 |
return tuple([source for source in self.sources |
1655 |
if source.support_relation(rtype) |
|
1656 |
or rtype in source.dont_cross_relations]) |
|
1482 | 1657 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1658 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1659 |
def can_cross_relation(self, rtype): |
6128 | 1660 |
return tuple([source for source in self.sources |
1661 |
if source.support_relation(rtype) |
|
1662 |
and rtype in source.cross_relations]) |
|
1482 | 1663 |
|
1228
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1664 |
@cached |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1665 |
def is_multi_sources_relation(self, rtype): |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1666 |
return any(source for source in self.sources |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1667 |
if not source is self.system_source |
91ae10ffb611
* refactor ms planner (renaming, reorganization)
sylvain.thenault@logilab.fr
parents:
1217
diff
changeset
|
1668 |
and source.support_relation(rtype)) |
1482 | 1669 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7064
diff
changeset
|
1670 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7064
diff
changeset
|
1671 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7064
diff
changeset
|
1672 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
0 | 1673 |
|
1674 |
def pyro_unregister(config): |
|
1675 |
"""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
|
1676 |
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
|
1677 |
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
|
1678 |
ns_unregister(appid, config['pyro-ns-group'], config['pyro-ns-host']) |
0 | 1679 |
|
1680 |
||
1681 |
from logging import getLogger |
|
1682 |
from cubicweb import set_log_methods |
|
1683 |
set_log_methods(Repository, getLogger('cubicweb.repository')) |