Code

. #514854 ] History: "User" is always ticket creator
[roundup.git] / roundup / backends / back_bsddb3.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_bsddb3.py,v 1.11 2002-01-14 02:20:15 richard Exp $
20 import bsddb3, 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, config, journaltag=None):
30         """Open a hyperdatabase given a specifier to some storage.
32         The 'storagelocator' is obtained from config.DATABASE.
33         The meaning of 'storagelocator' depends on the particular
34         implementation of the hyperdatabase.  It could be a file name,
35         a directory path, a socket descriptor for a connection to a
36         database over the network, etc.
38         The 'journaltag' is a token that will be attached to the journal
39         entries for any edits done on the database.  If 'journaltag' is
40         None, the database is opened in read-only mode: the Class.create(),
41         Class.set(), and Class.retire() methods are disabled.
42         """
43         self.config, self.journaltag = config, journaltag
44         self.dir = config.DATABASE
45         self.classes = {}
47     #
48     # Classes
49     #
50     def __getattr__(self, classname):
51         """A convenient way of calling self.getclass(classname)."""
52         return self.classes[classname]
54     def addclass(self, cl):
55         cn = cl.classname
56         if self.classes.has_key(cn):
57             raise ValueError, cn
58         self.classes[cn] = cl
60     def getclasses(self):
61         """Return a list of the names of all existing classes."""
62         l = self.classes.keys()
63         l.sort()
64         return l
66     def getclass(self, classname):
67         """Get the Class object representing a particular class.
69         If 'classname' is not a valid class name, a KeyError is raised.
70         """
71         return self.classes[classname]
73     #
74     # Class DBs
75     #
76     def clear(self):
77         for cn in self.classes.keys():
78             db = os.path.join(self.dir, 'nodes.%s'%cn)
79             bsddb3.btopen(db, 'c')
80             db = os.path.join(self.dir, 'journals.%s'%cn)
81             bsddb3.btopen(db, 'c')
83     def getclassdb(self, classname, mode='r'):
84         ''' grab a connection to the class db that will be used for
85             multiple actions
86         '''
87         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
88         if os.path.exists(path):
89             return bsddb3.btopen(path, mode)
90         else:
91             return bsddb3.btopen(path, 'c')
93     #
94     # Nodes
95     #
96     def addnode(self, classname, nodeid, node):
97         ''' add the specified node to its class's db
98         '''
99         db = self.getclassdb(classname, 'c')
100         # now save the marshalled data
101         db[nodeid] = marshal.dumps(node)
102         db.close()
103     setnode = addnode
105     def getnode(self, classname, nodeid, cldb=None):
106         ''' add the specified node to its class's db
107         '''
108         db = cldb or self.getclassdb(classname)
109         if not db.has_key(nodeid):
110             raise IndexError, nodeid
111         res = marshal.loads(db[nodeid])
112         if not cldb: db.close()
113         return res
115     def hasnode(self, classname, nodeid, cldb=None):
116         ''' add the specified node to its class's db
117         '''
118         db = cldb or self.getclassdb(classname)
119         res = db.has_key(nodeid)
120         if not cldb: db.close()
121         return res
123     def countnodes(self, classname, cldb=None):
124         db = cldb or self.getclassdb(classname)
125         return len(db.keys())
126         if not cldb: db.close()
127         return res
129     def getnodeids(self, classname, cldb=None):
130         db = cldb or self.getclassdb(classname)
131         res = db.keys()
132         if not cldb: db.close()
133         return res
135     #
136     # Journal
137     #
138     def addjournal(self, classname, nodeid, action, params):
139         ''' Journal the Action
140         'action' may be:
142             'create' or 'set' -- 'params' is a dictionary of property values
143             'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
144             'retire' -- 'params' is None
145         '''
146         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
147             params)
148         db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
149         if db.has_key(nodeid):
150             s = db[nodeid]
151             l = marshal.loads(db[nodeid])
152             l.append(entry)
153         else:
154             l = [entry]
155         db[nodeid] = marshal.dumps(l)
156         db.close()
158     def getjournal(self, classname, nodeid):
159         ''' get the journal for id
160         '''
161         # attempt to open the journal - in some rare cases, the journal may
162         # not exist
163         try:
164             db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname),
165                 'r')
166         except bsddb3.error, error:
167             if error.args[0] != 2: raise
168             return []
169         # mor handling of bad journals
170         if not db.has_key(nodeid): return []
171         journal = marshal.loads(db[nodeid])
172         res = []
173         for entry in journal:
174             (nodeid, date_stamp, self.journaltag, action, params) = entry
175             date_obj = date.Date(date_stamp)
176             res.append((nodeid, date_obj, self.journaltag, action, params))
177         db.close()
178         return res
180     def close(self):
181         ''' Close the Database - we must release the circular refs so that
182             we can be del'ed and the underlying bsddb connections closed
183             cleanly.
184         '''
185         self.classes = {}
188     #
189     # Basic transaction support
190     #
191     # TODO: well, write these methods (and then use them in other code)
192     def register_action(self):
193         ''' Register an action to the transaction undo log
194         '''
196     def commit(self):
197         ''' Commit the current transaction, start a new one
198         '''
200     def rollback(self):
201         ''' Reverse all actions from the current transaction
202         '''
205 #$Log: not supported by cvs2svn $
206 #Revision 1.10  2001/11/21 02:34:18  richard
207 #Added a target version field to the extended issue schema
209 #Revision 1.9  2001/10/09 23:58:10  richard
210 #Moved the data stringification up into the hyperdb.Class class' get, set
211 #and create methods. This means that the data is also stringified for the
212 #journal call, and removes duplication of code from the backends. The
213 #backend code now only sees strings.
215 #Revision 1.8  2001/10/09 07:25:59  richard
216 #Added the Password property type. See "pydoc roundup.password" for
217 #implementation details. Have updated some of the documentation too.
219 #Revision 1.7  2001/08/12 06:32:36  richard
220 #using isinstance(blah, Foo) now instead of isFooType
222 #Revision 1.6  2001/08/07 00:24:42  richard
223 #stupid typo
225 #Revision 1.5  2001/08/07 00:15:51  richard
226 #Added the copyright/license notice to (nearly) all files at request of
227 #Bizar Software.
229 #Revision 1.4  2001/08/03 02:45:47  anthonybaxter
230 #'n' -> 'c' for create.
232 #Revision 1.3  2001/07/30 02:36:23  richard
233 #Handle non-existence of db files in the other backends (code from anydbm).
235 #Revision 1.2  2001/07/30 01:41:36  richard
236 #Makes schema changes mucho easier.
238 #Revision 1.1  2001/07/24 04:26:03  anthonybaxter
239 #bsddb3 implementation. For now, it's the bsddb implementation with a "3"
240 #added in crayon.
242 #Revision 1.4  2001/07/23 08:25:33  richard
243 #more handling of bad journals
245 #Revision 1.3  2001/07/23 08:20:44  richard
246 #Moved over to using marshal in the bsddb and anydbm backends.
247 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
248 # retired - mod hyperdb.Class.list() so it lists retired nodes)
250 #Revision 1.2  2001/07/23 07:56:05  richard
251 #Storing only marshallable data in the db - no nasty pickled class references.
253 #Revision 1.1  2001/07/23 07:22:13  richard
254 #*sigh* some databases have _foo.so as their underlying implementation.
255 #This time for sure, Rocky.
257 #Revision 1.1  2001/07/23 07:15:57  richard
258 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
260 #Revision 1.1  2001/07/23 06:23:41  richard
261 #moved hyper_bsddb.py to the new backends package as bsddb.py
263 #Revision 1.2  2001/07/22 12:09:32  richard
264 #Final commit of Grande Splite
266 #Revision 1.1  2001/07/22 11:58:35  richard
267 #More Grande Splite