Code

adding the "minimal" template
[roundup.git] / roundup / backends / back_bsddb.py
index 7b4f96e47df01d1ad089d19eb3f15cf56432e3af..6f7aa9d7a15dd5a153282e3c9185b7878cab6109 100644 (file)
@@ -1,58 +1,36 @@
-#$Id: back_bsddb.py,v 1.2 2001-07-23 07:56:05 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.24 2002-09-13 08:20:11 richard Exp $
+'''
+This module defines a backend that saves the hyperdatabase in BSDDB.
+'''
 
 import bsddb, os, marshal
-# handle the older cPickle'd data
-import cPickle
 from roundup import hyperdb, date
 
+# these classes are so similar, we just use the anydbm methods
+from back_anydbm import Database, Class, FileClass, IssueClass
+
 #
 # Now the database
 #
-class Database(hyperdb.Database):
+class Database(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
     #
@@ -68,93 +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 res.keys():
-            if properties[key].isDateType:
-                res[key] = res[key].get_tuple()
-            elif properties[key].isIntervalType:
-                res[key] = res[key].get_tuple()
-        db[nodeid] = marshal.dumps(node, 1)
-        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
-        try:
-            res = marshal.loads(db[nodeid])
-            # convert the marshalled data to instances
-            properties = self.classes[classname].properties
-            for key in res.keys():
-                if properties[key].isDateType:
-                    res[key] = date.Date(res[key])
-                elif properties[key].isIntervalType:
-                    res[key] = date.Interval(res[key])
-        except ValueError, message:
-            if str(message) != 'bad marshal data':
-                raise
-            # handle the older cPickle'd data
-            res = cPickle.loads(db[nodeid])
-
-        if not cldb: db.close()
-        return res
+        if os.path.exists(path):
+            return bsddb.btopen(path, mode)
+        else:
+            return bsddb.btopen(path, 'c')
 
-    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, 'c')"%path
+            return bsddb.btopen(path, 'c')
+
+        # 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().journal_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
         '''
@@ -165,55 +82,28 @@ class Database(hyperdb.Database):
                 'r')
         except bsddb.error, error:
             if error.args[0] != 2: raise
-            return []
+            raise IndexError, 'no such %s %s'%(classname, nodeid)
+        # more handling of bad journals
+        if not db.has_key(nodeid):
+            raise IndexError, 'no such %s %s'%(classname, nodeid)
         journal = marshal.loads(db[nodeid])
         res = []
         for entry in journal:
-            (nodeid, date_stamp, self.journaltag, action, params) = entry
-            date_obj = date.Date(set=date_stamp)
-            res.append((nodeid, date_obj, self.journaltag, action, params))
+            (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 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
-
-
-    #
-    # 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
-        '''
-
-    def commit(self):
-        ''' Commit the current transaction, start a new one
-        '''
-
-    def rollback(self):
-        ''' Reverse all actions from the current transaction
+    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:
+            db = bsddb.btopen(os.path.join(self.dir, db_name), 'c')
+            self.databases[db_name] = db
+            return db
 
-#
-#$Log: not supported by cvs2svn $
-#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
-#