cubicweb/server/repository.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Fri, 27 Jan 2017 18:09:56 +0100
changeset 11950 68766861debe
parent 11934 ec69125c03a8
child 11962 36851c8b6763
permissions -rw-r--r--
[repository] possibility to disable connections pooling Add a new configuration option 'connections-pooler-enabled' (default yes). If set to no, disable connection pooling and open new connections to the database on demand. This allow to use CubicWeb with other poolers (such as pgbouncer).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
     1
# copyright 2003-2016 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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""Defines the central class for the CubicWeb RQL server: the repository.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
The repository is an abstraction allowing execution of rql queries against
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
data sources. Most of the work is actually done in helper classes. The
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
repository mainly:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
"""
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11763
diff changeset
    28
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
    29
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
    30
7507
4c043afb104a fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7503
diff changeset
    31
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
    32
from itertools import chain
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
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
    34
from contextlib import contextmanager
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
    35
from logging import getLogger
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
10609
e2d8e81bfe68 [py3k] import range using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10608
diff changeset
    37
from six.moves import range, queue
10608
7fc548d9dd8e [py3k] import queue using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10589
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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)
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11774
diff changeset
    49
from cubicweb import set_log_methods
2839
6419af16faa0 imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
    50
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
    51
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
    52
from cubicweb.server.session import Session, InternalManager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    54
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    55
NO_CACHE_RELATIONS = set([
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    56
    ('owned_by', 'object'),
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    57
    ('created_by', 'object'),
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    58
    ('cw_source', 'object'),
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    59
])
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    60
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
    61
8456
c912d82f2166 [repository] drop unused argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8433
diff changeset
    62
def prefill_entity_caches(entity):
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    63
    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
    64
    # 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
    65
    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
    66
        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
    67
        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
    68
            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
    69
        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
    70
            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
    71
        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
    72
            entity.cw_set_relation_cache(rtype, 'subject',
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    73
                                         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
    74
    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
    75
        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
    76
        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
    77
            continue
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    78
        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
    79
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
    80
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    81
def del_existing_rel_if_needed(cnx, eidfrom, rtype, eidto):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    """delete existing relation when adding a new one if card is 1 or ?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    have to be done once the new relation has been inserted to avoid having
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    an entity without a relation for some time
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    this kind of behaviour has to be done in the repository so we don't have
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    hooks order hazardness
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    """
7503
bc30c2faaadc [repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7502
diff changeset
    90
    # skip that if integrity explicitly disabled
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    91
    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
    92
        return
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
    93
    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
    94
    card = rdef.cardinality
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
    # one may be tented to check for neweids but this may cause more than one
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    # relation even with '1?'  cardinality if thoses relations are added in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
    # same transaction where the entity is being created. This never occurs from
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    # the web interface but may occurs during test or dbapi connection (though
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    # not expected for this).  So: don't do it, we pretend to ensure repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    # 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
   101
    #
6189
9fa02f352c8c cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6188
diff changeset
   102
    # notes:
9fa02f352c8c cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6188
diff changeset
   103
    # * inlined relations will be implicitly deleted for the subject entity
9fa02f352c8c cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6188
diff changeset
   104
    # * we don't want read permissions to be applied but we want delete
9fa02f352c8c cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6188
diff changeset
   105
    #   permission to be checked
7503
bc30c2faaadc [repository] refactor and optimize '?1' relation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7502
diff changeset
   106
    if card[0] 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 X eid %%(x)s, '
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   109
                        'NOT Y eid %%(y)s' % rtype,
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   110
                        {'x': eidfrom, 'y': eidto})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    if card[1] in '1?':
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   112
        with cnx.security_enabled(read=False):
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   113
            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
   114
                        'NOT X eid %%(x)s' % rtype,
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   115
                        {'x': eidfrom, 'y': eidto})
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   116
3694
33dbb1da1db9 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   117
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   118
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
   119
    """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
   120
    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
   121
    """
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
    relations = []
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   123
    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
   124
    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
   125
    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
   126
        rschema = eschema.subjrels[attr]
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   127
        if not rschema.final:  # inlined relation
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
   128
            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
   129
            relations.append((attr, value))
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   130
            cnx.update_rel_cache_add(entity.eid, attr, value)
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   131
            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
   132
            if rdef.cardinality[1] in '1?' and activeintegrity:
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   133
                with cnx.security_enabled(read=False):
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   134
                    cnx.execute('DELETE X %s Y WHERE Y eid %%(y)s' % attr,
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   135
                                {'x': entity.eid, 'y': 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
   136
    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
   137
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
   138
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
   139
class NullEventBus(object):
8214
ce9556358dbd [event bus] event has been renamed to publish
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8211
diff changeset
   140
    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
   141
        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
   142
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
    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
   144
        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
   145
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
    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
   147
        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
   148
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
   149
    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
   150
        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
   151
11934
ec69125c03a8 [server] Add a blank line before _CnxSetPool to fix PEP8 error
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11931
diff changeset
   152
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   153
class _CnxSetPool(object):
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   154
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   155
    def __init__(self, source, size):
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   156
        self._cnxsets = []
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   157
        if size is not None:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   158
            self._queue = queue.Queue()
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   159
            for i in range(size):
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   160
                cnxset = source.wrapped_connection()
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   161
                self._cnxsets.append(cnxset)
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   162
                self._queue.put_nowait(cnxset)
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   163
        else:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   164
            self._queue = None
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   165
            self._source = source
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   166
        super(_CnxSetPool, self).__init__()
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   167
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   168
    def qsize(self):
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   169
        q = self._queue
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   170
        if q is None:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   171
            return None
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   172
        return q.qsize()
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   173
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   174
    def get(self):
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   175
        q = self._queue
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   176
        if q is None:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   177
            return self._source.wrapped_connection()
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   178
        try:
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   179
            return self._queue.get(True, timeout=5)
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   180
        except queue.Empty:
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   181
            raise Exception('no connections set available after 5 secs, probably either a '
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   182
                            'bug in code (too many uncommited/rolled back '
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   183
                            'connections) or too much load on the server (in '
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   184
                            'which case you can try to set a bigger '
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   185
                            'connections pool size)')
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   186
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   187
    def release(self, cnxset):
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   188
        q = self._queue
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   189
        if q is None:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   190
            cnxset.close(True)
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   191
        else:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   192
            self._queue.put_nowait(cnxset)
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   193
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   194
    def __iter__(self):
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   195
        for cnxset in self._cnxsets:
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   196
            yield cnxset
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   197
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   198
    def close(self):
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   199
        q = self._queue
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   200
        if q is not None:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   201
            while not q.empty():
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   202
                cnxset = q.get_nowait()
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   203
                try:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   204
                    cnxset.close(True)
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   205
                except Exception:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   206
                    self.exception('error while closing %s' % cnxset)
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   207
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
   208
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
class Repository(object):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
    """a repository provides access to a set of persistent storages for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    entities and relations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
    """
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   213
8322
cb838b126b07 [repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8321
diff changeset
   214
    def __init__(self, config, tasks_manager=None, vreg=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
        self.config = config
11819
a85b7a898e13 Fix AttributeError on postcommit when db-create adds non-system CWSource entities
Florent Cayré <florent.cayre@logilab.fr>
parents: 11811
diff changeset
   216
        self.sources_by_eid = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
        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
   218
            vreg = cwvreg.CWRegistryStore(config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
        self.vreg = vreg
8322
cb838b126b07 [repository] move task manager instantiation outside repository.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8321
diff changeset
   220
        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
   221
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
   222
        self.app_instances_bus = NullEventBus()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
        self.info('starting repository from %s', self.config.apphome)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        # dictionary of opened sessions
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
        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
   226
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
        # list of functions to be called at regular interval
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        # list of running threads
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
        self._running_threads = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
        # initial schema, should be build or replaced latter
2839
6419af16faa0 imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   231
        self.schema = schema.CubicWebSchema(config.appid)
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   232
        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
   233
        # 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
   234
        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
   235
        # 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
   236
        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
   237
                                             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
   238
        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
   239
        # 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
   240
        self.querier = querier.QuerierHelper(self, self.schema)
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   241
        # cache eid -> type
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   242
        self._type_cache = {}
8946
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   243
        # 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
   244
        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
   245
            self.init_cnxset_pool()
8946
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   246
        # the hooks manager
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   247
        self.hm = hook.HooksManager(self.vreg)
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   248
8946
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   249
        # 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
   250
        @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
   251
        def fix_user_classes(self):
10577
935d577b2227 [server/repo] fix typo
Julien Cristau <julien.cristau@logilab.fr>
parents: 10540
diff changeset
   252
            # After registry reload the 'CWUser' class used for CWEtype
935d577b2227 [server/repo] fix typo
Julien Cristau <julien.cristau@logilab.fr>
parents: 10540
diff changeset
   253
            # 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
   254
            # 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
   255
            usercls = self.vreg['etypes'].etype_class('CWUser')
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   256
            for session in self._sessions.values():
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
   257
                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
   258
                    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
   259
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
   260
    def init_cnxset_pool(self):
8946
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   261
        """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
   262
        config = self.config
