Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / xfeatures.py @ 4a7b190f

History | View | Annotate | Download (5.9 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 a9b3f29d Antony Chazapis
    def xfeature_create(self, path):
86 a9b3f29d Antony Chazapis
        """Create and return a feature for path.
87 6f4bce7b Antony Chazapis
           If the path has a feature, return it.
88 a9b3f29d Antony Chazapis
        """
89 2715ade4 Sofia Papagiannaki
90 43763394 Antony Chazapis
        feature = self.xfeature_get(path)
91 43763394 Antony Chazapis
        if feature is not None:
92 43763394 Antony Chazapis
            return feature
93 a9b3f29d Antony Chazapis
        q = "insert into xfeatures (path) values (?)"
94 a9b3f29d Antony Chazapis
        id = self.execute(q, (path,)).lastrowid
95 a9b3f29d Antony Chazapis
        return id
96 2715ade4 Sofia Papagiannaki
97 a9b3f29d Antony Chazapis
    def xfeature_destroy(self, path):
98 a9b3f29d Antony Chazapis
        """Destroy a feature and all its key, value pairs."""
99 2715ade4 Sofia Papagiannaki
100 a9b3f29d Antony Chazapis
        q = "delete from xfeatures where path = ?"
101 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
102 2715ade4 Sofia Papagiannaki
103 4d15c94e Sofia Papagiannaki
    def xfeature_destroy_bulk(self, paths):
104 4d15c94e Sofia Papagiannaki
        """Destroy features and all their key, value pairs."""
105 2715ade4 Sofia Papagiannaki
106 4d15c94e Sofia Papagiannaki
        placeholders = ','.join('?' for path in paths)
107 4d15c94e Sofia Papagiannaki
        q = "delete from xfeatures where path in (%s)" % placeholders
108 4d15c94e Sofia Papagiannaki
        self.execute(q, paths)
109 2715ade4 Sofia Papagiannaki
110 6f4bce7b Antony Chazapis
    def feature_dict(self, feature):
111 6f4bce7b Antony Chazapis
        """Return a dict mapping keys to list of values for feature."""
112 2715ade4 Sofia Papagiannaki
113 62f915a1 Antony Chazapis
        q = "select key, value from xfeaturevals where feature_id = ?"
114 a9b3f29d Antony Chazapis
        self.execute(q, (feature,))
115 6f4bce7b Antony Chazapis
        d = defaultdict(list)
116 6f4bce7b Antony Chazapis
        for key, value in self.fetchall():
117 6f4bce7b Antony Chazapis
            d[key].append(value)
118 6f4bce7b Antony Chazapis
        return d
119 2715ade4 Sofia Papagiannaki
120 a9b3f29d Antony Chazapis
    def feature_set(self, feature, key, value):
121 a9b3f29d Antony Chazapis
        """Associate a key, value pair with a feature."""
122 2715ade4 Sofia Papagiannaki
123 29148653 Sofia Papagiannaki
        q = ("insert or ignore into xfeaturevals (feature_id, key, value) "
124 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
125 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
126 2715ade4 Sofia Papagiannaki
127 a9b3f29d Antony Chazapis
    def feature_setmany(self, feature, key, values):
128 a9b3f29d Antony Chazapis
        """Associate the given key, and values with a feature."""
129 2715ade4 Sofia Papagiannaki
130 29148653 Sofia Papagiannaki
        q = ("insert or ignore into xfeaturevals (feature_id, key, value) "
131 29148653 Sofia Papagiannaki
             "values (?, ?, ?)")
132 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
133 2715ade4 Sofia Papagiannaki
134 a9b3f29d Antony Chazapis
    def feature_unset(self, feature, key, value):
135 a9b3f29d Antony Chazapis
        """Disassociate a key, value pair from a feature."""
136 2715ade4 Sofia Papagiannaki
137 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
138 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
139 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
140 2715ade4 Sofia Papagiannaki
141 a9b3f29d Antony Chazapis
    def feature_unsetmany(self, feature, key, values):
142 a9b3f29d Antony Chazapis
        """Disassociate the key for the values given, from a feature."""
143 2715ade4 Sofia Papagiannaki
144 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
145 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
146 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
147 2715ade4 Sofia Papagiannaki
148 a9b3f29d Antony Chazapis
    def feature_get(self, feature, key):
149 a9b3f29d Antony Chazapis
        """Return the list of values for a key of a feature."""
150 2715ade4 Sofia Papagiannaki
151 a9b3f29d Antony Chazapis
        q = "select value from xfeaturevals where feature_id = ? and key = ?"
152 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))
153 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
154 2715ade4 Sofia Papagiannaki
155 a9b3f29d Antony Chazapis
    def feature_clear(self, feature, key):
156 a9b3f29d Antony Chazapis
        """Delete all key, value pairs for a key of a feature."""
157 2715ade4 Sofia Papagiannaki
158 a9b3f29d Antony Chazapis
        q = "delete from xfeaturevals where feature_id = ? and key = ?"
159 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))