Commit 5410225c authored by Grégory Wisniewski's avatar Grégory Wisniewski

Use a set instead of a dict

Signed-off-by: default avatarGrégory <gregory@nexedi.com>

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2648 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent a8e67b20
......@@ -26,9 +26,11 @@ from neo.lib.util import dump
class Partition(object):
"""This class abstracts the state of a partition."""
def __init__(self, offset):
def __init__(self, offset, tid):
self.offset = offset
self.tid = None
if tid is None:
tid = ZERO_TID
self.tid = tid
def getOffset(self):
return self.offset
......@@ -36,11 +38,6 @@ class Partition(object):
def getCriticalTID(self):
return self.tid
def setCriticalTID(self, tid):
if tid is None:
tid = ZERO_TID
self.tid = tid
def safe(self, min_pending_tid):
tid = self.tid
return tid is not None and (
......@@ -117,7 +114,7 @@ class Replicator(object):
namely, a list of serials. This is also done part by part, and
I ask only non-existing data. """
# new_partition_dict
# new_partition_set
# outdated partitions for which no critical tid was asked to primary
# master yet
# critical_tid_list
......@@ -148,7 +145,7 @@ class Replicator(object):
def __init__(self, app):
self.app = app
self.new_partition_dict = {}
self.new_partition_set = set()
self.critical_tid_list = []
self.partition_dict = {}
self.task_list = []
......@@ -174,9 +171,8 @@ class Replicator(object):
table is the one accepted by primary master.
Implies a reset.
"""
self.new_partition_dict = {}
for offset in self.app.pt.getOutdatedOffsetListFor(self.app.uuid):
self.new_partition_dict[offset] = Partition(offset)
partition_list = self.app.pt.getOutdatedOffsetListFor(self.app.uuid)
self.new_partition_set = set(partition_list)
self.partition_dict = {}
self.reset()
......@@ -191,7 +187,8 @@ class Replicator(object):
def pending(self):
"""Return whether there is any pending partition."""
return len(self.partition_dict) or len(self.new_partition_dict)
return len(self.partition_dict) or len(self.new_partition_set) \
or self.critical_tid_list
def getCurrentOffset(self):
assert self.current_partition is not None
......@@ -211,16 +208,15 @@ class Replicator(object):
def setCriticalTID(self, tid):
"""This is a callback from MasterOperationHandler."""
neo.lib.logging.debug('setting critical TID %s to %s', dump(tid),
', '.join([str(p.getOffset()) for p in self.critical_tid_list]))
for partition in self.critical_tid_list:
partition.setCriticalTID(tid)
', '.join([str(p) for p in self.critical_tid_list]))
for offset in self.critical_tid_list:
self.partition_dict[offset] = Partition(offset, tid)
self.critical_tid_list = []
def _askCriticalTID(self):
self.app.master_conn.ask(Packets.AskLastIDs())
self.critical_tid_list.extend(self.new_partition_dict.values())
self.partition_dict.update(self.new_partition_dict)
self.new_partition_dict = {}
self.critical_tid_list.extend(self.new_partition_set)
self.new_partition_set.clear()
def setUnfinishedTIDList(self, tid_list):
"""This is a callback from MasterOperationHandler."""
......@@ -289,7 +285,7 @@ class Replicator(object):
def act(self):
# If the new partition list is not empty, I must ask a critical
# TID to a primary master node.
if self.new_partition_dict:
if self.new_partition_set:
self._askCriticalTID()
if self.current_partition is not None:
......@@ -335,13 +331,12 @@ class Replicator(object):
def removePartition(self, offset):
"""This is a callback from MasterOperationHandler."""
self.partition_dict.pop(offset, None)
self.new_partition_dict.pop(offset, None)
self.new_partition_set.discard(offset)
def addPartition(self, offset):
"""This is a callback from MasterOperationHandler."""
if not self.partition_dict.has_key(offset) \
and not self.new_partition_dict.has_key(offset):
self.new_partition_dict[offset] = Partition(offset)
if not self.partition_dict.has_key(offset):
self.new_partition_set.add(offset)
def _addTask(self, key, func, args=(), kw=None):
task = Task(func, args, kw)
......
......@@ -39,14 +39,10 @@ class StorageReplicatorTests(NeoUnitTestBase):
'getOutdatedOffsetListFor': [0],
})
replicator = Replicator(app)
self.assertEqual(replicator.new_partition_dict, {})
self.assertEqual(replicator.new_partition_set, set())
replicator.replication_done = False
replicator.populate()
self.assertEqual(len(replicator.new_partition_dict), 1)
partition = replicator.new_partition_dict[0]
self.assertEqual(partition.getOffset(), 0)
self.assertEqual(partition.getCriticalTID(), None)
self.assertTrue(replicator.replication_done)
self.assertEqual(replicator.new_partition_set, set([0]))
def test_reset(self):
replicator = Replicator(None)
......@@ -66,15 +62,9 @@ class StorageReplicatorTests(NeoUnitTestBase):
def test_setCriticalTID(self):
replicator = Replicator(None)
partition_list = [Partition(0), Partition(5)]
replicator.critical_tid_list = partition_list[:]
critical_tid = self.getNextTID()
for partition in partition_list:
self.assertEqual(partition.getCriticalTID(), None)
replicator.setCriticalTID(critical_tid)
self.assertEqual(replicator.critical_tid_list, [])
for partition in partition_list:
self.assertEqual(partition.getCriticalTID(), critical_tid)
partition = Partition(0, critical_tid)
self.assertEqual(partition.getCriticalTID(), critical_tid)
def test_setUnfinishedTIDList(self):
replicator = Replicator(None)
......@@ -134,7 +124,7 @@ class StorageReplicatorTests(NeoUnitTestBase):
last_ids, unfinished_tids = [x.getParam(0) for x in \
app.master_conn.mockGetNamedCalls('ask')]
self.assertEqual(last_ids.getType(), Packets.AskLastIDs)
self.assertFalse(replicator.new_partition_dict)
self.assertFalse(replicator.new_partition_set)
self.assertEqual(unfinished_tids.getType(),
Packets.AskUnfinishedTransactions)
self.assertTrue(replicator.waiting_for_unfinished_tids)
......@@ -192,32 +182,29 @@ class StorageReplicatorTests(NeoUnitTestBase):
def test_removePartition(self):
replicator = Replicator(None)
replicator.partition_dict = {0: None, 2: None}
replicator.new_partition_dict = {1: None}
replicator.new_partition_set = set([1])
replicator.removePartition(0)
self.assertEqual(replicator.partition_dict, {2: None})
self.assertEqual(replicator.new_partition_dict, {1: None})
self.assertEqual(replicator.new_partition_set, set([1]))
replicator.removePartition(1)
replicator.removePartition(2)
self.assertEqual(replicator.partition_dict, {})
self.assertEqual(replicator.new_partition_dict, {})
self.assertEqual(replicator.new_partition_set, set())
# Must not raise
replicator.removePartition(3)
def test_addPartition(self):
replicator = Replicator(None)
replicator.partition_dict = {0: None}
replicator.new_partition_dict = {1: None}
replicator.new_partition_set = set([1])
replicator.addPartition(0)
replicator.addPartition(1)
self.assertEqual(replicator.partition_dict, {0: None})
self.assertEqual(replicator.new_partition_dict, {1: None})
self.assertEqual(replicator.new_partition_set, set([1]))
replicator.addPartition(2)
self.assertEqual(replicator.partition_dict, {0: None})
self.assertEqual(len(replicator.new_partition_dict), 2)
self.assertEqual(replicator.new_partition_dict[1], None)
partition = replicator.new_partition_dict[2]
self.assertEqual(partition.getOffset(), 2)
self.assertEqual(partition.getCriticalTID(), None)
self.assertEqual(len(replicator.new_partition_set), 2)
self.assertEqual(replicator.new_partition_set, set([1, 2]))
def test_processDelayedTasks(self):
replicator = Replicator(None)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment