/** * Splits the bucket in two equal halves (only in terms of ID range, the number * of elements in the two buckets may differ) and moves peers to the new bucket * if necessary.<br/> * The existing bucket retains the lower IDs; the new bucket will contain the * higher IDs.<br/> * In other words, the bucket is split into two sub-branches in the Kademlia * tree, with the old bucket representing the left branch and the new bucket * representing the right branch. * @return The new bucket * @see split(BigInteger) */ KBucket split() { BigInteger pivot = startId.add(endId).divide(BigInteger.valueOf(2)); return split(pivot); }
KBucket newBucket = bucket.split(); kBuckets.add(bucketIndex+1, newBucket); // the new bucket is one higher than the old bucket newBucket = bucket.split(); kBuckets.add(bucketIndex+1, newBucket); bucketIndex++; bucket = newBucket; newBucket = newBucket.split(); kBuckets.add(bucketIndex+1, newBucket);
@Test public void testSplit() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException { assertEquals("K must be an even number for this test to work.", 0, K%2); for (KBucket bucket: buckets) { KademliaPeer[] peers = destinationMap.get(bucket); int originalDepth = KademliaTestUtil.getDepth(bucket); for (int i=0; i<K; i++) { assertFalse(bucket.shouldSplit(peers[i])); bucket.addOrUpdate(peers[i]); } assertTrue(bucket.shouldSplit(peers[K])); KBucket newBucket = bucket.split(); assertNotNull(newBucket); assertEquals(originalDepth+1, KademliaTestUtil.getDepth(bucket)); assertEquals(originalDepth+1, KademliaTestUtil.getDepth(bucket)); assertTrue(maxId(bucket).compareTo(minId(newBucket)) < 0); } }