def __init__(self, connection, callback): log.Logger.__init__(self, connection) assert callable(callback), type(callback) self.connection = IRevisionStore(connection) self._callback = callback self._buffer = list()
class RevisionAnalytic(log.Logger): ''' The point of this class is to analyze if the document change notification has been caused the same or different database connection. It wraps around a callback and adds the own_change flag parameter. It uses private interface of Connection to get the information of the known revisions. ''' def __init__(self, connection, callback): log.Logger.__init__(self, connection) assert callable(callback), type(callback) self.connection = IRevisionStore(connection) self._callback = callback self._buffer = list() def on_change(self, doc_id, rev, deleted): self.log('Change notification received doc_id: %r, rev: %r, ' 'deleted: %r', doc_id, rev, deleted) if self.connection.analyzes_locked: self.connection.wait_unlocked(self.unlocked) self._buffer.append((doc_id, rev, deleted)) else: self.process_change(doc_id, rev, deleted) def unlocked(self): while len(self._buffer) > 0: self.process_change(*self._buffer.pop(0)) def process_change(self, doc_id, rev, deleted): own_change = False if doc_id in self.connection.known_revisions: rev_index, rev_hash = _parse_doc_revision(rev) last_index, last_hash = self.connection.known_revisions[doc_id] if last_index > rev_index: own_change = True if (last_index == rev_index) and (last_hash == rev_hash): own_change = True self._callback(doc_id, rev, deleted, own_change)