Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
Change-Id: I14abe49850d42f34576c6d1837ed5b620ffbda21
  • Loading branch information
zhoney committed Jun 3, 2021
1 parent 7fbe6e4 commit 8427bdf
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
public abstract class HugeElement implements Element, GraphType, Idfiable {

private static final MutableIntObjectMap<HugeProperty<?>> EMPTY_MAP =
new IntObjectHashMap<>();
CollectionFactory.newIntObjectMap();
private static final int MAX_PROPERTIES = BytesBuffer.UINT16_MAX;

private final HugeGraph graph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public class SingleThreadObjectIntMapping<V> implements ObjectIntMapping<V> {

private static final int MAGIC = 1 << 16;

private final IntObjectHashMap<V> int2IdMap;

public SingleThreadObjectIntMapping() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 8427bdf

Please sign in to comment.