HWRF
trunk@4391
|
Datastore transaction support. More...
Datastore transaction support.
Implements sqlite3 transactions on a Datastore. A transaction is a set of reads and updates that should either ALL be done, or NONE at all. Transactions also speed up the script, sometimes by as much of a factor of 300, by grouping I/O operations to the database into one large chunk. However, one must be careful in using them since it keeps the database locked for an extended period of time.
This class should not be used directly. Instead, one should do this to a Datum (Task or Product) object:
with datum_object.transaction() as t: ... do things to the datum object ... transaction is now complete, database is updated.
Definition at line 270 of file datastore.py.
Public Member Functions | |
def | __init__ (self, ds) |
Transaction constructor. More... | |
def | __enter__ (self) |
Locks the database for the current thread, if it isn't already locked. More... | |
def | __exit__ (self, etype, evalue, traceback) |
Releases the database lock if this is the last Transaction released for the current thread. More... | |
def | query |
Performs an SQL query returning the result of cursor.fetchall() More... | |
def | mutate |
Performs an SQL database modification, returning the result of cursor.lastrowid. More... | |
def | init_datum |
Add a Datum to the database if it is not there already. More... | |
def | update_datum (self, d) |
Update database availability and location records. More... | |
def | refresh_meta |
Replace Datum metadata with database values, add new metadata to database. More... | |
def | set_meta (self, d, k, v) |
Sets metadata key k to value v for the given Datum. More... | |
def | del_meta (self, d, k) |
Delete metadata from the database. More... | |
ds | |
The Datastore containing the database for which this is a transaction. | |
Public Attributes | |
ds | |
def produtil.datastore.Transaction.__init__ | ( | self, | |
ds | |||
) |
Transaction constructor.
Creates the Transaction object but does NOT initiate the transaction.
Definition at line 287 of file datastore.py.
def produtil.datastore.Transaction.__enter__ | ( | self | ) |
Locks the database for the current thread, if it isn't already locked.
Definition at line 294 of file datastore.py.
def produtil.datastore.Transaction.__exit__ | ( | self, | |
etype, | |||
evalue, | |||
traceback | |||
) |
Releases the database lock if this is the last Transaction released for the current thread.
etype,evalue | Exception type and value, if any. |
traceback | Exception traceback, if any. |
Definition at line 303 of file datastore.py.
def produtil.datastore.Transaction.del_meta | ( | self, | |
d, | |||
k | |||
) |
Delete metadata from the database.
Deletes the specified metadata key k, which must not be "location" or "available".
d | The Datum whose metadata is being deleted. |
k | The metadata key, which cannot be "available" or "location". |
Definition at line 410 of file datastore.py.
def produtil.datastore.Transaction.init_datum | ( | self, | |
d, | |||
meta = True |
|||
) |
Add a Datum to the database if it is not there already.
Given a Datum, add the object to the database if it is not there already and fill the object with data from the database.
d | the Datum |
meta | If True, also initialize metadata. |
Definition at line 327 of file datastore.py.
Referenced by produtil.datastore.Transaction.refresh_meta().
def produtil.datastore.Transaction.mutate | ( | self, | |
stmt, | |||
subvals = () |
|||
) |
Performs an SQL database modification, returning the result of cursor.lastrowid.
stmt | the SQL query |
subvals | the substitution values |
Definition at line 320 of file datastore.py.
Referenced by produtil.datastore.Transaction.del_meta(), produtil.datastore.Transaction.init_datum(), produtil.datastore.Transaction.set_meta(), and produtil.datastore.Transaction.update_datum().
def produtil.datastore.Transaction.query | ( | self, | |
stmt, | |||
subvals = () |
|||
) |
Performs an SQL query returning the result of cursor.fetchall()
stmt | the SQL query |
subvals | the substitution values |
Definition at line 314 of file datastore.py.
Referenced by produtil.datastore.Transaction.init_datum(), and produtil.datastore.Transaction.refresh_meta().
def produtil.datastore.Transaction.refresh_meta | ( | self, | |
d, | |||
or_add = True |
|||
) |
Replace Datum metadata with database values, add new metadata to database.
Given a Datum d, discards and replaces d._meta with the current metadata, location and availability. Will raise an exception if the product does not exist in the database.
d | The Datum. |
or_add | If True, then any metadata that does not exist in the database is created from values in d. |
Definition at line 368 of file datastore.py.
Referenced by produtil.datastore.Transaction.init_datum().
def produtil.datastore.Transaction.set_meta | ( | self, | |
d, | |||
k, | |||
v | |||
) |
Sets metadata key k to value v for the given Datum.
Modifies the database entries for key k and datum d to have the value v. If k is location or available, then the product table will be updated instead.
d | The Datum |
k | The metadata key. |
v | The value, a string. |
Definition at line 395 of file datastore.py.
def produtil.datastore.Transaction.update_datum | ( | self, | |
d | |||
) |
Update database availability and location records.
Given a Datum whose location and availability is set, update that information in the database, adding the Datum if necessary.
d | the Datum |
Definition at line 357 of file datastore.py.