Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / xfeatures.py @ 3759eddb

History | View | Annotate | Download (6.3 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 2715ade4 Sofia Papagiannaki
#
3 a9b3f29d Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 a9b3f29d Antony Chazapis
# without modification, are permitted provided that the following
5 a9b3f29d Antony Chazapis
# conditions are met:
6 2715ade4 Sofia Papagiannaki
#
7 a9b3f29d Antony Chazapis
#   1. Redistributions of source code must retain the above
8 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
9 a9b3f29d Antony Chazapis
#      disclaimer.
10 2715ade4 Sofia Papagiannaki
#
11 a9b3f29d Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 a9b3f29d Antony Chazapis
#      copyright notice, this list of conditions and the following
13 a9b3f29d Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 a9b3f29d Antony Chazapis
#      provided with the distribution.
15 2715ade4 Sofia Papagiannaki
#
16 a9b3f29d Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 a9b3f29d Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 a9b3f29d Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 a9b3f29d Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 a9b3f29d Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 a9b3f29d Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 a9b3f29d Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 a9b3f29d Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 a9b3f29d Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 a9b3f29d Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 a9b3f29d Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 a9b3f29d Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 2715ade4 Sofia Papagiannaki
#
29 a9b3f29d Antony Chazapis
# The views and conclusions contained in the software and
30 a9b3f29d Antony Chazapis
# documentation are those of the authors and should not be
31 a9b3f29d Antony Chazapis
# interpreted as representing official policies, either expressed
32 a9b3f29d Antony Chazapis
# or implied, of GRNET S.A.
33 a9b3f29d Antony Chazapis
34 6f4bce7b Antony Chazapis
from collections import defaultdict
35 6f4bce7b Antony Chazapis
36 a9b3f29d Antony Chazapis
from dbworker import DBWorker
37 a9b3f29d Antony Chazapis
38 a9b3f29d Antony Chazapis
39 a9b3f29d Antony Chazapis
class XFeatures(DBWorker):
40 a9b3f29d Antony Chazapis
    """XFeatures are path properties that allow non-nested
41 a9b3f29d Antony Chazapis
       inheritance patterns. Currently used for storing permissions.
42 a9b3f29d Antony Chazapis
    """
43 2715ade4 Sofia Papagiannaki
44 a9b3f29d Antony Chazapis
    def __init__(self, **params):
45 a9b3f29d Antony Chazapis
        DBWorker.__init__(self, **params)
46 a9b3f29d Antony Chazapis
        execute = self.execute
47 2715ade4 Sofia Papagiannaki
48 a9b3f29d Antony Chazapis
        execute(""" pragma foreign_keys = on """)
49 2715ade4 Sofia Papagiannaki
50 a9b3f29d Antony Chazapis
        execute(""" create table if not exists xfeatures
51 a9b3f29d Antony Chazapis
                          ( feature_id integer primary key,
52 a9b3f29d Antony Chazapis
                            path       text ) """)
53 a9b3f29d Antony Chazapis
        execute(""" create unique index if not exists idx_features_path
54 a9b3f29d Antony Chazapis
                    on xfeatures(path) """)
55 a9b3f29d Antony Chazapis
56 a9b3f29d Antony Chazapis
        execute(""" create table if not exists xfeaturevals
57 a9b3f29d Antony Chazapis
                          ( feature_id integer,
58 a9b3f29d Antony Chazapis
                            key        integer,
59 a9b3f29d Antony Chazapis
                            value      text,
60 a9b3f29d Antony Chazapis
                            primary key (feature_id, key, value)
61 29148653 Sofia Papagiannaki
                            foreign key (feature_id) references
62 29148653 Sofia Papagiannaki
                                xfeatures(feature_id)
63 a9b3f29d Antony Chazapis
                            on delete cascade ) """)
64 2715ade4 Sofia Papagiannaki
65 d83c93c9 Antony Chazapis
#     def xfeature_inherit(self, path):
66 d83c93c9 Antony Chazapis
#         """Return the (path, feature) inherited by the path, or None."""
67 2715ade4 Sofia Papagiannaki
#
68 d83c93c9 Antony Chazapis
#         q = ("select path, feature_id from xfeatures "
69 d83c93c9 Antony Chazapis
#              "where path <= ? "
70 d83c93c9 Antony Chazapis
#              "and ? like path || '%' " # XXX: Escape like...
71 d83c93c9 Antony Chazapis
#              "order by path desc")
72 d83c93c9 Antony Chazapis
#         self.execute(q, (path, path))
73 d83c93c9 Antony Chazapis
#         return self.fetchall()
74 2715ade4 Sofia Papagiannaki
75 43763394 Antony Chazapis
    def xfeature_get(self, path):
76 43763394 Antony Chazapis
        """Return feature for path."""
77 2715ade4 Sofia Papagiannaki
78 43763394 Antony Chazapis
        q = "select feature_id from xfeatures where path = ?"
79 43763394 Antony Chazapis
        self.execute(q, (path,))
80 43763394 Antony Chazapis
        r = self.fetchone()
81 43763394 Antony Chazapis
        if r is not None:
82 43763394 Antony Chazapis
            return r[0]
83 cf341da4 Antony Chazapis
        return None
84 2715ade4 Sofia Papagiannaki
85 d3c34119 Sofia Papagiannaki
    def xfeature_get_bulk(self, paths):
86 d3c34119 Sofia Papagiannaki
        """Return features for paths."""
87 d3c34119 Sofia Papagiannaki
88 d3c34119 Sofia Papagiannaki
        paths = list(set(paths))
89 d3c34119 Sofia Papagiannaki
        q = ("select feature_id, path from xfeatures "
90 d3c34119 Sofia Papagiannaki
             "where path in (%s) "
91 d3c34119 Sofia Papagiannaki
             "order by path") % ','.join('?' for _ in paths)
92 d3c34119 Sofia Papagiannaki
        self.execute(q, paths)
93 d3c34119 Sofia Papagiannaki
        rows = self.fetchall()
94 d3c34119 Sofia Papagiannaki
        if rows:
95 d3c34119 Sofia Papagiannaki
            return rows
96 d3c34119 Sofia Papagiannaki
        return None
97 d3c34119 Sofia Papagiannaki
98 a9b3f29d Antony Chazapis
    def xfeature_create(self, path):
99 a9b3f29d Antony Chazapis
        """Create and return a feature for path.
100 6f4bce7b Antony Chazapis
           If the path has a feature, return it.
101 a9b3f29d Antony Chazapis
        """
102 2715ade4 Sofia Papagiannaki
103 43763394 Antony Chazapis
        feature = self.xfeature_get(path)
104 43763394 Antony Chazapis
        if feature is not None:
105 43763394 Antony Chazapis
            return feature
106 a9b3f29d Antony Chazapis
        q = "insert into xfeatures (path) values (?)"
107 a9b3f29d Antony Chazapis
        id = self.execute(q, (path,)).lastrowid
108 a9b3f29d Antony Chazapis
        return id
109 2715ade4 Sofia Papagiannaki
110 a9b3f29d Antony Chazapis
    def xfeature_destroy(self, path):
111 a9b3f29d Antony Chazapis
        """Destroy a feature and all its key, value pairs."""
112 2715ade4 Sofia Papagiannaki
113 a9b3f29d Antony Chazapis
        q = "delete from xfeatures where path = ?"
114 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
115 2715ade4 Sofia Papagiannaki
116 4d15c94e Sofia Papagiannaki
    def xfeature_destroy_bulk(self, paths):
117 4d15c94e Sofia Papagiannaki
        """Destroy features and all their key, value pairs."""
118 2715ade4 Sofia Papagiannaki
119 4d15c94e Sofia Papagiannaki
        placeholders = ','.join('?' for path in paths)
120 4d15c94e Sofia Papagiannaki
        q = "delete from xfeatures where path in (%s)" % placeholders
121 4d15c94e Sofia Papagiannaki
        self.execute(q, paths)
122 2715ade4 Sofia Papagiannaki
123 6f4bce7b Antony Chazapis
    def feature_dict(self, feature):
124 6f4bce7b Antony Chazapis
        """Return a dict mapping keys to list of values for feature."""
125 2715ade4 Sofia Papagiannaki
126 62f915a1 Antony Chazapis
        q = "select key, value from xfeaturevals where feature_id = ?"
127 a9b3f29d Antony Chazapis
        self.execute(q, (feature,))
128 6f4bce7b Antony Chazapis
        d = defaultdict(list)
129 6f4bce7b Antony Chazapis
        for key, value in self.fetchall():
130 6f4bce7b Antony Chazapis
            d[key].append(value)
131 6f4bce7b Antony Chazapis
        return d
132 2715ade4 Sofia Papagiannaki
133 a9b3f29d Antony Chazapis
    def feature_set(self, feature, key, value):
134 a9b3f29d Antony Chazapis
        """Associate a key, value pair with a feature."""
135 2715ade4 Sofia Papagiannaki
136 29148653 Sofia Papagiannaki
        q = ("insert or ignore into xfeaturevals (feature_id, key, value) "
137 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
138 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
139 2715ade4 Sofia Papagiannaki
140 a9b3f29d Antony Chazapis
    def feature_setmany(self, feature, key, values):
141 a9b3f29d Antony Chazapis
        """Associate the given key, and values with a feature."""
142 2715ade4 Sofia Papagiannaki
143 29148653 Sofia Papagiannaki
        q = ("insert or ignore into xfeaturevals (feature_id, key, value) "
144 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
145 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
146 2715ade4 Sofia Papagiannaki
147 a9b3f29d Antony Chazapis
    def feature_unset(self, feature, key, value):
148 a9b3f29d Antony Chazapis
        """Disassociate a key, value pair from a feature."""
149 2715ade4 Sofia Papagiannaki
150 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
151 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
152 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
153 2715ade4 Sofia Papagiannaki
154 a9b3f29d Antony Chazapis
    def feature_unsetmany(self, feature, key, values):
155 a9b3f29d Antony Chazapis
        """Disassociate the key for the values given, from a feature."""
156 2715ade4 Sofia Papagiannaki
157 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
158 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
159 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
160 2715ade4 Sofia Papagiannaki
161 a9b3f29d Antony Chazapis
    def feature_get(self, feature, key):
162 a9b3f29d Antony Chazapis
        """Return the list of values for a key of a feature."""
163 2715ade4 Sofia Papagiannaki
164 a9b3f29d Antony Chazapis
        q = "select value from xfeaturevals where feature_id = ? and key = ?"
165 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))
166 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
167 2715ade4 Sofia Papagiannaki
168 a9b3f29d Antony Chazapis
    def feature_clear(self, feature, key):
169 a9b3f29d Antony Chazapis
        """Delete all key, value pairs for a key of a feature."""
170 2715ade4 Sofia Papagiannaki
171 a9b3f29d Antony Chazapis
        q = "delete from xfeaturevals where feature_id = ? and key = ?"
172 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))