author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 17 Mar 2011 13:43:07 +0100 | |
branch | oldstable |
changeset 7095 | 1831c3154581 |
parent 7037 | 6a8235456fe1 |
child 7040 | 9b1f9bc74f5d |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
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:
5124
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5824 | 18 |
"""cubicweb server sources support""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
22 |
from os.path import join, splitext |
1263 | 23 |
from datetime import datetime, timedelta |
0 | 24 |
from logging import getLogger |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
25 |
import itertools |
0 | 26 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
27 |
from cubicweb import set_log_methods, server |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
28 |
from cubicweb.schema import VIRTUAL_RTYPES |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
29 |
from cubicweb.server.sqlutils import SQL_PREFIX |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6057
diff
changeset
|
30 |
from cubicweb.server.ssplanner import EditedEntity |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
31 |
|
0 | 32 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
33 |
def dbg_st_search(uri, union, varmap, args, cachekey=None, prefix='rql for'): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
34 |
if server.DEBUG & server.DBG_RQL: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
35 |
print ' %s %s source: %s' % (prefix, uri, union.as_string()) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
36 |
if varmap: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
37 |
print ' using varmap', varmap |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
38 |
if server.DEBUG & server.DBG_MORE: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
print ' args', args |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
40 |
print ' cache key', cachekey |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
41 |
print ' solutions', ','.join(str(s.solutions) |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
42 |
for s in union.children) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
43 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
44 |
return True |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
45 |
|
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
46 |
def dbg_results(results): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
47 |
if server.DEBUG & server.DBG_RQL: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
48 |
if len(results) > 10: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
49 |
print ' -->', results[:10], '...', len(results) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
50 |
else: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
51 |
print ' -->', results |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
52 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
53 |
return True |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
54 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
55 |
class TimedCache(dict): |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
56 |
def __init__(self, ttl): |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
57 |
# time to live in seconds |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
58 |
if ttl <= 0: |
6582
8eb7883b4223
[pylint] fix a bug of pylint detected errors and i18n pb (calling builtins._ instead of req._)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
59 |
raise ValueError('TimedCache initialized with a ttl of %ss' % ttl.seconds) |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
60 |
self.ttl = timedelta(seconds=ttl) |
1792 | 61 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
62 |
def __setitem__(self, key, value): |
1263 | 63 |
dict.__setitem__(self, key, (datetime.now(), value)) |
1792 | 64 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
65 |
def __getitem__(self, key): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
66 |
return dict.__getitem__(self, key)[1] |
1792 | 67 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
68 |
def clear_expired(self): |
1263 | 69 |
now_ = datetime.now() |
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
70 |
ttl = self.ttl |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
71 |
for key, (timestamp, value) in self.items(): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
72 |
if now_ - timestamp > ttl: |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
73 |
del self[key] |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
74 |
|
0 | 75 |
|
76 |
class AbstractSource(object): |
|
77 |
"""an abstract class for sources""" |
|
78 |
||
79 |
# boolean telling if modification hooks should be called when something is |
|
80 |
# modified in this source |
|
81 |
should_call_hooks = True |
|
82 |
# boolean telling if the repository should connect to this source during |
|
83 |
# migration |
|
84 |
connect_for_migration = True |
|
1792 | 85 |
|
0 | 86 |
# mappings telling which entities and relations are available in the source |
87 |
# keys are supported entity/relation types and values are boolean indicating |
|
88 |
# wether the support is read-only (False) or read-write (True) |
|
89 |
support_entities = {} |
|
90 |
support_relations = {} |
|
91 |
# a global identifier for this source, which has to be set by the source |
|
92 |
# instance |
|
93 |
uri = None |
|
94 |
# a reference to the system information helper |
|
95 |
repo = None |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
96 |
# a reference to the instance'schema (may differs from the source'schema) |
0 | 97 |
schema = None |
1792 | 98 |
|
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
99 |
# multi-sources planning control |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
100 |
dont_cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
101 |
cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
102 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
103 |
# force deactivation (configuration error for instance) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
104 |
disabled = False |
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
105 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
106 |
def __init__(self, repo, source_config, *args, **kwargs): |
0 | 107 |
self.repo = repo |
108 |
self.uri = source_config['uri'] |
|
109 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
110 |
self.set_schema(repo.schema) |
0 | 111 |
self.support_relations['identity'] = False |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
112 |
self.eid = None |
6722
3341521d857b
[repo source] rename attribute so it's much easier to grasp its role
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
113 |
self.public_config = source_config.copy() |
3341521d857b
[repo source] rename attribute so it's much easier to grasp its role
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
114 |
self.remove_sensitive_information(self.public_config) |
1792 | 115 |
|
0 | 116 |
def init_creating(self): |
117 |
"""method called by the repository once ready to create a new instance""" |
|
118 |
pass |
|
1792 | 119 |
|
0 | 120 |
def init(self): |
121 |
"""method called by the repository once ready to handle request""" |
|
122 |
pass |
|
1792 | 123 |
|
5124
d4d0b4b2298a
fix #822890, missing argument on abstract source backup method make backup failed on multi-sources instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4913
diff
changeset
|
124 |
def backup(self, backupfile, confirm): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
125 |
"""method called to create a backup of source's data""" |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
126 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
127 |
|
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
128 |
def restore(self, backupfile, confirm, drop): |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
129 |
"""method called to restore a backup of source's data""" |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
130 |
pass |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
131 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
132 |
def close_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
133 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
134 |
pool._cursors.pop(self.uri, None) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
135 |
pool.source_cnxs[self.uri][1].close() |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
136 |
|
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
137 |
def open_pool_connections(self): |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
138 |
for pool in self.repo.pools: |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
139 |
pool.source_cnxs[self.uri] = (self, self.get_connection()) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
140 |
|
0 | 141 |
def reset_caches(self): |
142 |
"""method called during test to reset potential source caches""" |
|
143 |
pass |
|
1792 | 144 |
|
0 | 145 |
def clear_eid_cache(self, eid, etype): |
146 |
"""clear potential caches for the given eid""" |
|
147 |
pass |
|
1792 | 148 |
|
0 | 149 |
def __repr__(self): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
150 |
return '<%s source @%#x>' % (self.uri, id(self)) |
0 | 151 |
|
152 |
def __cmp__(self, other): |
|
153 |
"""simple comparison function to get predictable source order, with the |
|
154 |
system source at last |
|
155 |
""" |
|
156 |
if self.uri == other.uri: |
|
157 |
return 0 |
|
158 |
if self.uri == 'system': |
|
159 |
return 1 |
|
160 |
if other.uri == 'system': |
|
161 |
return -1 |
|
162 |
return cmp(self.uri, other.uri) |
|
1792 | 163 |
|
0 | 164 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
165 |
"""set the instance'schema""" |
0 | 166 |
self.schema = schema |
1792 | 167 |
|
0 | 168 |
def support_entity(self, etype, write=False): |
169 |
"""return true if the given entity's type is handled by this adapter |
|
170 |
if write is true, return true only if it's a RW support |
|
171 |
""" |
|
172 |
try: |
|
173 |
wsupport = self.support_entities[etype] |
|
174 |
except KeyError: |
|
175 |
return False |
|
176 |
if write: |
|
177 |
return wsupport |
|
178 |
return True |
|
1792 | 179 |
|
0 | 180 |
def support_relation(self, rtype, write=False): |
181 |
"""return true if the given relation's type is handled by this adapter |
|
182 |
if write is true, return true only if it's a RW support |
|
183 |
||
1792 | 184 |
current implementation return true if the relation is defined into |
185 |
`support_relations` or if it is a final relation of a supported entity |
|
0 | 186 |
type |
187 |
""" |
|
188 |
try: |
|
189 |
wsupport = self.support_relations[rtype] |
|
190 |
except KeyError: |
|
191 |
rschema = self.schema.rschema(rtype) |
|
6057
cb5c8852cbda
[ms] cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5849
diff
changeset
|
192 |
if not rschema.final or rschema.type == 'has_text': |
0 | 193 |
return False |
194 |
for etype in rschema.subjects(): |
|
195 |
try: |
|
196 |
wsupport = self.support_entities[etype] |
|
197 |
break |
|
198 |
except KeyError: |
|
199 |
continue |
|
200 |
else: |
|
201 |
return False |
|
202 |
if write: |
|
203 |
return wsupport |
|
1792 | 204 |
return True |
205 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
206 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
207 |
"""return True if the relation may be crossed among sources. Rules are: |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
208 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
209 |
* if this source support the relation, can't be crossed unless explicitly |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
210 |
specified in .cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
211 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
212 |
* if this source doesn't support the relation, can be crossed unless |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
213 |
explicitly specified in .dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
214 |
""" |
3633
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
215 |
# XXX find a way to have relation such as state_of in dont cross |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
216 |
# relation (eg composite relation without both end type available? |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
217 |
# card 1 relation ? ...) |
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
218 |
if self.support_relation(rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
219 |
return rtype in self.cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
220 |
return rtype not in self.dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
221 |
|
0 | 222 |
def eid2extid(self, eid, session=None): |
223 |
return self.repo.eid2extid(self, eid, session) |
|
224 |
||
1250
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1238
diff
changeset
|
225 |
def extid2eid(self, value, etype, session=None, **kwargs): |
5c20a7f13c84
new recreate argument to extid2eid when an external source want to recreate entities previously imported with a predictable ext id
sylvain.thenault@logilab.fr
parents:
1238
diff
changeset
|
226 |
return self.repo.extid2eid(self, value, etype, session, **kwargs) |
0 | 227 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
228 |
PUBLIC_KEYS = ('type', 'uri') |
0 | 229 |
def remove_sensitive_information(self, sourcedef): |
230 |
"""remove sensitive information such as login / password from source |
|
231 |
definition |
|
232 |
""" |
|
233 |
for key in sourcedef.keys(): |
|
234 |
if not key in self.PUBLIC_KEYS: |
|
235 |
sourcedef.pop(key) |
|
236 |
||
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
237 |
def _cleanup_system_relations(self, session): |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
238 |
"""remove relation in the system source referencing entities coming from |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
239 |
this source |
0 | 240 |
""" |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
241 |
cu = session.system_sql('SELECT eid FROM entities WHERE source=%(uri)s', |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
242 |
{'uri': self.uri}) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
243 |
myeids = ','.join(str(r[0]) for r in cu.fetchall()) |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
244 |
if not myeids: |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
245 |
return |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
246 |
# delete relations referencing one of those eids |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
247 |
eidcolum = SQL_PREFIX + 'eid' |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
248 |
for rschema in self.schema.relations(): |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3633
diff
changeset
|
249 |
if rschema.final or rschema.type in VIRTUAL_RTYPES: |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
250 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
251 |
if rschema.inlined: |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
252 |
column = SQL_PREFIX + rschema.type |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
253 |
for subjtype in rschema.subjects(): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
254 |
table = SQL_PREFIX + str(subjtype) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
255 |
for objtype in rschema.objects(subjtype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
256 |
if self.support_entity(objtype): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
257 |
sql = 'UPDATE %s SET %s=NULL WHERE %s IN (%s);' % ( |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
258 |
table, column, eidcolum, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
259 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
260 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
261 |
continue |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
262 |
for etype in rschema.subjects(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
263 |
if self.support_entity(etype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
264 |
sql = 'DELETE FROM %s_relation WHERE eid_from IN (%s);' % ( |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
265 |
rschema.type, myeids) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
266 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
267 |
break |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
268 |
for etype in rschema.objects(): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
269 |
if self.support_entity(etype): |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
270 |
sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % ( |
385 | 271 |
rschema.type, myeids) |
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
272 |
session.system_sql(sql) |
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
273 |
break |
1792 | 274 |
|
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
275 |
def cleanup_entities_info(self, session): |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
276 |
"""cleanup system tables from information for entities coming from |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
277 |
this source. This should be called when a source is removed to |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
278 |
properly cleanup the database |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
279 |
""" |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
280 |
self._cleanup_system_relations(session) |
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
281 |
# fti / entities tables cleanup |
0 | 282 |
# sqlite doesn't support DELETE FROM xxx USING yyy |
386
7af259b73c5b
don't try to remove relation if source has no entities
sylvain.thenault@logilab.fr
parents:
385
diff
changeset
|
283 |
dbhelper = session.pool.source('system').dbhelper |
0 | 284 |
session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM ' |
285 |
'entities WHERE entities.source=%%(uri)s)' |
|
286 |
% (dbhelper.fti_table, dbhelper.fti_table, |
|
287 |
dbhelper.fti_uid_attr), |
|
288 |
{'uri': self.uri}) |
|
289 |
session.system_sql('DELETE FROM entities WHERE source=%(uri)s', |
|
290 |
{'uri': self.uri}) |
|
1792 | 291 |
|
382
03964dd370e7
fix entities cleanup: source entities may be used in some relations
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
292 |
# abstract methods to override (at least) in concrete source classes ####### |
1792 | 293 |
|
0 | 294 |
def get_connection(self): |
295 |
"""open and return a connection to the source""" |
|
296 |
raise NotImplementedError() |
|
1792 | 297 |
|
0 | 298 |
def check_connection(self, cnx): |
299 |
"""check connection validity, return None if the connection is still valid |
|
300 |
else a new connection (called when the pool using the given connection is |
|
301 |
being attached to a session) |
|
302 |
||
303 |
do nothing by default |
|
304 |
""" |
|
305 |
pass |
|
1792 | 306 |
|
0 | 307 |
def pool_reset(self, cnx): |
308 |
"""the pool using the given connection is being reseted from its current |
|
309 |
attached session |
|
310 |
||
311 |
do nothing by default |
|
312 |
""" |
|
313 |
pass |
|
1792 | 314 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
315 |
def authenticate(self, session, login, **kwargs): |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6057
diff
changeset
|
316 |
"""if the source support CWUser entity type, it should implement |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
317 |
this method which should return CWUser eid for the given login/password |
0 | 318 |
if this account is defined in this source and valid login / password is |
319 |
given. Else raise `AuthenticationError` |
|
320 |
""" |
|
321 |
raise NotImplementedError() |
|
1792 | 322 |
|
0 | 323 |
def syntax_tree_search(self, session, union, |
324 |
args=None, cachekey=None, varmap=None, debug=0): |
|
1792 | 325 |
"""return result from this source for a rql query (actually from a rql |
326 |
syntax tree and a solution dictionary mapping each used variable to a |
|
0 | 327 |
possible type). If cachekey is given, the query necessary to fetch the |
328 |
results (but not the results themselves) may be cached using this key. |
|
329 |
""" |
|
330 |
raise NotImplementedError() |
|
1792 | 331 |
|
0 | 332 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
333 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
334 |
table (on-the-fly if possible, eg for the system source whose the given |
|
335 |
cursor come from). If not possible, inserts all data by calling |
|
336 |
.executemany(). |
|
337 |
""" |
|
338 |
res = self.syntax_tree_search(session, union, args, varmap=varmap) |
|
2627
d710278e0c1c
manual_insert is a public method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2625
diff
changeset
|
339 |
session.pool.source('system').manual_insert(res, table, session) |
1792 | 340 |
|
0 | 341 |
# system source don't have to implement the two methods below |
1792 | 342 |
|
0 | 343 |
def before_entity_insertion(self, session, lid, etype, eid): |
344 |
"""called by the repository when an eid has been attributed for an |
|
345 |
entity stored here but the entity has not been inserted in the system |
|
346 |
table yet. |
|
1792 | 347 |
|
0 | 348 |
This method must return the an Entity instance representation of this |
349 |
entity. |
|
350 |
""" |
|
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2627
diff
changeset
|
351 |
entity = self.repo.vreg['etypes'].etype_class(etype)(session) |
5557
1a534c596bff
[entity] continue cleanup of Entity/AnyEntity namespace
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
352 |
entity.eid = eid |
6142
8bc6eac1fac1
[session] cleanup hook / operation / entity edition api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6057
diff
changeset
|
353 |
entity.cw_edited = EditedEntity(entity) |
0 | 354 |
return entity |
1792 | 355 |
|
0 | 356 |
def after_entity_insertion(self, session, lid, entity): |
357 |
"""called by the repository after an entity stored here has been |
|
358 |
inserted in the system table. |
|
359 |
""" |
|
360 |
pass |
|
361 |
||
362 |
# read-only sources don't have to implement methods below |
|
363 |
||
364 |
def get_extid(self, entity): |
|
365 |
"""return the external id for the given newly inserted entity""" |
|
366 |
raise NotImplementedError() |
|
1792 | 367 |
|
0 | 368 |
def add_entity(self, session, entity): |
369 |
"""add a new entity to the source""" |
|
370 |
raise NotImplementedError() |
|
1792 | 371 |
|
0 | 372 |
def update_entity(self, session, entity): |
373 |
"""update an entity in the source""" |
|
374 |
raise NotImplementedError() |
|
375 |
||
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
376 |
def delete_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
377 |
"""delete several entities from the source""" |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
378 |
for entity in entities: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
379 |
self.delete_entity(session, entity) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
380 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
381 |
def delete_entity(self, session, entity): |
0 | 382 |
"""delete an entity from the source""" |
383 |
raise NotImplementedError() |
|
384 |
||
385 |
def add_relation(self, session, subject, rtype, object): |
|
386 |
"""add a relation to the source""" |
|
387 |
raise NotImplementedError() |
|
1792 | 388 |
|
0 | 389 |
def delete_relation(self, session, subject, rtype, object): |
390 |
"""delete a relation from the source""" |
|
391 |
raise NotImplementedError() |
|
392 |
||
393 |
# system source interface ################################################# |
|
394 |
||
395 |
def eid_type_source(self, session, eid): |
|
396 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
397 |
raise NotImplementedError() |
|
1792 | 398 |
|
0 | 399 |
def create_eid(self, session): |
400 |
raise NotImplementedError() |
|
401 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
402 |
def add_info(self, session, entity, source, extid): |
0 | 403 |
"""add type and source info for an eid into the system table""" |
404 |
raise NotImplementedError() |
|
405 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
406 |
def update_info(self, session, entity, need_fti_update): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
407 |
"""mark entity as being modified, fulltext reindex if needed""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
408 |
raise NotImplementedError() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
409 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
410 |
def delete_info(self, session, entity, uri, extid): |
0 | 411 |
"""delete system information on deletion of an entity by transfering |
412 |
record from the entities table to the deleted_entities table |
|
413 |
""" |
|
414 |
raise NotImplementedError() |
|
1792 | 415 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
416 |
def delete_info_multi(self, session, entities, uri, extids): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
417 |
"""ame as delete_info but accepts a list of entities with |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
418 |
the same etype and belinging to the same source. |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
419 |
""" |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
420 |
for entity, extid in itertools.izip(entities, extids): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
421 |
self.delete_info(session, entity, uri, extid) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
422 |
|
4807
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
423 |
def modified_entities(self, session, etypes, mtime): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
424 |
"""return a 2-uple: |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
425 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
426 |
modified since the given timestamp (actually entities whose full text |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
427 |
index content has changed) |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
428 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
429 |
deleted since the given timestamp |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
430 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
431 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
432 |
|
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
433 |
def index_entity(self, session, entity): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
434 |
"""create an operation to [re]index textual content of the given entity |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
435 |
on commit |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
436 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
437 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
438 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
439 |
def fti_unindex_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
440 |
"""remove text content for entities from the full text index |
0 | 441 |
""" |
442 |
raise NotImplementedError() |
|
1792 | 443 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
444 |
def fti_index_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
445 |
"""add text content of created/modified entities to the full text index |
0 | 446 |
""" |
447 |
raise NotImplementedError() |
|
1792 | 448 |
|
0 | 449 |
# sql system source interface ############################################# |
450 |
||
451 |
def sqlexec(self, session, sql, args=None): |
|
452 |
"""execute the query and return its result""" |
|
453 |
raise NotImplementedError() |
|
1792 | 454 |
|
0 | 455 |
def temp_table_def(self, selection, solution, table, basemap): |
456 |
raise NotImplementedError() |
|
1792 | 457 |
|
0 | 458 |
def create_index(self, session, table, column, unique=False): |
459 |
raise NotImplementedError() |
|
1792 | 460 |
|
0 | 461 |
def drop_index(self, session, table, column, unique=False): |
462 |
raise NotImplementedError() |
|
463 |
||
464 |
def create_temp_table(self, session, table, schema): |
|
465 |
raise NotImplementedError() |
|
466 |
||
467 |
def clean_temp_data(self, session, temptables): |
|
468 |
"""remove temporary data, usually associated to temporary tables""" |
|
469 |
pass |
|
470 |
||
1792 | 471 |
|
0 | 472 |
class TrFunc(object): |
473 |
"""lower, upper""" |
|
474 |
def __init__(self, trname, index, attrname=None): |
|
475 |
self._tr = trname.lower() |
|
476 |
self.index = index |
|
477 |
self.attrname = attrname |
|
1792 | 478 |
|
0 | 479 |
def apply(self, resdict): |
480 |
value = resdict.get(self.attrname) |
|
481 |
if value is not None: |
|
482 |
return getattr(value, self._tr)() |
|
483 |
return None |
|
484 |
||
485 |
||
486 |
class GlobTrFunc(TrFunc): |
|
487 |
"""count, sum, max, min, avg""" |
|
488 |
funcs = { |
|
489 |
'count': len, |
|
490 |
'sum': sum, |
|
491 |
'max': max, |
|
492 |
'min': min, |
|
493 |
# XXX avg |
|
494 |
} |
|
495 |
def apply(self, result): |
|
496 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
497 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
498 |
[(1, 7), (3, 4)] |
|
499 |
""" |
|
500 |
keys, values = [], {} |
|
501 |
for row in result: |
|
502 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
503 |
value = row[self.index] |
|
504 |
try: |
|
505 |
values[key].append(value) |
|
506 |
except KeyError: |
|
507 |
keys.append(key) |
|
508 |
values[key] = [value] |
|
509 |
result = [] |
|
510 |
trfunc = self.funcs[self._tr] |
|
511 |
for key in keys: |
|
512 |
row = list(key) |
|
513 |
row.insert(self.index, trfunc(values[key])) |
|
514 |
result.append(row) |
|
515 |
return result |
|
516 |
||
517 |
||
518 |
class ConnectionWrapper(object): |
|
519 |
def __init__(self, cnx=None): |
|
520 |
self.cnx = cnx |
|
521 |
def commit(self): |
|
522 |
pass |
|
523 |
def rollback(self): |
|
524 |
pass |
|
525 |
def cursor(self): |
|
526 |
return None # no actual cursor support |
|
7037
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
527 |
def close(self): |
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
528 |
if hasattr(self.cnx, 'close'): |
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
529 |
self.cnx.close() |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
530 |
|
0 | 531 |
from cubicweb.server import SOURCE_TYPES |
532 |
||
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
533 |
def source_adapter(source_type): |
0 | 534 |
try: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
535 |
return SOURCE_TYPES[source_type] |
0 | 536 |
except KeyError: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
537 |
raise RuntimeError('Unknown source type %r' % source_type) |
1792 | 538 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
539 |
def get_source(type, source_config, repo): |
0 | 540 |
"""return a source adapter according to the adapter field in the |
541 |
source's configuration |
|
542 |
""" |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
543 |
return source_adapter(type)(repo, source_config) |