Statistics
| Branch: | Tag: | Revision:

root / pithos / backends / lib / sqlite / xfeatures.py @ 95d47e1a

History | View | Annotate | Download (6.4 kB)

1 a9b3f29d Antony Chazapis
# Copyright 2011 GRNET S.A. All rights reserved.
2 a9b3f29d Antony Chazapis
# 
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 a9b3f29d Antony Chazapis
# 
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 a9b3f29d Antony Chazapis
# 
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 a9b3f29d Antony Chazapis
# 
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 a9b3f29d Antony Chazapis
# 
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 a9b3f29d Antony Chazapis
    
44 a9b3f29d Antony Chazapis
    def __init__(self, **params):
45 a9b3f29d Antony Chazapis
        DBWorker.__init__(self, **params)
46 a9b3f29d Antony Chazapis
        execute = self.execute
47 a9b3f29d Antony Chazapis
        
48 a9b3f29d Antony Chazapis
        execute(""" pragma foreign_keys = on """)
49 a9b3f29d Antony Chazapis
        
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 a9b3f29d Antony Chazapis
                            foreign key (feature_id) references xfeatures(feature_id)
62 a9b3f29d Antony Chazapis
                            on delete cascade ) """)
63 a9b3f29d Antony Chazapis
    
64 a9b3f29d Antony Chazapis
    def xfeature_inherit(self, path):
65 a9b3f29d Antony Chazapis
        """Return the (path, feature) inherited by the path, or None."""
66 a9b3f29d Antony Chazapis
        
67 a9b3f29d Antony Chazapis
        q = ("select path, feature_id from xfeatures "
68 a9b3f29d Antony Chazapis
             "where path <= ? "
69 a9b3f29d Antony Chazapis
             "order by path desc limit 1")
70 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
71 a9b3f29d Antony Chazapis
        r = self.fetchone()
72 a9b3f29d Antony Chazapis
        if r is not None and path.startswith(r[0]):
73 a9b3f29d Antony Chazapis
            return r
74 a9b3f29d Antony Chazapis
        return None
75 a9b3f29d Antony Chazapis
    
76 a9b3f29d Antony Chazapis
    def xfeature_list(self, path):
77 a9b3f29d Antony Chazapis
        """Return the list of the (prefix, feature) pairs matching path.
78 a9b3f29d Antony Chazapis
           A prefix matches path if either the prefix includes the path,
79 a9b3f29d Antony Chazapis
           or the path includes the prefix.
80 a9b3f29d Antony Chazapis
        """
81 a9b3f29d Antony Chazapis
        
82 a9b3f29d Antony Chazapis
        inherited = self.xfeature_inherit(path)
83 a9b3f29d Antony Chazapis
        if inherited:
84 a9b3f29d Antony Chazapis
            return [inherited]
85 a9b3f29d Antony Chazapis
        
86 a9b3f29d Antony Chazapis
        q = ("select path, feature_id from xfeatures "
87 7759260d Antony Chazapis
             "where path like ? escape '\\' and path != ? order by path")
88 7759260d Antony Chazapis
        self.execute(q, (self.escape_like(path) + '%', path,))
89 a9b3f29d Antony Chazapis
        return self.fetchall()
90 a9b3f29d Antony Chazapis
    
91 a9b3f29d Antony Chazapis
    def xfeature_create(self, path):
92 a9b3f29d Antony Chazapis
        """Create and return a feature for path.
93 a9b3f29d Antony Chazapis
           If the path already inherits a feature or
94 a9b3f29d Antony Chazapis
           bestows to paths already inheriting a feature,
95 a9b3f29d Antony Chazapis
           create no feature and return None.
96 6f4bce7b Antony Chazapis
           If the path has a feature, return it.
