1 #$Id: hyper_bsddb.py,v 1.2 2001-07-22 12:09:32 richard Exp $
3 import bsddb, os, cPickle
4 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 db = bsddb.btopen(os.path.join(self.dir, 'journals.%s'%classname), 'r')
135 res = cPickle.loads(db[nodeid])
136 db.close()
137 return res
139 def close(self):
140 ''' Close the Database - we must release the circular refs so that
141 we can be del'ed and the underlying bsddb connections closed
142 cleanly.
143 '''
144 self.classes = None
147 #
148 # Basic transaction support
149 #
150 # TODO: well, write these methods (and then use them in other code)
151 def register_action(self):
152 ''' Register an action to the transaction undo log
153 '''
155 def commit(self):
156 ''' Commit the current transaction, start a new one
157 '''
159 def rollback(self):
160 ''' Reverse all actions from the current transaction
161 '''
163 #
164 #$Log: not supported by cvs2svn $
165 #Revision 1.1 2001/07/22 11:58:35 richard
166 #More Grande Splite
167 #