|
2 | 2 | import metakit |
3 | 3 | from sessions import Sessions |
4 | 4 | import re, marshal, os, sys, weakref, time, calendar |
5 | | -from roundup import indexer |
| 5 | +from roundup import indexer |
| 6 | +import locking |
6 | 7 |
|
7 | | -class Database(hyperdb.Database): |
| 8 | +_dbs = {} |
| 9 | + |
| 10 | +def Database(config, journaltag=None): |
| 11 | + db = _dbs.get(config.DATABASE, None) |
| 12 | + if db is None or db._db is None: |
| 13 | + db = _Database(config, journaltag) |
| 14 | + _dbs[config.DATABASE] = db |
| 15 | + return db |
| 16 | + |
| 17 | +class _Database(hyperdb.Database): |
8 | 18 | def __init__(self, config, journaltag=None): |
9 | 19 | self.config = config |
10 | 20 | self.journaltag = journaltag |
11 | 21 | self.classes = {} |
12 | 22 | self._classes = [] |
13 | 23 | self.dirty = 0 |
| 24 | + self.lockfile = None |
14 | 25 | self._db = self.__open() |
15 | 26 | self.indexer = Indexer(self.config.DATABASE, self._db) |
16 | 27 | self.sessions = Sessions(self.config) |
@@ -60,13 +71,13 @@ def clear(self): |
60 | 71 | for cl in self.classes.values(): |
61 | 72 | cl._clear() |
62 | 73 | def hasnode(self, classname, nodeid): |
63 | | - return self.getclass(clasname).hasnode(nodeid) |
| 74 | + return self.getclass(classname).hasnode(nodeid) |
64 | 75 | def pack(self, pack_before): |
65 | 76 | pass |
66 | 77 | def addclass(self, cl): |
67 | 78 | self.classes[cl.classname] = cl |
68 | | - if self.tables.find(name=cl.classname) < 0: |
69 | | - self.tables.append(name=cl.classname) |
| 79 | + if self.tables.find(name=cl.classname) < 0: |
| 80 | + self.tables.append(name=cl.classname) |
70 | 81 | def addjournal(self, tablenm, nodeid, action, params): |
71 | 82 | tblid = self.tables.find(name=tablenm) |
72 | 83 | if tblid == -1: |
@@ -102,12 +113,19 @@ def close(self): |
102 | 113 | for cl in self.classes.values(): |
103 | 114 | cl.db = None |
104 | 115 | self._db = None |
| 116 | + locking.release_lock(self.lockfile) |
| 117 | + del _dbs[self.config.DATABASE] |
| 118 | + self.lockfile.close() |
105 | 119 | self.classes = {} |
106 | 120 | self.indexer = None |
107 | 121 |
|
108 | 122 | # --- internal |
109 | 123 | def __open(self): |
110 | 124 | self.dbnm = db = os.path.join(self.config.DATABASE, 'tracker.mk4') |
| 125 | + lockfilenm = db[:-3]+'lck' |
| 126 | + self.lockfile = locking.acquire_lock(lockfilenm) |
| 127 | + self.lockfile.write(str(os.getpid())) |
| 128 | + self.lockfile.flush() |
111 | 129 | self.fastopen = 0 |
112 | 130 | if os.path.exists(db): |
113 | 131 | dbtm = os.path.getmtime(db) |
@@ -153,7 +171,8 @@ def __open(self): |
153 | 171 | class Class: |
154 | 172 | privateprops = None |
155 | 173 | def __init__(self, db, classname, **properties): |
156 | | - self.db = weakref.proxy(db) |
| 174 | + #self.db = weakref.proxy(db) |
| 175 | + self.db = db |
157 | 176 | self.classname = classname |
158 | 177 | self.keyname = None |
159 | 178 | self.ruprops = properties |
@@ -831,10 +850,8 @@ def __getview(self): |
831 | 850 | else: |
832 | 851 | mkprop = None |
833 | 852 | if mkprop is None: |
834 | | - print "%s missing prop %s (%s)" % (self.classname, nm, rutyp.__class__.__name__) |
835 | 853 | break |
836 | 854 | if _typmap[rutyp.__class__] != mkprop.type: |
837 | | - print "%s - prop %s (%s) has wrong mktyp (%s)" % (self.classname, nm, rutyp.__class__.__name__, mkprop.type) |
838 | 855 | break |
839 | 856 | else: |
840 | 857 | return view.ordered(1) |
@@ -923,9 +940,6 @@ def create(self, **propvalues): |
923 | 940 | newid = Class.create(self, **propvalues) |
924 | 941 | if not content: |
925 | 942 | return newid |
926 | | - if content.startswith('/tracker/download.php?'): |
927 | | - self.set(newid, content='http://sourceforge.net'+content) |
928 | | - return newid |
929 | 943 | nm = bnm = '%s%s' % (self.classname, newid) |
930 | 944 | sd = str(int(int(newid) / 1000)) |
931 | 945 | d = os.path.join(self.db.config.DATABASE, 'files', self.classname, sd) |
@@ -1071,7 +1085,3 @@ def save_index(self): |
1071 | 1085 | if self.changed: |
1072 | 1086 | self.db.commit() |
1073 | 1087 | self.changed = 0 |
1074 | | - |
1075 | | - |
1076 | | - |
1077 | | - |
|
0 commit comments