11908
7904fe436e82 [repository] Fix connection-pool-size not set to 1 with quick_start enabled
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11821
diff changeset
   263
        # copy pool size here since config.init_cube() and config.load_schema()
7904fe436e82 [repository] Fix connection-pool-size not set to 1 with quick_start enabled
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11821
diff changeset
   264
        # reload configuration from file and could reset a manually set pool
7904fe436e82 [repository] Fix connection-pool-size not set to 1 with quick_start enabled
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11821
diff changeset
   265
        # size.
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   266
        if config['connections-pooler-enabled']:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   267
            pool_size, min_pool_size = config['connections-pool-size'], 1
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   268
        else:
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   269
            pool_size = min_pool_size = None
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   270
        # 0. init a cnxset that will be used to fetch bootstrap information from
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   271
        #    the database
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   272
        self.cnxsets = _CnxSetPool(self.system_source, min_pool_size)
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   273
        # 1. set used cubes
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   274
        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
   275
            config.bootstrap_cubes()
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   276
        else:
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   277
            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
   278
            config.init_cubes(self.get_cubes())
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   279
        # 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
   280
        if config.quick_start:
8946
fae2f561b0f5 [repo] minor cleanups to bootstrap sequence
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8943
diff changeset
   281
            # 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
   282
            # information (eg dump/restore/...)
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   283
            #
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   284
            # 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
   285
            # 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
   286
            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
   287
            config.cubicweb_appobject_path = set(('hooks', 'entities'))
11950
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   288
            # limit connections pool size
