Code

More informative error message
[roundup.git] / roundup / backends / back_bsddb.py
index fde8610beb62509cb11395bc50398c018341c666..6f8edd7112c1bafcab95999ad2751745251d093c 100644 (file)
@@ -1,56 +1,36 @@
-#$Id: back_bsddb.py,v 1.5 2001-07-30 01:41:36 richard Exp $
+#
+# Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
+# This module is free software, and you may redistribute it and/or modify
+# under the same terms as Python, so long as this copyright message and
+# disclaimer are retained in their original form.
+#
+# IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
+# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
+# OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
+# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
+# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
+# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+# 
+#$Id: back_bsddb.py,v 1.18 2002-05-15 06:21:21 richard Exp $
+'''
+This module defines a backend that saves the hyperdatabase in BSDDB.
+'''
 
 import bsddb, os, marshal
 from roundup import hyperdb, date
 
+# these classes are so similar, we just use the anydbm methods
+import back_anydbm
+
 #
 # Now the database
 #
-class Database(hyperdb.Database):
+class Database(back_anydbm.Database):
     """A database for storing records containing flexible data types."""
-
-    def __init__(self, storagelocator, journaltag=None):
-        """Open a hyperdatabase given a specifier to some storage.
-
-        The meaning of 'storagelocator' depends on the particular
-        implementation of the hyperdatabase.  It could be a file name,
-        a directory path, a socket descriptor for a connection to a
-        database over the network, etc.
-
-        The 'journaltag' is a token that will be attached to the journal
-        entries for any edits done on the database.  If 'journaltag' is
-        None, the database is opened in read-only mode: the Class.create(),
-        Class.set(), and Class.retire() methods are disabled.
-        """
-        self.dir, self.journaltag = storagelocator, journaltag
-        self.classes = {}
-
-    #
-    # Classes
-    #
-    def __getattr__(self, classname):
-        """A convenient way of calling self.getclass(classname)."""
-        return self.classes[classname]
-
-    def addclass(self, cl):
-        cn = cl.classname
-        if self.classes.has_key(cn):
-            raise ValueError, cn
-        self.classes[cn] = cl
-
-    def getclasses(self):
-        """Return a list of the names of all existing classes."""
-        l = self.classes.keys()
-        l.sort()
-        return l
-
-    def getclass(self, classname):
-        """Get the Class object representing a particular class.
-
-        If 'classname' is not a valid class name, a KeyError is raised.
-        """
-        return self.classes[classname]
-
     #
     # Class DBs
     #
@@ -66,91 +46,32 @@ class Database(hyperdb.Database):
             multiple actions
         '''
         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
-        return bsddb.btopen(path, mode)
-
-    #
-    # Nodes
-    #
-    def addnode(self, classname, nodeid, node):
-        ''' add the specified node to its class's db
-        '''
-        db = self.getclassdb(classname, 'c')
-
-        # convert the instance data to builtin types
-        properties = self.classes[classname].properties
-        for key in properties.keys():
-            if properties[key].isDateType:
-                node[key] = node[key].get_tuple()
-            elif properties[key].isIntervalType:
-                node[key] = node[key].get_tuple()
-
-        # now save the marshalled data
-        db[nodeid] = marshal.dumps(node)
-        db.close()
-    setnode = addnode
-
-    def getnode(self, classname, nodeid, cldb=None):
-        ''' add the specified node to its class's db
-        '''
-        db = cldb or self.getclassdb(classname)
-        if not db.has_key(nodeid):
-            raise IndexError, nodeid
-        res = marshal.loads(db[nodeid])
-
-        # convert the marshalled data to instances
-        properties = self.classes[classname].properties
-        for key in properties.keys():
-            if properties[key].isDateType:
-                res[key] = date.Date(res[key])
-            elif properties[key].isIntervalType:
-                res[key] = date.Interval(res[key])
-
-        if not cldb: db.close()
-        return res
+        if os.path.exists(path):
+            return bsddb.btopen(path, mode)
+        else:
+            return bsddb.btopen(path, 'n')
 
-    def hasnode(self, classname, nodeid, cldb=None):
-        ''' add the specified node to its class's db
+    def _opendb(self, name, mode):
+        '''Low-level database opener that gets around anydbm/dbm
+           eccentricities.
         '''
-        db = cldb or self.getclassdb(classname)
-        res = db.has_key(nodeid)
-        if not cldb: db.close()
-        return res
-
-    def countnodes(self, classname, cldb=None):
-        db = cldb or self.getclassdb(classname)
-        return len(db.keys())
-        if not cldb: db.close()
-        return res
-
-    def getnodeids(self, classname, cldb=None):
-        db = cldb or self.getclassdb(classname)
-        res = db.keys()
-        if not cldb: db.close()
-        return res
+        if __debug__:
+            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 bsddb.open(%r, 'n')"%path
+            return bsddb.btopen(path, 'n')
+
+        # open the database with the correct module
+        if __debug__:
+            print >>hyperdb.DEBUG, "_opendb bsddb.open(%r, %r)"%(path, mode)
+        return bsddb.btopen(path, mode)
 
     #
     # Journal
     #
