HWRF  trunk@4391
Public Member Functions | Public Attributes | List of all members
produtil.datastore.Transaction Class Reference

Datastore transaction support. More...

Detailed Description

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.

Inheritance diagram for produtil.datastore.Transaction:

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
 

Constructor & Destructor Documentation

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.

Member Function Documentation

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.

Parameters
etype,evalueException type and value, if any.
tracebackException 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".

Parameters
dThe Datum whose metadata is being deleted.
kThe 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.

Parameters
dthe Datum
metaIf 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.

Parameters
stmtthe SQL query
subvalsthe 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()

Parameters
stmtthe SQL query
subvalsthe 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.

Parameters
dThe Datum.
or_addIf 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.

Parameters
dThe Datum
kThe metadata key.
vThe 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.

Parameters
dthe Datum

Definition at line 357 of file datastore.py.


The documentation for this class was generated from the following file: