Code

3533b91c4802eb33aa7d70bf7e8043652482b883
[roundup.git] / roundup / backends / back_bsddb.py
1 #$Id: back_bsddb.py,v 1.1 2001-07-23 07:22:13 richard Exp $
3 import bsddb, os, cPickle
4 from roundup import hyperdb, date
6 #
7 # Now the database
8 #
9 class Database(hyperdb.Database):
10     """A database for storing records containing flexible data types."""
12     def __init__(self, storagelocator, journaltag=None):
13         """Open a hyperdatabase given a specifier to some storage.
15         The meaning of 'storagelocator' depends on the particular
16         implementation of the hyperdatabase.  It could be a file name,
17         a directory path, a socket descriptor for a connection to a
18         database over the network, etc.
20         The 'journaltag' is a token that will be attached to the journal
21         entries for any edits done on the database.  If 'journaltag' is
22         None, the database is opened in read-only mode: the Class.create(),
23         Class.set(), and Class.retire() methods are disabled.
24         """
25         self.dir, self.journaltag = storagelocator, journaltag
26         self.classes = {}
28     #
29     # Classes
30     #
31     def __getattr__(self, classname):
32         """A convenient way of calling self.getclass(classname)."""
33         return self.classes[classname]
35     def addclass(self, cl):
36         cn = cl.classname
37         if self.classes.has_key(cn):
38             raise ValueError, cn
39         self.classes[cn] = cl
41     def getclasses(self):
42         """Return a list of the names of all existing classes."""
43         l = self.classes.keys()
44         l.sort()
45         return l
47     def getclass(self, classname):
48         """Get the Class object representing a particular class.
50         If 'classname' is not a valid class name, a KeyError is raised.
51         """
52         return self.classes[classname]
54     #
55     # Class DBs
56     #
57     def clear(self):
58         for cn in self.classes.keys():
59             db = os.path.join(self.dir, 'nodes.%s'%cn)
60             bsddb.btopen(db, 'n')
61             db = os.path.join(self.dir, 'journals.%s'%cn)
62             bsddb.btopen(db, 'n')
64     def getclassdb(self, classname, mode='r'):
65         ''' grab a connection to the class db that will be used for
66             multiple actions
67         '''
68         path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname)
69         return bsddb.btopen(path, mode)
71     def addnode(self, classname, nodeid, node):
72         ''' add the specified node to its class's db
73         '''
74         db = self.getclassdb(classname, 'c')
75         db[nodeid] = cPickle.dumps(node, 1)
76         db.close()
77     setnode = addnode
79     def getnode(self, classname, nodeid, cldb=None):
80         ''' add the specified node to its class's db
81         '''
82         db = cldb or self.getclassdb(classname)
83         if not db.has_key(nodeid):
84             raise IndexError, nodeid
85         res = cPickle.loads(db[nodeid])
86         if not cldb: db.close()
87         return res
89     def hasnode(self, classname, nodeid, cldb=None):
90         ''' add the specified node to its class's db
91         '''
92         db = cldb or self.getclassdb(classname)
93         res = db.has_key(nodeid)
94         if not cldb: db.close()
95         return res
97     def countnodes(self, classname, cldb=None):
98         db = cldb or self.getclassdb(classname)
99         return len(db.keys())
100         if not cldb: db.close()
101         return res
103     def getnodeids(self, classname, cldb=None):
104         db = cldb or self.getclassdb(classname)
105         res = db.keys()
106         if not cldb: db.close()
107         return res
109     #
110     # Journal
111     #
112     def addjournal(self, classname, nodeid, action, params):
113         ''' Journal the Action
114         'action' may be:
116             'create' or 'set' -- 'params' is a dictionary of property values
117             'link' or 'unlink' -- 'params' is (classname, nodeid, propname)
118             'retire' -- 'params' is None
119         '''
120         entry = (nodeid, date.Date(), self.journaltag, action, params)
121         db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'c')
122         if db.has_key(nodeid):
123             s = db[nodeid]
124             l = cPickle.loads(db[nodeid])
125             l.append(entry)
126         else:
127             l = [entry]
128         db[nodeid] = cPickle.dumps(l)
129         db.close()
131     def getjournal(self, classname, nodeid):
132         ''' get the journal for id
133         '''
134         # attempt to open the journal - in some rare cases, the journal may
135         # not exist
136         try:
137             db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname),
138                 'r')
139         except bsddb.error, error:
140             if error.args[0] != 2: raise
141             return []
142         res = cPickle.loads(db[nodeid])
143         db.close()
144         return res
146     def close(self):
147         ''' Close the Database - we must release the circular refs so that
148             we can be del'ed and the underlying bsddb connections closed
149             cleanly.
150         '''
151         self.classes = None
154     #
155     # Basic transaction support
156     #
157     # TODO: well, write these methods (and then use them in other code)
158     def register_action(self):
159         ''' Register an action to the transaction undo log
160         '''
162     def commit(self):
163         ''' Commit the current transaction, start a new one
164         '''
166     def rollback(self):
167         ''' Reverse all actions from the current transaction
168         '''
171 #$Log: not supported by cvs2svn $
172 #Revision 1.1  2001/07/23 07:15:57  richard
173 #Moved the backends into the backends package. Anydbm hasn't been tested at all.
175 #Revision 1.1  2001/07/23 06:23:41  richard
176 #moved hyper_bsddb.py to the new backends package as bsddb.py
178 #Revision 1.2  2001/07/22 12:09:32  richard
179 #Final commit of Grande Splite
181 #Revision 1.1  2001/07/22 11:58:35  richard
182 #More Grande Splite