Mercurial > p > roundup > code
annotate roundup/backends/rdbms_common.py @ 1185:3b0735ef8207
fix to LRU cache
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Mon, 23 Sep 2002 07:15:32 +0000 |
| parents | 08a13a84ed43 |
| children | 84e40c9bcd99 |
| rev | line source |
|---|---|
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
1 # $Id: rdbms_common.py,v 1.12 2002-09-23 07:15:32 richard Exp $ |
| 1165 | 2 |
| 3 # standard python modules | |
| 4 import sys, os, time, re, errno, weakref, copy | |
| 5 | |
| 6 # roundup modules | |
| 7 from roundup import hyperdb, date, password, roundupdb, security | |
| 8 from roundup.hyperdb import String, Password, Date, Interval, Link, \ | |
| 9 Multilink, DatabaseError, Boolean, Number | |
| 10 | |
| 11 # support | |
| 12 from blobfiles import FileStorage | |
| 13 from roundup.indexer import Indexer | |
| 14 from sessions import Sessions | |
| 15 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
16 # number of rows to keep in memory |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
17 ROW_CACHE_SIZE = 100 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
18 |
| 1165 | 19 class Database(FileStorage, hyperdb.Database, roundupdb.Database): |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
20 ''' Wrapper around an SQL database that presents a hyperdb interface. |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
21 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
22 - some functionality is specific to the actual SQL database, hence |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
23 the sql_* methods that are NotImplemented |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
24 - we keep a cache of the latest ROW_CACHE_SIZE row fetches. |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
25 ''' |
| 1165 | 26 def __init__(self, config, journaltag=None): |
| 27 ''' Open the database and load the schema from it. | |
| 28 ''' | |
| 29 self.config, self.journaltag = config, journaltag | |
| 30 self.dir = config.DATABASE | |
| 31 self.classes = {} | |
| 32 self.indexer = Indexer(self.dir) | |
| 33 self.sessions = Sessions(self.config) | |
| 34 self.security = security.Security(self) | |
| 35 | |
| 36 # additional transaction support for external files and the like | |
| 37 self.transactions = [] | |
| 38 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
39 # keep a cache of the N most recently retrieved rows of any kind |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
40 # (classname, nodeid) = row |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
41 self.cache = {} |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
42 self.cache_lru = [] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
43 |
| 1165 | 44 # open a connection to the database, creating the "conn" attribute |
| 45 self.open_connection() | |
| 46 | |
|
1181
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
47 def clearCache(self): |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
48 self.cache = {} |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
49 self.cache_lru = [] |
|
49aebf5a8691
some speedups, some fixes to the benchmarking
Richard Jones <richard@users.sourceforge.net>
parents:
1176
diff
changeset
|
50 |
| 1165 | 51 def open_connection(self): |
| 52 ''' Open a connection to the database, creating it if necessary | |
| 53 ''' | |
| 54 raise NotImplemented | |
| 55 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
56 def sql(self, sql, args=None): |
| 1165 | 57 ''' Execute the sql with the optional args. |
| 58 ''' | |
| 59 if __debug__: | |
| 60 print >>hyperdb.DEBUG, (self, sql, args) | |
| 61 if args: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
62 self.cursor.execute(sql, args) |
| 1165 | 63 else: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
64 self.cursor.execute(sql) |
| 1165 | 65 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
66 def sql_fetchone(self): |
| 1165 | 67 ''' Fetch a single row. If there's nothing to fetch, return None. |
| 68 ''' | |
| 69 raise NotImplemented | |
| 70 | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
71 def sql_stringquote(self, value): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
72 ''' Quote the string so it's safe to put in the 'sql quotes' |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
73 ''' |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
74 return re.sub("'", "''", str(value)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
75 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
76 def save_dbschema(self, schema): |
| 1165 | 77 ''' Save the schema definition that the database currently implements |
| 78 ''' | |
| 79 raise NotImplemented | |
| 80 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
81 def load_dbschema(self): |
| 1165 | 82 ''' Load the schema definition that the database currently implements |
| 83 ''' | |
| 84 raise NotImplemented | |
| 85 | |
| 86 def post_init(self): | |
| 87 ''' Called once the schema initialisation has finished. | |
| 88 | |
| 89 We should now confirm that the schema defined by our "classes" | |
| 90 attribute actually matches the schema in the database. | |
| 91 ''' | |
| 92 # now detect changes in the schema | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
93 save = 0 |
| 1165 | 94 for classname, spec in self.classes.items(): |
| 95 if self.database_schema.has_key(classname): | |
| 96 dbspec = self.database_schema[classname] | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
97 if self.update_class(spec, dbspec): |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
98 self.database_schema[classname] = spec.schema() |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
99 save = 1 |
| 1165 | 100 else: |
| 101 self.create_class(spec) | |
| 102 self.database_schema[classname] = spec.schema() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
103 save = 1 |
| 1165 | 104 |
| 105 for classname in self.database_schema.keys(): | |
| 106 if not self.classes.has_key(classname): | |
| 107 self.drop_class(classname) | |
| 108 | |
| 109 # update the database version of the schema | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
110 if save: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
111 self.sql('delete from schema') |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
112 self.save_dbschema(self.database_schema) |
| 1165 | 113 |
| 114 # reindex the db if necessary | |
| 115 if self.indexer.should_reindex(): | |
| 116 self.reindex() | |
| 117 | |
| 118 # commit | |
| 119 self.conn.commit() | |
| 120 | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
121 # figure the "curuserid" |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
122 if self.journaltag is None: |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
123 self.curuserid = None |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
124 elif self.journaltag == 'admin': |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
125 # admin user may not exist, but always has ID 1 |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
126 self.curuserid = '1' |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
127 else: |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
128 self.curuserid = self.user.lookup(self.journaltag) |
|
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
129 |
| 1165 | 130 def reindex(self): |
| 131 for klass in self.classes.values(): | |
| 132 for nodeid in klass.list(): | |
| 133 klass.index(nodeid) | |
| 134 self.indexer.save_index() | |
| 135 | |
| 136 def determine_columns(self, properties): | |
| 137 ''' Figure the column names and multilink properties from the spec | |
| 138 | |
| 139 "properties" is a list of (name, prop) where prop may be an | |
| 140 instance of a hyperdb "type" _or_ a string repr of that type. | |
| 141 ''' | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
142 cols = ['_activity', '_creator', '_creation'] |
| 1165 | 143 mls = [] |
| 144 # add the multilinks separately | |
| 145 for col, prop in properties: | |
| 146 if isinstance(prop, Multilink): | |
| 147 mls.append(col) | |
| 148 elif isinstance(prop, type('')) and prop.find('Multilink') != -1: | |
| 149 mls.append(col) | |
| 150 else: | |
| 151 cols.append('_'+col) | |
| 152 cols.sort() | |
| 153 return cols, mls | |
| 154 | |
| 155 def update_class(self, spec, dbspec): | |
| 156 ''' Determine the differences between the current spec and the | |
| 157 database version of the spec, and update where necessary | |
| 158 ''' | |
| 159 spec_schema = spec.schema() | |
| 160 if spec_schema == dbspec: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
161 # no save needed for this one |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
162 return 0 |
| 1165 | 163 if __debug__: |
| 164 print >>hyperdb.DEBUG, 'update_class FIRING' | |
| 165 | |
| 166 # key property changed? | |
| 167 if dbspec[0] != spec_schema[0]: | |
| 168 if __debug__: | |
| 169 print >>hyperdb.DEBUG, 'update_class setting keyprop', `spec[0]` | |
| 170 # XXX turn on indexing for the key property | |
| 171 | |
| 172 # dict 'em up | |
| 173 spec_propnames,spec_props = [],{} | |
| 174 for propname,prop in spec_schema[1]: | |
| 175 spec_propnames.append(propname) | |
| 176 spec_props[propname] = prop | |
| 177 dbspec_propnames,dbspec_props = [],{} | |
| 178 for propname,prop in dbspec[1]: | |
| 179 dbspec_propnames.append(propname) | |
| 180 dbspec_props[propname] = prop | |
| 181 | |
| 182 # now compare | |
| 183 for propname in spec_propnames: | |
| 184 prop = spec_props[propname] | |
| 185 if dbspec_props.has_key(propname) and prop==dbspec_props[propname]: | |
| 186 continue | |
| 187 if __debug__: | |
| 188 print >>hyperdb.DEBUG, 'update_class ADD', (propname, prop) | |
| 189 | |
| 190 if not dbspec_props.has_key(propname): | |
| 191 # add the property | |
| 192 if isinstance(prop, Multilink): | |
| 193 # all we have to do here is create a new table, easy! | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
194 self.create_multilink_table(spec, propname) |
| 1165 | 195 continue |
| 196 | |
| 197 # no ALTER TABLE, so we: | |
| 198 # 1. pull out the data, including an extra None column | |
| 199 oldcols, x = self.determine_columns(dbspec[1]) | |
| 200 oldcols.append('id') | |
| 201 oldcols.append('__retired__') | |
| 202 cn = spec.classname | |
| 203 sql = 'select %s,%s from _%s'%(','.join(oldcols), self.arg, cn) | |
| 204 if __debug__: | |
| 205 print >>hyperdb.DEBUG, 'update_class', (self, sql, None) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
206 self.cursor.execute(sql, (None,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
207 olddata = self.cursor.fetchall() |
| 1165 | 208 |
| 209 # 2. drop the old table | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
210 self.cursor.execute('drop table _%s'%cn) |
| 1165 | 211 |
| 212 # 3. create the new table | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
213 cols, mls = self.create_class_table(spec) |
| 1165 | 214 # ensure the new column is last |
| 215 cols.remove('_'+propname) | |
| 216 assert oldcols == cols, "Column lists don't match!" | |
| 217 cols.append('_'+propname) | |
| 218 | |
| 219 # 4. populate with the data from step one | |
| 220 s = ','.join([self.arg for x in cols]) | |
| 221 scols = ','.join(cols) | |
| 222 sql = 'insert into _%s (%s) values (%s)'%(cn, scols, s) | |
| 223 | |
| 224 # GAH, nothing had better go wrong from here on in... but | |
| 225 # we have to commit the drop... | |
| 226 # XXX this isn't necessary in sqlite :( | |
| 227 self.conn.commit() | |
| 228 | |
| 229 # do the insert | |
| 230 for row in olddata: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
231 self.sql(sql, tuple(row)) |
| 1165 | 232 |
| 233 else: | |
| 234 # modify the property | |
| 235 if __debug__: | |
| 236 print >>hyperdb.DEBUG, 'update_class NOOP' | |
| 237 pass # NOOP in gadfly | |
| 238 | |
| 239 # and the other way - only worry about deletions here | |
| 240 for propname in dbspec_propnames: | |
| 241 prop = dbspec_props[propname] | |
| 242 if spec_props.has_key(propname): | |
| 243 continue | |
| 244 if __debug__: | |
| 245 print >>hyperdb.DEBUG, 'update_class REMOVE', `prop` | |
| 246 | |
| 247 # delete the property | |
| 248 if isinstance(prop, Multilink): | |
| 249 sql = 'drop table %s_%s'%(spec.classname, prop) | |
| 250 if __debug__: | |
| 251 print >>hyperdb.DEBUG, 'update_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
252 self.cursor.execute(sql) |
| 1165 | 253 else: |
| 254 # no ALTER TABLE, so we: | |
| 255 # 1. pull out the data, excluding the removed column | |
| 256 oldcols, x = self.determine_columns(spec.properties.items()) | |
| 257 oldcols.append('id') | |
| 258 oldcols.append('__retired__') | |
| 259 # remove the missing column | |
| 260 oldcols.remove('_'+propname) | |
| 261 cn = spec.classname | |
| 262 sql = 'select %s from _%s'%(','.join(oldcols), cn) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
263 self.cursor.execute(sql, (None,)) |
| 1165 | 264 olddata = sql.fetchall() |
| 265 | |
| 266 # 2. drop the old table | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
267 self.cursor.execute('drop table _%s'%cn) |
| 1165 | 268 |
| 269 # 3. create the new table | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
270 cols, mls = self.create_class_table(self, spec) |
| 1165 | 271 assert oldcols != cols, "Column lists don't match!" |
| 272 | |
| 273 # 4. populate with the data from step one | |
| 274 qs = ','.join([self.arg for x in cols]) | |
| 275 sql = 'insert into _%s values (%s)'%(cn, s) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
276 self.cursor.execute(sql, olddata) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
277 return 1 |
| 1165 | 278 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
279 def create_class_table(self, spec): |
| 1165 | 280 ''' create the class table for the given spec |
| 281 ''' | |
| 282 cols, mls = self.determine_columns(spec.properties.items()) | |
| 283 | |
| 284 # add on our special columns | |
| 285 cols.append('id') | |
| 286 cols.append('__retired__') | |
| 287 | |
| 288 # create the base table | |
| 289 scols = ','.join(['%s varchar'%x for x in cols]) | |
| 290 sql = 'create table _%s (%s)'%(spec.classname, scols) | |
| 291 if __debug__: | |
| 292 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
293 self.cursor.execute(sql) |
| 1165 | 294 |
| 295 return cols, mls | |
| 296 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
297 def create_journal_table(self, spec): |
| 1165 | 298 ''' create the journal table for a class given the spec and |
| 299 already-determined cols | |
| 300 ''' | |
| 301 # journal table | |
| 302 cols = ','.join(['%s varchar'%x | |
| 303 for x in 'nodeid date tag action params'.split()]) | |
| 304 sql = 'create table %s__journal (%s)'%(spec.classname, cols) | |
| 305 if __debug__: | |
| 306 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
307 self.cursor.execute(sql) |
| 1165 | 308 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
309 def create_multilink_table(self, spec, ml): |
| 1165 | 310 ''' Create a multilink table for the "ml" property of the class |
| 311 given by the spec | |
| 312 ''' | |
| 313 sql = 'create table %s_%s (linkid varchar, nodeid varchar)'%( | |
| 314 spec.classname, ml) | |
| 315 if __debug__: | |
| 316 print >>hyperdb.DEBUG, 'create_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
317 self.cursor.execute(sql) |
| 1165 | 318 |
| 319 def create_class(self, spec): | |
| 320 ''' Create a database table according to the given spec. | |
| 321 ''' | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
322 cols, mls = self.create_class_table(spec) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
323 self.create_journal_table(spec) |
| 1165 | 324 |
| 325 # now create the multilink tables | |
| 326 for ml in mls: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
327 self.create_multilink_table(spec, ml) |
| 1165 | 328 |
| 329 # ID counter | |
| 330 sql = 'insert into ids (name, num) values (%s,%s)'%(self.arg, self.arg) | |
| 331 vals = (spec.classname, 1) | |
| 332 if __debug__: | |
| 333 print >>hyperdb.DEBUG, 'create_class', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
334 self.cursor.execute(sql, vals) |
| 1165 | 335 |
| 336 def drop_class(self, spec): | |
| 337 ''' Drop the given table from the database. | |
| 338 | |
| 339 Drop the journal and multilink tables too. | |
| 340 ''' | |
| 341 # figure the multilinks | |
| 342 mls = [] | |
| 343 for col, prop in spec.properties.items(): | |
| 344 if isinstance(prop, Multilink): | |
| 345 mls.append(col) | |
| 346 | |
| 347 sql = 'drop table _%s'%spec.classname | |
| 348 if __debug__: | |
| 349 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
350 self.cursor.execute(sql) |
| 1165 | 351 |
| 352 sql = 'drop table %s__journal'%spec.classname | |
| 353 if __debug__: | |
| 354 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
355 self.cursor.execute(sql) |
| 1165 | 356 |
| 357 for ml in mls: | |
| 358 sql = 'drop table %s_%s'%(spec.classname, ml) | |
| 359 if __debug__: | |
| 360 print >>hyperdb.DEBUG, 'drop_class', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
361 self.cursor.execute(sql) |
| 1165 | 362 |
| 363 # | |
| 364 # Classes | |
| 365 # | |
| 366 def __getattr__(self, classname): | |
| 367 ''' A convenient way of calling self.getclass(classname). | |
| 368 ''' | |
| 369 if self.classes.has_key(classname): | |
| 370 if __debug__: | |
| 371 print >>hyperdb.DEBUG, '__getattr__', (self, classname) | |
| 372 return self.classes[classname] | |
| 373 raise AttributeError, classname | |
| 374 | |
| 375 def addclass(self, cl): | |
| 376 ''' Add a Class to the hyperdatabase. | |
| 377 ''' | |
| 378 if __debug__: | |
| 379 print >>hyperdb.DEBUG, 'addclass', (self, cl) | |
| 380 cn = cl.classname | |
| 381 if self.classes.has_key(cn): | |
| 382 raise ValueError, cn | |
| 383 self.classes[cn] = cl | |
| 384 | |
| 385 def getclasses(self): | |
| 386 ''' Return a list of the names of all existing classes. | |
| 387 ''' | |
| 388 if __debug__: | |
| 389 print >>hyperdb.DEBUG, 'getclasses', (self,) | |
| 390 l = self.classes.keys() | |
| 391 l.sort() | |
| 392 return l | |
| 393 | |
| 394 def getclass(self, classname): | |
| 395 '''Get the Class object representing a particular class. | |
| 396 | |
| 397 If 'classname' is not a valid class name, a KeyError is raised. | |
| 398 ''' | |
| 399 if __debug__: | |
| 400 print >>hyperdb.DEBUG, 'getclass', (self, classname) | |
| 401 try: | |
| 402 return self.classes[classname] | |
| 403 except KeyError: | |
| 404 raise KeyError, 'There is no class called "%s"'%classname | |
| 405 | |
| 406 def clear(self): | |
| 407 ''' Delete all database contents. | |
| 408 | |
| 409 Note: I don't commit here, which is different behaviour to the | |
| 410 "nuke from orbit" behaviour in the *dbms. | |
| 411 ''' | |
| 412 if __debug__: | |
| 413 print >>hyperdb.DEBUG, 'clear', (self,) | |
| 414 for cn in self.classes.keys(): | |
| 415 sql = 'delete from _%s'%cn | |
| 416 if __debug__: | |
| 417 print >>hyperdb.DEBUG, 'clear', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
418 self.cursor.execute(sql) |
| 1165 | 419 |
| 420 # | |
| 421 # Node IDs | |
| 422 # | |
| 423 def newid(self, classname): | |
| 424 ''' Generate a new id for the given class | |
| 425 ''' | |
| 426 # get the next ID | |
| 427 sql = 'select num from ids where name=%s'%self.arg | |
| 428 if __debug__: | |
| 429 print >>hyperdb.DEBUG, 'newid', (self, sql, classname) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
430 self.cursor.execute(sql, (classname, )) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
431 newid = self.cursor.fetchone()[0] |
| 1165 | 432 |
| 433 # update the counter | |
| 434 sql = 'update ids set num=%s where name=%s'%(self.arg, self.arg) | |
| 435 vals = (int(newid)+1, classname) | |
| 436 if __debug__: | |
| 437 print >>hyperdb.DEBUG, 'newid', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
438 self.cursor.execute(sql, vals) |
| 1165 | 439 |
| 440 # return as string | |
| 441 return str(newid) | |
| 442 | |
| 443 def setid(self, classname, setid): | |
| 444 ''' Set the id counter: used during import of database | |
| 445 ''' | |
| 446 sql = 'update ids set num=%s where name=%s'%(self.arg, self.arg) | |
|
1171
8784a11f8c2f
fix to setid
Richard Jones <richard@users.sourceforge.net>
parents:
1170
diff
changeset
|
447 vals = (setid, classname) |
| 1165 | 448 if __debug__: |
| 449 print >>hyperdb.DEBUG, 'setid', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
450 self.cursor.execute(sql, vals) |
| 1165 | 451 |
| 452 # | |
| 453 # Nodes | |
| 454 # | |
| 455 | |
| 456 def addnode(self, classname, nodeid, node): | |
| 457 ''' Add the specified node to its class's db. | |
| 458 ''' | |
| 459 if __debug__: | |
| 460 print >>hyperdb.DEBUG, 'addnode', (self, classname, nodeid, node) | |
| 461 # gadfly requires values for all non-multilink columns | |
| 462 cl = self.classes[classname] | |
| 463 cols, mls = self.determine_columns(cl.properties.items()) | |
| 464 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
465 # add the special props |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
466 node = node.copy() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
467 node['creation'] = node['activity'] = date.Date() |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
468 node['creator'] = self.curuserid |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
469 |
| 1165 | 470 # default the non-multilink columns |
| 471 for col, prop in cl.properties.items(): | |
| 472 if not isinstance(col, Multilink): | |
| 473 if not node.has_key(col): | |
| 474 node[col] = None | |
| 475 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
476 # clear this node out of the cache if it's in there |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
477 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
478 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
479 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
480 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
481 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
482 # make the node data safe for the DB |
| 1165 | 483 node = self.serialise(classname, node) |
| 484 | |
| 485 # make sure the ordering is correct for column name -> column value | |
| 486 vals = tuple([node[col[1:]] for col in cols]) + (nodeid, 0) | |
| 487 s = ','.join([self.arg for x in cols]) + ',%s,%s'%(self.arg, self.arg) | |
| 488 cols = ','.join(cols) + ',id,__retired__' | |
| 489 | |
| 490 # perform the inserts | |
| 491 sql = 'insert into _%s (%s) values (%s)'%(classname, cols, s) | |
| 492 if __debug__: | |
| 493 print >>hyperdb.DEBUG, 'addnode', (self, sql, vals) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
494 self.cursor.execute(sql, vals) |
| 1165 | 495 |
| 496 # insert the multilink rows | |
| 497 for col in mls: | |
| 498 t = '%s_%s'%(classname, col) | |
| 499 for entry in node[col]: | |
| 500 sql = 'insert into %s (linkid, nodeid) values (%s,%s)'%(t, | |
| 501 self.arg, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
502 self.sql(sql, (entry, nodeid)) |
| 1165 | 503 |
| 504 # make sure we do the commit-time extra stuff for this node | |
| 505 self.transactions.append((self.doSaveNode, (classname, nodeid, node))) | |
| 506 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
507 def setnode(self, classname, nodeid, values, multilink_changes): |
| 1165 | 508 ''' Change the specified node. |
| 509 ''' | |
| 510 if __debug__: | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
511 print >>hyperdb.DEBUG, 'setnode', (self, classname, nodeid, values) |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
512 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
513 # clear this node out of the cache if it's in there |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
514 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
515 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
516 del self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
517 self.cache_lru.remove(key) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
518 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
519 # add the special props |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
520 values = values.copy() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
521 values['activity'] = date.Date() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
522 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
523 # make db-friendly |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
524 values = self.serialise(classname, values) |
| 1165 | 525 |
| 526 cl = self.classes[classname] | |
| 527 cols = [] | |
| 528 mls = [] | |
| 529 # add the multilinks separately | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
530 props = cl.getprops() |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
531 for col in values.keys(): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
532 prop = props[col] |
| 1165 | 533 if isinstance(prop, Multilink): |
| 534 mls.append(col) | |
| 535 else: | |
| 536 cols.append('_'+col) | |
| 537 cols.sort() | |
| 538 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
539 # if there's any updates to regular columns, do them |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
540 if cols: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
541 # make sure the ordering is correct for column name -> column value |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
542 sqlvals = tuple([values[col[1:]] for col in cols]) + (nodeid,) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
543 s = ','.join(['%s=%s'%(x, self.arg) for x in cols]) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
544 cols = ','.join(cols) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
545 |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
546 # perform the update |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
547 sql = 'update _%s set %s where id=%s'%(classname, s, self.arg) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
548 if __debug__: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
549 print >>hyperdb.DEBUG, 'setnode', (self, sql, sqlvals) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
550 self.cursor.execute(sql, sqlvals) |
| 1165 | 551 |
| 552 # now the fun bit, updating the multilinks ;) | |
| 553 for col, (add, remove) in multilink_changes.items(): | |
| 554 tn = '%s_%s'%(classname, col) | |
| 555 if add: | |
| 556 sql = 'insert into %s (nodeid, linkid) values (%s,%s)'%(tn, | |
| 557 self.arg, self.arg) | |
| 558 for addid in add: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
559 self.sql(sql, (nodeid, addid)) |
| 1165 | 560 if remove: |
| 561 sql = 'delete from %s where nodeid=%s and linkid=%s'%(tn, | |
| 562 self.arg, self.arg) | |
| 563 for removeid in remove: | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
564 self.sql(sql, (nodeid, removeid)) |
| 1165 | 565 |
| 566 # make sure we do the commit-time extra stuff for this node | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
567 self.transactions.append((self.doSaveNode, (classname, nodeid, values))) |
| 1165 | 568 |
| 569 def getnode(self, classname, nodeid): | |
| 570 ''' Get a node from the database. | |
| 571 ''' | |
| 572 if __debug__: | |
| 573 print >>hyperdb.DEBUG, 'getnode', (self, classname, nodeid) | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
574 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
575 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
576 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
577 if self.cache.has_key(key): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
578 # push us back to the top of the LRU |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
579 self.cache_lru.remove(key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
580 self.cache_lru.insert(0, key) |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
581 # return the cached information |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
582 return self.cache[key] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
583 |
| 1165 | 584 # figure the columns we're fetching |
| 585 cl = self.classes[classname] | |
| 586 cols, mls = self.determine_columns(cl.properties.items()) | |
| 587 scols = ','.join(cols) | |
| 588 | |
| 589 # perform the basic property fetch | |
| 590 sql = 'select %s from _%s where id=%s'%(scols, classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
591 self.sql(sql, (nodeid,)) |
| 1165 | 592 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
593 values = self.sql_fetchone() |
| 1165 | 594 if values is None: |
| 595 raise IndexError, 'no such %s node %s'%(classname, nodeid) | |
| 596 | |
| 597 # make up the node | |
| 598 node = {} | |
| 599 for col in range(len(cols)): | |
| 600 node[cols[col][1:]] = values[col] | |
| 601 | |
| 602 # now the multilinks | |
| 603 for col in mls: | |
| 604 # get the link ids | |
| 605 sql = 'select linkid from %s_%s where nodeid=%s'%(classname, col, | |
| 606 self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
607 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 608 # extract the first column from the result |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
609 node[col] = [x[0] for x in self.cursor.fetchall()] |
| 1165 | 610 |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
611 # un-dbificate the node data |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
612 node = self.unserialise(classname, node) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
613 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
614 # save off in the cache |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
615 key = (classname, nodeid) |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
616 self.cache[key] = node |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
617 # update the LRU |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
618 self.cache_lru.insert(0, key) |
|
1185
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
619 if len(self.cache_lru) > ROW_CACHE_SIZE: |
|
3b0735ef8207
fix to LRU cache
Richard Jones <richard@users.sourceforge.net>
parents:
1183
diff
changeset
|
620 del self.cache[self.cache_lru.pop()] |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
621 |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
622 return node |
| 1165 | 623 |
| 624 def destroynode(self, classname, nodeid): | |
| 625 '''Remove a node from the database. Called exclusively by the | |
| 626 destroy() method on Class. | |
| 627 ''' | |
| 628 if __debug__: | |
| 629 print >>hyperdb.DEBUG, 'destroynode', (self, classname, nodeid) | |
| 630 | |
| 631 # make sure the node exists | |
| 632 if not self.hasnode(classname, nodeid): | |
| 633 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 634 | |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
635 # see if we have this node cached |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
636 if self.cache.has_key((classname, nodeid)): |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
637 del self.cache[(classname, nodeid)] |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
638 |
| 1165 | 639 # see if there's any obvious commit actions that we should get rid of |
| 640 for entry in self.transactions[:]: | |
| 641 if entry[1][:2] == (classname, nodeid): | |
| 642 self.transactions.remove(entry) | |
| 643 | |
| 644 # now do the SQL | |
| 645 sql = 'delete from _%s where id=%s'%(classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
646 self.sql(sql, (nodeid,)) |
| 1165 | 647 |
| 648 # remove from multilnks | |
| 649 cl = self.getclass(classname) | |
| 650 x, mls = self.determine_columns(cl.properties.items()) | |
| 651 for col in mls: | |
| 652 # get the link ids | |
| 653 sql = 'delete from %s_%s where nodeid=%s'%(classname, col, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
654 self.cursor.execute(sql, (nodeid,)) |
| 1165 | 655 |
| 656 # remove journal entries | |
| 657 sql = 'delete from %s__journal where nodeid=%s'%(classname, self.arg) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
658 self.sql(sql, (nodeid,)) |
| 1165 | 659 |
| 660 def serialise(self, classname, node): | |
| 661 '''Copy the node contents, converting non-marshallable data into | |
| 662 marshallable data. | |
| 663 ''' | |
| 664 if __debug__: | |
| 665 print >>hyperdb.DEBUG, 'serialise', classname, node | |
| 666 properties = self.getclass(classname).getprops() | |
| 667 d = {} | |
| 668 for k, v in node.items(): | |
| 669 # if the property doesn't exist, or is the "retired" flag then | |
| 670 # it won't be in the properties dict | |
| 671 if not properties.has_key(k): | |
| 672 d[k] = v | |
| 673 continue | |
| 674 | |
| 675 # get the property spec | |
| 676 prop = properties[k] | |
| 677 | |
| 678 if isinstance(prop, Password): | |
| 679 d[k] = str(v) | |
| 680 elif isinstance(prop, Date) and v is not None: | |
| 681 d[k] = v.serialise() | |
| 682 elif isinstance(prop, Interval) and v is not None: | |
| 683 d[k] = v.serialise() | |
| 684 else: | |
| 685 d[k] = v | |
| 686 return d | |
| 687 | |
| 688 def unserialise(self, classname, node): | |
| 689 '''Decode the marshalled node data | |
| 690 ''' | |
| 691 if __debug__: | |
| 692 print >>hyperdb.DEBUG, 'unserialise', classname, node | |
| 693 properties = self.getclass(classname).getprops() | |
| 694 d = {} | |
| 695 for k, v in node.items(): | |
| 696 # if the property doesn't exist, or is the "retired" flag then | |
| 697 # it won't be in the properties dict | |
| 698 if not properties.has_key(k): | |
| 699 d[k] = v | |
| 700 continue | |
| 701 | |
| 702 # get the property spec | |
| 703 prop = properties[k] | |
| 704 | |
| 705 if isinstance(prop, Date) and v is not None: | |
| 706 d[k] = date.Date(v) | |
| 707 elif isinstance(prop, Interval) and v is not None: | |
| 708 d[k] = date.Interval(v) | |
| 709 elif isinstance(prop, Password): | |
| 710 p = password.Password() | |
| 711 p.unpack(v) | |
| 712 d[k] = p | |
| 713 else: | |
| 714 d[k] = v | |
| 715 return d | |
| 716 | |
| 717 def hasnode(self, classname, nodeid): | |
| 718 ''' Determine if the database has a given node. | |
| 719 ''' | |
| 720 sql = 'select count(*) from _%s where id=%s'%(classname, self.arg) | |
| 721 if __debug__: | |
| 722 print >>hyperdb.DEBUG, 'hasnode', (self, sql, nodeid) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
723 self.cursor.execute(sql, (nodeid,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
724 return int(self.cursor.fetchone()[0]) |
| 1165 | 725 |
| 726 def countnodes(self, classname): | |
| 727 ''' Count the number of nodes that exist for a particular Class. | |
| 728 ''' | |
| 729 sql = 'select count(*) from _%s'%classname | |
| 730 if __debug__: | |
| 731 print >>hyperdb.DEBUG, 'countnodes', (self, sql) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
732 self.cursor.execute(sql) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
733 return self.cursor.fetchone()[0] |
| 1165 | 734 |
| 735 def getnodeids(self, classname, retired=0): | |
| 736 ''' Retrieve all the ids of the nodes for a particular Class. | |
| 737 | |
| 738 Set retired=None to get all nodes. Otherwise it'll get all the | |
| 739 retired or non-retired nodes, depending on the flag. | |
| 740 ''' | |
| 741 # flip the sense of the flag if we don't want all of them | |
| 742 if retired is not None: | |
| 743 retired = not retired | |
| 744 sql = 'select id from _%s where __retired__ <> %s'%(classname, self.arg) | |
| 745 if __debug__: | |
| 746 print >>hyperdb.DEBUG, 'getnodeids', (self, sql, retired) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
747 self.cursor.execute(sql, (retired,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
748 return [x[0] for x in self.cursor.fetchall()] |
| 1165 | 749 |
| 750 def addjournal(self, classname, nodeid, action, params, creator=None, | |
| 751 creation=None): | |
| 752 ''' Journal the Action | |
| 753 'action' may be: | |
| 754 | |
| 755 'create' or 'set' -- 'params' is a dictionary of property values | |
| 756 'link' or 'unlink' -- 'params' is (classname, nodeid, propname) | |
| 757 'retire' -- 'params' is None | |
| 758 ''' | |
| 759 # serialise the parameters now if necessary | |
| 760 if isinstance(params, type({})): | |
| 761 if action in ('set', 'create'): | |
| 762 params = self.serialise(classname, params) | |
| 763 | |
| 764 # handle supply of the special journalling parameters (usually | |
| 765 # supplied on importing an existing database) | |
| 766 if creator: | |
| 767 journaltag = creator | |
| 768 else: | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
769 journaltag = self.curuserid |
| 1165 | 770 if creation: |
| 771 journaldate = creation.serialise() | |
| 772 else: | |
| 773 journaldate = date.Date().serialise() | |
| 774 | |
| 775 # create the journal entry | |
| 776 cols = ','.join('nodeid date tag action params'.split()) | |
| 777 | |
| 778 if __debug__: | |
| 779 print >>hyperdb.DEBUG, 'addjournal', (nodeid, journaldate, | |
| 780 journaltag, action, params) | |
| 781 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
782 self.save_journal(classname, cols, nodeid, journaldate, |
| 1165 | 783 journaltag, action, params) |
| 784 | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
785 def save_journal(self, classname, cols, nodeid, journaldate, |
| 1165 | 786 journaltag, action, params): |
| 787 ''' Save the journal entry to the database | |
| 788 ''' | |
| 789 raise NotImplemented | |
| 790 | |
| 791 def getjournal(self, classname, nodeid): | |
| 792 ''' get the journal for id | |
| 793 ''' | |
| 794 # make sure the node exists | |
| 795 if not self.hasnode(classname, nodeid): | |
| 796 raise IndexError, '%s has no node %s'%(classname, nodeid) | |
| 797 | |
| 798 cols = ','.join('nodeid date tag action params'.split()) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
799 return self.load_journal(classname, cols, nodeid) |
| 1165 | 800 |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
801 def load_journal(self, classname, cols, nodeid): |
| 1165 | 802 ''' Load the journal from the database |
| 803 ''' | |
| 804 raise NotImplemented | |
| 805 | |
| 806 def pack(self, pack_before): | |
| 807 ''' Delete all journal entries except "create" before 'pack_before'. | |
| 808 ''' | |
| 809 # get a 'yyyymmddhhmmss' version of the date | |
| 810 date_stamp = pack_before.serialise() | |
| 811 | |
| 812 # do the delete | |
| 813 for classname in self.classes.keys(): | |
| 814 sql = "delete from %s__journal where date<%s and "\ | |
| 815 "action<>'create'"%(classname, self.arg) | |
| 816 if __debug__: | |
| 817 print >>hyperdb.DEBUG, 'pack', (self, sql, date_stamp) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
818 self.cursor.execute(sql, (date_stamp,)) |
| 1165 | 819 |
| 820 def sql_commit(self): | |
| 821 ''' Actually commit to the database. | |
| 822 ''' | |
| 823 self.conn.commit() | |
| 824 | |
| 825 def commit(self): | |
| 826 ''' Commit the current transactions. | |
| 827 | |
| 828 Save all data changed since the database was opened or since the | |
| 829 last commit() or rollback(). | |
| 830 ''' | |
| 831 if __debug__: | |
| 832 print >>hyperdb.DEBUG, 'commit', (self,) | |
| 833 | |
| 834 # commit the database | |
| 835 self.sql_commit() | |
| 836 | |
| 837 # now, do all the other transaction stuff | |
| 838 reindex = {} | |
| 839 for method, args in self.transactions: | |
| 840 reindex[method(*args)] = 1 | |
| 841 | |
| 842 # reindex the nodes that request it | |
| 843 for classname, nodeid in filter(None, reindex.keys()): | |
| 844 print >>hyperdb.DEBUG, 'commit.reindex', (classname, nodeid) | |
| 845 self.getclass(classname).index(nodeid) | |
| 846 | |
| 847 # save the indexer state | |
| 848 self.indexer.save_index() | |
| 849 | |
| 850 # clear out the transactions | |
| 851 self.transactions = [] | |
| 852 | |
| 853 def rollback(self): | |
| 854 ''' Reverse all actions from the current transaction. | |
| 855 | |
| 856 Undo all the changes made since the database was opened or the last | |
| 857 commit() or rollback() was performed. | |
| 858 ''' | |
| 859 if __debug__: | |
| 860 print >>hyperdb.DEBUG, 'rollback', (self,) | |
| 861 | |
| 862 # roll back | |
| 863 self.conn.rollback() | |
| 864 | |
| 865 # roll back "other" transaction stuff | |
| 866 for method, args in self.transactions: | |
| 867 # delete temporary files | |
| 868 if method == self.doStoreFile: | |
| 869 self.rollbackStoreFile(*args) | |
| 870 self.transactions = [] | |
| 871 | |
| 872 def doSaveNode(self, classname, nodeid, node): | |
| 873 ''' dummy that just generates a reindex event | |
| 874 ''' | |
| 875 # return the classname, nodeid so we reindex this content | |
| 876 return (classname, nodeid) | |
| 877 | |
| 878 def close(self): | |
| 879 ''' Close off the connection. | |
| 880 ''' | |
| 881 self.conn.close() | |
| 882 | |
| 883 # | |
| 884 # The base Class class | |
| 885 # | |
| 886 class Class(hyperdb.Class): | |
| 887 ''' The handle to a particular class of nodes in a hyperdatabase. | |
| 888 | |
| 889 All methods except __repr__ and getnode must be implemented by a | |
| 890 concrete backend Class. | |
| 891 ''' | |
| 892 | |
| 893 def __init__(self, db, classname, **properties): | |
| 894 '''Create a new class with a given name and property specification. | |
| 895 | |
| 896 'classname' must not collide with the name of an existing class, | |
| 897 or a ValueError is raised. The keyword arguments in 'properties' | |
| 898 must map names to property objects, or a TypeError is raised. | |
| 899 ''' | |
| 900 if (properties.has_key('creation') or properties.has_key('activity') | |
| 901 or properties.has_key('creator')): | |
| 902 raise ValueError, '"creation", "activity" and "creator" are '\ | |
| 903 'reserved' | |
| 904 | |
| 905 self.classname = classname | |
| 906 self.properties = properties | |
| 907 self.db = weakref.proxy(db) # use a weak ref to avoid circularity | |
| 908 self.key = '' | |
| 909 | |
| 910 # should we journal changes (default yes) | |
| 911 self.do_journal = 1 | |
| 912 | |
| 913 # do the db-related init stuff | |
| 914 db.addclass(self) | |
| 915 | |
| 916 self.auditors = {'create': [], 'set': [], 'retire': []} | |
| 917 self.reactors = {'create': [], 'set': [], 'retire': []} | |
| 918 | |
| 919 def schema(self): | |
| 920 ''' A dumpable version of the schema that we can store in the | |
| 921 database | |
| 922 ''' | |
| 923 return (self.key, [(x, repr(y)) for x,y in self.properties.items()]) | |
| 924 | |
| 925 def enableJournalling(self): | |
| 926 '''Turn journalling on for this class | |
| 927 ''' | |
| 928 self.do_journal = 1 | |
| 929 | |
| 930 def disableJournalling(self): | |
| 931 '''Turn journalling off for this class | |
| 932 ''' | |
| 933 self.do_journal = 0 | |
| 934 | |
| 935 # Editing nodes: | |
| 936 def create(self, **propvalues): | |
| 937 ''' Create a new node of this class and return its id. | |
| 938 | |
| 939 The keyword arguments in 'propvalues' map property names to values. | |
| 940 | |
| 941 The values of arguments must be acceptable for the types of their | |
| 942 corresponding properties or a TypeError is raised. | |
| 943 | |
| 944 If this class has a key property, it must be present and its value | |
| 945 must not collide with other key strings or a ValueError is raised. | |
| 946 | |
| 947 Any other properties on this class that are missing from the | |
| 948 'propvalues' dictionary are set to None. | |
| 949 | |
| 950 If an id in a link or multilink property does not refer to a valid | |
| 951 node, an IndexError is raised. | |
| 952 ''' | |
| 953 if propvalues.has_key('id'): | |
| 954 raise KeyError, '"id" is reserved' | |
| 955 | |
| 956 if self.db.journaltag is None: | |
| 957 raise DatabaseError, 'Database open read-only' | |
| 958 | |
| 959 if propvalues.has_key('creation') or propvalues.has_key('activity'): | |
| 960 raise KeyError, '"creation" and "activity" are reserved' | |
| 961 | |
| 962 self.fireAuditors('create', None, propvalues) | |
| 963 | |
| 964 # new node's id | |
| 965 newid = self.db.newid(self.classname) | |
| 966 | |
| 967 # validate propvalues | |
| 968 num_re = re.compile('^\d+$') | |
| 969 for key, value in propvalues.items(): | |
| 970 if key == self.key: | |
| 971 try: | |
| 972 self.lookup(value) | |
| 973 except KeyError: | |
| 974 pass | |
| 975 else: | |
| 976 raise ValueError, 'node with key "%s" exists'%value | |
| 977 | |
| 978 # try to handle this property | |
| 979 try: | |
| 980 prop = self.properties[key] | |
| 981 except KeyError: | |
| 982 raise KeyError, '"%s" has no property "%s"'%(self.classname, | |
| 983 key) | |
| 984 | |
| 985 if value is not None and isinstance(prop, Link): | |
| 986 if type(value) != type(''): | |
| 987 raise ValueError, 'link value must be String' | |
| 988 link_class = self.properties[key].classname | |
| 989 # if it isn't a number, it's a key | |
| 990 if not num_re.match(value): | |
| 991 try: | |
| 992 value = self.db.classes[link_class].lookup(value) | |
| 993 except (TypeError, KeyError): | |
| 994 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 995 key, value, link_class) | |
| 996 elif not self.db.getclass(link_class).hasnode(value): | |
| 997 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 998 | |
| 999 # save off the value | |
| 1000 propvalues[key] = value | |
| 1001 | |
| 1002 # register the link with the newly linked node | |
| 1003 if self.do_journal and self.properties[key].do_journal: | |
| 1004 self.db.addjournal(link_class, value, 'link', | |
| 1005 (self.classname, newid, key)) | |
| 1006 | |
| 1007 elif isinstance(prop, Multilink): | |
| 1008 if type(value) != type([]): | |
| 1009 raise TypeError, 'new property "%s" not a list of ids'%key | |
| 1010 | |
| 1011 # clean up and validate the list of links | |
| 1012 link_class = self.properties[key].classname | |
| 1013 l = [] | |
| 1014 for entry in value: | |
| 1015 if type(entry) != type(''): | |
| 1016 raise ValueError, '"%s" multilink value (%r) '\ | |
| 1017 'must contain Strings'%(key, value) | |
| 1018 # if it isn't a number, it's a key | |
| 1019 if not num_re.match(entry): | |
| 1020 try: | |
| 1021 entry = self.db.classes[link_class].lookup(entry) | |
| 1022 except (TypeError, KeyError): | |
| 1023 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1024 key, entry, self.properties[key].classname) | |
| 1025 l.append(entry) | |
| 1026 value = l | |
| 1027 propvalues[key] = value | |
| 1028 | |
| 1029 # handle additions | |
| 1030 for nodeid in value: | |
| 1031 if not self.db.getclass(link_class).hasnode(nodeid): | |
| 1032 raise IndexError, '%s has no node %s'%(link_class, | |
| 1033 nodeid) | |
| 1034 # register the link with the newly linked node | |
| 1035 if self.do_journal and self.properties[key].do_journal: | |
| 1036 self.db.addjournal(link_class, nodeid, 'link', | |
| 1037 (self.classname, newid, key)) | |
| 1038 | |
| 1039 elif isinstance(prop, String): | |
| 1040 if type(value) != type(''): | |
| 1041 raise TypeError, 'new property "%s" not a string'%key | |
| 1042 | |
| 1043 elif isinstance(prop, Password): | |
| 1044 if not isinstance(value, password.Password): | |
| 1045 raise TypeError, 'new property "%s" not a Password'%key | |
| 1046 | |
| 1047 elif isinstance(prop, Date): | |
| 1048 if value is not None and not isinstance(value, date.Date): | |
| 1049 raise TypeError, 'new property "%s" not a Date'%key | |
| 1050 | |
| 1051 elif isinstance(prop, Interval): | |
| 1052 if value is not None and not isinstance(value, date.Interval): | |
| 1053 raise TypeError, 'new property "%s" not an Interval'%key | |
| 1054 | |
| 1055 elif value is not None and isinstance(prop, Number): | |
| 1056 try: | |
| 1057 float(value) | |
| 1058 except ValueError: | |
| 1059 raise TypeError, 'new property "%s" not numeric'%key | |
| 1060 | |
| 1061 elif value is not None and isinstance(prop, Boolean): | |
| 1062 try: | |
| 1063 int(value) | |
| 1064 except ValueError: | |
| 1065 raise TypeError, 'new property "%s" not boolean'%key | |
| 1066 | |
| 1067 # make sure there's data where there needs to be | |
| 1068 for key, prop in self.properties.items(): | |
| 1069 if propvalues.has_key(key): | |
| 1070 continue | |
| 1071 if key == self.key: | |
| 1072 raise ValueError, 'key property "%s" is required'%key | |
| 1073 if isinstance(prop, Multilink): | |
| 1074 propvalues[key] = [] | |
| 1075 else: | |
| 1076 propvalues[key] = None | |
| 1077 | |
| 1078 # done | |
| 1079 self.db.addnode(self.classname, newid, propvalues) | |
| 1080 if self.do_journal: | |
| 1081 self.db.addjournal(self.classname, newid, 'create', propvalues) | |
| 1082 | |
| 1083 self.fireReactors('create', newid, None) | |
| 1084 | |
| 1085 return newid | |
| 1086 | |
| 1087 def export_list(self, propnames, nodeid): | |
| 1088 ''' Export a node - generate a list of CSV-able data in the order | |
| 1089 specified by propnames for the given node. | |
| 1090 ''' | |
| 1091 properties = self.getprops() | |
| 1092 l = [] | |
| 1093 for prop in propnames: | |
| 1094 proptype = properties[prop] | |
| 1095 value = self.get(nodeid, prop) | |
| 1096 # "marshal" data where needed | |
| 1097 if value is None: | |
| 1098 pass | |
| 1099 elif isinstance(proptype, hyperdb.Date): | |
| 1100 value = value.get_tuple() | |
| 1101 elif isinstance(proptype, hyperdb.Interval): | |
| 1102 value = value.get_tuple() | |
| 1103 elif isinstance(proptype, hyperdb.Password): | |
| 1104 value = str(value) | |
| 1105 l.append(repr(value)) | |
| 1106 return l | |
| 1107 | |
| 1108 def import_list(self, propnames, proplist): | |
| 1109 ''' Import a node - all information including "id" is present and | |
| 1110 should not be sanity checked. Triggers are not triggered. The | |
| 1111 journal should be initialised using the "creator" and "created" | |
| 1112 information. | |
| 1113 | |
| 1114 Return the nodeid of the node imported. | |
| 1115 ''' | |
| 1116 if self.db.journaltag is None: | |
| 1117 raise DatabaseError, 'Database open read-only' | |
| 1118 properties = self.getprops() | |
| 1119 | |
| 1120 # make the new node's property map | |
| 1121 d = {} | |
| 1122 for i in range(len(propnames)): | |
| 1123 # Use eval to reverse the repr() used to output the CSV | |
| 1124 value = eval(proplist[i]) | |
| 1125 | |
| 1126 # Figure the property for this column | |
| 1127 propname = propnames[i] | |
| 1128 prop = properties[propname] | |
| 1129 | |
| 1130 # "unmarshal" where necessary | |
| 1131 if propname == 'id': | |
| 1132 newid = value | |
| 1133 continue | |
| 1134 elif value is None: | |
| 1135 # don't set Nones | |
| 1136 continue | |
| 1137 elif isinstance(prop, hyperdb.Date): | |
| 1138 value = date.Date(value) | |
| 1139 elif isinstance(prop, hyperdb.Interval): | |
| 1140 value = date.Interval(value) | |
| 1141 elif isinstance(prop, hyperdb.Password): | |
| 1142 pwd = password.Password() | |
| 1143 pwd.unpack(value) | |
| 1144 value = pwd | |
| 1145 d[propname] = value | |
| 1146 | |
| 1147 # extract the extraneous journalling gumpf and nuke it | |
| 1148 if d.has_key('creator'): | |
| 1149 creator = d['creator'] | |
| 1150 del d['creator'] | |
| 1151 if d.has_key('creation'): | |
| 1152 creation = d['creation'] | |
| 1153 del d['creation'] | |
| 1154 if d.has_key('activity'): | |
| 1155 del d['activity'] | |
| 1156 | |
| 1157 # add the node and journal | |
| 1158 self.db.addnode(self.classname, newid, d) | |
| 1159 self.db.addjournal(self.classname, newid, 'create', d, creator, | |
| 1160 creation) | |
| 1161 return newid | |
| 1162 | |
| 1163 _marker = [] | |
| 1164 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 1165 '''Get the value of a property on an existing node of this class. | |
| 1166 | |
| 1167 'nodeid' must be the id of an existing node of this class or an | |
| 1168 IndexError is raised. 'propname' must be the name of a property | |
| 1169 of this class or a KeyError is raised. | |
| 1170 | |
| 1171 'cache' indicates whether the transaction cache should be queried | |
| 1172 for the node. If the node has been modified and you need to | |
| 1173 determine what its values prior to modification are, you need to | |
| 1174 set cache=0. | |
| 1175 ''' | |
| 1176 if propname == 'id': | |
| 1177 return nodeid | |
| 1178 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1179 # get the node's dict |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1180 d = self.db.getnode(self.classname, nodeid) |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1181 |
| 1165 | 1182 if propname == 'creation': |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1183 if d.has_key('creation'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1184 return d['creation'] |
| 1165 | 1185 else: |
| 1186 return date.Date() | |
| 1187 if propname == 'activity': | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1188 if d.has_key('activity'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1189 return d['activity'] |
| 1165 | 1190 else: |
| 1191 return date.Date() | |
| 1192 if propname == 'creator': | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1193 if d.has_key('creator'): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1194 return d['creator'] |
| 1165 | 1195 else: |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
1196 return self.db.curuserid |
| 1165 | 1197 |
| 1198 # get the property (raises KeyErorr if invalid) | |
| 1199 prop = self.properties[propname] | |
| 1200 | |
| 1201 if not d.has_key(propname): | |
| 1202 if default is self._marker: | |
| 1203 if isinstance(prop, Multilink): | |
| 1204 return [] | |
| 1205 else: | |
| 1206 return None | |
| 1207 else: | |
| 1208 return default | |
| 1209 | |
| 1210 # don't pass our list to other code | |
| 1211 if isinstance(prop, Multilink): | |
| 1212 return d[propname][:] | |
| 1213 | |
| 1214 return d[propname] | |
| 1215 | |
| 1216 def getnode(self, nodeid, cache=1): | |
| 1217 ''' Return a convenience wrapper for the node. | |
| 1218 | |
| 1219 'nodeid' must be the id of an existing node of this class or an | |
| 1220 IndexError is raised. | |
| 1221 | |
| 1222 'cache' indicates whether the transaction cache should be queried | |
| 1223 for the node. If the node has been modified and you need to | |
| 1224 determine what its values prior to modification are, you need to | |
| 1225 set cache=0. | |
| 1226 ''' | |
| 1227 return Node(self, nodeid, cache=cache) | |
| 1228 | |
| 1229 def set(self, nodeid, **propvalues): | |
| 1230 '''Modify a property on an existing node of this class. | |
| 1231 | |
| 1232 'nodeid' must be the id of an existing node of this class or an | |
| 1233 IndexError is raised. | |
| 1234 | |
| 1235 Each key in 'propvalues' must be the name of a property of this | |
| 1236 class or a KeyError is raised. | |
| 1237 | |
| 1238 All values in 'propvalues' must be acceptable types for their | |
| 1239 corresponding properties or a TypeError is raised. | |
| 1240 | |
| 1241 If the value of the key property is set, it must not collide with | |
| 1242 other key strings or a ValueError is raised. | |
| 1243 | |
| 1244 If the value of a Link or Multilink property contains an invalid | |
| 1245 node id, a ValueError is raised. | |
| 1246 ''' | |
| 1247 if not propvalues: | |
| 1248 return propvalues | |
| 1249 | |
| 1250 if propvalues.has_key('creation') or propvalues.has_key('activity'): | |
| 1251 raise KeyError, '"creation" and "activity" are reserved' | |
| 1252 | |
| 1253 if propvalues.has_key('id'): | |
| 1254 raise KeyError, '"id" is reserved' | |
| 1255 | |
| 1256 if self.db.journaltag is None: | |
| 1257 raise DatabaseError, 'Database open read-only' | |
| 1258 | |
| 1259 self.fireAuditors('set', nodeid, propvalues) | |
| 1260 # Take a copy of the node dict so that the subsequent set | |
| 1261 # operation doesn't modify the oldvalues structure. | |
| 1262 # XXX used to try the cache here first | |
| 1263 oldvalues = copy.deepcopy(self.db.getnode(self.classname, nodeid)) | |
| 1264 | |
| 1265 node = self.db.getnode(self.classname, nodeid) | |
| 1266 if self.is_retired(nodeid): | |
| 1267 raise IndexError, 'Requested item is retired' | |
| 1268 num_re = re.compile('^\d+$') | |
| 1269 | |
| 1270 # if the journal value is to be different, store it in here | |
| 1271 journalvalues = {} | |
| 1272 | |
| 1273 # remember the add/remove stuff for multilinks, making it easier | |
| 1274 # for the Database layer to do its stuff | |
| 1275 multilink_changes = {} | |
| 1276 | |
| 1277 for propname, value in propvalues.items(): | |
| 1278 # check to make sure we're not duplicating an existing key | |
| 1279 if propname == self.key and node[propname] != value: | |
| 1280 try: | |
| 1281 self.lookup(value) | |
| 1282 except KeyError: | |
| 1283 pass | |
| 1284 else: | |
| 1285 raise ValueError, 'node with key "%s" exists'%value | |
| 1286 | |
| 1287 # this will raise the KeyError if the property isn't valid | |
| 1288 # ... we don't use getprops() here because we only care about | |
| 1289 # the writeable properties. | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1290 try: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1291 prop = self.properties[propname] |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1292 except KeyError: |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1293 raise KeyError, '"%s" has no property named "%s"'%( |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1294 self.classname, propname) |
| 1165 | 1295 |
| 1296 # if the value's the same as the existing value, no sense in | |
| 1297 # doing anything | |
| 1298 if node.has_key(propname) and value == node[propname]: | |
| 1299 del propvalues[propname] | |
| 1300 continue | |
| 1301 | |
| 1302 # do stuff based on the prop type | |
| 1303 if isinstance(prop, Link): | |
| 1304 link_class = prop.classname | |
| 1305 # if it isn't a number, it's a key | |
| 1306 if value is not None and not isinstance(value, type('')): | |
| 1307 raise ValueError, 'property "%s" link value be a string'%( | |
| 1308 propname) | |
| 1309 if isinstance(value, type('')) and not num_re.match(value): | |
| 1310 try: | |
| 1311 value = self.db.classes[link_class].lookup(value) | |
| 1312 except (TypeError, KeyError): | |
| 1313 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1314 propname, value, prop.classname) | |
| 1315 | |
| 1316 if (value is not None and | |
| 1317 not self.db.getclass(link_class).hasnode(value)): | |
| 1318 raise IndexError, '%s has no node %s'%(link_class, value) | |
| 1319 | |
| 1320 if self.do_journal and prop.do_journal: | |
| 1321 # register the unlink with the old linked node | |
| 1322 if node[propname] is not None: | |
| 1323 self.db.addjournal(link_class, node[propname], 'unlink', | |
| 1324 (self.classname, nodeid, propname)) | |
| 1325 | |
| 1326 # register the link with the newly linked node | |
| 1327 if value is not None: | |
| 1328 self.db.addjournal(link_class, value, 'link', | |
| 1329 (self.classname, nodeid, propname)) | |
| 1330 | |
| 1331 elif isinstance(prop, Multilink): | |
| 1332 if type(value) != type([]): | |
| 1333 raise TypeError, 'new property "%s" not a list of'\ | |
| 1334 ' ids'%propname | |
| 1335 link_class = self.properties[propname].classname | |
| 1336 l = [] | |
| 1337 for entry in value: | |
| 1338 # if it isn't a number, it's a key | |
| 1339 if type(entry) != type(''): | |
| 1340 raise ValueError, 'new property "%s" link value ' \ | |
| 1341 'must be a string'%propname | |
| 1342 if not num_re.match(entry): | |
| 1343 try: | |
| 1344 entry = self.db.classes[link_class].lookup(entry) | |
| 1345 except (TypeError, KeyError): | |
| 1346 raise IndexError, 'new property "%s": %s not a %s'%( | |
| 1347 propname, entry, | |
| 1348 self.properties[propname].classname) | |
| 1349 l.append(entry) | |
| 1350 value = l | |
| 1351 propvalues[propname] = value | |
| 1352 | |
| 1353 # figure the journal entry for this property | |
| 1354 add = [] | |
| 1355 remove = [] | |
| 1356 | |
| 1357 # handle removals | |
| 1358 if node.has_key(propname): | |
| 1359 l = node[propname] | |
| 1360 else: | |
| 1361 l = [] | |
| 1362 for id in l[:]: | |
| 1363 if id in value: | |
| 1364 continue | |
| 1365 # register the unlink with the old linked node | |
| 1366 if self.do_journal and self.properties[propname].do_journal: | |
| 1367 self.db.addjournal(link_class, id, 'unlink', | |
| 1368 (self.classname, nodeid, propname)) | |
| 1369 l.remove(id) | |
| 1370 remove.append(id) | |
| 1371 | |
| 1372 # handle additions | |
| 1373 for id in value: | |
| 1374 if not self.db.getclass(link_class).hasnode(id): | |
| 1375 raise IndexError, '%s has no node %s'%(link_class, id) | |
| 1376 if id in l: | |
| 1377 continue | |
| 1378 # register the link with the newly linked node | |
| 1379 if self.do_journal and self.properties[propname].do_journal: | |
| 1380 self.db.addjournal(link_class, id, 'link', | |
| 1381 (self.classname, nodeid, propname)) | |
| 1382 l.append(id) | |
| 1383 add.append(id) | |
| 1384 | |
| 1385 # figure the journal entry | |
| 1386 l = [] | |
| 1387 if add: | |
| 1388 l.append(('+', add)) | |
| 1389 if remove: | |
| 1390 l.append(('-', remove)) | |
| 1391 multilink_changes[propname] = (add, remove) | |
| 1392 if l: | |
| 1393 journalvalues[propname] = tuple(l) | |
| 1394 | |
| 1395 elif isinstance(prop, String): | |
| 1396 if value is not None and type(value) != type(''): | |
| 1397 raise TypeError, 'new property "%s" not a string'%propname | |
| 1398 | |
| 1399 elif isinstance(prop, Password): | |
| 1400 if not isinstance(value, password.Password): | |
| 1401 raise TypeError, 'new property "%s" not a Password'%propname | |
| 1402 propvalues[propname] = value | |
| 1403 | |
| 1404 elif value is not None and isinstance(prop, Date): | |
| 1405 if not isinstance(value, date.Date): | |
| 1406 raise TypeError, 'new property "%s" not a Date'% propname | |
| 1407 propvalues[propname] = value | |
| 1408 | |
| 1409 elif value is not None and isinstance(prop, Interval): | |
| 1410 if not isinstance(value, date.Interval): | |
| 1411 raise TypeError, 'new property "%s" not an '\ | |
| 1412 'Interval'%propname | |
| 1413 propvalues[propname] = value | |
| 1414 | |
| 1415 elif value is not None and isinstance(prop, Number): | |
| 1416 try: | |
| 1417 float(value) | |
| 1418 except ValueError: | |
| 1419 raise TypeError, 'new property "%s" not numeric'%propname | |
| 1420 | |
| 1421 elif value is not None and isinstance(prop, Boolean): | |
| 1422 try: | |
| 1423 int(value) | |
| 1424 except ValueError: | |
| 1425 raise TypeError, 'new property "%s" not boolean'%propname | |
| 1426 | |
| 1427 # nothing to do? | |
| 1428 if not propvalues: | |
| 1429 return propvalues | |
| 1430 | |
| 1431 # do the set, and journal it | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1432 self.db.setnode(self.classname, nodeid, propvalues, multilink_changes) |
| 1165 | 1433 |
| 1434 if self.do_journal: | |
| 1435 propvalues.update(journalvalues) | |
| 1436 self.db.addjournal(self.classname, nodeid, 'set', propvalues) | |
| 1437 | |
| 1438 self.fireReactors('set', nodeid, oldvalues) | |
| 1439 | |
| 1440 return propvalues | |
| 1441 | |
| 1442 def retire(self, nodeid): | |
| 1443 '''Retire a node. | |
| 1444 | |
| 1445 The properties on the node remain available from the get() method, | |
| 1446 and the node's id is never reused. | |
| 1447 | |
| 1448 Retired nodes are not returned by the find(), list(), or lookup() | |
| 1449 methods, and other nodes may reuse the values of their key properties. | |
| 1450 ''' | |
| 1451 if self.db.journaltag is None: | |
| 1452 raise DatabaseError, 'Database open read-only' | |
| 1453 | |
| 1454 sql = 'update _%s set __retired__=1 where id=%s'%(self.classname, | |
| 1455 self.db.arg) | |
| 1456 if __debug__: | |
| 1457 print >>hyperdb.DEBUG, 'retire', (self, sql, nodeid) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1458 self.db.cursor.execute(sql, (nodeid,)) |
| 1165 | 1459 |
| 1460 def is_retired(self, nodeid): | |
| 1461 '''Return true if the node is rerired | |
| 1462 ''' | |
| 1463 sql = 'select __retired__ from _%s where id=%s'%(self.classname, | |
| 1464 self.db.arg) | |
| 1465 if __debug__: | |
| 1466 print >>hyperdb.DEBUG, 'is_retired', (self, sql, nodeid) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1467 self.db.cursor.execute(sql, (nodeid,)) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1468 return int(self.db.sql_fetchone()[0]) |
| 1165 | 1469 |
| 1470 def destroy(self, nodeid): | |
| 1471 '''Destroy a node. | |
| 1472 | |
| 1473 WARNING: this method should never be used except in extremely rare | |
| 1474 situations where there could never be links to the node being | |
| 1475 deleted | |
| 1476 WARNING: use retire() instead | |
| 1477 WARNING: the properties of this node will not be available ever again | |
| 1478 WARNING: really, use retire() instead | |
| 1479 | |
| 1480 Well, I think that's enough warnings. This method exists mostly to | |
| 1481 support the session storage of the cgi interface. | |
| 1482 | |
| 1483 The node is completely removed from the hyperdb, including all journal | |
| 1484 entries. It will no longer be available, and will generally break code | |
| 1485 if there are any references to the node. | |
| 1486 ''' | |
| 1487 if self.db.journaltag is None: | |
| 1488 raise DatabaseError, 'Database open read-only' | |
| 1489 self.db.destroynode(self.classname, nodeid) | |
| 1490 | |
| 1491 def history(self, nodeid): | |
| 1492 '''Retrieve the journal of edits on a particular node. | |
| 1493 | |
| 1494 'nodeid' must be the id of an existing node of this class or an | |
| 1495 IndexError is raised. | |
| 1496 | |
| 1497 The returned list contains tuples of the form | |
| 1498 | |
| 1499 (date, tag, action, params) | |
| 1500 | |
| 1501 'date' is a Timestamp object specifying the time of the change and | |
| 1502 'tag' is the journaltag specified when the database was opened. | |
| 1503 ''' | |
| 1504 if not self.do_journal: | |
| 1505 raise ValueError, 'Journalling is disabled for this class' | |
| 1506 return self.db.getjournal(self.classname, nodeid) | |
| 1507 | |
| 1508 # Locating nodes: | |
| 1509 def hasnode(self, nodeid): | |
| 1510 '''Determine if the given nodeid actually exists | |
| 1511 ''' | |
| 1512 return self.db.hasnode(self.classname, nodeid) | |
| 1513 | |
| 1514 def setkey(self, propname): | |
| 1515 '''Select a String property of this class to be the key property. | |
| 1516 | |
| 1517 'propname' must be the name of a String property of this class or | |
| 1518 None, or a TypeError is raised. The values of the key property on | |
| 1519 all existing nodes must be unique or a ValueError is raised. | |
| 1520 ''' | |
| 1521 # XXX create an index on the key prop column | |
| 1522 prop = self.getprops()[propname] | |
| 1523 if not isinstance(prop, String): | |
| 1524 raise TypeError, 'key properties must be String' | |
| 1525 self.key = propname | |
| 1526 | |
| 1527 def getkey(self): | |
| 1528 '''Return the name of the key property for this class or None.''' | |
| 1529 return self.key | |
| 1530 | |
| 1531 def labelprop(self, default_to_id=0): | |
| 1532 ''' Return the property name for a label for the given node. | |
| 1533 | |
| 1534 This method attempts to generate a consistent label for the node. | |
| 1535 It tries the following in order: | |
| 1536 1. key property | |
| 1537 2. "name" property | |
| 1538 3. "title" property | |
| 1539 4. first property from the sorted property name list | |
| 1540 ''' | |
| 1541 k = self.getkey() | |
| 1542 if k: | |
| 1543 return k | |
| 1544 props = self.getprops() | |
| 1545 if props.has_key('name'): | |
| 1546 return 'name' | |
| 1547 elif props.has_key('title'): | |
| 1548 return 'title' | |
| 1549 if default_to_id: | |
| 1550 return 'id' | |
| 1551 props = props.keys() | |
| 1552 props.sort() | |
| 1553 return props[0] | |
| 1554 | |
| 1555 def lookup(self, keyvalue): | |
| 1556 '''Locate a particular node by its key property and return its id. | |
| 1557 | |
| 1558 If this class has no key property, a TypeError is raised. If the | |
| 1559 'keyvalue' matches one of the values for the key property among | |
| 1560 the nodes in this class, the matching node's id is returned; | |
| 1561 otherwise a KeyError is raised. | |
| 1562 ''' | |
| 1563 if not self.key: | |
| 1564 raise TypeError, 'No key property set for class %s'%self.classname | |
| 1565 | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1566 sql = 'select id,__retired__ from _%s where _%s=%s'%(self.classname, |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1567 self.key, self.db.arg) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1568 self.db.sql(sql, (keyvalue,)) |
| 1165 | 1569 |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1570 # see if there was a result that's not retired |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1571 l = self.db.cursor.fetchall() |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1572 if not l or int(l[0][1]): |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1573 raise KeyError, 'No key (%s) value "%s" for "%s"'%(self.key, |
|
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1574 keyvalue, self.classname) |
| 1165 | 1575 |
| 1576 # return the id | |
| 1577 return l[0][0] | |
| 1578 | |
| 1579 def find(self, **propspec): | |
| 1580 '''Get the ids of nodes in this class which link to the given nodes. | |
| 1581 | |
| 1582 'propspec' consists of keyword args propname={nodeid:1,} | |
| 1583 'propname' must be the name of a property in this class, or a | |
| 1584 KeyError is raised. That property must be a Link or Multilink | |
| 1585 property, or a TypeError is raised. | |
| 1586 | |
| 1587 Any node in this class whose 'propname' property links to any of the | |
| 1588 nodeids will be returned. Used by the full text indexing, which knows | |
| 1589 that "foo" occurs in msg1, msg3 and file7, so we have hits on these | |
| 1590 issues: | |
| 1591 | |
| 1592 db.issue.find(messages={'1':1,'3':1}, files={'7':1}) | |
| 1593 ''' | |
| 1594 if __debug__: | |
| 1595 print >>hyperdb.DEBUG, 'find', (self, propspec) | |
| 1596 if not propspec: | |
| 1597 return [] | |
| 1598 queries = [] | |
| 1599 tables = [] | |
| 1600 allvalues = () | |
| 1601 for prop, values in propspec.items(): | |
| 1602 allvalues += tuple(values.keys()) | |
| 1603 a = self.db.arg | |
| 1604 tables.append('select nodeid from %s_%s where linkid in (%s)'%( | |
| 1605 self.classname, prop, ','.join([a for x in values.keys()]))) | |
| 1606 sql = '\nintersect\n'.join(tables) | |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1607 self.db.sql(sql, allvalues) |
| 1165 | 1608 try: |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1609 l = [x[0] for x in self.db.cursor.fetchall()] |
| 1165 | 1610 except gadfly.database.error, message: |
| 1611 if message == 'no more results': | |
| 1612 l = [] | |
| 1613 raise | |
| 1614 if __debug__: | |
| 1615 print >>hyperdb.DEBUG, 'find ... ', l | |
| 1616 return l | |
| 1617 | |
| 1618 def list(self): | |
| 1619 ''' Return a list of the ids of the active nodes in this class. | |
| 1620 ''' | |
| 1621 return self.db.getnodeids(self.classname, retired=0) | |
| 1622 | |
| 1623 def filter(self, search_matches, filterspec, sort, group): | |
| 1624 ''' Return a list of the ids of the active nodes in this class that | |
| 1625 match the 'filter' spec, sorted by the group spec and then the | |
| 1626 sort spec | |
| 1627 | |
| 1628 "filterspec" is {propname: value(s)} | |
| 1629 "sort" and "group" are (dir, prop) where dir is '+', '-' or None | |
| 1630 and prop is a prop name or None | |
| 1631 "search_matches" is {nodeid: marker} | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1632 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1633 The filter must match all properties specificed - but if the |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1634 property value to match is a list, any one of the values in the |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1635 list may match for that property to match. |
| 1165 | 1636 ''' |
| 1637 cn = self.classname | |
| 1638 | |
| 1639 # figure the WHERE clause from the filterspec | |
| 1640 props = self.getprops() | |
| 1641 frum = ['_'+cn] | |
| 1642 where = [] | |
| 1643 args = [] | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1644 a = self.db.arg |
| 1165 | 1645 for k, v in filterspec.items(): |
| 1646 propclass = props[k] | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1647 # now do other where clause stuff |
| 1165 | 1648 if isinstance(propclass, Multilink): |
| 1649 tn = '%s_%s'%(cn, k) | |
| 1650 frum.append(tn) | |
| 1651 if isinstance(v, type([])): | |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1652 s = ','.join([a for x in v]) |
| 1165 | 1653 where.append('id=%s.nodeid and %s.linkid in (%s)'%(tn,tn,s)) |
| 1654 args = args + v | |
| 1655 else: | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1656 where.append('id=%s.nodeid and %s.linkid = %s'%(tn, tn, a)) |
| 1165 | 1657 args.append(v) |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1658 elif isinstance(propclass, String): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1659 if not isinstance(v, type([])): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1660 v = [v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1661 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1662 # Quote the bits in the string that need it and then embed |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1663 # in a "substring" search. Note - need to quote the '%' so |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1664 # they make it through the python layer happily |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1665 v = ['%%'+self.db.sql_stringquote(s)+'%%' for s in v] |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1666 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1667 # now add to the where clause |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1668 where.append(' or '.join(["_%s LIKE '%s'"%(k, s) for s in v])) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1669 # note: args are embedded in the query string now |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1670 elif isinstance(propclass, Link): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1671 if isinstance(v, type([])): |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1672 if '-1' in v: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1673 v.remove('-1') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1674 xtra = ' or _%s is NULL'%k |
|
1173
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
1675 else: |
|
58f1a2c174ed
simple LRU cache for SQL databases
Richard Jones <richard@users.sourceforge.net>
parents:
1172
diff
changeset
|
1676 xtra = '' |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1677 s = ','.join([a for x in v]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1678 where.append('(_%s in (%s)%s)'%(k, s, xtra)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1679 args = args + v |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1680 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1681 if v == '-1': |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1682 v = None |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1683 where.append('_%s is NULL'%k) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1684 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1685 where.append('_%s=%s'%(k, a)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1686 args.append(v) |
| 1165 | 1687 else: |
| 1688 if isinstance(v, type([])): | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1689 s = ','.join([a for x in v]) |
| 1165 | 1690 where.append('_%s in (%s)'%(k, s)) |
| 1691 args = args + v | |
| 1692 else: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1693 where.append('_%s=%s'%(k, a)) |
| 1165 | 1694 args.append(v) |
| 1695 | |
| 1696 # add results of full text search | |
| 1697 if search_matches is not None: | |
| 1698 v = search_matches.keys() | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1699 s = ','.join([a for x in v]) |
| 1165 | 1700 where.append('id in (%s)'%s) |
| 1701 args = args + v | |
| 1702 | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1703 # "grouping" is just the first-order sorting in the SQL fetch |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1704 # can modify it...) |
| 1165 | 1705 orderby = [] |
| 1706 ordercols = [] | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1707 if group[0] is not None and group[1] is not None: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1708 if group[0] != '-': |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1709 orderby.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1710 ordercols.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1711 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1712 orderby.append('_'+group[1]+' desc') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1713 ordercols.append('_'+group[1]) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1714 |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1715 # now add in the sorting |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1716 group = '' |
| 1165 | 1717 if sort[0] is not None and sort[1] is not None: |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1718 direction, colname = sort |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1719 if direction != '-': |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1720 if colname == 'id': |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1721 orderby.append(colname) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1722 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1723 orderby.append('_'+colname) |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1724 ordercols.append('_'+colname) |
| 1165 | 1725 else: |
|
1174
8e318dfaf479
Verify contents of tracker module when the tracker is opened
Richard Jones <richard@users.sourceforge.net>
parents:
1173
diff
changeset
|
1726 if colname == 'id': |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1727 orderby.append(colname+' desc') |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1728 ordercols.append(colname) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1729 else: |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1730 orderby.append('_'+colname+' desc') |
|
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1731 ordercols.append('_'+colname) |
| 1165 | 1732 |
| 1733 # construct the SQL | |
| 1734 frum = ','.join(frum) | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1735 if where: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1736 where = ' where ' + (' and '.join(where)) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1737 else: |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1738 where = '' |
| 1165 | 1739 cols = ['id'] |
| 1740 if orderby: | |
| 1741 cols = cols + ordercols | |
| 1742 order = ' order by %s'%(','.join(orderby)) | |
| 1743 else: | |
| 1744 order = '' | |
| 1745 cols = ','.join(cols) | |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1746 sql = 'select %s from %s %s%s%s'%(cols, frum, where, group, order) |
| 1165 | 1747 args = tuple(args) |
| 1748 if __debug__: | |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1749 print >>hyperdb.DEBUG, 'filter', (self, sql, args) |
|
1183
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1750 self.db.cursor.execute(sql, args) |
|
08a13a84ed43
Some speedups - both of the SQL backends can handle using only one cursor.
Richard Jones <richard@users.sourceforge.net>
parents:
1181
diff
changeset
|
1751 l = self.db.cursor.fetchall() |
| 1165 | 1752 |
|
1170
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1753 # return the IDs (the first column) |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1754 # XXX The filter(None, l) bit is sqlite-specific... if there's _NO_ |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1755 # XXX matches to a fetch, it returns NULL instead of nothing!?! |
|
af104fa52746
Added some words to the installation doc about choosing backends.
Richard Jones <richard@users.sourceforge.net>
parents:
1168
diff
changeset
|
1756 return filter(None, [row[0] for row in l]) |
|
1168
94620e088e3a
fixes to the rdbms backends
Richard Jones <richard@users.sourceforge.net>
parents:
1165
diff
changeset
|
1757 |
| 1165 | 1758 def count(self): |
| 1759 '''Get the number of nodes in this class. | |
| 1760 | |
| 1761 If the returned integer is 'numnodes', the ids of all the nodes | |
| 1762 in this class run from 1 to numnodes, and numnodes+1 will be the | |
| 1763 id of the next node to be created in this class. | |
| 1764 ''' | |
| 1765 return self.db.countnodes(self.classname) | |
| 1766 | |
| 1767 # Manipulating properties: | |
| 1768 def getprops(self, protected=1): | |
| 1769 '''Return a dictionary mapping property names to property objects. | |
| 1770 If the "protected" flag is true, we include protected properties - | |
| 1771 those which may not be modified. | |
| 1772 ''' | |
| 1773 d = self.properties.copy() | |
| 1774 if protected: | |
| 1775 d['id'] = String() | |
| 1776 d['creation'] = hyperdb.Date() | |
| 1777 d['activity'] = hyperdb.Date() | |
|
1176
bd3b57859c37
On second thought, that last checkin was dumb.
Richard Jones <richard@users.sourceforge.net>
parents:
1175
diff
changeset
|
1778 d['creator'] = hyperdb.Link('user') |
| 1165 | 1779 return d |
| 1780 | |
| 1781 def addprop(self, **properties): | |
| 1782 '''Add properties to this class. | |
| 1783 | |
| 1784 The keyword arguments in 'properties' must map names to property | |
| 1785 objects, or a TypeError is raised. None of the keys in 'properties' | |
| 1786 may collide with the names of existing properties, or a ValueError | |
| 1787 is raised before any properties have been added. | |
| 1788 ''' | |
| 1789 for key in properties.keys(): | |
| 1790 if self.properties.has_key(key): | |
| 1791 raise ValueError, key | |
| 1792 self.properties.update(properties) | |
| 1793 | |
| 1794 def index(self, nodeid): | |
| 1795 '''Add (or refresh) the node to search indexes | |
| 1796 ''' | |
| 1797 # find all the String properties that have indexme | |
| 1798 for prop, propclass in self.getprops().items(): | |
| 1799 if isinstance(propclass, String) and propclass.indexme: | |
| 1800 try: | |
| 1801 value = str(self.get(nodeid, prop)) | |
| 1802 except IndexError: | |
| 1803 # node no longer exists - entry should be removed | |
| 1804 self.db.indexer.purge_entry((self.classname, nodeid, prop)) | |
| 1805 else: | |
| 1806 # and index them under (classname, nodeid, property) | |
| 1807 self.db.indexer.add_text((self.classname, nodeid, prop), | |
| 1808 value) | |
| 1809 | |
| 1810 | |
| 1811 # | |
| 1812 # Detector interface | |
| 1813 # | |
| 1814 def audit(self, event, detector): | |
| 1815 '''Register a detector | |
| 1816 ''' | |
| 1817 l = self.auditors[event] | |
| 1818 if detector not in l: | |
| 1819 self.auditors[event].append(detector) | |
| 1820 | |
| 1821 def fireAuditors(self, action, nodeid, newvalues): | |
| 1822 '''Fire all registered auditors. | |
| 1823 ''' | |
| 1824 for audit in self.auditors[action]: | |
| 1825 audit(self.db, self, nodeid, newvalues) | |
| 1826 | |
| 1827 def react(self, event, detector): | |
| 1828 '''Register a detector | |
| 1829 ''' | |
| 1830 l = self.reactors[event] | |
| 1831 if detector not in l: | |
| 1832 self.reactors[event].append(detector) | |
| 1833 | |
| 1834 def fireReactors(self, action, nodeid, oldvalues): | |
| 1835 '''Fire all registered reactors. | |
| 1836 ''' | |
| 1837 for react in self.reactors[action]: | |
| 1838 react(self.db, self, nodeid, oldvalues) | |
| 1839 | |
| 1840 class FileClass(Class): | |
| 1841 '''This class defines a large chunk of data. To support this, it has a | |
| 1842 mandatory String property "content" which is typically saved off | |
| 1843 externally to the hyperdb. | |
| 1844 | |
| 1845 The default MIME type of this data is defined by the | |
| 1846 "default_mime_type" class attribute, which may be overridden by each | |
| 1847 node if the class defines a "type" String property. | |
| 1848 ''' | |
| 1849 default_mime_type = 'text/plain' | |
| 1850 | |
| 1851 def create(self, **propvalues): | |
| 1852 ''' snaffle the file propvalue and store in a file | |
| 1853 ''' | |
| 1854 content = propvalues['content'] | |
| 1855 del propvalues['content'] | |
| 1856 newid = Class.create(self, **propvalues) | |
| 1857 self.db.storefile(self.classname, newid, None, content) | |
| 1858 return newid | |
| 1859 | |
| 1860 def import_list(self, propnames, proplist): | |
| 1861 ''' Trap the "content" property... | |
| 1862 ''' | |
| 1863 # dupe this list so we don't affect others | |
| 1864 propnames = propnames[:] | |
| 1865 | |
| 1866 # extract the "content" property from the proplist | |
| 1867 i = propnames.index('content') | |
| 1868 content = eval(proplist[i]) | |
| 1869 del propnames[i] | |
| 1870 del proplist[i] | |
| 1871 | |
| 1872 # do the normal import | |
| 1873 newid = Class.import_list(self, propnames, proplist) | |
| 1874 | |
| 1875 # save off the "content" file | |
| 1876 self.db.storefile(self.classname, newid, None, content) | |
| 1877 return newid | |
| 1878 | |
| 1879 _marker = [] | |
| 1880 def get(self, nodeid, propname, default=_marker, cache=1): | |
| 1881 ''' trap the content propname and get it from the file | |
| 1882 ''' | |
| 1883 | |
| 1884 poss_msg = 'Possibly a access right configuration problem.' | |
| 1885 if propname == 'content': | |
| 1886 try: | |
| 1887 return self.db.getfile(self.classname, nodeid, None) | |
| 1888 except IOError, (strerror): | |
| 1889 # BUG: by catching this we donot see an error in the log. | |
| 1890 return 'ERROR reading file: %s%s\n%s\n%s'%( | |
| 1891 self.classname, nodeid, poss_msg, strerror) | |
| 1892 if default is not self._marker: | |
| 1893 return Class.get(self, nodeid, propname, default, cache=cache) | |
| 1894 else: | |
| 1895 return Class.get(self, nodeid, propname, cache=cache) | |
| 1896 | |
| 1897 def getprops(self, protected=1): | |
| 1898 ''' In addition to the actual properties on the node, these methods | |
| 1899 provide the "content" property. If the "protected" flag is true, | |
| 1900 we include protected properties - those which may not be | |
| 1901 modified. | |
| 1902 ''' | |
| 1903 d = Class.getprops(self, protected=protected).copy() | |
| 1904 d['content'] = hyperdb.String() | |
| 1905 return d | |
| 1906 | |
| 1907 def index(self, nodeid): | |
| 1908 ''' Index the node in the search index. | |
| 1909 | |
| 1910 We want to index the content in addition to the normal String | |
| 1911 property indexing. | |
| 1912 ''' | |
| 1913 # perform normal indexing | |
| 1914 Class.index(self, nodeid) | |
| 1915 | |
| 1916 # get the content to index | |
| 1917 content = self.get(nodeid, 'content') | |
| 1918 | |
| 1919 # figure the mime type | |
| 1920 if self.properties.has_key('type'): | |
| 1921 mime_type = self.get(nodeid, 'type') | |
| 1922 else: | |
| 1923 mime_type = self.default_mime_type | |
| 1924 | |
| 1925 # and index! | |
| 1926 self.db.indexer.add_text((self.classname, nodeid, 'content'), content, | |
| 1927 mime_type) | |
| 1928 | |
| 1929 # XXX deviation from spec - was called ItemClass | |
| 1930 class IssueClass(Class, roundupdb.IssueClass): | |
| 1931 # Overridden methods: | |
| 1932 def __init__(self, db, classname, **properties): | |
| 1933 '''The newly-created class automatically includes the "messages", | |
| 1934 "files", "nosy", and "superseder" properties. If the 'properties' | |
| 1935 dictionary attempts to specify any of these properties or a | |
| 1936 "creation" or "activity" property, a ValueError is raised. | |
| 1937 ''' | |
| 1938 if not properties.has_key('title'): | |
| 1939 properties['title'] = hyperdb.String(indexme='yes') | |
| 1940 if not properties.has_key('messages'): | |
| 1941 properties['messages'] = hyperdb.Multilink("msg") | |
| 1942 if not properties.has_key('files'): | |
| 1943 properties['files'] = hyperdb.Multilink("file") | |
| 1944 if not properties.has_key('nosy'): | |
| 1945 # note: journalling is turned off as it really just wastes | |
| 1946 # space. this behaviour may be overridden in an instance | |
| 1947 properties['nosy'] = hyperdb.Multilink("user", do_journal="no") | |
| 1948 if not properties.has_key('superseder'): | |
| 1949 properties['superseder'] = hyperdb.Multilink(classname) | |
| 1950 Class.__init__(self, db, classname, **properties) | |
| 1951 |