97 a9b3f29d Antony Chazapis
        """
98 a9b3f29d Antony Chazapis
        
99 a9b3f29d Antony Chazapis
        prefixes = self.xfeature_list(path)
100 a9b3f29d Antony Chazapis
        pl = len(prefixes)
101 a9b3f29d Antony Chazapis
        if (pl > 1) or (pl == 1 and prefixes[0][0] != path):
102 a9b3f29d Antony Chazapis
            return None
103 6f4bce7b Antony Chazapis
        if pl == 1 and prefixes[0][0] == path:
104 6f4bce7b Antony Chazapis
            return prefixes[0][1]
105 a9b3f29d Antony Chazapis
        q = "insert into xfeatures (path) values (?)"
106 a9b3f29d Antony Chazapis
        id = self.execute(q, (path,)).lastrowid
107 a9b3f29d Antony Chazapis
        return id
108 a9b3f29d Antony Chazapis
    
109 a9b3f29d Antony Chazapis
    def xfeature_destroy(self, path):
110 a9b3f29d Antony Chazapis
        """Destroy a feature and all its key, value pairs."""
111 a9b3f29d Antony Chazapis
        
112 a9b3f29d Antony Chazapis
        q = "delete from xfeatures where path = ?"
113 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
114 a9b3f29d Antony Chazapis
    
115 6f4bce7b Antony Chazapis
    def feature_dict(self, feature):
116 6f4bce7b Antony Chazapis
        """Return a dict mapping keys to list of values for feature."""
117 a9b3f29d Antony Chazapis
        
118 62f915a1 Antony Chazapis
        q = "select key, value from xfeaturevals where feature_id = ?"
119 a9b3f29d Antony Chazapis
        self.execute(q, (feature,))
120 6f4bce7b Antony Chazapis
        d = defaultdict(list)
121 6f4bce7b Antony Chazapis
        for key, value in self.fetchall():
122 6f4bce7b Antony Chazapis
            d[key].append(value)
123 6f4bce7b Antony Chazapis
        return d
124 a9b3f29d Antony Chazapis
    
125 a9b3f29d Antony Chazapis
    def feature_set(self, feature, key, value):
126 a9b3f29d Antony Chazapis
        """Associate a key, value pair with a feature."""
127 a9b3f29d Antony Chazapis
        
128 a9b3f29d Antony Chazapis
        q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
129 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
130 a9b3f29d Antony Chazapis
    
131 a9b3f29d Antony Chazapis
    def feature_setmany(self, feature, key, values):
132 a9b3f29d Antony Chazapis
        """Associate the given key, and values with a feature."""
133 a9b3f29d Antony Chazapis
        
134 a9b3f29d Antony Chazapis
        q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
135 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
136 a9b3f29d Antony Chazapis
    
137 a9b3f29d Antony Chazapis
    def feature_unset(self, feature, key, value):
138 a9b3f29d Antony Chazapis
        """Disassociate a key, value pair from a feature."""
139 a9b3f29d Antony Chazapis
        
140 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
141 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
142 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
143 a9b3f29d Antony Chazapis
    
144 a9b3f29d Antony Chazapis
    def feature_unsetmany(self, feature, key, values):
145 a9b3f29d Antony Chazapis
        """Disassociate the key for the values given, from a feature."""
146 a9b3f29d Antony Chazapis
        
147 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
148 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
149 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
150 a9b3f29d Antony Chazapis
    
151 a9b3f29d Antony Chazapis
    def feature_get(self, feature, key):
152 a9b3f29d Antony Chazapis
        """Return the list of values for a key of a feature."""
153 a9b3f29d Antony Chazapis
        
154 a9b3f29d Antony Chazapis
        q = "select value from xfeaturevals where feature_id = ? and key = ?"
155 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))
156 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
157 a9b3f29d Antony Chazapis
    
158 a9b3f29d Antony Chazapis
    def feature_clear(self, feature, key):
159 a9b3f29d Antony Chazapis
        """Delete all key, value pairs for a key of a feature."""
160 a9b3f29d Antony Chazapis
        
161 a9b3f29d Antony Chazapis
        q = "delete from xfeaturevals where feature_id = ? and key = ?"
162 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))