root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / xfeatures.py @ e7117103
History | View | Annotate | Download (7.9 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from collections import defaultdict |
35 |
from sqlalchemy import Table, Column, String, Integer, MetaData, ForeignKey |
36 |
from sqlalchemy.sql import select, and_ |
37 |
from sqlalchemy.schema import Index |
38 |
from sqlalchemy.sql.expression import desc |
39 |
from sqlalchemy.exc import NoSuchTableError |
40 |
|
41 |
from dbworker import DBWorker |
42 |
|
43 |
|
44 |
def create_tables(engine): |
45 |
metadata = MetaData() |
46 |
columns = [] |
47 |
columns.append(Column('feature_id', Integer, primary_key=True)) |
48 |
columns.append(Column('path', String(2048))) |
49 |
xfeatures = Table('xfeatures', metadata, *columns, mysql_engine='InnoDB') |
50 |
# place an index on path
|
51 |
Index('idx_features_path', xfeatures.c.path, unique=True) |
52 |
|
53 |
columns = [] |
54 |
columns.append(Column('feature_id', Integer,
|
55 |
ForeignKey('xfeatures.feature_id',
|
56 |
ondelete='CASCADE'),
|
57 |
primary_key=True))
|
58 |
columns.append(Column('key', Integer, primary_key=True, |
59 |
autoincrement=False))
|
60 |
columns.append(Column('value', String(256), primary_key=True)) |
61 |
xfeaturevals = Table( |
62 |
'xfeaturevals', metadata, *columns, mysql_engine='InnoDB') |
63 |
|
64 |
metadata.create_all(engine) |
65 |
return metadata.sorted_tables
|
66 |
|
67 |
|
68 |
class XFeatures(DBWorker): |
69 |
"""XFeatures are path properties that allow non-nested
|
70 |
inheritance patterns. Currently used for storing permissions.
|
71 |
"""
|
72 |
|
73 |
def __init__(self, **params): |
74 |
DBWorker.__init__(self, **params)
|
75 |
try:
|
76 |
metadata = MetaData(self.engine)
|
77 |
self.xfeatures = Table('xfeatures', metadata, autoload=True) |
78 |
self.xfeaturevals = Table('xfeaturevals', metadata, autoload=True) |
79 |
except NoSuchTableError:
|
80 |
tables = create_tables(self.engine)
|
81 |
map(lambda t: self.__setattr__(t.name, t), tables) |
82 |
|
83 |
# def xfeature_inherit(self, path):
|
84 |
# """Return the (path, feature) inherited by the path, or None."""
|
85 |
#
|
86 |
# s = select([self.xfeatures.c.path, self.xfeatures.c.feature_id])
|
87 |
# s = s.where(self.xfeatures.c.path <= path)
|
88 |
# #s = s.where(self.xfeatures.c.path.like(self.escape_like(path) + '%', escape='\\')) # XXX: Implement reverse and escape like...
|
89 |
# s = s.order_by(desc(self.xfeatures.c.path))
|
90 |
# r = self.conn.execute(s)
|
91 |
# l = r.fetchall()
|
92 |
# r.close()
|
93 |
# return l
|
94 |
|
95 |
def xfeature_get(self, path): |
96 |
"""Return feature for path."""
|
97 |
|
98 |
s = select([self.xfeatures.c.feature_id])
|
99 |
s = s.where(self.xfeatures.c.path == path)
|
100 |
s = s.order_by(self.xfeatures.c.path)
|
101 |
r = self.conn.execute(s)
|
102 |
row = r.fetchone() |
103 |
r.close() |
104 |
if row:
|
105 |
return row[0] |
106 |
return None |
107 |
|
108 |
def xfeature_create(self, path): |
109 |
"""Create and return a feature for path.
|
110 |
If the path has a feature, return it.
|
111 |
"""
|
112 |
|
113 |
feature = self.xfeature_get(path)
|
114 |
if feature is not None: |
115 |
return feature
|
116 |
s = self.xfeatures.insert()
|
117 |
r = self.conn.execute(s, path=path)
|
118 |
inserted_primary_key = r.inserted_primary_key[0]
|
119 |
r.close() |
120 |
return inserted_primary_key
|
121 |
|
122 |
def xfeature_destroy(self, path): |
123 |
"""Destroy a feature and all its key, value pairs."""
|
124 |
|
125 |
s = self.xfeatures.delete().where(self.xfeatures.c.path == path) |
126 |
r = self.conn.execute(s)
|
127 |
r.close() |
128 |
|
129 |
def xfeature_destroy_bulk(self, paths): |
130 |
"""Destroy features and all their key, value pairs."""
|
131 |
|
132 |
if not paths: |
133 |
return
|
134 |
s = self.xfeatures.delete().where(self.xfeatures.c.path.in_(paths)) |
135 |
r = self.conn.execute(s)
|
136 |
r.close() |
137 |
|
138 |
def feature_dict(self, feature): |
139 |
"""Return a dict mapping keys to list of values for feature."""
|
140 |
|
141 |
s = select([self.xfeaturevals.c.key, self.xfeaturevals.c.value]) |
142 |
s = s.where(self.xfeaturevals.c.feature_id == feature)
|
143 |
r = self.conn.execute(s)
|
144 |
d = defaultdict(list)
|
145 |
for key, value in r.fetchall(): |
146 |
d[key].append(value) |
147 |
r.close() |
148 |
return d
|
149 |
|
150 |
def feature_set(self, feature, key, value): |
151 |
"""Associate a key, value pair with a feature."""
|
152 |
|
153 |
s = self.xfeaturevals.select()
|
154 |
s = s.where(self.xfeaturevals.c.feature_id == feature)
|
155 |
s = s.where(self.xfeaturevals.c.key == key)
|
156 |
s = s.where(self.xfeaturevals.c.value == value)
|
157 |
r = self.conn.execute(s)
|
158 |
xfeaturevals = r.fetchall() |
159 |
r.close() |
160 |
if len(xfeaturevals) == 0: |
161 |
s = self.xfeaturevals.insert()
|
162 |
r = self.conn.execute(s, feature_id=feature, key=key, value=value)
|
163 |
r.close() |
164 |
|
165 |
def feature_setmany(self, feature, key, values): |
166 |
"""Associate the given key, and values with a feature."""
|
167 |
|
168 |
#TODO: more efficient way to do it
|
169 |
for v in values: |
170 |
self.feature_set(feature, key, v)
|
171 |
|
172 |
def feature_unset(self, feature, key, value): |
173 |
"""Disassociate a key, value pair from a feature."""
|
174 |
|
175 |
s = self.xfeaturevals.delete()
|
176 |
s = s.where(and_(self.xfeaturevals.c.feature_id == feature,
|
177 |
self.xfeaturevals.c.key == key,
|
178 |
self.xfeaturevals.c.value == value))
|
179 |
r = self.conn.execute(s)
|
180 |
r.close() |
181 |
|
182 |
def feature_unsetmany(self, feature, key, values): |
183 |
"""Disassociate the key for the values given, from a feature."""
|
184 |
|
185 |
for v in values: |
186 |
conditional = and_(self.xfeaturevals.c.feature_id == feature,
|
187 |
self.xfeaturevals.c.key == key,
|
188 |
self.xfeaturevals.c.value == v)
|
189 |
s = self.xfeaturevals.delete().where(conditional)
|
190 |
r = self.conn.execute(s)
|
191 |
r.close() |
192 |
|
193 |
def feature_get(self, feature, key): |
194 |
"""Return the list of values for a key of a feature."""
|
195 |
|
196 |
s = select([self.xfeaturevals.c.value])
|
197 |
s = s.where(and_(self.xfeaturevals.c.feature_id == feature,
|
198 |
self.xfeaturevals.c.key == key))
|
199 |
r = self.conn.execute(s)
|
200 |
l = [row[0] for row in r.fetchall()] |
201 |
r.close() |
202 |
return l
|
203 |
|
204 |
def feature_clear(self, feature, key): |
205 |
"""Delete all key, value pairs for a key of a feature."""
|
206 |
|
207 |
s = self.xfeaturevals.delete()
|
208 |
s = s.where(and_(self.xfeaturevals.c.feature_id == feature,
|
209 |
self.xfeaturevals.c.key == key))
|
210 |
r = self.conn.execute(s)
|
211 |
r.close() |