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.12 2002-05-21 05:52:11 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, 'n')
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, 'n')"%path
62 return bsddb3.btopen(path, 'n')
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()
116 #
117 #$Log: not supported by cvs2svn $
118 #Revision 1.11 2002/01/14 02:20:15 richard
119 # . changed all config accesses so they access either the instance or the
120 # config attriubute on the db. This means that all config is obtained from
121 # instance_config instead of the mish-mash of classes. This will make
122 # switching to a ConfigParser setup easier too, I hope.
123 #
124 #At a minimum, this makes migration a _little_ easier (a lot easier in the
125 #0.5.0 switch, I hope!)
126 #
127 #Revision 1.10 2001/11/21 02:34:18 richard
128 #Added a target version field to the extended issue schema
129 #
130 #Revision 1.9 2001/10/09 23:58:10 richard
131 #Moved the data stringification up into the hyperdb.Class class' get, set
132 #and create methods. This means that the data is also stringified for the
133 #journal call, and removes duplication of code from the backends. The
134 #backend code now only sees strings.
135 #
136 #Revision 1.8 2001/10/09 07:25:59 richard
137 #Added the Password property type. See "pydoc roundup.password" for
138 #implementation details. Have updated some of the documentation too.
139 #
140 #Revision 1.7 2001/08/12 06:32:36 richard
141 #using isinstance(blah, Foo) now instead of isFooType
142 #
143 #Revision 1.6 2001/08/07 00:24:42 richard
144 #stupid typo
145 #
146 #Revision 1.5 2001/08/07 00:15:51 richard
147 #Added the copyright/license notice to (nearly) all files at request of
148 #Bizar Software.
149 #
150 #Revision 1.4 2001/08/03 02:45:47 anthonybaxter
151 #'n' -> 'c' for create.
152 #
153 #Revision 1.3 2001/07/30 02:36:23 richard
154 #Handle non-existence of db files in the other backends (code from anydbm).
155 #
156 #Revision 1.2 2001/07/30 01:41:36 richard
157 #Makes schema changes mucho easier.
158 #
159 #Revision 1.1 2001/07/24 04:26:03 anthonybaxter
160 #bsddb3 implementation. For now, it's the bsddb implementation with a "3"
161 #added in crayon.
162 #
163 #Revision 1.4 2001/07/23 08:25:33 richard
164 #more handling of bad journals
165 #
166 #Revision 1.3 2001/07/23 08:20:44 richard
167 #Moved over to using marshal in the bsddb and anydbm backends.
168 #roundup-admin now has a "freshen" command that'll load/save all nodes (not
169 # retired - mod hyperdb.Class.list() so it lists retired nodes)
170 #
171 #Revision 1.2 2001/07/23 07:56:05 richard
172 #Storing only marshallable data in the db - no nasty pickled class references.
173 #
174 #Revision 1.1 2001/07/23 07:22:13 richard
175 #*sigh* some databases have _foo.so as their underlying implementation.
176 #This time for sure, Rocky.
177 #
178 #Revision 1.1 2001/07/23 07:15:57 richard
179 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
180 #
181 #Revision 1.1 2001/07/23 06:23:41 richard
182 #moved hyper_bsddb.py to the new backends package as bsddb.py
183 #
184 #Revision 1.2 2001/07/22 12:09:32 richard
185 #Final commit of Grande Splite
186 #
187 #Revision 1.1 2001/07/22 11:58:35 richard
188 #More Grande Splite
189 #