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