68766861debe [repository] possibility to disable connections pooling
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11934
diff changeset
   289
            pool_size = min_pool_size
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   290
        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
   291
            # 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
   292
            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
   293
                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
   294
            self.set_schema(config.load_schema(expand_cubes=True))
11090
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11008
diff changeset
   295
            if not config.creating:
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11008
diff changeset
   296
                # set eids on entities schema
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11008
diff changeset
   297
                with self.internal_cnx() as cnx:
b4b854c25de5 [repository] set .eid on eschema when schema is loaded from the filesystem
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11008
diff changeset
   298
                    for etype, eid in cnx.execute('Any XN,X WHERE X is CWEType, X name XN'):
11477
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   299
                        try:
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   300
                            self.schema.eschema(etype).eid = eid
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   301
                        except KeyError:
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   302
                            # etype in the database doesn't exist in the fs schema, this may occur
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   303
                            # during dev and we shouldn't crash
3b4d41566de3 [repo] Don't crash on start when fs schema is missing some db schema entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   304
                            self.warning('No %s entity type in the file system schema', etype)
8592
df16bd045cae [server] fix repository initialisation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8580
diff changeset
   305
        else:
df16bd045cae [server] fix repository initialisation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8580
diff changeset
   306
            # 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
   307
            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
   308
            self.set_schema(self.deserialize_schema())
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   309
        # 3. initialize data sources
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   310
        if config.creating:
6128
fbb8398f80dc cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   311
            # call init_creating so that for instance native source can
fbb8398f80dc cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6126
diff changeset
   312
            # 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
   313
            self.system_source.init_creating()
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   314
        else:
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   315
            self.init_sources_from_database()
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   316
            if 'CWProperty' in self.schema:
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   317
                self.vreg.init_properties(self.properties())
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   318
        # 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
   319
        #    proper initialization
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   320
        self.cnxsets.close()
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   321
        self.cnxsets = _CnxSetPool(self.system_source, pool_size)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   322
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    # internals ###############################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   325
    def init_sources_from_database(self):
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   326
        if self.config.quick_start or 'CWSource' not 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
   327
            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
   328
            return
9510
a5231a557ac0 [server/repo] use internal_cnx in init_sources_from_database
Julien Cristau <julien.cristau@logilab.fr>
parents: 9507
diff changeset
   329
        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
   330
            # 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
   331
            for sourceent in cnx.execute(
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   332
                    'Any S, SN, SA, SC WHERE S is_instance_of CWSource, '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   333
                    'S name SN, S type SA, S config SC').entities():
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   334
                if sourceent.name == 'system':
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   335
                    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
   336
                    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
   337
                    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
   338
                    continue
9448
3e7cad3967c5 [multi-sources-removal] Drop the "true" multi-sources planner
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9404
diff changeset
   339
                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
   340
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   341
    def _clear_planning_caches(self):
9456
a79e88aad555 [multi-sources-removal] Kill repo.sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9455
diff changeset
   342
        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
   343
9448
3e7cad3967c5 [multi-sources-removal] Drop the "true" multi-sources planner
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9404
diff changeset
   344
    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
   345
        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
   346
            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
   347
                                     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
   348
        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
   349
            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
   350
                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
   351
                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
   352
            raise
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   353
        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
   354
        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
   355
        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
   356
            # 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
   357
            # 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
   358
            # 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
   359
            # initialized)
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6953
diff changeset
   360
            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
   361
        else:
