author | Samuel Trégouët <samuel.tregouet@logilab.fr> |
Fri, 11 Sep 2015 14:52:09 +0200 | |
changeset 10589 | 7c23b7de2b8d |
parent 10579 | 9af9d59a1972 |
child 10608 | 7fc548d9dd8e |
permissions | -rw-r--r-- |
9629
8114bed10e78
some copyright updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9623
diff
changeset
|
1 |
# copyright 2003-2014 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 |
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
|
27 |
""" |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
28 |
from __future__ import print_function |
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
29 |
|
0 | 30 |
__docformat__ = "restructuredtext en" |
31 |
||
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
|
32 |
import threading |
0 | 33 |
import Queue |
7507
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
34 |
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
|
35 |
from itertools import chain |
0 | 36 |
from time import time, localtime, strftime |
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
37 |
from contextlib import contextmanager |
0 | 38 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
39 |
from logilab.common.decorators import cached, clear_cache |
9034
cc3442054e48
[repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9033
diff
changeset
|
40 |
from logilab.common.deprecation import deprecated |
0 | 41 |
|
42 |
from yams import BadSchemaDefinition |
|
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
43 |
from rql.utils import rqlvar_maker |
0 | 44 |
|
9468
39b7a91a3f4c
[repo] pylint cleanup, mainly of imports, with a bit of style
Julien Cristau <julien.cristau@logilab.fr>
parents:
9467
diff
changeset
|
45 |
from cubicweb import (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
|
46 |
UnknownEid, AuthenticationError, ExecutionError, |
10488
83b87bdefd0e
[server] missing import
Julien Cristau <julien.cristau@logilab.fr>
parents:
10446
diff
changeset
|
47 |
BadConnectionId, ValidationError, Unauthorized, |
10446
1e6655cff5ab
add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents:
10368
diff
changeset
|
48 |
UniqueTogetherError, onevent, ViolatedConstraint) |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
49 |
from cubicweb import cwvreg, schema, server |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9460
diff
changeset
|
50 |
from cubicweb.server import ShuttingDown, utils, hook, querier, sources |
10345
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
51 |
from cubicweb.server.session import Session, InternalManager |
0 | 52 |
|
7780
a1d5365fefc1
[cubification] moved CWPermission handling into a localperms cube. closes #1914011
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7723
diff
changeset
|
53 |
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
|
54 |
('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
|
55 |
('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
|
56 |
]) |
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
|
57 |
|
8456
c912d82f2166
[repository] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8433
diff
changeset
|
58 |
def prefill_entity_caches(entity): |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
59 |
cnx = entity._cw |
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
|
60 |
# 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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
entity.cw_set_relation_cache(rtype, 'subject', |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
69 |
cnx.empty_rset()) |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
continue |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
74 |
entity.cw_set_relation_cache(rtype, 'object', cnx.empty_rset()) |
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
|
75 |
|
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
76 |
def del_existing_rel_if_needed(cnx, eidfrom, rtype, eidto): |
0 | 77 |
"""delete existing relation when adding a new one if card is 1 or ? |
78 |
||
79 |
have to be done once the new relation has been inserted to avoid having |
|
80 |
an entity without a relation for some time |
|
81 |
||
82 |
this kind of behaviour has to be done in the repository so we don't have |
|
83 |
hooks order hazardness |
|
84 |
""" |
|
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
85 |
# skip that if integrity explicitly disabled |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
86 |
if not cnx.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
|
87 |
return |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
88 |
rdef = cnx.rtype_eids_rdef(rtype, eidfrom, eidto) |
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
|
89 |
card = rdef.cardinality |
0 | 90 |
# one may be tented to check for neweids but this may cause more than one |
91 |
# relation even with '1?' cardinality if thoses relations are added in the |
|
92 |
# same transaction where the entity is being created. This never occurs from |
|
93 |
# the web interface but may occurs during test or dbapi connection (though |
|
94 |
# not expected for this). So: don't do it, we pretend to ensure repository |
|
95 |
# 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
|
96 |
# |
6189 | 97 |
# notes: |
98 |
# * inlined relations will be implicitly deleted for the subject entity |
|
99 |
# * we don't want read permissions to be applied but we want delete |
|
100 |
# permission to be checked |
|
7503
bc30c2faaadc
[repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7502
diff
changeset
|
101 |
if card[0] in '1?': |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
102 |
with cnx.security_enabled(read=False): |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
103 |
cnx.execute('DELETE X %s Y WHERE X eid %%(x)s, ' |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
104 |
'NOT Y eid %%(y)s' % rtype, |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
105 |
{'x': eidfrom, 'y': eidto}) |
0 | 106 |
if card[1] in '1?': |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
107 |
with cnx.security_enabled(read=False): |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
108 |
cnx.execute('DELETE X %s Y WHERE Y eid %%(y)s, ' |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
109 |
'NOT X eid %%(x)s' % rtype, |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
110 |
{'x': eidfrom, 'y': eidto}) |
1482 | 111 |
|
3694 | 112 |
|
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
113 |
def preprocess_inlined_relations(cnx, entity): |
8547
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
114 |
"""when an entity is added, check if it has some inlined relation which |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
115 |
requires to be extrated for proper call hooks |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
116 |
""" |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
117 |
relations = [] |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
118 |
activeintegrity = cnx.is_hook_category_activated('activeintegrity') |
8547
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
119 |
eschema = entity.e_schema |
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
120 |
for attr in entity.cw_edited: |
8547
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
121 |
rschema = eschema.subjrels[attr] |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
122 |
if not rschema.final: # inlined relation |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
123 |
value = entity.cw_edited[attr] |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
124 |
relations.append((attr, value)) |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
125 |
cnx.update_rel_cache_add(entity.eid, attr, value) |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
126 |
rdef = cnx.rtype_eids_rdef(attr, entity.eid, value) |
8547
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
127 |
if rdef.cardinality[1] in '1?' and activeintegrity: |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
128 |
with cnx.security_enabled(read=False): |
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
129 |
cnx.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr, |
8547
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
130 |
{'x': entity.eid, 'y': value}) |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
131 |
return relations |
f23ac525ddd1
[datafeed] properly call hooks for inlined relations on entity creation. Closes #2481156
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
132 |
|
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
133 |
|
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
|
134 |
class NullEventBus(object): |
8214
ce9556358dbd
[event bus] event has been renamed to publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8211
diff
changeset
|
135 |
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
|
136 |
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
|
137 |
|
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
|
138 |
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
|
139 |
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
|
140 |
|
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
|
141 |
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
|
142 |
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
|
143 |
|
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
|
144 |
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
|
145 |
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
|
146 |
|
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
|
147 |
|
0 | 148 |
class Repository(object): |
149 |
"""a repository provides access to a set of persistent storages for |
|
150 |
entities and relations |
|
151 |
""" |
|
1482 | 152 |
|
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
153 |
def __init__(self, config, tasks_manager=None, vreg=None): |
0 | 154 |
self.config = config |
155 |
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
|
156 |
vreg = cwvreg.CWRegistryStore(config) |
0 | 157 |
self.vreg = vreg |
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
158 |
self._tasks_manager = tasks_manager |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
159 |
|
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
|
160 |
self.app_instances_bus = NullEventBus() |
0 | 161 |
self.info('starting repository from %s', self.config.apphome) |
162 |
# dictionary of opened sessions |
|
163 |
self._sessions = {} |
|
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
164 |
|
0 | 165 |
# list of functions to be called at regular interval |
166 |
# list of running threads |
|
167 |
self._running_threads = [] |
|
168 |
# initial schema, should be build or replaced latter |
|
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
169 |
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
|
170 |
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
|
171 |
# 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
|
172 |
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
|
173 |
# 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
|
174 |
self.system_source = self.get_source('native', 'system', |
9460
a2a0bc984863
[config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9458
diff
changeset
|
175 |
config.system_source_config.copy()) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
176 |
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
|
177 |
# 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
|
178 |
self.querier = querier.QuerierHelper(self, self.schema) |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
179 |
# cache eid -> (type, extid, actual source) |
0 | 180 |
self._type_source_cache = {} |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
181 |
# cache extid -> eid |
0 | 182 |
self._extid_cache = {} |
8946
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
183 |
# open some connection sets |
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
|
184 |
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
|
185 |
self.init_cnxset_pool() |
8946
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
186 |
# the hooks manager |
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
187 |
self.hm = hook.HooksManager(self.vreg) |
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
188 |
# registry hook to fix user class on registry reload |
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
|
189 |
@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
|
190 |
def fix_user_classes(self): |
10577
935d577b2227
[server/repo] fix typo
Julien Cristau <julien.cristau@logilab.fr>
parents:
10540
diff
changeset
|
191 |
# After registry reload the 'CWUser' class used for CWEtype |
935d577b2227
[server/repo] fix typo
Julien Cristau <julien.cristau@logilab.fr>
parents:
10540
diff
changeset
|
192 |
# changed. So any existing user object have a different class than |
8277
8b173e0a617f
{repository] add documentation explaining ``fix_user_classes`` hook
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8275
diff
changeset
|
193 |
# the new loaded one. We are hot fixing this. |
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
|
194 |
usercls = self.vreg['etypes'].etype_class('CWUser') |
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
195 |
for session in self._sessions.itervalues(): |
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
|
196 |
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
|
197 |
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
|
198 |
|
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
|
199 |
def init_cnxset_pool(self): |
8946
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
200 |
"""should be called bootstrap_repository, as this is what it does""" |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2929
diff
changeset
|
201 |
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
|
202 |
self._cnxsets_pool = Queue.Queue() |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
203 |
# 0. init a cnxset that will be used to fetch bootstrap information from |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
204 |
# the database |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9460
diff
changeset
|
205 |
self._cnxsets_pool.put_nowait(self.system_source.wrapped_connection()) |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
206 |
# 1. set used cubes |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
207 |
if config.creating or not config.read_instance_schema: |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
208 |
config.bootstrap_cubes() |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
209 |
else: |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
210 |
self.set_schema(self.config.load_bootstrap_schema(), resetvreg=False) |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
211 |
config.init_cubes(self.get_cubes()) |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
212 |
# 2. 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
|
213 |
if config.quick_start: |
8946
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
214 |
# quick start: 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
|
215 |
# information (eg dump/restore/...) |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
216 |
# |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
217 |
# restrict appobject_path to only load hooks and entity classes in |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
218 |
# the registry |
8537
e30d0a7f0087
[config] turn internal configuration methods building appobjects search path into normal method rather than class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
219 |
config.cube_appobject_path = set(('hooks', 'entities')) |
e30d0a7f0087
[config] turn internal configuration methods building appobjects search path into normal method rather than class method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8456
diff
changeset
|
220 |
config.cubicweb_appobject_path = set(('hooks', 'entities')) |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
221 |
# limit connections pool to 1 |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
222 |
config['connections-pool-size'] = 1 |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
223 |
if config.quick_start or config.creating or not config.read_instance_schema: |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
224 |
# load schema from the file system |
8580
d753d6a6798f
[repository] move modification of appobject_path to repository initialization code so we can restore it later to avoid side effect on the config. Fix regression introduced in d32ab8570e5d
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8573
diff
changeset
|
225 |
if not config.creating: |
10540
d2449691fd48
[server] don't issue a warning for something expected
Julien Cristau <julien.cristau@logilab.fr>
parents:
10539
diff
changeset
|
226 |
self.info("set fs instance'schema") |
9165
7ebf3b1cdb4c
Use the list of cubes from the filesystem when reading the schema from the filesystem
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
9132
diff
changeset
|
227 |
self.set_schema(config.load_schema(expand_cubes=True)) |
8592
df16bd045cae
[server] fix repository initialisation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8580
diff
changeset
|
228 |
else: |
df16bd045cae
[server] fix repository initialisation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8580
diff
changeset
|
229 |
# normal start: load the instance schema from the database |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
230 |
self.info('loading schema from the repository') |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
231 |
self.set_schema(self.deserialize_schema()) |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
232 |
# 3. initialize data sources |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
233 |
if config.creating: |
6128 | 234 |
# call init_creating so that for instance native source can |
235 |
# configurate tsearch according to postgres version |
|
9456
a79e88aad555
[multi-sources-removal]Â Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9455
diff
changeset
|
236 |
self.system_source.init_creating() |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
237 |
else: |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
238 |
self.init_sources_from_database() |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
239 |
if 'CWProperty' in self.schema: |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
240 |
self.vreg.init_properties(self.properties()) |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
241 |
# 4. close initialization connection set and reopen fresh ones for |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
242 |
# proper initialization |
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
|
243 |
self._get_cnxset().close(True) |
8946
fae2f561b0f5
[repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8943
diff
changeset
|
244 |
self.cnxsets = [] # list of available cnxsets (can't iterate on a Queue) |
0 | 245 |
for i in xrange(config['connections-pool-size']): |
9466
c3a5f4507f12
[multi-sources-removal] Turn ConnectionsSet into simpler ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9460
diff
changeset
|
246 |
self.cnxsets.append(self.system_source.wrapped_connection()) |
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
|
247 |
self._cnxsets_pool.put_nowait(self.cnxsets[-1]) |
1482 | 248 |
|
0 | 249 |
# internals ############################################################### |
250 |
||
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
return |
9510
a5231a557ac0
[server/repo] use internal_cnx in init_sources_from_database
Julien Cristau <julien.cristau@logilab.fr>
parents:
9507
diff
changeset
|
257 |
with self.internal_cnx() as cnx: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
258 |
# FIXME: sources should be ordered (add_entity priority) |
9510
a5231a557ac0
[server/repo] use internal_cnx in init_sources_from_database
Julien Cristau <julien.cristau@logilab.fr>
parents:
9507
diff
changeset
|
259 |
for sourceent in cnx.execute( |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
260 |
'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
|
261 |
'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
|
262 |
if sourceent.name == 'system': |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
continue |
9448
3e7cad3967c5
[multi-sources-removal] Drop the "true" multi-sources planner
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9404
diff
changeset
|
267 |
self.add_source(sourceent) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
268 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
269 |
def _clear_planning_caches(self): |
9456
a79e88aad555
[multi-sources-removal]Â Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9455
diff
changeset
|
270 |
clear_cache(self, 'source_defs') |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
271 |
|
9448
3e7cad3967c5
[multi-sources-removal] Drop the "true" multi-sources planner
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9404
diff
changeset
|
272 |
def add_source(self, sourceent): |
9602
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
273 |
try: |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
274 |
source = self.get_source(sourceent.type, sourceent.name, |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
275 |
sourceent.host_config, sourceent.eid) |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
276 |
except RuntimeError: |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
277 |
if self.config.repairing: |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
278 |
self.exception('cant setup source %s, skipped', sourceent.name) |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
279 |
return |
9fb2f15d5e85
[sources] Skip problematic sources when starting shell instead of crashing. Closes #3670208
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9581
diff
changeset
|
280 |
raise |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
# 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
|
285 |
# 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
|
286 |
# 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
|
287 |
# initialized) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
288 |
source.init(True, sourceent) |
6724
24bf6f181d0e
[pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
289 |
else: |
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6953
diff
changeset
|
290 |
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
|
291 |
self._clear_planning_caches() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
292 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
293 |
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
|
294 |
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
|
295 |
del self.sources_by_eid[source.eid] |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
296 |
self._clear_planning_caches() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
297 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6941
diff
changeset
|
298 |
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
|
299 |
# 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
|
300 |
# source_defs() |
0 | 301 |
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
|
302 |
source_config['type'] = type |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6941
diff
changeset
|
303 |
return sources.get_source(type, source_config, self, eid) |
1482 | 304 |
|
8943
58b3b2d9c965
[repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8916
diff
changeset
|
305 |
def set_schema(self, schema, resetvreg=True): |
0 | 306 |
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
|
307 |
if resetvreg: |
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
|
308 |
# 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
|
309 |
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
|
310 |
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
|
311 |
self.vreg._set_schema(schema) |
0 | 312 |
self.querier.set_schema(schema) |
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
313 |
for source in self.sources_by_uri.itervalues(): |
0 | 314 |
source.set_schema(schema) |
315 |
self.schema = schema |
|
316 |
||
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
317 |
def deserialize_schema(self): |
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
318 |
"""load schema from the database""" |
0 | 319 |
from cubicweb.server.schemaserial import deserialize_schema |
2839
6419af16faa0
imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2835
diff
changeset
|
320 |
appschema = schema.CubicWebSchema(self.config.appid) |
0 | 321 |
self.debug('deserializing db schema into %s %#x', appschema.name, id(appschema)) |
9490
b3d2c4065e6a
[server] use internal_cnx instead of internal_session in deserialize_schema
Julien Cristau <julien.cristau@logilab.fr>
parents:
9488
diff
changeset
|
322 |
with self.internal_cnx() as cnx: |
0 | 323 |
try: |
9490
b3d2c4065e6a
[server] use internal_cnx instead of internal_session in deserialize_schema
Julien Cristau <julien.cristau@logilab.fr>
parents:
9488
diff
changeset
|
324 |
deserialize_schema(appschema, cnx) |
0 | 325 |
except BadSchemaDefinition: |
326 |
raise |
|
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8694
diff
changeset
|
327 |
except Exception as ex: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
328 |
import traceback |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
329 |
traceback.print_exc() |
10110
f601d2fdeff7
[repository] don't mangle the stack trace on exception
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10039
diff
changeset
|
330 |
raise Exception('Is the database initialised ? (cause: %s)' % ex) |
8947
3bbd416b09ec
[repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8946
diff
changeset
|
331 |
return appschema |
8321
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
332 |
|
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
333 |
def _prepare_startup(self): |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
334 |
"""Prepare "Repository as a server" for startup. |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
335 |
|
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
336 |
* trigger server startup hook, |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
337 |
* register session clean up task. |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
338 |
""" |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5032
diff
changeset
|
339 |
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
|
340 |
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
|
341 |
# 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
|
342 |
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
|
343 |
# 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
|
344 |
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
|
345 |
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
|
346 |
cleanup_session_interval = min(60*60, self.cleanup_session_time / 3) |
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
347 |
assert self._tasks_manager is not None, "This Repository is not intended to be used as a server" |
8321
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
348 |
self._tasks_manager.add_looping_task(cleanup_session_interval, |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
349 |
self.clean_sessions) |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
350 |
|
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
351 |
def start_looping_tasks(self): |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
352 |
"""Actual "Repository as a server" startup. |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
353 |
|
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
354 |
* trigger server startup hook, |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
355 |
* register session clean up task, |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
356 |
* start all tasks. |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
357 |
|
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
358 |
XXX Other startup related stuffs are done elsewhere. In Repository |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
359 |
XXX __init__ or in external codes (various server managers). |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
360 |
""" |
b5d5a5630649
[repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8320
diff
changeset
|
361 |
self._prepare_startup() |
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
362 |
assert self._tasks_manager is not None, "This Repository is not intended to be used as a server" |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
363 |
self._tasks_manager.start() |
0 | 364 |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2667
diff
changeset
|
365 |
def looping_task(self, interval, func, *args): |
0 | 366 |
"""register a function to be called every `interval` seconds. |
1482 | 367 |
|
0 | 368 |
looping tasks can only be registered during repository initialization, |
369 |
once done this method will fail. |
|
370 |
""" |
|
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
371 |
assert self._tasks_manager is not None, "This Repository is not intended to be used as a server" |
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
372 |
self._tasks_manager.add_looping_task(interval, func, *args) |
0 | 373 |
|
374 |
def threaded_task(self, func): |
|
375 |
"""start function in a separated thread""" |
|
8320
cd2d332b3063
[repo looping task] move looping task logic in a dedicated object (progress #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8280
diff
changeset
|
376 |
utils.RepoThread(func, self._running_threads).start() |
1482 | 377 |
|
0 | 378 |
#@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
|
379 |
def _get_cnxset(self): |
0 | 380 |
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
|
381 |
return self._cnxsets_pool.get(True, timeout=5) |
0 | 382 |
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
|
383 |
raise Exception('no connections set available after 5 secs, probably either a ' |
9267
24d9b86dfa54
spelling: rollbacked -> rolled back
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9226
diff
changeset
|
384 |
'bug in code (too many uncommited/rolled back ' |
4706
6035e96b64dd
added stats for munin collecting #615844 - from 027bbff3659f
arthur
parents:
4689
diff
changeset
|
385 |
'connections) or too much load on the server (in ' |
0 | 386 |
'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
|
387 |
'connections pool size)') |
1482 | 388 |
|
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 |
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
|
390 |
self._cnxsets_pool.put_nowait(cnxset) |
0 | 391 |
|
392 |
def shutdown(self): |
|
393 |
"""called on server stop event to properly close opened sessions and |
|
394 |
connections |
|
395 |
""" |
|
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
|
396 |
assert not self.shutting_down, 'already shutting down' |
8393
77c7158916c1
[events] add a new before_shutdown event (closes #2345728)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8366
diff
changeset
|
397 |
if not (self.config.creating or self.config.repairing |
77c7158916c1
[events] add a new before_shutdown event (closes #2345728)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8366
diff
changeset
|
398 |
or self.config.quick_start): |
77c7158916c1
[events] add a new before_shutdown event (closes #2345728)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8366
diff
changeset
|
399 |
# then, the system source is still available |
77c7158916c1
[events] add a new before_shutdown event (closes #2345728)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8366
diff
changeset
|
400 |
self.hm.call_hooks('before_server_shutdown', repo=self) |
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
|
401 |
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
|
402 |
self.system_source.shutdown() |
8322
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
403 |
if self._tasks_manager is not None: |
cb838b126b07
[repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8321
diff
changeset
|
404 |
self._tasks_manager.stop() |
8351
02f4f01375e8
[repository] fire 'server_shutdown' hooks before waiting for theads
David Douard <david.douard@logilab.fr>
parents:
8322
diff
changeset
|
405 |
if not (self.config.creating or self.config.repairing |
02f4f01375e8
[repository] fire 'server_shutdown' hooks before waiting for theads
David Douard <david.douard@logilab.fr>
parents:
8322
diff
changeset
|
406 |
or self.config.quick_start): |
02f4f01375e8
[repository] fire 'server_shutdown' hooks before waiting for theads
David Douard <david.douard@logilab.fr>
parents:
8322
diff
changeset
|
407 |
self.hm.call_hooks('server_shutdown', repo=self) |
0 | 408 |
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
|
409 |
self.info('waiting thread %s...', thread.getName()) |
0 | 410 |
thread.join() |
5376
2c3f14bc2590
[python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5292
diff
changeset
|
411 |
self.info('thread %s finished', thread.getName()) |
0 | 412 |
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
|
413 |
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
|
414 |
cnxset = self._cnxsets_pool.get_nowait() |
0 | 415 |
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
|
416 |
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
|
417 |
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
|
418 |
self.exception('error while closing %s' % cnxset) |
0 | 419 |
continue |
420 |
hits, misses = self.querier.cache_hit, self.querier.cache_miss |
|
421 |
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
|
422 |
self.info('rql st cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
0 | 423 |
(hits * 100) / (hits + misses)) |
424 |
hits, misses = self.system_source.cache_hit, self.system_source.cache_miss |
|
425 |
self.info('sql cache hit/miss: %s/%s (%s%% hits)', hits, misses, |
|
426 |
(hits * 100) / (hits + misses)) |
|
427 |
nocache = self.system_source.no_cache |
|
428 |
self.info('sql cache usage: %s/%s (%s%%)', hits+ misses, nocache, |
|
429 |
((hits + misses) * 100) / (hits + misses + nocache)) |
|
430 |
except ZeroDivisionError: |
|
431 |
pass |
|
1482 | 432 |
|
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
433 |
def check_auth_info(self, cnx, login, authinfo): |
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
|
434 |
"""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
|
435 |
associated CWUser's eid on success. |
0 | 436 |
""" |
8229
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
437 |
# 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
|
438 |
# contain copy based sources |
b7bc631816f7
[ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8214
diff
changeset
|
439 |
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
|
440 |
if self.config.source_enabled(source) and source.support_entity('CWUser'): |
0 | 441 |
try: |
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
442 |
return source.authenticate(cnx, login, **authinfo) |
0 | 443 |
except AuthenticationError: |
444 |
continue |
|
445 |
else: |
|
446 |
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
|
447 |
|
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
448 |
def authenticate_user(self, cnx, login, **authinfo): |
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
|
449 |
"""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
|
450 |
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
|
451 |
""" |
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
452 |
eid = self.check_auth_info(cnx, login, authinfo) |
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
453 |
cwuser = self._build_user(cnx, eid) |
0 | 454 |
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
|
455 |
not cwuser.cw_adapt_to('IWorkflowable').state in cwuser.AUTHENTICABLE_STATES: |
0 | 456 |
raise AuthenticationError('user is not in authenticable state') |
2268
2f336fd5e040
euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
457 |
return cwuser |
0 | 458 |
|
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
459 |
def _build_user(self, cnx, eid): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
460 |
"""return a CWUser entity for user with the given eid""" |
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
461 |
cls = self.vreg['etypes'].etype_class('CWUser') |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
462 |
st = cls.fetch_rqlst(cnx.user, ordermethod=None) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
463 |
st.add_eid_restriction(st.get_variable('X'), 'x', 'Substitute') |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
464 |
rset = cnx.execute(st.as_string(), {'x': eid}) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
465 |
assert len(rset) == 1, rset |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
466 |
cwuser = rset.get_entity(0, 0) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
467 |
# pylint: disable=W0104 |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
468 |
# prefetch / cache cwuser's groups and properties. This is especially |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
469 |
# useful for internal sessions to avoid security insertions |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
470 |
cwuser.groups |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
471 |
cwuser.properties |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
472 |
return cwuser |
1482 | 473 |
|
0 | 474 |
# public (dbapi) interface ################################################ |
1482 | 475 |
|
9683
74359402bfb0
[repo] fix deprecation messages for stats and gc_stats
Julien Cristau <julien.cristau@logilab.fr>
parents:
9676
diff
changeset
|
476 |
@deprecated("[3.19] use _cw.call_service('repo_stats')") |
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
|
477 |
def stats(self): # XXX restrict to managers session? |
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
478 |
"""Return a dictionary containing some statistics about the repository |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
479 |
resources usage. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
480 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
481 |
This is a public method, not requiring a session id. |
9034
cc3442054e48
[repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9033
diff
changeset
|
482 |
|
cc3442054e48
[repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9033
diff
changeset
|
483 |
This method is deprecated in favor of using _cw.call_service('repo_stats') |
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
484 |
""" |
9507
540cb068a7f9
[repo] Use a connection instead of a session for repo.stats()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9506
diff
changeset
|
485 |
with self.internal_cnx() as cnx: |
540cb068a7f9
[repo] Use a connection instead of a session for repo.stats()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9506
diff
changeset
|
486 |
return cnx.call_service('repo_stats') |
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
|
487 |
|
9683
74359402bfb0
[repo] fix deprecation messages for stats and gc_stats
Julien Cristau <julien.cristau@logilab.fr>
parents:
9676
diff
changeset
|
488 |
@deprecated("[3.19] use _cw.call_service('repo_gc_stats')") |
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
|
489 |
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
|
490 |
"""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
|
491 |
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
|
492 |
|
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
|
493 |
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
|
494 |
|
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
|
495 |
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
|
496 |
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
|
497 |
""" |
9506
7f6883783969
[repo] Use a connection instead of a session for repo.gc_stats()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9505
diff
changeset
|
498 |
with self.internal_cnx() as cnx: |
7f6883783969
[repo] Use a connection instead of a session for repo.gc_stats()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9505
diff
changeset
|
499 |
return cnx.call_service('repo_gc_stats', nmax=nmax) |
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
|
500 |
|
0 | 501 |
def get_schema(self): |
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
502 |
"""Return the instance schema. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
503 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
504 |
This is a public method, not requiring a session id. |
0 | 505 |
""" |
7723
badfd5524ab6
[repo] Stop setting hashmode on schema
Julien Cristau <julien.cristau@logilab.fr>
parents:
7716
diff
changeset
|
506 |
return self.schema |
0 | 507 |
|
508 |
def get_cubes(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
509 |
"""Return the list of cubes used by this instance. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
510 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
511 |
This is a public method, not requiring a session id. |
0 | 512 |
""" |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2268
diff
changeset
|
513 |
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
|
514 |
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
|
515 |
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
|
516 |
or self.config.mode == 'test')) |
0 | 517 |
cubes = list(versions) |
518 |
cubes.remove('cubicweb') |
|
519 |
return cubes |
|
520 |
||
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
|
521 |
def get_option_value(self, option, foreid=None): |
9458
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
522 |
"""Return the value for `option` in the configuration. |
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
|
523 |
|
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
|
524 |
This is a public method, not requiring a session id. |
9458
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
525 |
|
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
526 |
`foreid` argument is deprecated and now useless (as of 3.19). |
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
|
527 |
""" |
9458
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
528 |
if foreid is not None: |
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
529 |
warn('[3.19] foreid argument is deprecated', DeprecationWarning, |
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
530 |
stacklevel=2) |
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
|
531 |
# XXX we may want to check we don't give sensible information |
9458
e2dfdd313dfe
[multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9456
diff
changeset
|
532 |
return self.config[option] |
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
|
533 |
|
0 | 534 |
@cached |
535 |
def get_versions(self, checkversions=False): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
536 |
"""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
|
537 |
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
|
538 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
539 |
This is a public method, not requiring a session id. |
0 | 540 |
""" |
541 |
from logilab.common.changelog import Version |
|
542 |
vcconf = {} |
|
9505
53225f66e7ca
[repo] Use a connection instead of a session for repo.get_versions()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9496
diff
changeset
|
543 |
with self.internal_cnx() as cnx: |
53225f66e7ca
[repo] Use a connection instead of a session for repo.get_versions()
Julien Cristau <julien.cristau@logilab.fr>
parents:
9496
diff
changeset
|
544 |
for pk, version in cnx.execute( |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1372
diff
changeset
|
545 |
'Any K,V WHERE P is CWProperty, P value V, P pkey K, ' |
0 | 546 |
'P pkey ~="system.version.%"', build_descr=False): |
547 |
cube = pk.split('.')[-1] |
|
548 |
# XXX cubicweb migration |
|
549 |
if cube in CW_MIGRATION_MAP: |
|
550 |
cube = CW_MIGRATION_MAP[cube] |
|
551 |
version = Version(version) |
|
552 |
vcconf[cube] = version |
|
553 |
if checkversions: |
|
554 |
if cube != 'cubicweb': |
|
555 |
fsversion = self.config.cube_version(cube) |
|
556 |
else: |
|
557 |
fsversion = self.config.cubicweb_version() |
|
558 |
if version < fsversion: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
559 |
msg = ('instance has %s version %s but %s ' |
0 | 560 |
'is installed. Run "cubicweb-ctl upgrade".') |
561 |
raise ExecutionError(msg % (cube, version, fsversion)) |
|
562 |
return vcconf |
|
1482 | 563 |
|
0 | 564 |
@cached |
565 |
def source_defs(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
566 |
"""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
|
567 |
dictionary describing each source as value. |
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. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
570 |
""" |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
571 |
sources = {} |
0 | 572 |
# remove sensitive information |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
573 |
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
|
574 |
sources[uri] = source.public_config |
0 | 575 |
return sources |
576 |
||
577 |
def properties(self): |
|
6256
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
578 |
"""Return a result set containing system wide properties. |
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
579 |
|
da77d3f95079
[repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6211
diff
changeset
|
580 |
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
|
581 |
""" |
9491
e82370a30018
[repo] restore python 2.6 compatibility
Julien Cristau <julien.cristau@logilab.fr>
parents:
9490
diff
changeset
|
582 |
with self.internal_cnx() as cnx: |
9685
0033aa71e077
[server] make internal_cnx get and keep a connection to the db
Julien Cristau <julien.cristau@logilab.fr>
parents:
9683
diff
changeset
|
583 |
# don't use cnx.execute, we don't want rset.req set |
0033aa71e077
[server] make internal_cnx get and keep a connection to the db
Julien Cristau <julien.cristau@logilab.fr>
parents:
9683
diff
changeset
|
584 |
return self.querier.execute(cnx, 'Any K,V WHERE P is CWProperty,' |
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
|
585 |
'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
|
586 |
build_descr=False) |
0 | 587 |
|
9687
00c2356faba7
[server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents:
9685
diff
changeset
|
588 |
@deprecated("[3.19] Use session.call_service('register_user') instead'") |
1372
d4264cd876e1
register_user can now also set an email
Florent <florent@secondweb.fr>
parents:
1320
diff
changeset
|
589 |
def register_user(self, login, password, email=None, **kwargs): |
0 | 590 |
"""check a user with the given login exists, if not create it with the |
591 |
given password. This method is designed to be used for anonymous |
|
592 |
registration on public web site. |
|
593 |
""" |
|
9607
6942622fd5dc
[repository] Use an internal connection in register_user
Julien Cristau <julien.cristau@logilab.fr>
parents:
9604
diff
changeset
|
594 |
with self.internal_cnx() as cnx: |
9687
00c2356faba7
[server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents:
9685
diff
changeset
|
595 |
cnx.call_service('register_user', login=login, password=password, |
00c2356faba7
[server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents:
9685
diff
changeset
|
596 |
email=email, **kwargs) |
9685
0033aa71e077
[server] make internal_cnx get and keep a connection to the db
Julien Cristau <julien.cristau@logilab.fr>
parents:
9683
diff
changeset
|
597 |
cnx.commit() |
1482 | 598 |
|
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
599 |
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
|
600 |
"""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
|
601 |
(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
|
602 |
|
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
603 |
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
|
604 |
mind). |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
605 |
|
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
606 |
`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
|
607 |
`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
|
608 |
""" |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
609 |
assert query_attrs |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
610 |
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
|
611 |
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
|
612 |
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
|
613 |
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
|
614 |
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
|
615 |
cwuserattrs = self._cwuser_attrs |
8696
0bb18407c053
[toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8695
diff
changeset
|
616 |
for k in chain(fetch_attrs, query_attrs): |
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
617 |
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
|
618 |
raise Exception('bad input for find_user') |
10345
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
619 |
with self.internal_cnx() as cnx: |
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
620 |
varmaker = rqlvar_maker() |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
621 |
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
|
622 |
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
|
623 |
rql += ','.join('X %s %s' % (var[0], var[1]) for var in vars) + ',' |
10345
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
624 |
rset = cnx.execute(rql + ','.join('X %s %%(%s)s' % (attr, attr) |
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
625 |
for attr in query_attrs), |
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
626 |
query_attrs) |
6390
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
627 |
return rset.rows |
3766853656d7
[repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
6368
diff
changeset
|
628 |
|
10018
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
629 |
def new_session(self, login, **kwargs): |
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
630 |
"""open a new session for a given user |
0 | 631 |
|
632 |
raise `AuthenticationError` if the authentication failed |
|
633 |
raise `ConnectionError` if we can't open a connection |
|
634 |
""" |
|
8538
00597256de18
[request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8537
diff
changeset
|
635 |
cnxprops = kwargs.pop('cnxprops', None) |
0 | 636 |
# use an internal connection |
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
637 |
with self.internal_cnx() as cnx: |
8433
ff9d6d269877
[server/session,repo] turn InternalSession, hence repo.internal_session, into a context manager (closes #2393651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8393
diff
changeset
|
638 |
# try to get a user object |
9512
88dc96fc9fc1
[server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents:
9510
diff
changeset
|
639 |
user = self.authenticate_user(cnx, login, **kwargs) |
0 | 640 |
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
|
641 |
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
|
642 |
user.cw_clear_relation_cache() |
9496
e699fbcc9a61
[server/repo] Use session.sessionid instead of session.id
Julien Cristau <julien.cristau@logilab.fr>
parents:
9491
diff
changeset
|
643 |
self._sessions[session.sessionid] = session |
e699fbcc9a61
[server/repo] Use session.sessionid instead of session.id
Julien Cristau <julien.cristau@logilab.fr>
parents:
9491
diff
changeset
|
644 |
self.info('opened session %s for user %s', session.sessionid, login) |
9622
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
645 |
with session.new_cnx() as cnx: |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
646 |
self.hm.call_hooks('session_open', cnx) |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
647 |
# commit connection at this point in case write operation has been |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
648 |
# done during `session_open` hooks |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
649 |
cnx.commit() |
10018
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
650 |
return session |
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
651 |
|
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
652 |
def connect(self, login, **kwargs): |
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
653 |
"""open a new session for a given user and return its sessionid """ |
72f3249b2f7d
[repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9984
diff
changeset
|
654 |
return self.new_session(login, **kwargs).sessionid |
0 | 655 |
|
656 |
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
|
657 |
"""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
|
658 |
return its latest activity timestamp. |
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
659 |
""" |
10346
b926ff4ef4a8
[repoapi,session] remove all session-as-cnx backward compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10345
diff
changeset
|
660 |
return self._get_session(sessionid).timestamp |
0 | 661 |
|
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
|
662 |
def close(self, sessionid, txid=None, checkshuttingdown=True): |
0 | 663 |
"""close the session with the given id""" |
9623
2171da5b9234
[repo] don't set cnxset when getting the session to close it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9622
diff
changeset
|
664 |
session = self._get_session(sessionid, txid=txid, |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
665 |
checkshuttingdown=checkshuttingdown) |
9267
24d9b86dfa54
spelling: rollbacked -> rolled back
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9226
diff
changeset
|
666 |
# operation uncommited before close are rolled back before hook is called |
9622
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
667 |
with session.new_cnx() as cnx: |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
668 |
self.hm.call_hooks('session_close', cnx) |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
669 |
# commit connection at this point in case write operation has been |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
670 |
# done during `session_close` hooks |
637a12b0d3a2
[repo] handle connection explicitly when calling session open/close hooks
Julien Cristau <julien.cristau@logilab.fr>
parents:
9609
diff
changeset
|
671 |
cnx.commit() |
0 | 672 |
session.close() |
673 |
del self._sessions[sessionid] |
|
674 |
self.info('closed session %s for user %s', sessionid, session.user.login) |
|
1482 | 675 |
|
0 | 676 |
# session handling ######################################################## |
1482 | 677 |
|
0 | 678 |
def close_sessions(self): |
679 |
"""close every opened sessions""" |
|
8699
864ffd247d70
[towards py3k] fix bug introduced by 0bb18407c053
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8696
diff
changeset
|
680 |
for sessionid in list(self._sessions): |
0 | 681 |
try: |
1939
67e7379edd96
#343379: disturbing message on upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1883
diff
changeset
|
682 |
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
|
683 |
except Exception: # XXX BaseException? |
0 | 684 |
self.exception('error while closing session %s' % sessionid) |
685 |
||
686 |
def clean_sessions(self): |
|
687 |
"""close sessions not used since an amount of time specified in the |
|
688 |
configuration |
|
689 |
""" |
|
6012
d56fd78006cd
[session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5920
diff
changeset
|
690 |
mintime = time() - self.cleanup_session_time |
0 | 691 |
self.debug('cleaning session unused since %s', |
9885
9f546848ba48
[server] Replace non portable strftime formatter (closes #4132161)
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9800
diff
changeset
|
692 |
strftime('%H:%M:%S', localtime(mintime))) |
0 | 693 |
nbclosed = 0 |
8890
57ffd0e0643d
[clean_session] revert itervalues usage from 0bb18407c053
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8881
diff
changeset
|
694 |
for session in self._sessions.values(): |
0 | 695 |
if session.timestamp < mintime: |
9496
e699fbcc9a61
[server/repo] Use session.sessionid instead of session.id
Julien Cristau <julien.cristau@logilab.fr>
parents:
9491
diff
changeset
|
696 |
self.close(session.sessionid) |
0 | 697 |
nbclosed += 1 |
698 |
return nbclosed |
|
1482 | 699 |
|
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
700 |
@contextmanager |
9127
aff75b69db92
[repository] drop safe attribute on ``internal_cnx``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9113
diff
changeset
|
701 |
def internal_cnx(self): |
9747
10108d9f502a
[repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9687
diff
changeset
|
702 |
"""Context manager returning a Connection using internal user which have |
10108d9f502a
[repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9687
diff
changeset
|
703 |
every access rights on the repository. |
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
704 |
|
9747
10108d9f502a
[repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9687
diff
changeset
|
705 |
Beware that unlike the older :meth:`internal_session`, internal |
10108d9f502a
[repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9687
diff
changeset
|
706 |
connections have all hooks beside security enabled. |
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
707 |
""" |
10345
ef54ea75a642
[server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents:
10342
diff
changeset
|
708 |
with Session(InternalManager(), self) as session: |
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
709 |
with session.new_cnx() as cnx: |
10539
08c5d26417f6
[server] stick a _cw on internal connnection's user
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
710 |
cnx.user._cw = cnx # XXX remove when "vreg = user._cw.vreg" |
08c5d26417f6
[server] stick a _cw on internal connnection's user
Julien Cristau <julien.cristau@logilab.fr>
parents:
10510
diff
changeset
|
711 |
# hack in entity.py is gone |
9747
10108d9f502a
[repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9687
diff
changeset
|
712 |
with cnx.security_enabled(read=False, write=False): |
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
713 |
yield cnx |
9113
af6efc15fc90
[repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
9105
diff
changeset
|
714 |
|
10346
b926ff4ef4a8
[repoapi,session] remove all session-as-cnx backward compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10345
diff
changeset
|
715 |
def _get_session(self, sessionid, txid=None, checkshuttingdown=True): |
9272
68744f5154c4
[server] fix a number of typos, mostly in docstrings
Julien Cristau <julien.cristau@logilab.fr>
parents:
9267
diff
changeset
|
716 |
"""return the session associated with 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
|
717 |
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
|
718 |
raise ShuttingDown('Repository is shutting down') |
0 | 719 |
try: |
720 |
session = self._sessions[sessionid] |
|
721 |
except KeyError: |
|
722 |
raise BadConnectionId('No such session %s' % sessionid) |
|
723 |
return session |
|
724 |
||
725 |
# data sources handling ################################################### |
|
726 |
# * correspondance between eid and (type, source) |
|
727 |
# * correspondance between eid and local id (i.e. specific to a given source) |
|
1482 | 728 |
|
9800
029f9eafe574
[repository] session -> cnx renaming (already done in the source object)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
729 |
def type_and_source_from_eid(self, eid, cnx): |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
730 |
"""return a tuple `(type, extid, actual source uri)` for the entity of |
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
731 |
the given `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
|
732 |
""" |
0 | 733 |
try: |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8699
diff
changeset
|
734 |
eid = int(eid) |
0 | 735 |
except ValueError: |
736 |
raise UnknownEid(eid) |
|
737 |
try: |
|
738 |
return self._type_source_cache[eid] |
|
739 |
except KeyError: |
|
9800
029f9eafe574
[repository] session -> cnx renaming (already done in the source object)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
740 |
etype, extid, auri = self.system_source.eid_type_source(cnx, eid) |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
741 |
self._type_source_cache[eid] = (etype, extid, auri) |
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
742 |
return etype, extid, auri |
0 | 743 |
|
744 |
def clear_caches(self, eids): |
|
745 |
etcache = self._type_source_cache |
|
746 |
extidcache = self._extid_cache |
|
747 |
rqlcache = self.querier._rql_cache |
|
748 |
for eid in eids: |
|
749 |
try: |
|
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
750 |
etype, extid, auri = etcache.pop(int(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
|
751 |
rqlcache.pop( ('%s X WHERE X eid %s' % (etype, eid),), None) |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
752 |
extidcache.pop(extid, None) |
0 | 753 |
except KeyError: |
754 |
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
|
755 |
rqlcache.pop( ('Any X WHERE X eid %s' % eid,), None) |
9456
a79e88aad555
[multi-sources-removal]Â Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9455
diff
changeset
|
756 |
self.system_source.clear_eid_cache(eid, etype) |
1482 | 757 |
|
9800
029f9eafe574
[repository] session -> cnx renaming (already done in the source object)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
758 |
def type_from_eid(self, eid, cnx): |
0 | 759 |
"""return the type of the entity with id <eid>""" |
9800
029f9eafe574
[repository] session -> cnx renaming (already done in the source object)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9774
diff
changeset
|
760 |
return self.type_and_source_from_eid(eid, cnx)[0] |
1482 | 761 |
|
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
762 |
def querier_cache_key(self, cnx, rql, args, eidkeys): |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
763 |
cachekey = [rql] |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
764 |
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
|
765 |
try: |
10368
1691be295517
[repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10365
diff
changeset
|
766 |
etype = self.type_from_eid(args[key], cnx) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
767 |
except KeyError: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
768 |
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
|
769 |
except TypeError: |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
770 |
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
|
771 |
key, args[key])) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
772 |
cachekey.append(etype) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
773 |
# ensure eid is correctly typed in args |
8748
f5027f8d2478
drop typed_eid() in favour of int() (closes #2742462)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8699
diff
changeset
|
774 |
args[key] = int(args[key]) |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
775 |
return tuple(cachekey) |
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5115
diff
changeset
|
776 |
|
9666
9a40a62d54bf
[repo] make extid2eid work with either a session or a connection
Julien Cristau <julien.cristau@logilab.fr>
parents:
9665
diff
changeset
|
777 |
def extid2eid(self, source, extid, etype, cnx, insert=True, |
9665
887ad08e3a61
[repository]Â extid2eid's 'commit' argument no more necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9664
diff
changeset
|
778 |
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
|
779 |
"""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
|
780 |
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
|
781 |
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
|
782 |
|
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
|
783 |
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
|
784 |
|
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
|
785 |
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
|
786 |
|
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
|
787 |
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
|
788 |
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
|
789 |
|
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
|
790 |
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
|
791 |
'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
|
792 |
|
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
|
793 |
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
|
794 |
|
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
|
795 |
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
|
796 |
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
|
797 |
|
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
|
798 |
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
|
799 |
'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
|
800 |
""" |
0 | 801 |
try: |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
802 |
return self._extid_cache[extid] |
0 | 803 |
except KeyError: |
804 |
pass |
|
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
805 |
eid = self.system_source.extid2eid(cnx, extid) |
0 | 806 |
if eid is not None: |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
807 |
self._extid_cache[extid] = eid |
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
808 |
self._type_source_cache[eid] = (etype, extid, source.uri) |
0 | 809 |
return eid |
810 |
if not insert: |
|
811 |
return |
|
9666
9a40a62d54bf
[repo] make extid2eid work with either a session or a connection
Julien Cristau <julien.cristau@logilab.fr>
parents:
9665
diff
changeset
|
812 |
# no link between extid and eid, create one |
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
813 |
# write query, ensure connection's mode is 'write' so connections |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
814 |
# won't be released until commit/rollback |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
815 |
try: |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
816 |
eid = self.system_source.create_eid(cnx) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
817 |
self._extid_cache[extid] = eid |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
818 |
self._type_source_cache[eid] = (etype, extid, source.uri) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
819 |
entity = source.before_entity_insertion( |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
820 |
cnx, extid, etype, eid, sourceparams) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
821 |
if source.should_call_hooks: |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
822 |
# get back a copy of operation for later restore if |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
823 |
# necessary, see below |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
824 |
pending_operations = cnx.pending_operations[:] |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
825 |
self.hm.call_hooks('before_add_entity', cnx, entity=entity) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
826 |
self.add_info(cnx, entity, source, extid) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
827 |
source.after_entity_insertion(cnx, extid, entity, sourceparams) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
828 |
if source.should_call_hooks: |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
829 |
self.hm.call_hooks('after_add_entity', cnx, entity=entity) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
830 |
return eid |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
831 |
except Exception: |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
832 |
# XXX do some cleanup manually so that the transaction has a |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
833 |
# chance to be commited, with simply this entity discarded |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
834 |
self._extid_cache.pop(extid, None) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
835 |
self._type_source_cache.pop(eid, None) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
836 |
if 'entity' in locals(): |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
837 |
hook.CleanupDeletedEidsCacheOp.get_instance(cnx).add_data(entity.eid) |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
838 |
self.system_source.delete_info_multi(cnx, [entity]) |
9665
887ad08e3a61
[repository]Â extid2eid's 'commit' argument no more necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9664
diff
changeset
|
839 |
if source.should_call_hooks: |
10365
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
840 |
cnx.pending_operations = pending_operations |
21461f80f348
[connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10354
diff
changeset
|
841 |
raise |
1482 | 842 |
|
10315
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
843 |
def add_info(self, cnx, entity, source, extid=None): |
0 | 844 |
"""add type and source info for an eid into the system table, |
845 |
and index the entity with the full text index |
|
846 |
""" |
|
847 |
# begin by inserting eid/type/source/extid into the entities table |
|
10315
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
848 |
hook.CleanupNewEidsCacheOp.get_instance(cnx).add_data(entity.eid) |
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
849 |
self.system_source.add_info(cnx, entity, source, extid) |
1482 | 850 |
|
10315
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
851 |
def _delete_cascade_multi(self, cnx, entities): |
10203
1443fe643a38
[server] change order of entities table modification vs entity creation/deletion
Julien Cristau <julien.cristau@logilab.fr>
parents:
10110
diff
changeset
|
852 |
"""same as _delete_cascade but accepts a list of entities with |
1443fe643a38
[server] change order of entities table modification vs entity creation/deletion
Julien Cristau <julien.cristau@logilab.fr>
parents:
10110
diff
changeset
|
853 |
the same etype and belonging to the same source. |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
854 |
""" |
10315
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
855 |
pendingrtypes = cnx.transaction_data.get('pendingrtypes', ()) |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
856 |
# 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
|
857 |
# delete all its relations without security checking |
10315
0f3489d622ba
[repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents:
10301
diff
changeset
|
858 |
with cnx.security_enabled(read=False, write=False): |
7895 | 859 |
in_eids = ','.join([str(_e.eid) for _e in entities]) |
10351
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
860 |
with cnx.running_hooks_ops(): |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
861 |
for rschema, _, role in entities[0].e_schema.relation_definitions(): |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
862 |
if rschema.rule: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
863 |
continue # computed relation |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
864 |
rtype = rschema.type |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
865 |
if rtype in schema.VIRTUAL_RTYPES or rtype in pendingrtypes: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
866 |
continue |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
867 |
if role == 'subject': |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
868 |
# don't skip inlined relation so they are regularly |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
869 |
# deleted and so hooks are correctly called |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
870 |
rql = 'DELETE X %s Y WHERE X eid IN (%s)' % (rtype, in_eids) |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
871 |
else: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
872 |
rql = 'DELETE Y %s X WHERE X eid IN (%s)' % (rtype, in_eids) |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
873 |
try: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
874 |
cnx.execute(rql, build_descr=False) |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
875 |
except ValidationError: |
7885
9454b7ef5ae4
[ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7884
diff
changeset
|
876 |
raise |
10351
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
877 |
except Unauthorized: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
878 |
self.exception('Unauthorized exception while cascading delete for entity %s. ' |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
879 |
'RQL: %s.\nThis should not happen since security is disabled here.', |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
880 |
entities, rql) |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
881 |
raise |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
882 |
except Exception: |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
883 |
if self.config.mode == 'test': |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
884 |
raise |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
885 |
self.exception('error while cascading delete for entity %s. RQL: %s', |
91e63306e277
[connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
10346
diff
changeset
|
886 |
entities, rql) |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
887 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
888 |
def init_entity_caches(self, cnx, entity, source): |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
889 |
"""add entity to connection entities cache and repo's extid cache. |
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
|
890 |
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
|
891 |
""" |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
892 |
cnx.set_entity_cache(entity) |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
893 |
if source.uri == '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
|
894 |
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
|
895 |
else: |
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
|
896 |
extid = source.get_extid(entity) |
9469
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
897 |
self._extid_cache[str(extid)] = entity.eid |
032825bbacab
[multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9468
diff
changeset
|
898 |
self._type_source_cache[entity.eid] = (entity.cw_etype, extid, 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
|
899 |
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
|
900 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
901 |
def glob_add_entity(self, cnx, edited): |
0 | 902 |
"""add an entity to the repository |
1482 | 903 |
|
10039
1d1afe3b5081
[server] typo fix
Julien Cristau <julien.cristau@logilab.fr>
parents:
10018
diff
changeset
|
904 |
the entity eid should originally be None and a unique eid is assigned to |
0 | 905 |
the entity instance |
906 |
""" |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
907 |
entity = edited.entity |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
908 |
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
|
909 |
# 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
|
910 |
entity.cw_edited = edited |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
911 |
source = self.system_source |
5072
072ae171aeb0
[cleanup] style fixes, add nodes, 0.2 cents refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5068
diff
changeset
|
912 |
# allocate an eid to the entity before calling hooks |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
913 |
entity.eid = self.system_source.create_eid(cnx) |
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
|
914 |
# set caches asap |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
915 |
extid = self.init_entity_caches(cnx, 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
|
916 |
if server.DEBUG & server.DBG_REPO: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
917 |
print('ADD entity', self, entity.cw_etype, entity.eid, edited) |
8456
c912d82f2166
[repository] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8433
diff
changeset
|
918 |
prefill_entity_caches(entity) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
919 |
self.hm.call_hooks('before_add_entity', cnx, entity=entity) |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
920 |
relations = preprocess_inlined_relations(cnx, entity) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
921 |
edited.set_defaults() |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
922 |
if cnx.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
|
923 |
edited.check(creation=True) |
10203
1443fe643a38
[server] change order of entities table modification vs entity creation/deletion
Julien Cristau <julien.cristau@logilab.fr>
parents:
10110
diff
changeset
|
924 |
self.add_info(cnx, entity, source, extid) |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
925 |
try: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
926 |
source.add_entity(cnx, entity) |
10446
1e6655cff5ab
add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents:
10368
diff
changeset
|
927 |
except (UniqueTogetherError, ViolatedConstraint) as exc: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
928 |
userhdlr = cnx.vreg['adapters'].select( |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
929 |
'IUserFriendlyError', cnx, entity=entity, exc=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
|
930 |
userhdlr.raise_user_exception() |
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
|
931 |
edited.saved = entity._cw_is_saved = True |
0 | 932 |
# trigger after_add_entity after after_add_relation |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
933 |
self.hm.call_hooks('after_add_entity', cnx, entity=entity) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
934 |
# call hooks for inlined relations |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
935 |
for attr, value in relations: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
936 |
self.hm.call_hooks('before_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
937 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
938 |
self.hm.call_hooks('after_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
939 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
0 | 940 |
return entity.eid |
1482 | 941 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
942 |
def glob_update_entity(self, cnx, edited): |
0 | 943 |
"""replace an entity in the repository |
944 |
the type and the eid of an entity must not be changed |
|
945 |
""" |
|
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
946 |
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
|
947 |
if server.DEBUG & server.DBG_REPO: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
948 |
print('UPDATE entity', entity.cw_etype, entity.eid, |
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
949 |
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
|
950 |
hm = self.hm |
0 | 951 |
eschema = entity.e_schema |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
952 |
cnx.set_entity_cache(entity) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
953 |
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
|
954 |
entity.cw_edited = edited |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
955 |
source = self.system_source |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
956 |
try: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
957 |
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
|
958 |
relations = [] |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
959 |
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
|
960 |
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
|
961 |
continue |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
962 |
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
|
963 |
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
|
964 |
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
|
965 |
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
|
966 |
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
|
967 |
else: |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
968 |
# inlined relation |
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
969 |
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
|
970 |
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
|
971 |
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
|
972 |
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
|
973 |
previous_value = None |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
974 |
else: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
975 |
hm.call_hooks('before_delete_relation', cnx, |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
976 |
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
|
977 |
eidto=previous_value) |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
978 |
relations.append((attr, edited[attr], previous_value)) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
979 |
# call hooks for inlined relations |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
980 |
for attr, value, _t in relations: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
981 |
hm.call_hooks('before_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
982 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
983 |
if not only_inline_rels: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
984 |
hm.call_hooks('before_update_entity', cnx, entity=entity) |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
985 |
if cnx.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
|
986 |
edited.check() |
6211
e9d125fd1465
nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6189
diff
changeset
|
987 |
try: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
988 |
source.update_entity(cnx, entity) |
6225 | 989 |
edited.saved = True |
10446
1e6655cff5ab
add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents:
10368
diff
changeset
|
990 |
except (UniqueTogetherError, ViolatedConstraint) as exc: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
991 |
userhdlr = cnx.vreg['adapters'].select( |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
992 |
'IUserFriendlyError', cnx, entity=entity, exc=exc) |
9226
653f1d4a1101
[repository] properly use IUserFriendlyError when UniqueTogetherError is raised during entity update. Closes #3096638
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9184
diff
changeset
|
993 |
userhdlr.raise_user_exception() |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
994 |
self.system_source.update_info(cnx, entity, need_fti_update) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
995 |
if not only_inline_rels: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
996 |
hm.call_hooks('after_update_entity', cnx, entity=entity) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
997 |
for attr, value, prevvalue in relations: |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
998 |
# if the relation is already cached, update existant cache |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
999 |
relcache = entity.cw_relation_cached(attr, 'subject') |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1000 |
if prevvalue is not None: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1001 |
hm.call_hooks('after_delete_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1002 |
eidfrom=entity.eid, rtype=attr, eidto=prevvalue) |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1003 |
if relcache is not None: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1004 |
cnx.update_rel_cache_del(entity.eid, attr, prevvalue) |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1005 |
del_existing_rel_if_needed(cnx, entity.eid, attr, value) |
9770
112c884b2d8d
merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
diff
changeset
|
1006 |
cnx.update_rel_cache_add(entity.eid, attr, value) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1007 |
hm.call_hooks('after_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1008 |
eidfrom=entity.eid, rtype=attr, eidto=value) |
5115
2e43ef618d14
[repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5093
diff
changeset
|
1009 |
finally: |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6139
diff
changeset
|
1010 |
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
|
1011 |
entity.cw_edited = orig_edited |
0 | 1012 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1013 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1014 |
def glob_delete_entities(self, cnx, eids): |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1015 |
"""delete a list of entities and all related entities from the repository""" |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1016 |
# mark eids as being deleted in cnx info and setup cache update |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1017 |
# 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
|
1018 |
# multiple call to glob_delete_entities) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1019 |
op = hook.CleanupDeletedEidsCacheOp.get_instance(cnx) |
7507
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1020 |
if not isinstance(eids, (set, frozenset)): |
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1021 |
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
|
1022 |
stacklevel=2) |
4c043afb104a
fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7503
diff
changeset
|
1023 |
eids = frozenset(eids) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1024 |
eids = eids - op._container |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1025 |
op._container |= eids |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1026 |
data_by_etype = {} # values are [list of entities] |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1027 |
# |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1028 |
# 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
|
1029 |
# 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
|
1030 |
# of the Python interpreter advertises large perf improvements |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1031 |
# in setdefault, this should not be changed without profiling. |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1032 |
for eid in eids: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1033 |
etype = self.type_from_eid(eid, cnx) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1034 |
# XXX should cache entity's cw_metainformation |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1035 |
entity = cnx.entity_from_eid(eid, etype) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1036 |
try: |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1037 |
data_by_etype[etype].append(entity) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1038 |
except KeyError: |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1039 |
data_by_etype[etype] = [entity] |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1040 |
source = self.system_source |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1041 |
for etype, entities in data_by_etype.iteritems(): |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6801
diff
changeset
|
1042 |
if server.DEBUG & server.DBG_REPO: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
1043 |
print('DELETE entities', etype, [entity.eid for entity in entities]) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1044 |
self.hm.call_hooks('before_delete_entity', cnx, entities=entities) |
10203
1443fe643a38
[server] change order of entities table modification vs entity creation/deletion
Julien Cristau <julien.cristau@logilab.fr>
parents:
10110
diff
changeset
|
1045 |
self._delete_cascade_multi(cnx, entities) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1046 |
source.delete_entities(cnx, entities) |
10203
1443fe643a38
[server] change order of entities table modification vs entity creation/deletion
Julien Cristau <julien.cristau@logilab.fr>
parents:
10110
diff
changeset
|
1047 |
source.delete_info_multi(cnx, entities) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1048 |
self.hm.call_hooks('after_delete_entity', cnx, entities=entities) |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7373
diff
changeset
|
1049 |
# don't clear cache here, it is done in a hook on commit |
1482 | 1050 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1051 |
def glob_add_relation(self, cnx, subject, rtype, object): |
0 | 1052 |
"""add a relation to the repository""" |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1053 |
self.glob_add_relations(cnx, {rtype: [(subject, object)]}) |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1054 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1055 |
def glob_add_relations(self, cnx, relations): |
7237
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1056 |
"""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
|
1057 |
|
9f619715665b
[server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7134
diff
changeset
|
1058 |
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
|
1059 |
""" |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1060 |
source = self.system_source |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1061 |
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
|
1062 |
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
|
1063 |
objects_by_types = {} |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1064 |
activintegrity = cnx.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
|
1065 |
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
|
1066 |
if server.DEBUG & server.DBG_REPO: |
8548
eeabc752c32e
[repo] fix debug code in DBG_REPO mode (closes #2469942)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
8547
diff
changeset
|
1067 |
for subjeid, objeid in eids_subj_obj: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
1068 |
print('ADD relation', subjeid, rtype, objeid) |
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
|
1069 |
for subjeid, objeid in eids_subj_obj: |
7238
576abb8c4626
fix implementation of repository.glob_add_relations (closes ##1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7237
diff
changeset
|
1070 |
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
|
1071 |
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
|
1072 |
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
|
1073 |
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
|
1074 |
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
|
1075 |
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
|
1076 |
# 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
|
1077 |
# be inserted later, we've remove duplicated eids since they |
10510
51321946da37
Spelling fixes in comments and docstrings
Rémi Cardona <remi.cardona@logilab.fr>
parents:
10488
diff
changeset
|
1078 |
# won't be caught by `del_existing_rel_if_needed` |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1079 |
rdef = cnx.rtype_eids_rdef(rtype, subjeid, objeid) |
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
|
1080 |
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
|
1081 |
if card[0] in '?1': |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1082 |
with cnx.security_enabled(read=False): |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1083 |
cnx.execute('DELETE X %s Y WHERE X eid %%(x)s, ' |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1084 |
'NOT Y eid %%(y)s' % rtype, |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1085 |
{'x': subjeid, 'y': objeid}) |
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
|
1086 |
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
|
1087 |
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
|
1088 |
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
|
1089 |
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
|
1090 |
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
|
1091 |
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
|
1092 |
if card[1] in '?1': |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1093 |
with cnx.security_enabled(read=False): |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1094 |
cnx.execute('DELETE X %s Y WHERE Y eid %%(y)s, ' |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1095 |
'NOT X eid %%(x)s' % rtype, |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1096 |
{'x': subjeid, 'y': objeid}) |
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
|
1097 |
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
|
1098 |
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
|
1099 |
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
|
1100 |
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
|
1101 |
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
|
1102 |
objects[objeid] = len(relations_by_rtype[rtype]) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1103 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1104 |
self.hm.call_hooks('before_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1105 |
rtype=rtype, eids_from_to=source_relations) |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1106 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1107 |
source.add_relations(cnx, rtype, source_relations) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1108 |
rschema = self.schema.rschema(rtype) |
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1109 |
for subjeid, objeid in source_relations: |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1110 |
cnx.update_rel_cache_add(subjeid, rtype, objeid, rschema.symmetric) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1111 |
for rtype, source_relations in relations_by_rtype.iteritems(): |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1112 |
self.hm.call_hooks('after_add_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1113 |
rtype=rtype, eids_from_to=source_relations) |
0 | 1114 |
|
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1115 |
def glob_delete_relation(self, cnx, subject, rtype, object): |
0 | 1116 |
"""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
|
1117 |
if server.DEBUG & server.DBG_REPO: |
10589
7c23b7de2b8d
[py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents:
10579
diff
changeset
|
1118 |
print('DELETE relation', subject, rtype, object) |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1119 |
source = self.system_source |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1120 |
self.hm.call_hooks('before_delete_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1121 |
eidfrom=subject, rtype=rtype, eidto=object) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1122 |
source.delete_relation(cnx, 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
|
1123 |
rschema = self.schema.rschema(rtype) |
9609
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1124 |
cnx.update_rel_cache_del(subject, rtype, object, rschema.symmetric) |
e7d38148799e
[repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents:
9607
diff
changeset
|
1125 |
self.hm.call_hooks('after_delete_relation', cnx, |
9454
a173f9cf9f26
[multi-sources-removal] drop source location search on glob_*[entity|relation] methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
9452
diff
changeset
|
1126 |
eidfrom=subject, rtype=rtype, eidto=object) |
0 | 1127 |
|
1128 |
||
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
|
1129 |
|
1482 | 1130 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7064
diff
changeset
|
1131 |
# 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
|
1132 |
# only defining here to prevent pylint from complaining |
9468
39b7a91a3f4c
[repo] pylint cleanup, mainly of imports, with a bit of style
Julien Cristau <julien.cristau@logilab.fr>
parents:
9467
diff
changeset
|
1133 |
info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None |
0 | 1134 |
|
1135 |
from logging import getLogger |
|
1136 |
from cubicweb import set_log_methods |
|
1137 |
set_log_methods(Repository, getLogger('cubicweb.repository')) |