Code

missed removing this one
[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.15 2002-07-19 03:36:34 richard Exp $
20 import bsddb3, os, marshal
21 from roundup import hyperdb, date
23 # these classes are so similar, we just use the anydbm methods
24 from back_anydbm import Database, Class, FileClass, IssueClass
26 #
27 # Now the database
28 #
29 class Database(Database):
30     """A database for storing records containing flexible data types."""
31     #
32     # Class DBs
33     #
34     def clear(self):
35         for cn in self.classes.keys():
36             db = os.path.join(self.dir, 'nodes.%s'%cn)
37             bsddb3.btopen(db, 'n')
38             db = os.path.join(self.dir, 'journals.%s'%cn)
39             bsddb3.btopen(db, 'n')
41     def getclassdb(self, classname, mode='r'):
42         ''' grab a connection to the class db that will be used for
43             multiple actions
44         '''
45         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
46         if os.path.exists(path):
47             return bsddb3.btopen(path, mode)
48         else:
49             return bsddb3.btopen(path, 'c')
51     def opendb(self, name, mode):
52         '''Low-level database opener that gets around anydbm/dbm
53            eccentricities.
54         '''
55         if __debug__:
56             print >>hyperdb.DEBUG, self, 'opendb', (self, name, mode)
57         # determine which DB wrote the class file
58         path = os.path.join(os.getcwd(), self.dir, name)
59         if not os.path.exists(path):
60             if __debug__:
61                 print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, 'c')"%path
62             return bsddb3.btopen(path, 'c')
64         # open the database with the correct module
65         if __debug__:
66             print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, %r)"%(path, mode)
67         return bsddb3.btopen(path, mode)
69     #
70     # Journal
71     #
72     def getjournal(self, classname, nodeid):
73         ''' get the journal for id
74         '''
75         # attempt to open the journal - in some rare cases, the journal may
76         # not exist
77         try:
78             db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname),
79                 'r')
80         except bsddb3._db.DBNoSuchFileError:
81             raise IndexError, 'no such %s %s'%(classname, nodeid)
82         # more handling of bad journals
83         if not db.has_key(nodeid):
84             raise IndexError, 'no such %s %s'%(classname, nodeid)
85         journal = marshal.loads(db[nodeid])
86         res = []
87         for entry in journal:
88             (nodeid, date_stamp, user, action, params) = entry
89             date_obj = date.Date(date_stamp)
90             res.append((nodeid, date_obj, user, action, params))
91         db.close()
92         return res
94     def getCachedJournalDB(self, classname):
95         ''' get the journal db, looking in our cache of databases for commit
96         '''
97         # get the database handle
98         db_name = 'journals.%s'%classname
99         if self.databases.has_key(db_name):
100             return self.databases[db_name]
101         else:
102             db = bsddb3.btopen(os.path.join(self.dir, db_name), 'c')
103             self.databases[db_name] = db
104             return db
106     def doSaveJournal(self, classname, nodeid, action, params):
107         # serialise first
108         if action in ('set', 'create'):
109             params = self.serialise(classname, params)
111         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
112             params)
114         if __debug__:
115             print >>hyperdb.DEBUG, 'doSaveJournal', entry
117         db = self.getCachedJournalDB(classname)
119         if db.has_key(nodeid):
120             s = db[nodeid]
121             l = marshal.loads(s)
122             l.append(entry)
123         else:
124             l = [entry]
126         db[nodeid] = marshal.dumps(l)
129 #$Log: not supported by cvs2svn $
130 #Revision 1.14  2002/07/14 02:05:54  richard
131 #. all storage-specific code (ie. backend) is now implemented by the backends
133 #Revision 1.13  2002/07/08 06:41:03  richard
134 #Was reopening the database with 'n'.
136 #Revision 1.12  2002/05/21 05:52:11  richard
137 #Well whadya know, bsddb3 works again.
138 #The backend is implemented _exactly_ the same as bsddb - so there's no
139 #using its transaction or locking support. It'd be nice to use those some
140 #day I suppose.
142 #Revision 1.11  2002/01/14 02:20:15  richard
143 # . changed all config accesses so they access either the instance or the
144 #   config attriubute on the db. This means that all config is obtained from
145 #   instance_config instead of the mish-mash of classes. This will make
146 #   switching to a ConfigParser setup easier too, I hope.
148 #At a minimum, this makes migration a _little_ easier (a lot easier in the
149 #0.5.0 switch, I hope!)
151 #Revision 1.10  2001/11/21 02:34:18  richard
152 #Added a target version field to the extended issue schema
154 #Revision 1.9  2001/10/09 23:58:10  richard
155 #Moved the data stringification up into the hyperdb.Class class' get, set
156 #and create methods. This means that the data is also stringified for the
157 #journal call, and removes duplication of code from the backends. The
158 #backend code now only sees strings.
160 #Revision 1.8  2001/10/09 07:25:59  richard
161 #Added the Password property type. See "pydoc roundup.password" for
162 #implementation details. Have updated some of the documentation too.
164 #Revision 1.7  2001/08/12 06:32:36  richard
165 #using isinstance(blah, Foo) now instead of isFooType
167 #Revision 1.6  2001/08/07 00:24:42  richard
168 #stupid typo
170 #Revision 1.5  2001/08/07 00:15:51  richard
171 #Added the copyright/license notice to (nearly) all files at request of
172 #Bizar Software.
174 #Revision 1.4  2001/08/03 02:45:47  anthonybaxter
175 #'n' -> 'c' for create.
177 #Revision 1.3  2001/07/30 02:36:23  richard
178 #Handle non-existence of db files in the other backends (code from anydbm).
180 #Revision 1.2  2001/07/30 01:41:36  richard
181 #Makes schema changes mucho easier.
183 #Revision 1.1  2001/07/24 04:26:03  anthonybaxter
184 #bsddb3 implementation. For now, it's the bsddb implementation with a "3"
185 #added in crayon.
187 #Revision 1.4  2001/07/23 08:25:33  richard
188 #more handling of bad journals
190 #Revision 1.3  2001/07/23 08:20:44  richard
191 #Moved over to using marshal in the bsddb and anydbm backends.
192 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
193 # retired - mod hyperdb.Class.list() so it lists retired nodes)
195 #Revision 1.2  2001/07/23 07:56:05  richard
196 #Storing only marshallable data in the db - no nasty pickled class references.
198 #Revision 1.1  2001/07/23 07:22:13  richard
199 #*sigh* some databases have _foo.so as their underlying implementation.
200 #This time for sure, Rocky.
202 #Revision 1.1  2001/07/23 07:15:57  richard
203 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
205 #Revision 1.1  2001/07/23 06:23:41  richard
206 #moved hyper_bsddb.py to the new backends package as bsddb.py
208 #Revision 1.2  2001/07/22 12:09:32  richard
209 #Final commit of Grande Splite
211 #Revision 1.1  2001/07/22 11:58:35  richard
212 #More Grande Splite