# Path: mongodb_consistent_backup/Errors.py
# class OperationError(Error):
#     """Raised when an operation failed in an expected but unrecoverable way"""
#     pass


import os
import logging
from gzip import GzipFile
from bson import BSON, decode_file_iter
from bson.codec_options import CodecOptions
from time import time
from mongodb_consistent_backup.Errors import OperationError
        if self._oplog:
            return self._oplog.read(b)

    def load(self):
        try:
            oplog = self.open()
            logging.debug("Reading oplog file %s" % self.oplog_file)
            for change in decode_file_iter(oplog, CodecOptions(unicode_decode_error_handler="ignore")):
                if 'ts' in change:
                    self._last_ts = change['ts']
                if self._first_ts is None and self._last_ts is not None:
                    self._first_ts = self._last_ts
                self._count += 1
            oplog.close()
        except Exception, e:
            logging.fatal("Error reading oplog file %s! Error: %s" % (self.oplog_file, e))
            raise OperationError(e)

    def add(self, doc, autoflush=True):
        try:
            self._oplog.write(BSON.encode(doc))
            self._writes_unflushed += 1
            self._count            += 1
            if not self._first_ts:
                self._first_ts = doc['ts']
            self._last_ts = doc['ts']
            if autoflush:
                self.autoflush()
        except Exception, e:
            logging.fatal("Cannot write to oplog file %s! Error: %s" % (self.oplog_file, e))

# Path: mongodb_consistent_backup/Errors.py
# class OperationError(Error):
#     """Raised when an operation failed in an expected but unrecoverable way"""
#     pass


# Path: mongodb_consistent_backup/Oplog/Oplog.py
import os
import logging
from gzip import GzipFile
from bson import BSON, decode_file_iter
from bson.codec_options import CodecOptions
from time import time
from mongodb_consistent_backup.Errors import OperationError

        if self._oplog:
            return self._oplog.read(b)

    def load(self):
        try:
            oplog = self.open()
            logging.debug("Reading oplog file %s" % self.oplog_file)
            for change in decode_file_iter(oplog, CodecOptions(unicode_decode_error_handler="ignore")):
                if 'ts' in change:
                    self._last_ts = change['ts']
                if self._first_ts is None and self._last_ts is not None:
                    self._first_ts = self._last_ts
                self._count += 1
            oplog.close()
        except Exception, e:
            logging.fatal("Error reading oplog file %s! Error: %s" % (self.oplog_file, e))
            raise OperationError(e)

    def add(self, doc, autoflush=True):
        try:
            self._oplog.write(BSON.encode(doc))
            self._writes_unflushed += 1
            self._count            += 1
            if not self._first_ts:
                self._first_ts = doc['ts']
            self._last_ts = doc['ts']
            if autoflush:
                self.autoflush()
        except Exception, e:
            logging.fatal("Cannot write to oplog file %s! Error: %s" % (self.oplog_file, e))

            raise OperationError(e)