-    def addjournal(self, classname, nodeid, action, params):
-        ''' Journal the Action
-        'action' may be:
-
-            'create' or 'set' -- 'params' is a dictionary of property values
-            'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
-            'retire' -- 'params' is None
-        '''
-        entry = (nodeid, date.Date().get_tuple(), self.journaltag, action,
-            params)
-        db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
-        if db.has_key(nodeid):
-            s = db[nodeid]
-            l = marshal.loads(db[nodeid])
-            l.append(entry)
-        else:
-            l = [entry]
-        db[nodeid] = marshal.dumps(l)
-        db.close()
-
     def getjournal(self, classname, nodeid):
         ''' get the journal for id
         '''
@@ -167,38 +88,95 @@ class Database(hyperdb.Database):
         journal = marshal.loads(db[nodeid])
         res = []
         for entry in journal:
-            (nodeid, date_stamp, self.journaltag, action, params) = entry
+            (nodeid, date_stamp, user, action, params) = entry
             date_obj = date.Date(date_stamp)
-            res.append((nodeid, date_obj, self.journaltag, action, params))
+            res.append((nodeid, date_obj, user, action, params))
         db.close()
         return res
 
-    def close(self):
-        ''' Close the Database - we must release the circular refs so that
-            we can be del'ed and the underlying bsddb connections closed
-            cleanly.
-        '''
-        self.classes = None
+    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)
 
-    #
-    # Basic transaction support
-    #
-    # TODO: well, write these methods (and then use them in other code)
-    def register_action(self):
-        ''' Register an action to the transaction undo log
-        '''
+        if __debug__:
+            print >>hyperdb.DEBUG, '_doSaveJournal', entry
 
-    def commit(self):
-        ''' Commit the current transaction, start a new one
-        '''
+        db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
 
-    def rollback(self):
-        ''' Reverse all actions from the current transaction
-        '''
+        if db.has_key(nodeid):
+            s = db[nodeid]
+            l = marshal.loads(s)
+            l.append(entry)
+        else:
+            l = [entry]
+
+        db[nodeid] = marshal.dumps(l)
+        db.close()
 
 #
 #$Log: not supported by cvs2svn $
+#Revision 1.17  2002/04/03 05:54:31  richard
+#Fixed serialisation problem by moving the serialisation step out of the
+#hyperdb.Class (get, set) into the hyperdb.Database.
+#
+#Also fixed htmltemplate after the showid changes I made yesterday.
+#
+#Unit tests for all of the above written.
+#
+#Revision 1.16  2002/02/27 03:40:59  richard
+#Ran it through pychecker, made fixes
+#
+#Revision 1.15  2002/02/16 09:15:33  richard
+#forgot to patch bsddb backend too
+#
+#Revision 1.14  2002/01/22 07:21:13  richard
+#. fixed back_bsddb so it passed the journal tests
+#
+#... it didn't seem happy using the back_anydbm _open method, which is odd.
+#Yet another occurrance of whichdb not being able to recognise older bsddb
+#databases. Yadda yadda. Made the HYPERDBDEBUG stuff more sane in the
+#process.
+#
+#Revision 1.13  2001/12/10 22:20:01  richard
+#Enabled transaction support in the bsddb backend. It uses the anydbm code
+#where possible, only replacing methods where the db is opened (it uses the
+#btree opener specifically.)
+#Also cleaned up some change note generation.
+#Made the backends package work with pydoc too.
+#
+#Revision 1.12  2001/11/21 02:34:18  richard
+#Added a target version field to the extended issue schema
+#
+#Revision 1.11  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.10  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.9  2001/08/12 06:32:36  richard
+#using isinstance(blah, Foo) now instead of isFooType
+#
+#Revision 1.8  2001/08/07 00:24:42  richard
+#stupid typo
+#
+#Revision 1.7  2001/08/07 00:15:51  richard
+#Added the copyright/license notice to (nearly) all files at request of
+#Bizar Software.
+#
+#Revision 1.6  2001/07/30 02:36:23  richard
+#Handle non-existence of db files in the other backends (code from anydbm).
+#
+#Revision 1.5  2001/07/30 01:41:36  richard
+#Makes schema changes mucho easier.
+#
 #Revision 1.4  2001/07/23 08:25:33  richard
 #more handling of bad journals
 #