Skip to content

Commit b2849aa

Browse files
committed
Finish rename Oplog.Oplog
1 parent 172ddaf commit b2849aa

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
import logging
3+
4+
from gzip import GzipFile
5+
# Skip bson in requirements , pymongo provides
6+
# noinspection PyPackageRequirements
7+
from bson import decode_file_iter
8+
9+
10+
class Oplog:
11+
def __init__(self, oplog_file, dump_gzip=False):
12+
self.oplog_file = oplog_file
13+
self.dump_gzip = dump_gzip
14+
15+
self._count = 0
16+
self._first_ts = None
17+
self._last_ts = None
18+
19+
self.read()
20+
21+
def read(self):
22+
if os.path.isfile(self.oplog_file):
23+
try:
24+
logging.debug("Reading oplog file %s" % self.oplog_file)
25+
26+
if self.dump_gzip:
27+
oplog = GzipFile(self.oplog_file)
28+
else:
29+
oplog = open(self.oplog_file)
30+
31+
for change in decode_file_iter(oplog):
32+
if 'ts' in change:
33+
self._last_ts = change['ts']
34+
if self._first_ts is None and self._last_ts is not None:
35+
self._first_ts = self._last_ts
36+
self._count += 1
37+
oplog.close()
38+
except Exception, e:
39+
logging.fatal("Error reading oplog file %s! Error: %s" % (self.oplog_file, e))
40+
raise e
41+
42+
def count(self):
43+
return self._count
44+
45+
def first_ts(self):
46+
return self._first_ts
47+
48+
def last_ts(self):
49+
return self._last_ts

0 commit comments

Comments
 (0)