Code

...except of course it's nice to use valid Python syntax
[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.13 2002-07-08 06:41:03 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 import back_anydbm
26 #
27 # Now the database
28 #
29 class Database(back_anydbm.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.NoSuchFileError:
81             return []
82         # mor handling of bad journals
83         if not db.has_key(nodeid): return []
84         journal = marshal.loads(db[nodeid])
85         res = []
86         for entry in journal:
87             (nodeid, date_stamp, user, action, params) = entry
88             date_obj = date.Date(date_stamp)
89             res.append((nodeid, date_obj, user, action, params))
90         db.close()
91         return res
93     def _doSaveJournal(self, classname, nodeid, action, params):
94         # serialise first
95         if action in ('set', 'create'):
96             params = self.serialise(classname, params)
98         entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
99             params)
101         if __debug__:
102             print >>hyperdb.DEBUG, '_doSaveJournal', entry
104         db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
106         if db.has_key(nodeid):
107             s = db[nodeid]
108             l = marshal.loads(s)
109             l.append(entry)
110         else:
111             l = [entry]
113         db[nodeid] = marshal.dumps(l)
114         db.close()
117 #$Log: not supported by cvs2svn $
118 #Revision 1.12  2002/05/21 05:52:11  richard
119 #Well whadya know, bsddb3 works again.
120 #The backend is implemented _exactly_ the same as bsddb - so there's no
121 #using its transaction or locking support. It'd be nice to use those some
122 #day I suppose.
124 #Revision 1.11  2002/01/14 02:20:15  richard
125 # . changed all config accesses so they access either the instance or the
126 #   config attriubute on the db. This means that all config is obtained from
127 #   instance_config instead of the mish-mash of classes. This will make
128 #   switching to a ConfigParser setup easier too, I hope.
130 #At a minimum, this makes migration a _little_ easier (a lot easier in the
131 #0.5.0 switch, I hope!)
133 #Revision 1.10  2001/11/21 02:34:18  richard
134 #Added a target version field to the extended issue schema
136 #Revision 1.9  2001/10/09 23:58:10  richard
137 #Moved the data stringification up into the hyperdb.Class class' get, set
138 #and create methods. This means that the data is also stringified for the
139 #journal call, and removes duplication of code from the backends. The
140 #backend code now only sees strings.
142 #Revision 1.8  2001/10/09 07:25:59  richard
143 #Added the Password property type. See "pydoc roundup.password" for
144 #implementation details. Have updated some of the documentation too.
146 #Revision 1.7  2001/08/12 06:32:36  richard
147 #using isinstance(blah, Foo) now instead of isFooType
149 #Revision 1.6  2001/08/07 00:24:42  richard
150 #stupid typo
152 #Revision 1.5  2001/08/07 00:15:51  richard
153 #Added the copyright/license notice to (nearly) all files at request of
154 #Bizar Software.
156 #Revision 1.4  2001/08/03 02:45:47  anthonybaxter
157 #'n' -> 'c' for create.
159 #Revision 1.3  2001/07/30 02:36:23  richard
160 #Handle non-existence of db files in the other backends (code from anydbm).
162 #Revision 1.2  2001/07/30 01:41:36  richard
163 #Makes schema changes mucho easier.
165 #Revision 1.1  2001/07/24 04:26:03  anthonybaxter
166 #bsddb3 implementation. For now, it's the bsddb implementation with a "3"
167 #added in crayon.
169 #Revision 1.4  2001/07/23 08:25:33  richard
170 #more handling of bad journals
172 #Revision 1.3  2001/07/23 08:20:44  richard
173 #Moved over to using marshal in the bsddb and anydbm backends.
174 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
175 # retired - mod hyperdb.Class.list() so it lists retired nodes)
177 #Revision 1.2  2001/07/23 07:56:05  richard
178 #Storing only marshallable data in the db - no nasty pickled class references.
180 #Revision 1.1  2001/07/23 07:22:13  richard
181 #*sigh* some databases have _foo.so as their underlying implementation.
182 #This time for sure, Rocky.
184 #Revision 1.1  2001/07/23 07:15:57  richard
185 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
187 #Revision 1.1  2001/07/23 06:23:41  richard
188 #moved hyper_bsddb.py to the new backends package as bsddb.py
190 #Revision 1.2  2001/07/22 12:09:32  richard
191 #Final commit of Grande Splite
193 #Revision 1.1  2001/07/22 11:58:35  richard
194 #More Grande Splite