本文整理汇总了Python中mongo_connector.oplog_manager.OplogThread.retrieve_doc方法的典型用法代码示例。如果您正苦于以下问题:Python OplogThread.retrieve_doc方法的具体用法?Python OplogThread.retrieve_doc怎么用?Python OplogThread.retrieve_doc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongo_connector.oplog_manager.OplogThread
的用法示例。
在下文中一共展示了OplogThread.retrieve_doc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestOplogManagerSharded
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import retrieve_doc [as 别名]
#.........这里部分代码省略.........
oplog_progress_dict=oplog_progress,
namespace_set=["test.mcsharded", "test.mcunsharded"],
auth_key=None,
auth_username=None
)
self.opman2 = OplogThread(
primary_conn=self.shard2_conn,
main_address='%s:%d' % (mongo_host, self.mongos_p),
oplog_coll=self.shard2_conn["local"]["oplog.rs"],
is_sharded=True,
doc_manager=doc_manager,
oplog_progress_dict=oplog_progress,
namespace_set=["test.mcsharded", "test.mcunsharded"],
auth_key=None,
auth_username=None
)
def tearDown(self):
try:
self.opman1.join()
except RuntimeError:
pass # thread may not have been started
try:
self.opman2.join()
except RuntimeError:
pass # thread may not have been started
self.mongos_conn.close()
self.shard1_conn.close()
self.shard2_conn.close()
self.shard1_secondary_conn.close()
self.shard2_secondary_conn.close()
kill_all()
def test_retrieve_doc(self):
""" Test the retrieve_doc method """
# Trivial case where the oplog entry is None
self.assertEqual(self.opman1.retrieve_doc(None), None)
# Retrieve a document from insert operation in oplog
doc = {"name": "mango", "type": "fruit",
"ns": "test.mcsharded", "weight": 3.24, "i": 1}
self.mongos_conn["test"]["mcsharded"].insert(doc)
oplog_entries = self.shard1_conn["local"]["oplog.rs"].find(
sort=[("ts", pymongo.DESCENDING)],
limit=1
)
oplog_entry = next(oplog_entries)
self.assertEqual(self.opman1.retrieve_doc(oplog_entry), doc)
# Retrieve a document from update operation in oplog
self.mongos_conn["test"]["mcsharded"].update(
{"i": 1},
{"$set": {"sounds-like": "mongo"}}
)
oplog_entries = self.shard1_conn["local"]["oplog.rs"].find(
sort=[("ts", pymongo.DESCENDING)],
limit=1
)
doc["sounds-like"] = "mongo"
self.assertEqual(self.opman1.retrieve_doc(next(oplog_entries)), doc)
# Retrieve a document from remove operation in oplog
# (expected: None)
self.mongos_conn["test"]["mcsharded"].remove({
"i": 1
示例2: TestOplogManager
# 需要导入模块: from mongo_connector.oplog_manager import OplogThread [as 别名]
# 或者: from mongo_connector.oplog_manager.OplogThread import retrieve_doc [as 别名]
class TestOplogManager(unittest.TestCase):
"""Defines all the testing methods, as well as a method that sets up the
cluster
"""
def setUp(self):
_, _, self.primary_p = start_replica_set('test-oplog-manager')
self.primary_conn = pymongo.MongoClient(mongo_host, self.primary_p)
self.oplog_coll = self.primary_conn.local['oplog.rs']
self.opman = OplogThread(
primary_conn=self.primary_conn,
main_address='%s:%d' % (mongo_host, self.primary_p),
oplog_coll=self.oplog_coll,
is_sharded=False,
doc_manager=DocManager(),
oplog_progress_dict=LockingDict(),
namespace_set=None,
auth_key=None,
auth_username=None,
repl_set='test-oplog-manager'
)
def tearDown(self):
try:
self.opman.join()
except RuntimeError:
pass # OplogThread may not have been started
self.primary_conn.close()
kill_replica_set('test-oplog-manager')
def test_retrieve_doc(self):
""" Test the retrieve_doc method """
# Trivial case where the oplog entry is None
self.assertEqual(self.opman.retrieve_doc(None), None)
# Retrieve a document from insert operation in oplog
doc = {"name": "mango", "type": "fruit",
"ns": "test.test", "weight": 3.24, "i": 1}
self.primary_conn["test"]["test"].insert(doc)
oplog_entries = self.primary_conn["local"]["oplog.rs"].find(
sort=[("ts", pymongo.DESCENDING)],
limit=1
)
oplog_entry = next(oplog_entries)
self.assertEqual(self.opman.retrieve_doc(oplog_entry), doc)
# Retrieve a document from update operation in oplog
self.primary_conn["test"]["test"].update(
{"i": 1},
{"$set": {"sounds-like": "mongo"}}
)
oplog_entries = self.primary_conn["local"]["oplog.rs"].find(
sort=[("ts", pymongo.DESCENDING)],
limit=1
)
doc["sounds-like"] = "mongo"
self.assertEqual(self.opman.retrieve_doc(next(oplog_entries)), doc)
# Retrieve a document from remove operation in oplog
# (expected: None)
self.primary_conn["test"]["test"].remove({
"i": 1
})
oplog_entries = self.primary_conn["local"]["oplog.rs"].find(
sort=[("ts", pymongo.DESCENDING)],
limit=1
)
self.assertEqual(self.opman.retrieve_doc(next(oplog_entries)), None)
# Retrieve a document with bad _id
# (expected: None)
oplog_entry["o"]["_id"] = "ThisIsNotAnId123456789"
self.assertEqual(self.opman.retrieve_doc(oplog_entry), None)
def test_get_oplog_cursor(self):
'''Test the get_oplog_cursor method'''
# Trivial case: timestamp is None
self.assertEqual(self.opman.get_oplog_cursor(None), None)
# earliest entry is after given timestamp
doc = {"ts": bson.Timestamp(1000, 0), "i": 1}
self.primary_conn["test"]["test"].insert(doc)
self.assertEqual(self.opman.get_oplog_cursor(
bson.Timestamp(1, 0)), None)
# earliest entry is the only one at/after timestamp
latest_timestamp = self.opman.get_last_oplog_timestamp()
cursor = self.opman.get_oplog_cursor(latest_timestamp)
self.assertNotEqual(cursor, None)
self.assertEqual(cursor.count(), 1)
self.assertEqual(self.opman.retrieve_doc(next(cursor)), doc)
# many entries before and after timestamp
self.primary_conn["test"]["test"].insert(
{"i": i} for i in range(2, 1002))
oplog_cursor = self.oplog_coll.find(
sort=[("ts", pymongo.ASCENDING)]
)
#.........这里部分代码省略.........