Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlite / xfeatures.py @ 345dcf39

History | View | Annotate | Download (5.8 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 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 d83c93c9 Antony Chazapis
#     def xfeature_inherit(self, path):
65 d83c93c9 Antony Chazapis
#         """Return the (path, feature) inherited by the path, or None."""
66 d83c93c9 Antony Chazapis
#         
67 d83c93c9 Antony Chazapis
#         q = ("select path, feature_id from xfeatures "
68 d83c93c9 Antony Chazapis
#              "where path <= ? "
69 d83c93c9 Antony Chazapis
#              "and ? like path || '%' " # XXX: Escape like...
70 d83c93c9 Antony Chazapis
#              "order by path desc")
71 d83c93c9 Antony Chazapis
#         self.execute(q, (path, path))
72 d83c93c9 Antony Chazapis
#         return self.fetchall()
73 a9b3f29d Antony Chazapis
    
74 43763394 Antony Chazapis
    def xfeature_get(self, path):
75 43763394 Antony Chazapis
        """Return feature for path."""
76 a9b3f29d Antony Chazapis
        
77 43763394 Antony Chazapis
        q = "select feature_id from xfeatures where path = ?"
78 43763394 Antony Chazapis
        self.execute(q, (path,))
79 43763394 Antony Chazapis
        r = self.fetchone()
80 43763394 Antony Chazapis
        if r is not None:
81 43763394 Antony Chazapis
            return r[0]
82 cf341da4 Antony Chazapis
        return None
83 a9b3f29d Antony Chazapis
    
84 a9b3f29d Antony Chazapis
    def xfeature_create(self, path):
85 a9b3f29d Antony Chazapis
        """Create and return a feature for path.
86 6f4bce7b Antony Chazapis
           If the path has a feature, return it.
87 a9b3f29d Antony Chazapis
        """
88 a9b3f29d Antony Chazapis
        
89 43763394 Antony Chazapis
        feature = self.xfeature_get(path)
90 43763394 Antony Chazapis
        if feature is not None:
91 43763394 Antony Chazapis
            return feature
92 a9b3f29d Antony Chazapis
        q = "insert into xfeatures (path) values (?)"
93 a9b3f29d Antony Chazapis
        id = self.execute(q, (path,)).lastrowid
94 a9b3f29d Antony Chazapis
        return id
95 a9b3f29d Antony Chazapis
    
96 a9b3f29d Antony Chazapis
    def xfeature_destroy(self, path):
97 a9b3f29d Antony Chazapis
        """Destroy a feature and all its key, value pairs."""
98 a9b3f29d Antony Chazapis
        
99 a9b3f29d Antony Chazapis
        q = "delete from xfeatures where path = ?"
100 a9b3f29d Antony Chazapis
        self.execute(q, (path,))
101 a9b3f29d Antony Chazapis
    
102 6f4bce7b Antony Chazapis
    def feature_dict(self, feature):
103 6f4bce7b Antony Chazapis
        """Return a dict mapping keys to list of values for feature."""
104 a9b3f29d Antony Chazapis
        
105 62f915a1 Antony Chazapis
        q = "select key, value from xfeaturevals where feature_id = ?"
106 a9b3f29d Antony Chazapis
        self.execute(q, (feature,))
107 6f4bce7b Antony Chazapis
        d = defaultdict(list)
108 6f4bce7b Antony Chazapis
        for key, value in self.fetchall():
109 6f4bce7b Antony Chazapis
            d[key].append(value)
110 6f4bce7b Antony Chazapis
        return d
111 a9b3f29d Antony Chazapis
    
112 a9b3f29d Antony Chazapis
    def feature_set(self, feature, key, value):
113 a9b3f29d Antony Chazapis
        """Associate a key, value pair with a feature."""
114 a9b3f29d Antony Chazapis
        
115 a9b3f29d Antony Chazapis
        q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
116 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
117 a9b3f29d Antony Chazapis
    
118 a9b3f29d Antony Chazapis
    def feature_setmany(self, feature, key, values):
119 a9b3f29d Antony Chazapis
        """Associate the given key, and values with a feature."""
120 a9b3f29d Antony Chazapis
        
121 a9b3f29d Antony Chazapis
        q = "insert or ignore into xfeaturevals (feature_id, key, value) values (?, ?, ?)"
122 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
123 a9b3f29d Antony Chazapis
    
124 a9b3f29d Antony Chazapis
    def feature_unset(self, feature, key, value):
125 a9b3f29d Antony Chazapis
        """Disassociate a key, value pair from a feature."""
126 a9b3f29d Antony Chazapis
        
127 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
128 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
129 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key, value))
130 a9b3f29d Antony Chazapis
    
131 a9b3f29d Antony Chazapis
    def feature_unsetmany(self, feature, key, values):
132 a9b3f29d Antony Chazapis
        """Disassociate the key for the values given, from a feature."""
133 a9b3f29d Antony Chazapis
        
134 a9b3f29d Antony Chazapis
        q = ("delete from xfeaturevals where "
135 a9b3f29d Antony Chazapis
             "feature_id = ? and key = ? and value = ?")
136 a9b3f29d Antony Chazapis
        self.executemany(q, ((feature, key, v) for v in values))
137 a9b3f29d Antony Chazapis
    
138 a9b3f29d Antony Chazapis
    def feature_get(self, feature, key):
139 a9b3f29d Antony Chazapis
        """Return the list of values for a key of a feature."""
140 a9b3f29d Antony Chazapis
        
141 a9b3f29d Antony Chazapis
        q = "select value from xfeaturevals where feature_id = ? and key = ?"
142 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))
143 a9b3f29d Antony Chazapis
        return [r[0] for r in self.fetchall()]
144 a9b3f29d Antony Chazapis
    
145 a9b3f29d Antony Chazapis
    def feature_clear(self, feature, key):
146 a9b3f29d Antony Chazapis
        """Delete all key, value pairs for a key of a feature."""
147 a9b3f29d Antony Chazapis
        
148 a9b3f29d Antony Chazapis
        q = "delete from xfeaturevals where feature_id = ? and key = ?"
149 a9b3f29d Antony Chazapis
        self.execute(q, (feature, key))