diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java index cbd03d37f3..825eb73585 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/structure/HugeElement.java @@ -59,7 +59,7 @@ public abstract class HugeElement implements Element, GraphType, Idfiable { private static final MutableIntObjectMap> EMPTY_MAP = - new IntObjectHashMap<>(); + CollectionFactory.newIntObjectMap(); private static final int MAX_PROPERTIES = BytesBuffer.UINT16_MAX; private final HugeGraph graph; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/SingleThreadObjectIntMapping.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/SingleThreadObjectIntMapping.java index 4cf5d0198d..b254565adb 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/SingleThreadObjectIntMapping.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/util/collection/SingleThreadObjectIntMapping.java @@ -28,6 +28,7 @@ public class SingleThreadObjectIntMapping implements ObjectIntMapping { private static final int MAGIC = 1 << 16; + private final IntObjectHashMap int2IdMap; public SingleThreadObjectIntMapping() { @@ -37,19 +38,22 @@ public SingleThreadObjectIntMapping() { @Watched @SuppressWarnings("unchecked") public int object2Code(Object object) { - int key = object.hashCode(); + int code = object.hashCode(); // TODO: improve hash algorithm for (int i = 1; i > 0; i <<= 1) { - for (int j = 0; i >= MAGIC && j < 10; j++) { - Id existed = (Id) this.int2IdMap.get(key); + for (int j = 0; j < 10; j++) { + Id existed = (Id) this.int2IdMap.get(code); if (existed == null) { - this.int2IdMap.put(key, (V) object); - return key; + this.int2IdMap.put(code, (V) object); + return code; } if (existed.equals(object)) { - return key; + return code; + } + code = code + i + j; + if (i < MAGIC) { + break; } - key = key + i + j; } } throw new HugeException("Failed to get code for id: %s", object); diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/Int2IntsMapTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/Int2IntsMapTest.java index 52b2ca1b81..cfe8abbcf3 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/Int2IntsMapTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/Int2IntsMapTest.java @@ -104,11 +104,11 @@ public void testInt2IntsMap() { 263,264,265,266,267,268,269}; results[4] = new int[]{270,271,272,273,274,275,276,277,278,279, 280,281,282,283,284,285,286,287,288,289, - 290, 291,292,293,294,295,296,297,298,299}; + 290,291,292,293,294,295,296,297,298,299}; for (int ii = 0; ii < 5; ii++) { int[] result = map.getValues(ii + 1); - Assert.assertTrue(Arrays.equals(results[ii], result)); + Assert.assertArrayEquals(results[ii], result); } }