Exemple #1
0
class ReadOnlyLogger:

    __version__ = '0.0.0'

    def __init__(self, backend):
        self._backend = TranslatingBackend(Log, backend)

    def get(self, ref):
        return self._backend.get(ref)

    def latest_snapshot(self):
        return self._backend.first(Q('operation', 'eq', Operation.SNAPSHOT),
                                   O('modified_on', O.DESCENDING))

    def after(self, timestamp):
        return self._backend.query(Q('modified_on', 'gt', timestamp),
                                   O('modified_on', O.ASCENDING))

    def history(self, record_id):
        """Returns all logs pretaining to the specified record_id newest to oldest
            >>> self.history('myid')[0]  # Current state
            >>> self.history('myid')[-1]  # Initial value, generally a rename or create
        """
        return self._backend.query(Q('record_id', 'eq', record_id),
                                   O('modified_on', O.DESCENDING))

    def list(self, order=None):
        return self._backend.list(order)

    def bulk_read(self, keys):
        return self._backend.query(Q('ref', 'in', keys))

    def __repr__(self):
        return '<{}({})>'.format(self.__class__.__name__, self._backend)
Exemple #2
0
class ReadOnlyLogger:

    __version__ = "0.0.0"

    def __init__(self, backend):
        self._backend = TranslatingBackend(Log, backend)

    def get(self, ref):
        return self._backend.get(ref)

    def latest_snapshot(self):
        return self._backend.first(Q("operation", "eq", Operation.SNAPSHOT), O("modified_on", O.DESCENDING))

    def after(self, timestamp):
        return self._backend.query(Q("modified_on", "gt", timestamp), O("modified_on", O.ASCENDING))

    def history(self, record_id):
        """Returns all logs pretaining to the specified record_id newest to oldest
            >>> self.history('myid')[0]  # Current state
            >>> self.history('myid')[-1]  # Initial value, generally a rename or create
        """
        return self._backend.query(Q("record_id", "eq", record_id), O("modified_on", O.DESCENDING))

    def list(self, order=None):
        return self._backend.list(order)

    def bulk_read(self, keys):
        return self._backend.query(Q("ref", "in", keys))

    def __repr__(self):
        return "<{}({})>".format(self.__class__.__name__, self._backend)
Exemple #3
0
class State:

    def __init__(self, backend):
        self._backend = TranslatingBackend(Document, backend)

    def clear(self):
        self._backend.unset_all()

    def get(self, key):
        return self._backend.get(key)

    def list(self):
        return self._backend.list()

    def keys(self):
        return self._backend.keys()

    def _create(self, log, data, safe=True):
        doc = Document.create(log, data)
        self._backend.set(log.record_id, doc)
        return doc

    def _rename(self, log, data, safe=True):
        if 'to' in log.operation_parameters:
            self._backend.unset(log.record_id)
        else:
            doc = Document.create(log, data)
            self._backend.set(log.record_id, doc)
            return doc

    def _delete(self, log, data, safe=True):
        self._backend.unset(log.record_id)

    def _replace(self, log, data, safe=True):
        return self._create(log, data)

    def _update(self, log, data, safe=True):
        return self._create(log, data, safe=True)

    def apply(self, log, data, safe=True):
        # TODO implement safe
        # Snapshot logs may be out of order, which should not matter
        # Deleting a key that doesn't exist would cause an exception, safe would just swallow that failure
        try:
            op = {
                Operation.CREATE: self._create,
                Operation.DELETE: self._delete,
                Operation.RENAME: self._rename,
                Operation.REPLACE: self._replace,
                Operation.UPDATE: self._update,
            }[log.operation]
        except KeyError:
            raise Exception('Unknown operation {}'.format(log.operation))
        return op(log, data, safe=safe)

    def __repr__(self):
        return '<{}({})>'.format(self.__class__.__name__, self._backend)
Exemple #4
0
class State:
    def __init__(self, backend):
        self._backend = TranslatingBackend(Document, backend)

    def clear(self):
        self._backend.unset_all()

    def get(self, key):
        return self._backend.get(key)

    def list(self):
        return self._backend.list()

    def keys(self):
        return self._backend.keys()

    def _create(self, log, data, safe=True):
        doc = Document.create(log, data)
        self._backend.set(log.record_id, doc)
        return doc

    def _rename(self, log, data, safe=True):
        if "to" in log.operation_parameters:
            self._backend.unset(log.record_id)
        else:
            doc = Document.create(log, data)
            self._backend.set(log.record_id, doc)
            return doc

    def _delete(self, log, data, safe=True):
        self._backend.unset(log.record_id)

    def _replace(self, log, data, safe=True):
        return self._create(log, data)

    def _update(self, log, data, safe=True):
        return self._create(log, data, safe=True)

    def apply(self, log, data, safe=True):
        # TODO implement safe
        # Snapshot logs may be out of order, which should not matter
        # Deleting a key that doesn't exist would cause an exception, safe would just swallow that failure
        try:
            op = {
                Operation.CREATE: self._create,
                Operation.DELETE: self._delete,
                Operation.RENAME: self._rename,
                Operation.REPLACE: self._replace,
                Operation.UPDATE: self._update,
            }[log.operation]
        except KeyError:
            raise Exception("Unknown operation {}".format(log.operation))
        return op(log, data, safe=safe)

    def __repr__(self):
        return "<{}({})>".format(self.__class__.__name__, self._backend)