Mercurial > p > roundup > code
annotate roundup/backends/back_bsddb3.py @ 1244:8dd4f736370b
merge from maintenance branch
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Thu, 03 Oct 2002 06:56:30 +0000 |
| parents | c7119e74fcf8 |
| children | 8a908bbad1ef |
| rev | line source |
|---|---|
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
1 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
3 # This module is free software, and you may redistribute it and/or modify |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
4 # under the same terms as Python, so long as this copyright message and |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
5 # disclaimer are retained in their original form. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
6 # |
| 214 | 7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR |
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
10 # POSSIBILITY OF SUCH DAMAGE. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
11 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
196
diff
changeset
|
17 # |
|
1244
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1143
diff
changeset
|
18 #$Id: back_bsddb3.py,v 1.18 2002-10-03 06:56:29 richard Exp $ |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1143
diff
changeset
|
19 ''' |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1143
diff
changeset
|
20 This module defines a backend that saves the hyperdatabase in BSDDB3. |
|
8dd4f736370b
merge from maintenance branch
Richard Jones <richard@users.sourceforge.net>
parents:
1143
diff
changeset
|
21 ''' |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
22 |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
23 import bsddb3, os, marshal |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
24 from roundup import hyperdb, date |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
25 |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
26 # these classes are so similar, we just use the anydbm methods |
|
858
2dd862af72ee
all storage-specific code (ie. backend) is now implemented by the backends
Richard Jones <richard@users.sourceforge.net>
parents:
815
diff
changeset
|
27 from back_anydbm import Database, Class, FileClass, IssueClass |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
28 |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
29 # |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
30 # Now the database |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
31 # |
|
858
2dd862af72ee
all storage-specific code (ie. backend) is now implemented by the backends
Richard Jones <richard@users.sourceforge.net>
parents:
815
diff
changeset
|
32 class Database(Database): |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
33 """A database for storing records containing flexible data types.""" |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
34 # |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
35 # Class DBs |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
36 # |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
37 def clear(self): |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
38 for cn in self.classes.keys(): |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
39 db = os.path.join(self.dir, 'nodes.%s'%cn) |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
40 bsddb3.btopen(db, 'n') |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
41 db = os.path.join(self.dir, 'journals.%s'%cn) |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
42 bsddb3.btopen(db, 'n') |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
43 |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
44 def getclassdb(self, classname, mode='r'): |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
45 ''' grab a connection to the class db that will be used for |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
46 multiple actions |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
47 ''' |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
48 path = os.path.join(os.getcwd(), self.dir, 'nodes.%s'%classname) |
|
152
80c062af1b7d
Handle non-existence of db files in the other backends (code from anydbm).
Richard Jones <richard@users.sourceforge.net>
parents:
149
diff
changeset
|
49 if os.path.exists(path): |
|
80c062af1b7d
Handle non-existence of db files in the other backends (code from anydbm).
Richard Jones <richard@users.sourceforge.net>
parents:
149
diff
changeset
|
50 return bsddb3.btopen(path, mode) |
|
80c062af1b7d
Handle non-existence of db files in the other backends (code from anydbm).
Richard Jones <richard@users.sourceforge.net>
parents:
149
diff
changeset
|
51 else: |
|
815
f4ead46205ac
Was reopening the database with 'n'.
Richard Jones <richard@users.sourceforge.net>
parents:
726
diff
changeset
|
52 return bsddb3.btopen(path, 'c') |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
53 |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
54 def opendb(self, name, mode): |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
55 '''Low-level database opener that gets around anydbm/dbm |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
56 eccentricities. |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
57 ''' |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
58 if __debug__: |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
59 print >>hyperdb.DEBUG, self, 'opendb', (self, name, mode) |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
60 # determine which DB wrote the class file |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
61 path = os.path.join(os.getcwd(), self.dir, name) |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
62 if not os.path.exists(path): |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
63 if __debug__: |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
64 print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, 'c')"%path |
|
815
f4ead46205ac
Was reopening the database with 'n'.
Richard Jones <richard@users.sourceforge.net>
parents:
726
diff
changeset
|
65 return bsddb3.btopen(path, 'c') |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
66 |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
67 # open the database with the correct module |
|
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
68 if __debug__: |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
69 print >>hyperdb.DEBUG, "opendb bsddb3.open(%r, %r)"%(path, mode) |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
70 return bsddb3.btopen(path, mode) |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
71 |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
72 # |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
73 # Journal |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
74 # |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
75 def getjournal(self, classname, nodeid): |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
76 ''' get the journal for id |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
77 ''' |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
78 # attempt to open the journal - in some rare cases, the journal may |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
79 # not exist |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
80 try: |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
81 db = bsddb3.btopen(os.path.join(self.dir, 'journals.%s'%classname), |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
82 'r') |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
83 except bsddb3._db.DBNoSuchFileError: |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
84 raise IndexError, 'no such %s %s'%(classname, nodeid) |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
85 # more handling of bad journals |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
86 if not db.has_key(nodeid): |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
87 raise IndexError, 'no such %s %s'%(classname, nodeid) |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
88 journal = marshal.loads(db[nodeid]) |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
89 res = [] |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
90 for entry in journal: |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
91 (nodeid, date_stamp, user, action, params) = entry |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
92 date_obj = date.Date(date_stamp) |
|
726
e3d742c75ac3
Well whadya know, bsddb3 works again.
Richard Jones <richard@users.sourceforge.net>
parents:
524
diff
changeset
|
93 res.append((nodeid, date_obj, user, action, params)) |
|
64
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
94 db.close() |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
95 return res |
|
8bce2210ef81
bsddb3 implementation.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
diff
changeset
|
96 |
|
891
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
97 def getCachedJournalDB(self, classname): |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
98 ''' get the journal db, looking in our cache of databases for commit |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
99 ''' |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
100 # get the database handle |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
101 db_name = 'journals.%s'%classname |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
102 if self.databases.has_key(db_name): |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
103 return self.databases[db_name] |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
104 else: |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
105 db = bsddb3.btopen(os.path.join(self.dir, db_name), 'c') |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
106 self.databases[db_name] = db |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
107 return db |
|
974a4b94c5e3
Implemented the destroy() method needed by the session database...
Richard Jones <richard@users.sourceforge.net>
parents:
858
diff
changeset
|
108 |
