Skip to content

Commit

Permalink
Modify the consistent hash
Browse files Browse the repository at this point in the history
Based on crossoverJie’s review, made code modifications
  • Loading branch information
baiyina committed Sep 12, 2024
1 parent bc373f9 commit 758cfeb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected void sort(){}
* @return
*/
public String process(List<String> values,String key){

// fix https://github.com/crossoverJie/cim/issues/79
clear();
for (String value : values) {
add(hash(value), value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.crossoverjie.cim.common.route.algorithm.consistenthash;

import com.crossoverjie.cim.common.data.construct.SortArrayMap;
import com.google.common.annotations.VisibleForTesting;

import java.util.TreeMap;

/**
* Function:自定义排序 Map 实现
Expand Down Expand Up @@ -32,6 +35,15 @@ public void sort() {
sortArrayMap.sort();
}

/**
* Used only in test.
* @return Return the data structure of the current algorithm.
*/
@VisibleForTesting
public SortArrayMap getSortArrayMap() {
return sortArrayMap;
}

@Override
protected void clear() {
sortArrayMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.crossoverjie.cim.common.enums.StatusEnum;
import com.crossoverjie.cim.common.exception.CIMException;
import com.google.common.annotations.VisibleForTesting;

import java.util.SortedMap;
import java.util.TreeMap;
Expand Down Expand Up @@ -35,6 +36,15 @@ protected void clear() {
treeMap.clear();
}

/**
* Used only in test.
* @return Return the data structure of the current algorithm.
*/
@VisibleForTesting
public TreeMap getTreeMap() {
return treeMap;
}

@Override
public String getFirstNodeValue(String value) {
long hash = super.hash(value);
Expand All @@ -48,4 +58,5 @@ public String getFirstNodeValue(String value) {
}
return treeMap.firstEntry().getValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,17 @@ public void getFirstNodeValue8() {

@Test
public void testVirtualNode() throws NoSuchFieldException, IllegalAccessException {
AbstractConsistentHash map = new SortArrayMapConsistentHash();
SortArrayMapConsistentHash map = new SortArrayMapConsistentHash();

List<String> strings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
strings.add("127.0.0." + i);
}

String process = map.process(strings,"zhangsan");
Field sortArrayMapField = SortArrayMapConsistentHash.class.getDeclaredField("sortArrayMap");
sortArrayMapField.setAccessible(true);
Field virtualNodeSizeField = SortArrayMapConsistentHash.class.getDeclaredField("VIRTUAL_NODE_SIZE");
virtualNodeSizeField.setAccessible(true);

SortArrayMap sortArrayMap = (SortArrayMap) sortArrayMapField.get(map);
int virtualNodeSize = (int) virtualNodeSizeField.get(map);
SortArrayMap sortArrayMap = map.getSortArrayMap();
int virtualNodeSize = 2;

System.out.println("sortArrayMapSize = " + sortArrayMap.size() + "\n" + "virtualNodeSize = " + virtualNodeSize);
Assert.assertEquals(sortArrayMap.size(), (virtualNodeSize + 1) * 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public void getFirstNodeValue2() {
String process = map.process(strings, "zhangsan2");
Assert.assertEquals(PROCESS, process);
}


// Assert.assertEquals("127.0.0.9",process);
}


Expand Down Expand Up @@ -79,21 +76,17 @@ public void getFirstNodeValue4() {

@Test
public void testVirtualNode() throws NoSuchFieldException, IllegalAccessException {
AbstractConsistentHash map = new TreeMapConsistentHash();
TreeMapConsistentHash map = new TreeMapConsistentHash();

List<String> strings = new ArrayList<>();
for (int i = 0; i < 10; i++) {
strings.add("127.0.0." + i);
}

String process = map.process(strings,"zhangsan");
Field treeMapField = TreeMapConsistentHash.class.getDeclaredField("treeMap");
treeMapField.setAccessible(true);
Field virtualNodeSizeField = TreeMapConsistentHash.class.getDeclaredField("VIRTUAL_NODE_SIZE");
virtualNodeSizeField.setAccessible(true);

TreeMap treeMap = (TreeMap) treeMapField.get(map);
int virtualNodeSize = (int) virtualNodeSizeField.get(map);
TreeMap treeMap = map.getTreeMap();
int virtualNodeSize = 2;

System.out.println("treeMapSize = " + treeMap.size() + "\n" + "virtualNodeSize = " + virtualNodeSize);
Assert.assertEquals(treeMap.size(), (virtualNodeSize + 1) * 10);
Expand Down

0 comments on commit 758cfeb

Please sign in to comment.