def split_bucket(self, old_bucket_index): """ Split the specified KBucket into two new buckets which together cover the same range in the key/ID space. @param old_bucket_index: The index of KBucket to split (in this table's list of KBuckets) @type old_bucket_index: int """ # Halve the range of the current (old) KBucket. old_bucket = self.buckets[old_bucket_index] split_point = (old_bucket.range_max - (old_bucket.range_max - old_bucket.range_min) // 2) # Create a new KBucket to cover the range split off from the old one. new_bucket = kbucket.KBucket(split_point, old_bucket.range_max, self.market_id) old_bucket.range_max = split_point # Now, add the new bucket into the routing table tree self.buckets.insert(old_bucket_index + 1, new_bucket) # Finally, copy all nodes that belong to the new KBucket into it... for contact in old_bucket.contacts: if new_bucket.key_in_range(contact.guid): new_bucket.add_contact(contact) # ...and remove them from the old bucket for contact in new_bucket.contacts: old_bucket.remove_contact(contact)
def test_init(self): k = kbucket.KBucket(1, 2) self.assertEqual(k.lastAccessed, 0) self.assertEqual(k.rangeMin, 1) self.assertEqual(k.rangeMax, 2) self.assertEqual(k.contacts, []) self.assertTrue(hasattr(k, 'log'))
def test_init(self): k = kbucket.KBucket(1, 2, market_id=self.market_id) self.assertEqual(k.last_accessed, 0) self.assertEqual(k.range_min, 1) self.assertEqual(k.range_max, 2) self.assertEqual(k.market_id, self.market_id) self.assertEqual(k.contacts, []) self.assertTrue(hasattr(k, 'log'))
def setUp(self): self.bucket = kbucket.KBucket(self.range_min, self.range_max, market_id=42) low = self.range_min high = low + self.init_contact_count for i in range(low, high): self.bucket.addContact(self._mk_contact_by_num(i))
def _make_kbucket(cls, count=None): if count is None: count = cls.init_contact_count new_kbucket = kbucket.KBucket(cls.range_min, cls.range_max, market_id=cls.market_id) for i in range(cls.range_min, cls.range_min + count): new_kbucket.add_contact(cls._mk_contact_by_num(i)) return new_kbucket
def __init__(self, parent_node_id, market_id): """ Initialize a new OptimizedTreeRoutingTable. For details, see RoutingTable documentation. """ super(OptimizedTreeRoutingTable, self).__init__(parent_node_id, market_id) # Cache containing nodes eligible to replace stale KBucket entries self.replacement_cache = {} self.buckets = [ kbucket.KBucket(range_min=0, range_max=2**constants.BIT_NODE_ID_LEN, market_id=market_id) ]
def _make_KBucket(range_min, range_max, market_id): return kbucket.KBucket(rangeMin=range_min, rangeMax=range_max, market_id=market_id)