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.22 2002-09-09 02:58:34 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 from back_anydbm import Database, Class, FileClass, IssueClass
29 #
30 # Now the database
31 #
32 class Database(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, 'c')
54 def opendb(self, name, mode):
55 '''Low-level database opener that gets around anydbm/dbm
56 eccentricities.
57 '''
58 if __debug__:
59 print >>hyperdb.DEBUG, 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 __debug__:
64 print >>hyperdb.DEBUG, "opendb bsddb.open(%r, 'c')"%path
65 return bsddb.btopen(path, 'c')
67 # open the database with the correct module
68 if __debug__:
69 print >>hyperdb.DEBUG, "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 raise IndexError, 'no such %s %s'%(classname, nodeid)
86 # more handling of bad journals
87 if not db.has_key(nodeid):
88 raise IndexError, 'no such %s %s'%(classname, nodeid)
89 journal = marshal.loads(db[nodeid])
90 res = []
91 for entry in journal:
92 (nodeid, date_stamp, user, action, params) = entry
93 date_obj = date.Date(date_stamp)
94 res.append((nodeid, date_obj, user, action, params))
95 db.close()
96 return res
98 def getCachedJournalDB(self, classname):
99 ''' get the journal db, looking in our cache of databases for commit
100 '''
101 # get the database handle
102 db_name = 'journals.%s'%classname
103 if self.databases.has_key(db_name):
104 return self.databases[db_name]
105 else:
106 db = bsddb.btopen(os.path.join(self.dir, db_name), 'c')
107 self.databases[db_name] = db
108 return db
110 def doSaveJournal(self, classname, nodeid, action, params):
111 # serialise first
112 if action in ('set', 'create'):
113 params = self.serialise(classname, params)
115 entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
116 params)
118 if __debug__:
119 print >>hyperdb.DEBUG, 'doSaveJournal', entry
121 db = self.getCachedJournalDB(classname)
123 if db.has_key(nodeid):
124 s = db[nodeid]
125 l = marshal.loads(s)
126 l.append(entry)
127 else:
128 l = [entry]
130 db[nodeid] = marshal.dumps(l)
132 #
133 #$Log: not supported by cvs2svn $
134 #Revision 1.21 2002/09/03 07:33:01 richard
135 #allow overiding of the index args roundup/cgi/templating.py
136 #
137 #Revision 1.20 2002/07/19 03:36:34 richard
138 #Implemented the destroy() method needed by the session database (and possibly
139 #others). At the same time, I removed the leading underscores from the hyperdb
140 #methods that Really Didn't Need Them.
141 #The journal also raises IndexError now for all situations where there is a
142 #request for the journal of a node that doesn't have one. It used to return
143 #[] in _some_ situations, but not all. This _may_ break code, but the tests
144 #pass...
145 #
146 #Revision 1.19 2002/07/14 02:05:53 richard
147 #. all storage-specific code (ie. backend) is now implemented by the backends
148 #
149 #Revision 1.18 2002/05/15 06:21:21 richard
150 # . node caching now works, and gives a small boost in performance
151 #
152 #As a part of this, I cleaned up the DEBUG output and implemented TRACE
153 #output (HYPERDBTRACE='file to trace to') with checkpoints at the start of
154 #CGI requests. Run roundup with python -O to skip all the DEBUG/TRACE stuff
155 #(using if __debug__ which is compiled out with -O)
156 #
157 #Revision 1.17 2002/04/03 05:54:31 richard
158 #Fixed serialisation problem by moving the serialisation step out of the
159 #hyperdb.Class (get, set) into the hyperdb.Database.
160 #
161 #Also fixed htmltemplate after the showid changes I made yesterday.
162 #
163 #Unit tests for all of the above written.
164 #
165 #Revision 1.16 2002/02/27 03:40:59 richard
166 #Ran it through pychecker, made fixes
167 #
168 #Revision 1.15 2002/02/16 09:15:33 richard
169 #forgot to patch bsddb backend too
170 #
171 #Revision 1.14 2002/01/22 07:21:13 richard
172 #. fixed back_bsddb so it passed the journal tests
173 #
174 #... it didn't seem happy using the back_anydbm _open method, which is odd.
175 #Yet another occurrance of whichdb not being able to recognise older bsddb
176 #databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
177 #process.
178 #
179 #Revision 1.13 2001/12/10 22:20:01 richard
180 #Enabled transaction support in the bsddb backend. It uses the anydbm code
181 #where possible, only replacing methods where the db is opened (it uses the
182 #btree opener specifically.)
183 #Also cleaned up some change note generation.
184 #Made the backends package work with pydoc too.
185 #
186 #Revision 1.12 2001/11/21 02:34:18 richard
187 #Added a target version field to the extended issue schema
188 #
189 #Revision 1.11 2001/10/09 23:58:10 richard
190 #Moved the data stringification up into the hyperdb.Class class' get, set
191 #and create methods. This means that the data is also stringified for the
192 #journal call, and removes duplication of code from the backends. The
193 #backend code now only sees strings.
194 #
195 #Revision 1.10 2001/10/09 07:25:59 richard
196 #Added the Password property type. See "pydoc roundup.password" for
197 #implementation details. Have updated some of the documentation too.
198 #
199 #Revision 1.9 2001/08/12 06:32:36 richard
200 #using isinstance(blah, Foo) now instead of isFooType
201 #
202 #Revision 1.8 2001/08/07 00:24:42 richard
203 #stupid typo
204 #
205 #Revision 1.7 2001/08/07 00:15:51 richard
206 #Added the copyright/license notice to (nearly) all files at request of
207 #Bizar Software.
208 #
209 #Revision 1.6 2001/07/30 02:36:23 richard
210 #Handle non-existence of db files in the other backends (code from anydbm).
211 #
212 #Revision 1.5 2001/07/30 01:41:36 richard
213 #Makes schema changes mucho easier.
214 #
215 #Revision 1.4 2001/07/23 08:25:33 richard
216 #more handling of bad journals
217 #
218 #Revision 1.3 2001/07/23 08:20:44 richard
219 #Moved over to using marshal in the bsddb and anydbm backends.
220 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
221 # retired - mod hyperdb.Class.list() so it lists retired nodes)
222 #
223 #Revision 1.2 2001/07/23 07:56:05 richard
224 #Storing only marshallable data in the db - no nasty pickled class references.
225 #
226 #Revision 1.1 2001/07/23 07:22:13 richard
227 #*sigh* some databases have _foo.so as their underlying implementation.
228 #This time for sure, Rocky.
229 #
230 #Revision 1.1 2001/07/23 07:15:57 richard
231 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
232 #
233 #Revision 1.1 2001/07/23 06:23:41 richard
234 #moved hyper_bsddb.py to the new backends package as bsddb.py
235 #
236 #Revision 1.2 2001/07/22 12:09:32 richard
237 #Final commit of Grande Splite
238 #
239 #Revision 1.1 2001/07/22 11:58:35 richard
240 #More Grande Splite
241 #