Package advene :: Package model :: Package backends :: Module sqlite :: Class _FlushableIterator
[hide private]
[frames] | no frames]

Class _FlushableIterator

source code

object --+
         |
        _FlushableIterator

Cursor based iterator that may flush the cursor whenever needed.

Transactions to the database cannot be commited while a cursor is being used. So it is unsafe for _SqliteBackend to return cursors direcly because it may hinder further execution of methods using transactions.

On the other hand, it may be inefficient to flush all cursors into lists before returning them. This class provides an efficient solution.

All cursors (or cursor based iterators) are wrapped info a _FlushableIterator before being returned, and the latter is weakly referenced by the backend instance. Whenever a transaction is started, all known _FlushableIterators are flushed, i.e. they internally change their underlying iterator into a list, so that the transaction can be committed, but users can continue to transparently use them.

Note that this implementation uses the iterable interface of sqlite cursors rather than the DB-API cursor interface. This is less portable, but allows to wrap iterators that are not cursors but relying on a cursor, e.g.::

   return _FlusableIterator(( r[1] for r in conn.execute(query) ), be)
Instance Methods [hide private]
 
__init__(self, cursor, backend)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__iter__(self) source code
 
flush(self)
Flush the underlying cursor.
source code
 
next(self) source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

Properties [hide private]
  _cursor

Inherited from object: __class__

Method Details [hide private]

__init__(self, cursor, backend)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)