Code

help for winzip users
[roundup.git] / roundup / backends / back_bsddb3.py
index 287b2abdbab421638fff89691351e95b0bbb45ec..dbf8675e42b8e5684f6afa249de3021d07c50ed3 100644 (file)
 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 # 
-#$Id: back_bsddb3.py,v 1.13 2002-07-08 06:41:03 richard Exp $
+#$Id: back_bsddb3.py,v 1.22 2004-02-11 23:55:08 richard Exp $
+'''This module defines a backend that saves the hyperdatabase in BSDDB3.
+'''
+__docformat__ = 'restructuredtext'
 
 import bsddb3, os, marshal
 from roundup import hyperdb, date
 
 # these classes are so similar, we just use the anydbm methods
-import back_anydbm
+from back_anydbm import Database, Class, FileClass, IssueClass
 
 #
 # Now the database
 #
-class Database(back_anydbm.Database):
+class Database(Database):
     """A database for storing records containing flexible data types."""
     #
     # Class DBs
@@ -48,22 +51,22 @@ class Database(back_anydbm.Database):
         else:
             return bsddb3.btopen(path, 'c')
 
-    def _opendb(self, name, mode):
+    def opendb(self, name, mode):
         '''Low-level database opener that gets around anydbm/dbm
            eccentricities.
         '''
         if __debug__:
-            print >>hyperdb.DEBUG, self, '_opendb', (self, name, mode)
+            print >>hyperdb.DEBUG, self, 'opendb', (self, name, mode)
         # determine which DB wrote the class file
         path = os.path.join(os.getcwd(), self.dir, name)
         if not os.path.exists(path):
             if __debug__:
-                print >>hyperdb.DEBUG, "_opendb bsddb3.open(%r, 'c')"%path
+                print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, 'c')"%path
             return bsddb3.btopen(path, 'c')
 
         # open the database with the correct module
         if __debug__:
-            print >>hyperdb.DEBUG, "_opendb bsddb3.open(%r, %r)"%(path, mode)
+            print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, %r)"%(path, mode)
         return bsddb3.btopen(path, mode)
 
     #
@@ -71,125 +74,65 @@ class Database(back_anydbm.Database):
     #
     def getjournal(self, classname, nodeid):
         ''' get the journal for id
+
+            Raise IndexError if the node doesn't exist (as per history()'s
+            API)
         '''
+        if __debug__:
+            print >>hyperdb.DEBUG, 'getjournal', (self, classname, nodeid)
+
+        # our journal result
+        res = []
+
+        # add any journal entries for transactions not committed to the
+        # database
+        for method, args in self.transactions:
+            if method != self.doSaveJournal:
+                continue
+            (cache_classname, cache_nodeid, cache_action, cache_params,
+                cache_creator, cache_creation) = args
+            if cache_classname == classname and cache_nodeid == nodeid:
+                if not cache_creator:
+                    cache_creator = self.getuid()
+                if not cache_creation:
+                    cache_creation = date.Date()
+                res.append((cache_nodeid, cache_creation, cache_creator,
+                    cache_action, cache_params))
+
         # attempt to open the journal - in some rare cases, the journal may
         # not exist
         try:
             db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname),
                 'r')
-        except bsddb3.NoSuchFileError:
-            return []
-        # mor handling of bad journals
-        if not db.has_key(nodeid): return []
+        except bsddb3._db.DBNoSuchFileError:
+            if res:
+                # we have unsaved journal entries, return them
+                return res
+            raise IndexError, 'no such %s %s'%(classname, nodeid)
+        # more handling of bad journals
+        if not db.has_key(nodeid):
+            db.close()
+            if res:
+                # we have some unsaved journal entries, be happy!
+                return res
+            raise IndexError, 'no such %s %s'%(classname, nodeid)
         journal = marshal.loads(db[nodeid])
-        res = []
-        for entry in journal:
-            (nodeid, date_stamp, user, action, params) = entry
-            date_obj = date.Date(date_stamp)
-            res.append((nodeid, date_obj, user, action, params))
         db.close()
-        return res
-
-    def _doSaveJournal(self, classname, nodeid, action, params):
-        # serialise first
-        if action in ('set', 'create'):
-            params = self.serialise(classname, params)
-
-        entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
-            params)
 
