コード例 #1
0
ファイル: transaction.py プロジェクト: stungkit/tinyrecord
class transaction(object):
    """
    Create an atomic transaction for the given
    *table*. All IO actions during the transaction are
    executed within a database-local lock.

    :param table: A TinyDB table.
    """

    _locks = WeakKeyDictionary()

    def __init__(self, table):
        self.record = Changeset(table)
        self.lock = (self._locks.get(table)
                     or self._locks.setdefault(table, Lock()))

    update_callable = records(UpdateCallable)
    insert_multiple = records(InsertMultiple)
    remove = records(Remove)

    def insert(self, row):
        return self.insert_multiple((row, ))

    def update(self, fields, query=null_query, doc_ids=[], eids=[]):
        updator = lambda doc: doc.update(fields)
        if eids and doc_ids:
            raise TypeError('cannot pass both eids and doc_ids')
        return self.update_callable(updator, query, doc_ids, eids)

    def __enter__(self):
        """
        Enter a transaction.
        """
        return self

    def __exit__(self, type, value, traceback):
        """
        Commits the transaction and raises a traceback
        if it is not an ``AbortSignal``. All actions
        are executed within a lock.
        """
        if not traceback:
            with self.lock:
                self.record.execute()
        return isinstance(value, AbortSignal)
コード例 #2
0
ファイル: transaction.py プロジェクト: eugene-eeo/tinyrecord
class transaction(object):
    """
    Create an atomic transaction for the given
    *table*. All IO actions during the transaction are
    executed within a database-local lock.

    :param table: A TinyDB table.
    """

    _locks = WeakKeyDictionary()

    def __init__(self, table):
        self.record = Changeset(table)
        self.lock = (self._locks.get(table) or
                     self._locks.setdefault(table, Lock()))

    update_callable = records(UpdateCallable)
    insert_multiple = records(InsertMultiple)
    remove = records(Remove)

    def insert(self, row):
        return self.insert_multiple((row,))

    def update(self, fields, query=null_query, doc_ids=[], eids=[]):
        updator = lambda doc: doc.update(fields)
        if eids and doc_ids:
            raise TypeError('cannot pass both eids and doc_ids')
        return self.update_callable(updator, query, doc_ids, eids)

    def __enter__(self):
        """
        Enter a transaction.
        """
        return self

    def __exit__(self, type, value, traceback):
        """
        Commits the transaction and raises a traceback
        if it is not an ``AbortSignal``. All actions
        are executed within a lock.
        """
        if not traceback:
            with self.lock:
                self.record.execute()
        return isinstance(value, AbortSignal)
コード例 #3
0
ファイル: transaction.py プロジェクト: raychorn/tinyrecord
class transaction:
    """
    Create an atomic transaction for the given
    *table*. All IO actions during the transaction are
    executed within a database-local lock.

    :param table: A TinyDB table.
    """

    _locks = WeakKeyDictionary()

    def __init__(self, table):
        self.record = Changeset(table)
        self.lock = (self._locks.get(table)
                     or self._locks.setdefault(table, Lock()))

    insert_multiple = records(InsertMultiple)
    update = records(Update)
    remove = records(Remove)

    def insert(self, row):
        return self.insert_multiple((row, ))

    def __enter__(self):
        """
        Enter a transaction.
        """
        return self

    def __exit__(self, type, value, traceback):
        """
        Commits the transaction and raises a traceback
        if it is not an ``AbortSignal``. All actions
        are executed within a lock.
        """
        if not traceback:
            with self.lock:
                self.record.execute()
        return isinstance(value, AbortSignal)
コード例 #4
0
ファイル: transaction.py プロジェクト: carriercomm/tinyrecord
 def __init__(self, table):
     self.record = Changeset(table)
     self.lock = (self._locks.get(table) or
                  self._locks.setdefault(table, RLock()))
コード例 #5
0
ファイル: transaction.py プロジェクト: stungkit/tinyrecord
 def __init__(self, table):
     self.record = Changeset(table)
     self.lock = (self._locks.get(table)
                  or self._locks.setdefault(table, Lock()))