Code

3923fbd66279368154718def6c94ce00a3c375bc
[roundup.git] / roundup / backends / back_bsddb.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: back_bsddb.py,v 1.10 2001-10-09 07:25:59 richard Exp $
20 import bsddb, os, marshal
21 from roundup import hyperdb, date, password
23 #
24 # Now the database
25 #
26 class Database(hyperdb.Database):
27     """A database for storing records containing flexible data types."""
29     def __init__(self, storagelocator, journaltag=None):
30         """Open a hyperdatabase given a specifier to some storage.
32         The meaning of 'storagelocator' depends on the particular
33         implementation of the hyperdatabase.  It could be a file name,
34         a directory path, a socket descriptor for a connection to a
35         database over the network, etc.
37         The 'journaltag' is a token that will be attached to the journal
38         entries for any edits done on the database.  If 'journaltag' is
39         None, the database is opened in read-only mode: the Class.create(),
40         Class.set(), and Class.retire() methods are disabled.
41         """
42         self.dir, self.journaltag = storagelocator, journaltag
43         self.classes = {}
45     #
46     # Classes
47     #
48     def __getattr__(self, classname):
49         """A convenient way of calling self.getclass(classname)."""
50         return self.classes[classname]
52     def addclass(self, cl):
53         cn = cl.classname
54         if self.classes.has_key(cn):
55             raise ValueError, cn
56         self.classes[cn] = cl
58     def getclasses(self):
59         """Return a list of the names of all existing classes."""
60         l = self.classes.keys()
61         l.sort()
62         return l
64     def getclass(self, classname):
65         """Get the Class object representing a particular class.
67         If 'classname' is not a valid class name, a KeyError is raised.
68         """
69         return self.classes[classname]
71     #
72     # Class DBs
73     #
74     def clear(self):
75         for cn in self.classes.keys():
76             db = os.path.join(self.dir, 'nodes.%s'%cn)
77             bsddb.btopen(db, 'n')
78             db = os.path.join(self.dir, 'journals.%s'%cn)
79             bsddb.btopen(db, 'n')
81     def getclassdb(self, classname, mode='r'):
82         ''' grab a connection to the class db that will be used for
83             multiple actions
84         '''
85         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
86         if os.path.exists(path):
87             return bsddb.btopen(path, mode)
88         else:
89             return bsddb.btopen(path, 'n')
91     #
92     # Nodes
93     #
94     def addnode(self, classname, nodeid, node):
95         ''' add the specified node to its class's db
96         '''
97         db = self.getclassdb(classname, 'c')
99         # convert the instance data to builtin types
100         properties = self.classes[classname].properties
101         for key in properties.keys():
102             if isinstance(properties[key], hyperdb.Date):
103                 node[key] = node[key].get_tuple()
104             elif isinstance(properties[key], hyperdb.Interval):
105                 node[key] = node[key].get_tuple()
106             elif isinstance(properties[key], hyperdb.Password):
107                 node[key] = str(node[key])
109         # now save the marshalled data
110         db[nodeid] = marshal.dumps(node)
111         db.close()
112     setnode = addnode
114     def getnode(self, classname, nodeid, cldb=None):
115         ''' add the specified node to its class's db
116         '''
117         db = cldb or self.getclassdb(classname)
118         if not db.has_key(nodeid):
119             raise IndexError, nodeid
120         res = marshal.loads(db[nodeid])
122         # convert the marshalled data to instances
123         properties = self.classes[classname].properties
124         for key in properties.keys():
125             if isinstance(properties[key], hyperdb.Date):
126                 res[key] = date.Date(res[key])
127             elif isinstance(properties[key], hyperdb.Interval):
128                 res[key] = date.Interval(res[key])
129             elif isinstance(properties[key], hyperdb.Password):
130                 p = password.Password()
131                 p.unpack(res[key])
132                 res[key] = p
134         if not cldb: db.close()
135         return res
137     def hasnode(self, classname, nodeid, cldb=None):
138         ''' add the specified node to its class's db
139         '''
140         db = cldb or self.getclassdb(classname)
141         res = db.has_key(nodeid)
142         if not cldb: db.close()
143         return res
145     def countnodes(self, classname, cldb=None):
146         db = cldb or self.getclassdb(classname)
147         return len(db.keys())
148         if not cldb: db.close()
149         return res
151     def getnodeids(self, classname, cldb=None):
152         db = cldb or self.getclassdb(classname)
153         res = db.keys()
154         if not cldb: db.close()
155         return res
157     #
158     # Journal
159     #
160     def addjournal(self, classname, nodeid, action, params):
161         ''' Journal the Action
162         'action' may be:
164             'create' or 'set' -- 'params' is a dictionary of property values
165             'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
166             'retire' -- 'params' is None
167         '''
168         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
169             params)
170         db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
171         if db.has_key(nodeid):
172             s = db[nodeid]
173             l = marshal.loads(db[nodeid])
174             l.append(entry)
175         else:
176             l = [entry]
177         db[nodeid] = marshal.dumps(l)
178         db.close()
180     def getjournal(self, classname, nodeid):
181         ''' get the journal for id
182         '''
183         # attempt to open the journal - in some rare cases, the journal may
184         # not exist
185         try:
186             db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname),
187                 'r')
188         except bsddb.error, error:
189             if error.args[0] != 2: raise
190             return []
191         # mor handling of bad journals
192         if not db.has_key(nodeid): return []
193         journal = marshal.loads(db[nodeid])
194         res = []
195         for entry in journal:
196             (nodeid, date_stamp, self.journaltag, action, params) = entry
197             date_obj = date.Date(date_stamp)
198             res.append((nodeid, date_obj, self.journaltag, action, params))
199         db.close()
200         return res
202     def close(self):
203         ''' Close the Database - we must release the circular refs so that
204             we can be del'ed and the underlying bsddb connections closed
205             cleanly.
206         '''
207         self.classes = None
210     #
211     # Basic transaction support
212     #
213     # TODO: well, write these methods (and then use them in other code)
214     def register_action(self):
215         ''' Register an action to the transaction undo log
216         '''
218     def commit(self):
219         ''' Commit the current transaction, start a new one
220         '''
222     def rollback(self):
223         ''' Reverse all actions from the current transaction
224         '''
227 #$Log: not supported by cvs2svn $
228 #Revision 1.9  2001/08/12 06:32:36  richard
229 #using isinstance(blah, Foo) now instead of isFooType
231 #Revision 1.8  2001/08/07 00:24:42  richard
232 #stupid typo
234 #Revision 1.7  2001/08/07 00:15:51  richard
235 #Added the copyright/license notice to (nearly) all files at request of
236 #Bizar Software.
238 #Revision 1.6  2001/07/30 02:36:23  richard
239 #Handle non-existence of db files in the other backends (code from anydbm).
241 #Revision 1.5  2001/07/30 01:41:36  richard
242 #Makes schema changes mucho easier.
244 #Revision 1.4  2001/07/23 08:25:33  richard
245 #more handling of bad journals
247 #Revision 1.3  2001/07/23 08:20:44  richard
248 #Moved over to using marshal in the bsddb and anydbm backends.
249 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
250 # retired - mod hyperdb.Class.list() so it lists retired nodes)
252 #Revision 1.2  2001/07/23 07:56:05  richard
253 #Storing only marshallable data in the db - no nasty pickled class references.
255 #Revision 1.1  2001/07/23 07:22:13  richard
256 #*sigh* some databases have _foo.so as their underlying implementation.
257 #This time for sure, Rocky.
259 #Revision 1.1  2001/07/23 07:15:57  richard
260 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
262 #Revision 1.1  2001/07/23 06:23:41  richard
263 #moved hyper_bsddb.py to the new backends package as bsddb.py
265 #Revision 1.2  2001/07/22 12:09:32  richard
266 #Final commit of Grande Splite
268 #Revision 1.1  2001/07/22 11:58:35  richard
269 #More Grande Splite