-        if __debug__:
-            print >>hyperdb.DEBUG, '_doSaveJournal', entry
-
-        db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
+        # add all the saved journal entries for this node
+        for nodeid, date_stamp, user, action, params in journal:
+            res.append((nodeid, date.Date(date_stamp), user, action, params))
+        return res
 
-        if db.has_key(nodeid):
-            s = db[nodeid]
-            l = marshal.loads(s)
-            l.append(entry)
+    def getCachedJournalDB(self, classname):
+        ''' get the journal db, looking in our cache of databases for commit
+        '''
+        # get the database handle
+        db_name = 'journals.%s'%classname
+        if self.databases.has_key(db_name):
+            return self.databases[db_name]
         else:
-            l = [entry]
+            db = bsddb3.btopen(os.path.join(self.dir, db_name), 'c')
+            self.databases[db_name] = db
+            return db
 
-        db[nodeid] = marshal.dumps(l)
-        db.close()
-
-#
-#$Log: not supported by cvs2svn $
-#Revision 1.12  2002/05/21 05:52:11  richard
-#Well whadya know, bsddb3 works again.
-#The backend is implemented _exactly_ the same as bsddb - so there's no
-#using its transaction or locking support. It'd be nice to use those some
-#day I suppose.
-#
-#Revision 1.11  2002/01/14 02:20:15  richard
-# . changed all config accesses so they access either the instance or the
-#   config attriubute on the db. This means that all config is obtained from
-#   instance_config instead of the mish-mash of classes. This will make
-#   switching to a ConfigParser setup easier too, I hope.
-#
-#At a minimum, this makes migration a _little_ easier (a lot easier in the
-#0.5.0 switch, I hope!)
-#
-#Revision 1.10  2001/11/21 02:34:18  richard
-#Added a target version field to the extended issue schema
-#
-#Revision 1.9  2001/10/09 23:58:10  richard
-#Moved the data stringification up into the hyperdb.Class class' get, set
-#and create methods. This means that the data is also stringified for the
-#journal call, and removes duplication of code from the backends. The
-#backend code now only sees strings.
-#
-#Revision 1.8  2001/10/09 07:25:59  richard
-#Added the Password property type. See "pydoc roundup.password" for
-#implementation details. Have updated some of the documentation too.
-#
-#Revision 1.7  2001/08/12 06:32:36  richard
-#using isinstance(blah, Foo) now instead of isFooType
-#
-#Revision 1.6  2001/08/07 00:24:42  richard
-#stupid typo
-#
-#Revision 1.5  2001/08/07 00:15:51  richard
-#Added the copyright/license notice to (nearly) all files at request of
-#Bizar Software.
-#
-#Revision 1.4  2001/08/03 02:45:47  anthonybaxter
-#'n' -> 'c' for create.
-#
-#Revision 1.3  2001/07/30 02:36:23  richard
-#Handle non-existence of db files in the other backends (code from anydbm).
-#
-#Revision 1.2  2001/07/30 01:41:36  richard
-#Makes schema changes mucho easier.
-#
-#Revision 1.1  2001/07/24 04:26:03  anthonybaxter
-#bsddb3 implementation. For now, it's the bsddb implementation with a "3"
-#added in crayon.
-#
-#Revision 1.4  2001/07/23 08:25:33  richard
-#more handling of bad journals
-#
-#Revision 1.3  2001/07/23 08:20:44  richard
-#Moved over to using marshal in the bsddb and anydbm backends.
-#roundup-admin now has a "freshen" command that'll load/save all nodes (not
-# retired - mod hyperdb.Class.list() so it lists retired nodes)
-#
-#Revision 1.2  2001/07/23 07:56:05  richard
-#Storing only marshallable data in the db - no nasty pickled class references.
-#
-#Revision 1.1  2001/07/23 07:22:13  richard
-#*sigh* some databases have _foo.so as their underlying implementation.
-#This time for sure, Rocky.
-#
-#Revision 1.1  2001/07/23 07:15:57  richard
-#Moved the backends into the backends package. Anydbm hasn't been tested at all.
-#
-#Revision 1.1  2001/07/23 06:23:41  richard
-#moved hyper_bsddb.py to the new backends package as bsddb.py
-#
-#Revision 1.2  2001/07/22 12:09:32  richard
-#Final commit of Grande Splite
-#
-#Revision 1.1  2001/07/22 11:58:35  richard
-#More Grande Splite
-#