6957
ffda12be2e9f [repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6953
diff changeset
   362
            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
   363
        self._clear_planning_caches()
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   364
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   365
    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
   366
        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
   367
        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
   368
        self._clear_planning_caches()
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   369
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6941
diff changeset
   370
    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
   371
        # 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
   372
        # source_defs()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        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
   374
        source_config['type'] = type
6945
28bf94d062a9 [sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6941
diff changeset
   375
        return sources.get_source(type, source_config, self, eid)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   376
8943
58b3b2d9c965 [repo] kill rebuildinfered feature from Repository.set_schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8916
diff changeset
   377
    def set_schema(self, schema, resetvreg=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        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
   379
        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
   380
            # 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
   381
            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
   382
        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
   383
            self.vreg._set_schema(schema)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
        self.querier.set_schema(schema)
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   385
        for source in self.sources_by_uri.values():
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
            source.set_schema(schema)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        self.schema = schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
8947
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   389
    def deserialize_schema(self):
3bbd416b09ec [repo] straightforward bootstrap sequence. Closes #2841188
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8946
diff changeset
   390
        """load schema from the database"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
        from cubicweb.server.schemaserial import deserialize_schema
2839
6419af16faa0 imports cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2835
diff changeset
   392
        appschema = schema.CubicWebSchema(self.config.appid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
        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
   394
        with self.internal_cnx() as cnx:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
            try:
9490
b3d2c4065e6a [server] use internal_cnx instead of internal_session in deserialize_schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 9488
diff changeset
   396
                deserialize_schema(appschema, cnx)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
            except BadSchemaDefinition:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
                raise
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   399
            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
   400
                import traceback
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1372
diff changeset
   401
                traceback.print_exc()
10110
f601d2fdeff7 [repository] don't mangle the stack trace on exception
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10039
diff changeset
   402
                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
   403
        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
   404
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   405
    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
   406
        """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
   407
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   408
        * 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
   409
        * 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
   410
        """
5043
fe52dd3936cf [repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5032
diff changeset
   411
        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
   412
                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
   413
            # 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
   414
            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
   415
            # 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
   416
            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
   417
            assert self.cleanup_session_time > 0
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   418
            cleanup_session_interval = min(60 * 60, self.cleanup_session_time / 3)
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   419
            assert self._tasks_manager is not None, \
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   420
                "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
   421
            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
   422
                                                 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
   423
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   424
    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
   425
        """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
   426
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   427
        * 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
   428
        * 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
   429
        * 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
   430
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   431
        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
   432
        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
   433
        """
b5d5a5630649 [repository] split repo initialization from starting looping task (closes #2204047)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8320
diff changeset
   434
        self._prepare_startup()
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   435
        assert self._tasks_manager is not None,\
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   436
            "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
   437
        self._tasks_manager.start()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
2708
60d728bdcba5 allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2667
diff changeset
   439
    def looping_task(self, interval, func, *args):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
        """register a function to be called every `interval` seconds.
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   441
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
        looping tasks can only be registered during repository initialization,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
        once done this method will fail.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   444
        """
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   445
        assert self._tasks_manager is not None,\
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   446
            "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
   447
        self._tasks_manager.add_looping_task(interval, func, *args)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
    def threaded_task(self, func):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
        """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
   451
        utils.RepoThread(func, self._running_threads).start()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   452
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
    def shutdown(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
        """called on server stop event to properly close opened sessions and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
        connections
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
        """
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
   457
        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
   458
        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
   459
                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
   460
            # 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
   461
            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
   462
        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
   463
        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
   464
        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
   465
            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
   466
        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
   467
                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
   468
            self.hm.call_hooks('server_shutdown', repo=self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
        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
   470
            self.info('waiting thread %s...', thread.getName())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
            thread.join()
5376
2c3f14bc2590 [python2.6] don't add a name property on Thread
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5292
diff changeset
   472
            self.info('thread %s finished', thread.getName())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
        self.close_sessions()
11931
ad7796dabeaa [repository] move cnxset pool handling to a helper class
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11908
diff changeset
   474
        self.cnxsets.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
        hits, misses = self.querier.cache_hit, self.querier.cache_miss
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
        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
   477
            self.info('rql st cache hit/miss: %s/%s (%s%% hits)', hits, misses,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
                      (hits * 100) / (hits + misses))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
            hits, misses = self.system_source.cache_hit, self.system_source.cache_miss
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
            self.info('sql cache hit/miss: %s/%s (%s%% hits)', hits, misses,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   481
                      (hits * 100) / (hits + misses))
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   482
            nocache = self.system_source.no_cache
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   483
            self.info('sql cache usage: %s/%s (%s%%)', hits + misses, nocache,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
                      ((hits + misses) * 100) / (hits + misses + nocache))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
        except ZeroDivisionError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
            pass
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   487
9512
88dc96fc9fc1 [server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents: 9510
diff changeset
   488
    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
   489
        """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
   490
        associated CWUser's eid on success.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
        """
8229
b7bc631816f7 [ldapfeed] make authentication actually working
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8214
diff changeset
   492
        # 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
   493
        # contain copy based sources
10663
54b8a1f249fb [py3k] dict.itervalues → dict.values
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
   494
        for source in self.sources_by_uri.values():
11750
18e7b9829471 [sources] Drop source support_entity / support_relation source API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11699
diff changeset
   495
            if self.config.source_enabled(source):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
                try:
10365
21461f80f348 [connection] remove ensure_cnx_set context manager uses
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
   497
                    return source.authenticate(cnx, login, **authinfo)
11750
18e7b9829471 [sources] Drop source support_entity / support_relation source API
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11699
diff changeset
   498
                except (NotImplementedError, AuthenticationError):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
            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
   502
9512
88dc96fc9fc1 [server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents: 9510
diff changeset
   503
    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
   504
        """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
   505
        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
   506
        """
9512
88dc96fc9fc1 [server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents: 9510
diff changeset
   507
        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
   508
        cwuser = self._build_user(cnx, eid)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
        if self.config.consider_user_state and \
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   510
           not cwuser.cw_adapt_to('IWorkflowable').state in cwuser.AUTHENTICABLE_STATES:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
            raise AuthenticationError('user is not in authenticable state')
2268
2f336fd5e040 euser->cwuser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2267
diff changeset
   512
        return cwuser
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
9512
88dc96fc9fc1 [server] use a connection instead of a session for user authentication
Julien Cristau <julien.cristau@logilab.fr>
parents: 9510
diff changeset
   514
    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
   515
        """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
   516
        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
   517
        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
   518
        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
   519
        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
   520
        assert len(rset) == 1, rset
11699
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11477
diff changeset
   521
        return rset.get_entity(0, 0)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   522
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
    # public (dbapi) interface ################################################
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   524
9683
74359402bfb0 [repo] fix deprecation messages for stats and gc_stats
Julien Cristau <julien.cristau@logilab.fr>
parents: 9676
diff changeset
   525
    @deprecated("[3.19] use _cw.call_service('repo_stats')")
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   526
    def stats(self):  # XXX restrict to managers session?
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   527
        """Return a dictionary containing some statistics about the repository
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   528
        resources usage.
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   529
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   530
        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
   531
cc3442054e48 [repo] move repo stats to Service (closes #2951067)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9033
diff changeset
   532
        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
   533
        """
9507
540cb068a7f9 [repo] Use a connection instead of a session for repo.stats()
Julien Cristau <julien.cristau@logilab.fr>
parents: 9506
diff changeset
   534
        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
   535
            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
   536
9683
74359402bfb0 [repo] fix deprecation messages for stats and gc_stats
Julien Cristau <julien.cristau@logilab.fr>
parents: 9676
diff changeset
   537
    @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
   538
    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
   539
        """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
   540
        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
   541
e35d4d4f7eb3 [repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents: 8133
diff changeset
   542
        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
   543
e35d4d4f7eb3 [repository] add a ``gc_stats`` method and 2 more cache size for ``stats`` (closes #2179735)
David Douard <david.douard@logilab.fr>
parents: 8133
diff changeset
   544
        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
   545
        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
   546
        """
9506
7f6883783969 [repo] Use a connection instead of a session for repo.gc_stats()
Julien Cristau <julien.cristau@logilab.fr>
parents: 9505
diff changeset
   547
        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
   548
            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
   549
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   550
    def get_schema(self):
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   551
        """Return the instance schema.
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   552
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   553
        This is a public method, not requiring a session id.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
        """
7723
badfd5524ab6 [repo] Stop setting hashmode on schema
Julien Cristau <julien.cristau@logilab.fr>
parents: 7716
diff changeset
   555
        return self.schema
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   556
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
    def get_cubes(self):
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   558
        """Return the list of cubes used by this instance.
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   559
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   560
        This is a public method, not requiring a session id.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
        """
2473
490f88fb99b6 new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2268
diff changeset
   562
        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
   563
                                          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
   564
                                          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
   565
                                          or self.config.mode == 'test'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
        cubes = list(versions)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
        cubes.remove('cubicweb')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
        return cubes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   569
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
   570
    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
   571
        """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
   572
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
   573
        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
   574
e2dfdd313dfe [multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9456
diff changeset
   575
        `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
   576
        """
9458
e2dfdd313dfe [multi-sources-removal] Drop foreid argument of repo.get_option_value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9456
diff changeset
   577
        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
   578
            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
   579
                 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
   580
        # 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
   581
        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
   582
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
    def get_versions(self, checkversions=False):
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   585
        """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
   586
        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
   587
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   588
        This is a public method, not requiring a session id.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   589
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   590
        from logilab.common.changelog import Version
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
        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
   592
        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
   593
            for pk, version in cnx.execute(
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   594
                    'Any K,V WHERE P is CWProperty, P value V, P pkey K, '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   595
                    'P pkey ~="system.version.%"', build_descr=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
                cube = pk.split('.')[-1]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
                # XXX cubicweb migration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
                if cube in CW_MIGRATION_MAP:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
                    cube = CW_MIGRATION_MAP[cube]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
                version = Version(version)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
                vcconf[cube] = version
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   602
                if checkversions:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
                    if cube != 'cubicweb':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
                        fsversion = self.config.cube_version(cube)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   605
                    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   606
                        fsversion = self.config.cubicweb_version()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   607
                    if version < fsversion:
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2473
diff changeset
   608
                        msg = ('instance has %s version %s but %s '
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   609
                               'is installed. Run "cubicweb-ctl upgrade".')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   610
                        raise ExecutionError(msg % (cube, version, fsversion))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
        return vcconf
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   612
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   613
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   614
    def source_defs(self):
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   615
        """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
   616
        dictionary describing each source as value.
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   617
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   618
        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
   619
        """
6427
c8a5ac2d1eaa [schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6426
diff changeset
   620
        sources = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   621
        # remove sensitive information
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   622
        for uri, source in self.sources_by_uri.items():
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
   623
            sources[uri] = source.public_config
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   624
        return sources
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   625
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   626
    def properties(self):
6256
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   627
        """Return a result set containing system wide properties.
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   628
da77d3f95079 [repo] docstring cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6211
diff changeset
   629
        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
   630
        """
9491
e82370a30018 [repo] restore python 2.6 compatibility
Julien Cristau <julien.cristau@logilab.fr>
parents: 9490
diff changeset
   631
        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
   632
            # 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
   633
            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
   634
                                        '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
   635
                                        build_descr=False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   636
9687
00c2356faba7 [server] Refactor Repository.register_user into a CubicWeb service (closes #3020639)
Vladimir Popescu <vladimir.popescu@logilab.fr>
parents: 9685
diff changeset
   637
    @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
   638
    def register_user(self, login, password, email=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   639
        """check a user with the given login exists, if not create it with the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   640
        given password. This method is designed to be used for anonymous
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   641
        registration on public web site.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   642
        """
9607
6942622fd5dc [repository] Use an internal connection in register_user
Julien Cristau <julien.cristau@logilab.fr>
parents: 9604
diff changeset
   643
        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
   644
            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
   645
                             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
   646
            cnx.commit()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   647
6390
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   648
    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
   649
        """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
   650
        (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
   651
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   652
        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
   653
        mind).
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   654
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   655
        `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
   656
        `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
   657
        """
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   658
        assert query_attrs
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   659
        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
   660
            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
   661
            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
   662
                                     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
   663
                                     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
   664
        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
   665
        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
   666
            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
   667
                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
   668
        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
   669
            varmaker = rqlvar_maker()
10669
155c29e0ed1c [py3k] use next builtin instead of next method
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10663
diff changeset
   670
            vars = [(attr, next(varmaker)) for attr in fetch_attrs]
6390
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   671
            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
   672
            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
   673
            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
   674
                                              for attr in query_attrs),
ef54ea75a642 [server] drop repo.internal_session and InternalSession
Julien Cristau <julien.cristau@logilab.fr>
parents: 10342
diff changeset
   675
                               query_attrs)
6390
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   676
            return rset.rows
3766853656d7 [repo] add a find_users method for under-privileged accounts
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6368
diff changeset
   677
10018
72f3249b2f7d [repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9984
diff changeset
   678
    def new_session(self, login, **kwargs):
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   679
        """open a *new* session for a given user
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   680
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   681
        raise `AuthenticationError` if the authentication failed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
        raise `ConnectionError` if we can't open a connection
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
        # 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
   685
        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
   686
            # 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
   687
            user = self.authenticate_user(cnx, login, **kwargs)
11199
2fe2077cc92d [session,repo] remove last trace of "cnxprops"
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11195
diff changeset
   688
        session = Session(user, self)
3379
9192ba07890d use .cw_rset instead of rset on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
   689
        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
   690
        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
   691
        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
   692
        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
   693
        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
   694
            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
   695
            # 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
   696
            # 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
   697
            cnx.commit()
10018
72f3249b2f7d [repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9984
diff changeset
   698
        return session
72f3249b2f7d [repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9984
diff changeset
   699
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   700
    @deprecated('[3.23] use .new_session instead (and get a plain session object)')
10018
72f3249b2f7d [repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9984
diff changeset
   701
    def connect(self, login, **kwargs):
72f3249b2f7d [repository] provide a .new_session entry point
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9984
diff changeset
   702
        return self.new_session(login, **kwargs).sessionid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   703
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   704
    @deprecated('[3.23] use session.close() directly')
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   705
    def close(self, sessionid):
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   706
        self._get_session(sessionid).close()
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   707
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   708
    # session handling ########################################################
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   709
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   710
    def close_sessions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   711
        """close every opened sessions"""
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   712
        for session in list(self._sessions.values()):
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   713
            session.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
    def clean_sessions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   716
        """close sessions not used since an amount of time specified in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
        configuration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   718
        """
6012
d56fd78006cd [session] cleanup session-time / cleanup-session-time...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5920
diff changeset
   719
        mintime = time() - self.cleanup_session_time
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
        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
   721
                   strftime('%H:%M:%S', localtime(mintime)))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   722
        nbclosed = 0
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   723
        for session in list(self._sessions.values()):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   724
            if session.timestamp < mintime:
11195
5de859b95988 [session, repository] deprecate repo.connect and move .close reponsibility to session object
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11129
diff changeset
   725
                session.close()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   726
                nbclosed += 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
        return nbclosed
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   728
9113
af6efc15fc90 [repository] add an ``internal_cnx`` method to replace ``internal_session``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9105
diff changeset
   729
    @contextmanager
9127
aff75b69db92 [repository] drop safe attribute on ``internal_cnx``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9113
diff changeset
   730
    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
   731
        """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
   732
        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
   733
9747
10108d9f502a [repo] make it clear in internal_cnx that security is disabled
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9687
diff changeset
   734
        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
   735
        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
   736
        """
11204
71057a8bb19a [session] drop session-as-a-context-manager
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11199
diff changeset
   737
        with Session(InternalManager(), self).new_cnx() as cnx:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   738
            cnx.user._cw = cnx  # XXX remove when "vreg = user._cw.vreg" hack in entity.py is gone
11204
71057a8bb19a [session] drop session-as-a-context-manager
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11199
diff changeset
   739
            with cnx.security_enabled(read=False, write=False):
71057a8bb19a [session] drop session-as-a-context-manager
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 11199
diff changeset
   740
                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
   741
10346
b926ff4ef4a8 [repoapi,session] remove all session-as-cnx backward compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10345
diff changeset
   742
    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
   743
        """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
   744
        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
   745
            raise ShuttingDown('Repository is shutting down')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
            session = self._sessions[sessionid]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
            raise BadConnectionId('No such session %s' % sessionid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
        return session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
    # data sources handling ###################################################
11763
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   753
    # * correspondance between eid and type
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
    # * correspondance between eid and local id (i.e. specific to a given source)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   755
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   756
    def clear_caches(self, eids):
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   757
        etcache = self._type_cache
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   758
        rqlcache = self.querier._rql_cache
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   759
        for eid in eids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
            try:
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   761
                etype = etcache.pop(int(eid))  # may be a string in some cases
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   762
                rqlcache.pop(('%s X WHERE X eid %s' % (etype, eid),), None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   763
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   764
                etype = None
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   765
            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
   766
            self.system_source.clear_eid_cache(eid, etype)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   767
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   768
    def type_from_eid(self, eid, cnx):
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   769
        """Return the type of the entity with id `eid`"""
11763
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   770
        try:
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   771
            eid = int(eid)
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   772
        except ValueError:
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   773
            raise UnknownEid(eid)
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   774
        try:
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   775
            return self._type_cache[eid]
11763
39df042f4ab4 [repository] Drop type_and_source_from_eid and rename related cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11756
diff changeset
   776
        except KeyError:
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   777
            etype = self.system_source.eid_type(cnx, eid)
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   778
            self._type_cache[eid] = etype
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   779
            return etype
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   780
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   781
    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
   782
        cachekey = [rql]
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   783
        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
   784
            try:
10368
1691be295517 [repository] rename session -> cnx
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10365
diff changeset
   785
                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
   786
            except KeyError:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   787
                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
   788
            except TypeError:
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   789
                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
   790
                    key, args[key]))
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   791
            cachekey.append(etype)
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   792
            # 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
   793
            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
   794
        return tuple(cachekey)
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5115
diff changeset
   795
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   796
    def add_info(self, cnx, entity, source):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   797
        """add type and source info for an eid into the system table,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   798
        and index the entity with the full text index
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   799
        """
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   800
        # begin by inserting eid/type/source 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
   801
        hook.CleanupNewEidsCacheOp.get_instance(cnx).add_data(entity.eid)
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   802
        self.system_source.add_info(cnx, entity, source)
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   803
10315
0f3489d622ba [repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents: 10301
diff changeset
   804
    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
   805
        """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
   806
        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
   807
        """
10315
0f3489d622ba [repository] replace session with cnx in a few places
Julien Cristau <julien.cristau@logilab.fr>
parents: 10301
diff changeset
   808
        pendingrtypes = cnx.transaction_data.get('pendingrtypes', ())
6889
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   809
        # 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
   810
        # 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
   811
        with cnx.security_enabled(read=False, write=False):
7895
0a967180794b cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7887
diff changeset
   812
            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
   813
            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
   814
                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
   815
                    if rschema.rule:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   816
                        continue  # computed relation
10351
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   817
                    rtype = rschema.type
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   818
                    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
   819
                        continue
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   820
                    if role == 'subject':
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   821
                        # 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
   822
                        # 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
   823
                        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
   824
                    else:
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   825
                        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
   826
                    try:
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   827
                        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
   828
                    except ValidationError:
7885
9454b7ef5ae4 [ms, test] propagate exception in test mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7884
diff changeset
   829
                        raise
10351
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   830
                    except Unauthorized:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   831
                        self.exception(
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   832
                            'Unauthorized exception while cascading delete for entity %s. '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   833
                            'RQL: %s.\nThis should not happen since security is disabled here.',
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   834
                            entities, rql)
10351
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   835
                        raise
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   836
                    except Exception:
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   837
                        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
   838
                            raise
91e63306e277 [connection] replace .running_dbapi_query with .hooks_in_progress
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10346
diff changeset
   839
                        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
   840
                                       entities, rql)
6889
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   841
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   842
    def init_entity_caches(self, cnx, entity, source):
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   843
        """Add entity to connection entities cache and repo's cache."""
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   844
        cnx.set_entity_cache(entity)
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   845
        self._type_cache[entity.eid] = entity.cw_etype
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
   846
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   847
    def glob_add_entity(self, cnx, edited):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   848
        """add an entity to the repository
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   849
10039
1d1afe3b5081 [server] typo fix
Julien Cristau <julien.cristau@logilab.fr>
parents: 10018
diff changeset
   850
        the entity eid should originally be None and a unique eid is assigned to
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   851
        the entity instance
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   852
        """
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6139
diff changeset
   853
        entity = edited.entity
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   854
        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
   855
        # 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
   856
        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
   857
        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
   858
        # 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
   859
        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
   860
        # set caches asap
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   861
        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
   862
        if server.DEBUG & server.DBG_REPO:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
   863
            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
   864
        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
   865
        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
   866
        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
   867
        edited.set_defaults()
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   868
        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
   869
            edited.check(creation=True)
11774
51c160677afe [repository] Drop the entities.extid column and associated cache
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   870
        self.add_info(cnx, entity, source)
6211
e9d125fd1465 nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6189
diff changeset
   871
        try:
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   872
            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
   873
        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
   874
            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
   875
                '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
   876
            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
   877
        edited.saved = entity._cw_is_saved = True
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   878
        # 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
   879
        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
   880
        # 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
   881
        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
   882
            self.hm.call_hooks('before_add_relation', cnx,
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   883
                               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
   884
            self.hm.call_hooks('after_add_relation', cnx,
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   885
                               eidfrom=entity.eid, rtype=attr, eidto=value)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   886
        return entity.eid
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
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 glob_update_entity(self, cnx, edited):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   889
        """replace an entity in the repository
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   890
        the type and the eid of an entity must not be changed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   891
        """
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6139
diff changeset
   892
        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
   893
        if server.DEBUG & server.DBG_REPO:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
   894
            print('UPDATE entity', entity.cw_etype, entity.eid,
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
   895
                  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
   896
        hm = self.hm
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   897
        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
   898
        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
   899
        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
   900
        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
   901
        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
   902
        try:
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   903
            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
   904
            relations = []
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6139
diff changeset
   905
            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
   906
                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
   907
                    continue
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   908
                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
   909
                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
   910
                    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
   911
                        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
   912
                    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
   913
                else:
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   914
                    # inlined relation
2e43ef618d14 [repository] forbid usage of set_attributes() in before_add_entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5093
diff changeset
   915
                    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
   916
                    if previous_value is not None:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   917
                        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
   918
                        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
   919
                            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
   920
                        else:
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   921
                            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
   922
                                          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
   923
                                          eidto=previous_value)
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6139
diff changeset
   924
                    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
   925
            # 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
   926
            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
   927
                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
   928
                              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
   929
            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
   930
                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
   931
            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
   932
                edited.check()
6211
e9d125fd1465 nicer error reporting for unique together constraints
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6189
diff changeset
   933
            try:
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   934
                source.update_entity(cnx, entity)
6225
a176e68b7d0d backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6142 6211
diff changeset
   935
                edited.saved = True
10446
1e6655cff5ab add IUserFriendlyError adapter for violation of check constraints
Julien Cristau <julien.cristau@logilab.fr>
parents: 10368
diff changeset
   936
            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
   937
                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
   938
                    '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
   939
                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
   940
            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
   941
            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
   942
                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
   943
            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
   944
                # 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
   945
                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
   946
                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
   947
                    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
   948
                                  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
   949
                    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
   950
                        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
   951
                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>
parents: 9757 9769
diff changeset
   952
                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
   953
                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
   954
                              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
   955
        finally:
6142
8bc6eac1fac1 [session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6139
diff changeset
   956
            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
   957
                entity.cw_edited = orig_edited
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   958
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   959
    def glob_delete_entities(self, cnx, eids):
6889
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   960
        """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
   961
        # 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
   962
        # 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
   963
        # 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
   964
        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
   965
        if not isinstance(eids, (set, frozenset)):
4c043afb104a fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7503
diff changeset
   966
            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
   967
                 stacklevel=2)
4c043afb104a fix failures introduced by recent refactoring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7503
diff changeset
   968
            eids = frozenset(eids)
7501
2983dd24494a [repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7373
diff changeset
   969
        eids = eids - op._container
2983dd24494a [repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7373
diff changeset
   970
        op._container |= eids
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   971
        data_by_etype = {}  # values are [list of entities]
6889
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   972
        #
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   973
        # 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
   974
        # 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
   975
        # of the Python interpreter advertises large perf improvements
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   976
        # in setdefault, this should not be changed without profiling.
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   977
        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
   978
            etype = self.type_from_eid(eid, cnx)
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   979
            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
   980
            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
   981
                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
   982
            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
   983
                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
   984
        source = self.system_source
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
   985
        for etype, entities in data_by_etype.items():
6889
37668bf302f5 improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6801
diff changeset
   986
            if server.DEBUG & server.DBG_REPO:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11204
diff changeset
   987
                print('DELETE entities', etype, [e.eid for e in entities])
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   988
            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
   989
            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
   990
            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
   991
            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
   992
            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
   993
        # don't clear cache here, it is done in a hook on commit
1482
93c613913912 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   994
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   995
    def glob_add_relation(self, cnx, subject, rtype, object):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   996
        """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
   997
        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
   998
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
   999
    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
  1000
        """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
  1001
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7134
diff changeset
  1002
        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
  1003
        """
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
  1004
        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
  1005
        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
  1006
        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
  1007
        objects_by_types = {}
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
  1008
        activintegrity = cnx.is_hook_category_activated('activeintegrity')
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
  1009
        for rtype, eids_subj_obj in relations.items():
7237
9f619715665b [server] improve the speed of setting relations between entities (closes #1625257)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7134
diff changeset
  1010
            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
  1011
                for subjeid, objeid in eids_subj_obj:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
  1012
                    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
  1013
            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
  1014
                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
  1015
                    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
  1016
                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
  1017
                    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
  1018
                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
  1019
                    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
  1020
                # 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
  1021
                # 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
  1022
                # 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
  1023
                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
  1024
                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
  1025
                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
  1026
                    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
  1027
                        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
  1028
                                    '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
  1029
                                    {'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
  1030
                    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
  1031
                    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
  1032
                        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
  1033
                        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
  1034
                        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
  1035
                    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
  1036
                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
  1037
                    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
  1038
                        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
  1039
                                    '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
  1040
                                    {'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
  1041
                    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
  1042
                    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
  1043
                        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
  1044
                        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
  1045
                        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
  1046
                    objects[objeid] = len(relations_by_rtype[rtype])
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
  1047
        for rtype, source_relations in relations_by_rtype.items():
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('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
  1049
                               rtype=rtype, eids_from_to=source_relations)
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
  1050
        for rtype, source_relations in relations_by_rtype.items():
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
  1051
            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
  1052
            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
  1053
            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
  1054
                cnx.update_rel_cache_add(subjeid, rtype, objeid, rschema.symmetric)
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10609
diff changeset
  1055
        for rtype, source_relations in relations_by_rtype.items():
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
  1056
            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
  1057
                               rtype=rtype, eids_from_to=source_relations)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1058
9609
e7d38148799e [repository] operations get a connection instead of a session
Julien Cristau <julien.cristau@logilab.fr>
parents: 9607
diff changeset
  1059
    def glob_delete_relation(self, cnx, subject, rtype, object):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1060
        """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
  1061
        if server.DEBUG & server.DBG_REPO:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10579
diff changeset
  1062
            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
  1063
        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
  1064
        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
  1065
                           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
  1066
        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
  1067
        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
  1068
        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
  1069
        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
  1070
                           eidfrom=subject, rtype=rtype, eidto=object)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1071
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7064
diff changeset
  1072
    # 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
  1073
    # 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
  1074
    info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1075
11821
7534b32c45e3 Fix (new) flake8 errors
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11819
diff changeset
  1076
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1077
set_log_methods(Repository, getLogger('cubicweb.repository'))