Code

Fixed serialisation problem by moving the serialisation step out of the
[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.17 2002-04-03 05:54:31 richard Exp $
19 '''
20 This module defines a backend that saves the hyperdatabase in BSDDB.
21 '''
23 import bsddb, os, marshal
24 from roundup import hyperdb, date
26 # these classes are so similar, we just use the anydbm methods
27 import back_anydbm
29 #
30 # Now the database
31 #
32 class Database(back_anydbm.Database):
33     """A database for storing records containing flexible data types."""
34     #
35     # Class DBs
36     #
37     def clear(self):
38         for cn in self.classes.keys():
39             db = os.path.join(self.dir, 'nodes.%s'%cn)
40             bsddb.btopen(db, 'n')
41             db = os.path.join(self.dir, 'journals.%s'%cn)
42             bsddb.btopen(db, 'n')
44     def getclassdb(self, classname, mode='r'):
45         ''' grab a connection to the class db that will be used for
46             multiple actions
47         '''
48         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
49         if os.path.exists(path):
50             return bsddb.btopen(path, mode)
51         else:
52             return bsddb.btopen(path, 'n')
54     def _opendb(self, name, mode):
55         '''Low-level database opener that gets around anydbm/dbm
56            eccentricities.
57         '''
58         if hyperdb.DEBUG:
59             print self, '_opendb', (self, name, mode)
60         # determine which DB wrote the class file
61         path = os.path.join(os.getcwd(), self.dir, name)
62         if not os.path.exists(path):
63             if hyperdb.DEBUG:
64                 print "_opendb bsddb.open(%r, 'n')"%path
65             return bsddb.btopen(path, 'n')
67         # open the database with the correct module
68         if hyperdb.DEBUG:
69             print "_opendb bsddb.open(%r, %r)"%(path, mode)
70         return bsddb.btopen(path, mode)
72     #
73     # Journal
74     #
75     def getjournal(self, classname, nodeid):
76         ''' get the journal for id
77         '''
78         # attempt to open the journal - in some rare cases, the journal may
79         # not exist
80         try:
81             db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname),
82                 'r')
83         except bsddb.error, error:
84             if error.args[0] != 2: raise
85             return []
86         # mor handling of bad journals
87         if not db.has_key(nodeid): return []
88         journal = marshal.loads(db[nodeid])
89         res = []
90         for entry in journal:
91             (nodeid, date_stamp, user, action, params) = entry
92             date_obj = date.Date(date_stamp)
93             res.append((nodeid, date_obj, user, action, params))
94         db.close()
95         return res
97     def _doSaveJournal(self, classname, nodeid, action, params):
98         # serialise first
99         if action in ('set', 'create'):
100             params = self.serialise(classname, params)
102         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
103             params)
105         if hyperdb.DEBUG:
106             print '_doSaveJournal', entry
108         db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
110         if db.has_key(nodeid):
111             s = db[nodeid]
112             l = marshal.loads(s)
113             l.append(entry)
114         else:
115             l = [entry]
117         db[nodeid] = marshal.dumps(l)
118         db.close()
121 #$Log: not supported by cvs2svn $
122 #Revision 1.16  2002/02/27 03:40:59  richard
123 #Ran it through pychecker, made fixes
125 #Revision 1.15  2002/02/16 09:15:33  richard
126 #forgot to patch bsddb backend too
128 #Revision 1.14  2002/01/22 07:21:13  richard
129 #. fixed back_bsddb so it passed the journal tests
131 #... it didn't seem happy using the back_anydbm _open method, which is odd.
132 #Yet another occurrance of whichdb not being able to recognise older bsddb
133 #databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
134 #process.
136 #Revision 1.13  2001/12/10 22:20:01  richard
137 #Enabled transaction support in the bsddb backend. It uses the anydbm code
138 #where possible, only replacing methods where the db is opened (it uses the
139 #btree opener specifically.)
140 #Also cleaned up some change note generation.
141 #Made the backends package work with pydoc too.
143 #Revision 1.12  2001/11/21 02:34:18  richard
144 #Added a target version field to the extended issue schema
146 #Revision 1.11  2001/10/09 23:58:10  richard
147 #Moved the data stringification up into the hyperdb.Class class' get, set
148 #and create methods. This means that the data is also stringified for the
149 #journal call, and removes duplication of code from the backends. The
150 #backend code now only sees strings.
152 #Revision 1.10  2001/10/09 07:25:59  richard
153 #Added the Password property type. See "pydoc roundup.password" for
154 #implementation details. Have updated some of the documentation too.
156 #Revision 1.9  2001/08/12 06:32:36  richard
157 #using isinstance(blah, Foo) now instead of isFooType
159 #Revision 1.8  2001/08/07 00:24:42  richard
160 #stupid typo
162 #Revision 1.7  2001/08/07 00:15:51  richard
163 #Added the copyright/license notice to (nearly) all files at request of
164 #Bizar Software.
166 #Revision 1.6  2001/07/30 02:36:23  richard
167 #Handle non-existence of db files in the other backends (code from anydbm).
169 #Revision 1.5  2001/07/30 01:41:36  richard
170 #Makes schema changes mucho easier.
172 #Revision 1.4  2001/07/23 08:25:33  richard
173 #more handling of bad journals
175 #Revision 1.3  2001/07/23 08:20:44  richard
176 #Moved over to using marshal in the bsddb and anydbm backends.
177 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
178 # retired - mod hyperdb.Class.list() so it lists retired nodes)
180 #Revision 1.2  2001/07/23 07:56:05  richard
181 #Storing only marshallable data in the db - no nasty pickled class references.
183 #Revision 1.1  2001/07/23 07:22:13  richard
184 #*sigh* some databases have _foo.so as their underlying implementation.
185 #This time for sure, Rocky.
187 #Revision 1.1  2001/07/23 07:15:57  richard
188 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
190 #Revision 1.1  2001/07/23 06:23:41  richard
191 #moved hyper_bsddb.py to the new backends package as bsddb.py
193 #Revision 1.2  2001/07/22 12:09:32  richard
194 #Final commit of Grande Splite
196 #Revision 1.1  2001/07/22 11:58:35  richard
197 #More Grande Splite