Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add IntMapByDynamicHash V1 implement #2377

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: fix some review
  • Loading branch information
conghuhu committed Dec 6, 2023
commit 326de034f01a44916e715839e29cb314898f9233
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
*/
public IntMapByDynamicHash(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Illegal Initial Capacity: " + initialCapacity);

Check warning on line 85 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L85

Added line #L85 was not covered by tests
}
if (initialCapacity > MAXIMUM_CAPACITY) {
initialCapacity = MAXIMUM_CAPACITY;

Check warning on line 88 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L88

Added line #L88 was not covered by tests
}
long size = (long) (1.0 + (long) initialCapacity / LOAD_FACTOR);
int cap = (size >= (long) MAXIMUM_CAPACITY) ?
Expand All @@ -93,7 +93,7 @@
if (cap >= PARTITIONED_SIZE_THRESHOLD) {
// we want 7 extra slots, and 64 bytes for each slot int are 4 bytes,
// so 64 bytes are 16 ints.
this.partitionedSize =

Check warning on line 96 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L96

Added line #L96 was not covered by tests
new int[SIZE_BUCKETS * 16];
}
// The end index is for resizeContainer
Expand Down Expand Up @@ -124,15 +124,17 @@
}

/* ---------------- Table element access -------------- */

private static long entryOffset(int index) {
return ((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE;
}

private static Object tableAt(Object[] array, int index) {
return UNSAFE.getObjectVolatile(array,
((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE);
return UNSAFE.getObjectVolatile(array, entryOffset(index));
}

private static boolean casTableAt(Object[] array, int index, Object expected, Object newValue) {
return UNSAFE.compareAndSwapObject(array,
((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE,
expected, newValue);
return UNSAFE.compareAndSwapObject(array, entryOffset(index), expected, newValue);
}

/**
Expand Down Expand Up @@ -205,7 +207,7 @@
if (IntMapByDynamicHash.casTableAt(currentTable, index, o, newEntry)) {
return oldVal;
}
} else {

Check warning on line 210 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L210

Added line #L210 was not covered by tests
// Key not found, add a new entry
Entry newEntry = new Entry(key, value, o);
if (IntMapByDynamicHash.casTableAt(currentTable, index, o, newEntry)) {
Expand All @@ -213,7 +215,7 @@
return NULL_VALUE;
}
}
}

Check warning on line 218 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L218

Added line #L218 was not covered by tests
}
}

Expand All @@ -239,7 +241,7 @@
return e.value;
}
}
return NULL_VALUE;

Check warning on line 244 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L244

Added line #L244 was not covered by tests
}

/**
Expand All @@ -265,9 +267,9 @@
if (candidate == key) {
return e.getValue();
}
e = e.getNext();
}
return NULL_VALUE;

Check warning on line 272 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L270-L272

Added lines #L270 - L272 were not covered by tests
}
}
}
Expand All @@ -280,27 +282,27 @@
*/
@Override
public boolean remove(int key) {
int hash = this.hash(key);
Entry[] currentTable = this.table;
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentTable, hash);

Check warning on line 287 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L285-L287

Added lines #L285 - L287 were not covered by tests
if (o == RESIZED || o == RESIZING) {
return this.slowRemove(key, currentTable) != null;
}

Entry e = o;

Check warning on line 292 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L292

Added line #L292 was not covered by tests
while (e != null) {
int candidate = e.getKey();

Check warning on line 294 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L294

Added line #L294 was not covered by tests
if (candidate == key) {
Entry replacement = this.createReplacementChainForRemoval(o, e);

Check warning on line 296 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L296

Added line #L296 was not covered by tests
if (IntMapByDynamicHash.casTableAt(currentTable, hash, o, replacement)) {
this.addToSize(-1);
return true;

Check warning on line 299 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L298-L299

Added lines #L298 - L299 were not covered by tests
}
return this.slowRemove(key, currentTable) != null;
}
e = e.getNext();
}
return false;

Check warning on line 305 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L303-L305

Added lines #L303 - L305 were not covered by tests
}

/**
Expand All @@ -317,35 +319,35 @@
Entry o;

while (true) {
length = currentTable.length;
index = this.hash(key, length);
o = (Entry) IntMapByDynamicHash.tableAt(currentTable, index);

Check warning on line 324 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L322-L324

Added lines #L322 - L324 were not covered by tests
if (o == RESIZED || o == RESIZING) {
currentTable = this.helpWithResizeWhileCurrentIndex(currentTable, index);

Check warning on line 326 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L326

Added line #L326 was not covered by tests
} else {
Entry e = o;
Entry prev = null;

Check warning on line 329 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L328-L329

Added lines #L328 - L329 were not covered by tests

while (e != null) {
int candidate = e.getKey();

Check warning on line 332 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L332

Added line #L332 was not covered by tests
if (candidate == key) {
Entry replacement = this.createReplacementChainForRemoval(o, e);

Check warning on line 334 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L334

Added line #L334 was not covered by tests
if (IntMapByDynamicHash.casTableAt(currentTable, index, o, replacement)) {
this.addToSize(-1);
return e;

Check warning on line 337 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L336-L337

Added lines #L336 - L337 were not covered by tests
}
// Key found, but CAS failed, restart the loop
break;
}
prev = e;
e = e.getNext();
}

Check warning on line 344 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L342-L344

Added lines #L342 - L344 were not covered by tests

if (prev != null) {
// Key doesn't found
return null;

Check warning on line 348 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L348

Added line #L348 was not covered by tests
}
}

Check warning on line 350 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L350

Added line #L350 was not covered by tests
}
}

Expand Down Expand Up @@ -382,8 +384,8 @@
for (int i = 0; i < currentArray.length - 1; i++) {
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, i);
if (o == RESIZED || o == RESIZING) {
resizeContainer =
(ResizeContainer) IntMapByDynamicHash.tableAt(currentArray,

Check warning on line 388 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L387-L388

Added lines #L387 - L388 were not covered by tests
currentArray.length - 1);
} else if (o != null) {
Entry e = o;
Expand All @@ -399,10 +401,10 @@
}
if (resizeContainer != null) {
if (resizeContainer.isNotDone()) {
this.helpWithResize(currentArray);
resizeContainer.waitForAllResizers();

Check warning on line 405 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L404-L405

Added lines #L404 - L405 were not covered by tests
}
currentArray = resizeContainer.nextArray;

Check warning on line 407 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L407

Added line #L407 was not covered by tests
}
} while (resizeContainer != null);
}
Expand All @@ -420,7 +422,7 @@

@Override
public boolean concurrent() {
return true;

Check warning on line 425 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L425

Added line #L425 was not covered by tests
}

private int hash(int key) {
Expand All @@ -438,7 +440,7 @@
int index = this.hash(key, length);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
currentArray = this.helpWithResizeWhileCurrentIndex(currentArray, index);

Check warning on line 443 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L443

Added line #L443 was not covered by tests
} else {
Entry e = o;
while (e != null) {
Expand All @@ -450,7 +452,7 @@
}
return null;
}
}

Check warning on line 455 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L455

Added line #L455 was not covered by tests
}

private void addToSize(int value) {
Expand All @@ -475,9 +477,9 @@
localSize + value)) {
return true;
}
}

Check warning on line 480 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L480

Added line #L480 was not covered by tests
}
return false;

Check warning on line 482 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L482

Added line #L482 was not covered by tests
}

private void incrementLocalSize(int value) {
Expand All @@ -493,15 +495,15 @@
if (original == toRemove) {
return original.getNext();
}
Entry replacement = null;
Entry e = original;

Check warning on line 499 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L498-L499

Added lines #L498 - L499 were not covered by tests
while (e != null) {
if (e != toRemove) {
replacement = new Entry(e.getKey(), e.getValue(), replacement);

Check warning on line 502 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L502

Added line #L502 was not covered by tests
}
e = e.getNext();

Check warning on line 504 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L504

Added line #L504 was not covered by tests
}
return replacement;

Check warning on line 506 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L506

Added line #L506 was not covered by tests
}

private void incrementSizeAndPossiblyResize(Entry[] currentArray, int length, Entry prev) {
Expand All @@ -519,10 +521,10 @@
Entry[] newArray = this.helpWithResize(currentArray);
int helpCount = 0;
while (IntMapByDynamicHash.tableAt(currentArray, index) != RESIZED) {
helpCount++;
newArray = this.helpWithResize(currentArray);

Check warning on line 525 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L524-L525

Added lines #L524 - L525 were not covered by tests
if ((helpCount & 7) == 0) {
Thread.yield();

Check warning on line 527 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L527

Added line #L527 was not covered by tests
}
}
return newArray;
Expand All @@ -547,10 +549,10 @@
int end = oldCapacity - 1;
Entry last = (Entry) IntMapByDynamicHash.tableAt(oldTable, end);
if (this.size() < end && last == RESIZE_SENTINEL) {
return;

Check warning on line 552 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L552

Added line #L552 was not covered by tests
}
if (oldCapacity >= MAXIMUM_CAPACITY) {
throw new RuntimeException("max capacity of map exceeded");

Check warning on line 555 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L555

Added line #L555 was not covered by tests
}
ResizeContainer resizeContainer = null;
// This ownResize records whether current thread need to perform the expansion operation of
Expand Down Expand Up @@ -580,7 +582,7 @@
to set the table
*/
if (src != oldTable) {
this.helpWithResize(src);

Check warning on line 585 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L585

Added line #L585 was not covered by tests
}
}
} else {
Expand Down Expand Up @@ -669,13 +671,13 @@
int end = start + ResizeContainer.QUEUE_INCREMENT;
if (end > 0) {
if (start < 0) {
start = 0;

Check warning on line 674 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L674

Added line #L674 was not covered by tests
}
for (int j = end - 1; j >= start; ) {
Entry o = (Entry) IntMapByDynamicHash.tableAt(src, j);
if (o == null) {
if (IntMapByDynamicHash.casTableAt(src, j, null, RESIZED)) {
j--;

Check warning on line 680 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L680

Added line #L680 was not covered by tests
}
} else if (o == RESIZED || o == RESIZING) {
resizeContainer.zeroOutQueuePosition();
Expand All @@ -694,7 +696,7 @@
}
}
}
}

Check warning on line 699 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L699

Added line #L699 was not covered by tests

/**
* Copies an entry from the old table to the new table. This method is called during the resize
Expand All @@ -711,9 +713,9 @@
int index = this.hash(toCopyEntry.getKey(), length);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
currentArray =
((ResizeContainer) IntMapByDynamicHash.tableAt(currentArray,
length - 1)).nextArray;

Check warning on line 718 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L716-L718

Added lines #L716 - L718 were not covered by tests
} else {
Entry newEntry;
if (o == null) {
Expand All @@ -723,13 +725,13 @@
newEntry = new Entry(toCopyEntry.getKey(), toCopyEntry.getValue());
}
} else {
newEntry = new Entry(toCopyEntry.getKey(), toCopyEntry.getValue(), o);

Check warning on line 728 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L728

Added line #L728 was not covered by tests
}
if (IntMapByDynamicHash.casTableAt(currentArray, index, o, newEntry)) {
return;
}
}
}

Check warning on line 734 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L734

Added line #L734 was not covered by tests
}

/**
Expand Down Expand Up @@ -778,7 +780,7 @@
if (this.resizers.get() > 0) {
for (int i = 0; i < 16; i++) {
if (this.resizers.get() == 0) {
break;

Check warning on line 783 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L783

Added line #L783 was not covered by tests
}
}
for (int i = 0; i < 16; i++) {
Expand All @@ -789,15 +791,15 @@
}
}
if (this.resizers.get() > 0) {
synchronized (this) {

Check warning on line 794 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L794

Added line #L794 was not covered by tests
while (this.resizers.get() > 0) {
try {
this.wait();
} catch (InterruptedException e) {

Check warning on line 798 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L797-L798

Added lines #L797 - L798 were not covered by tests
// ignore
}

Check warning on line 800 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L800

Added line #L800 was not covered by tests
}
}

Check warning on line 802 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L802

Added line #L802 was not covered by tests
}
}

Expand Down Expand Up @@ -859,7 +861,7 @@

@Override
public String toString() {
return this.key + "=" + this.value;

Check warning on line 864 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L864

Added line #L864 was not covered by tests
}
}

Expand All @@ -875,11 +877,11 @@
this.end = this.currentTable.length - 1;
}

private IteratorState(Entry[] currentTable, int start, int end) {
this.currentTable = currentTable;
this.start = start;
this.end = end;
}

Check warning on line 884 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L880-L884

Added lines #L880 - L884 were not covered by tests
}

/**
Expand Down Expand Up @@ -913,30 +915,30 @@
Entry o =
(Entry) IntMapByDynamicHash.tableAt(this.currentState.currentTable, this.index);
if (o == RESIZED || o == RESIZING) {
Entry[] nextArray =
IntMapByDynamicHash.this.helpWithResizeWhileCurrentIndex(
this.currentState.currentTable, this.index);
int endResized = this.index + 1;

Check warning on line 921 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L918-L921

Added lines #L918 - L921 were not covered by tests
while (endResized < this.currentState.end) {
if (IntMapByDynamicHash.tableAt(this.currentState.currentTable,

Check warning on line 923 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L923

Added line #L923 was not covered by tests
endResized) != RESIZED) {
break;

Check warning on line 925 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L925

Added line #L925 was not covered by tests
}
endResized++;

Check warning on line 927 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L927

Added line #L927 was not covered by tests
}
if (this.todo == null) {
this.todo = new ArrayList<>(4);

Check warning on line 930 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L930

Added line #L930 was not covered by tests
}
if (endResized < this.currentState.end) {
this.todo.add(new IteratorState(
this.currentState.currentTable, endResized, this.currentState.end));

Check warning on line 934 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L933-L934

Added lines #L933 - L934 were not covered by tests
}
int powerTwoLength = this.currentState.currentTable.length - 1;
this.todo.add(new IteratorState(nextArray, this.index + powerTwoLength,

Check warning on line 937 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L936-L937

Added lines #L936 - L937 were not covered by tests
endResized + powerTwoLength));
this.currentState.currentTable = nextArray;
this.currentState.end = endResized;
this.currentState.start = this.index;

Check warning on line 941 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L939-L941

Added lines #L939 - L941 were not covered by tests
} else if (o != null) {
this.next = o;
this.index++;
Expand All @@ -947,9 +949,9 @@
}
if (this.next == null && this.index == this.currentState.end && this.todo != null &&
!this.todo.isEmpty()) {
this.currentState = this.todo.remove(this.todo.size() - 1);
this.index = this.currentState.start;
this.findNext();

Check warning on line 954 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L952-L954

Added lines #L952 - L954 were not covered by tests
}
}

Expand All @@ -961,7 +963,7 @@
final Entry nextEntry() {
Entry e = this.next;
if (e == null) {
throw new NoSuchElementException();

Check warning on line 966 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L966

Added line #L966 was not covered by tests
}

if ((this.next = e.getNext()) == null) {
Expand Down Expand Up @@ -999,7 +1001,7 @@
ENTRY_ARRAY_BASE = UNSAFE.arrayBaseOffset(tableClass);
int objectArrayScale = UNSAFE.arrayIndexScale(tableClass);
if ((objectArrayScale & (objectArrayScale - 1)) != 0) {
throw new AssertionError("data type scale not a power of two");

Check warning on line 1004 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L1004

Added line #L1004 was not covered by tests
}
ENTRY_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(objectArrayScale);

Expand All @@ -1007,14 +1009,14 @@
INT_ARRAY_BASE = UNSAFE.arrayBaseOffset(intArrayClass);
int intArrayScale = UNSAFE.arrayIndexScale(intArrayClass);
if ((intArrayScale & (intArrayScale - 1)) != 0) {
throw new AssertionError("data type scale not a power of two");

Check warning on line 1012 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L1012

Added line #L1012 was not covered by tests
}
INT_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(intArrayScale);

Class<?> mapClass = IntMapByDynamicHash.class;
SIZE_OFFSET = UNSAFE.objectFieldOffset(mapClass.getDeclaredField("size"));
} catch (NoSuchFieldException | SecurityException e) {
throw new AssertionError(e);

Check warning on line 1019 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L1018-L1019

Added lines #L1018 - L1019 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,27 @@ public class MapRandomGetPutThroughputTest {
@Param(value = {"1000", "10000", "100000", "1000000"})
private int MAP_CAPACITY;

private ConcurrentHashMap<Integer, Integer> concurrentHashMapNonCap;
private ConcurrentHashMap<Integer, Integer> concurrentHashMapWithoutCap;

private ConcurrentHashMap<Integer, Integer> concurrentHashMap;
private ConcurrentHashMap<Integer, Integer> concurrentHashMapWithCap;

private IntMap intMapBySegments;
private IntMap intMapBySegmentsWithCap;

private IntMap intMapByDynamicHashNonCap;
private IntMap intMapByDynamicHashWithoutCap;

private IntMap intMapByDynamicHash;
private IntMap intMapByDynamicHashWithCap;

private static final int THREAD_COUNT = 8;

private static final String OUTPUT_FILE_NAME = "map_random_get_put_result.json";

@Setup(Level.Trial)
public void prepareMap() {
this.concurrentHashMapNonCap = new ConcurrentHashMap<>();
this.concurrentHashMap = new ConcurrentHashMap<>(MAP_CAPACITY);
this.intMapBySegments = new IntMap.IntMapBySegments(MAP_CAPACITY);
this.intMapByDynamicHashNonCap = new IntMapByDynamicHash();
this.intMapByDynamicHash = new IntMapByDynamicHash(MAP_CAPACITY);
this.concurrentHashMapWithoutCap = new ConcurrentHashMap<>();
this.concurrentHashMapWithCap = new ConcurrentHashMap<>(MAP_CAPACITY);
this.intMapBySegmentsWithCap = new IntMap.IntMapBySegments(MAP_CAPACITY);
this.intMapByDynamicHashWithoutCap = new IntMapByDynamicHash();
this.intMapByDynamicHashWithCap = new IntMapByDynamicHash(MAP_CAPACITY);
}

/**
Expand All @@ -94,50 +94,50 @@ int next() {
@Threads(THREAD_COUNT)
public void randomGetPutOfConcurrentHashMapWithNoneInitCap(ThreadState state) {
int key = state.next();
if (!this.concurrentHashMapNonCap.containsKey(key)) {
this.concurrentHashMapNonCap.put(key, state.next());
if (!this.concurrentHashMapWithoutCap.containsKey(key)) {
this.concurrentHashMapWithoutCap.put(key, state.next());
}
this.concurrentHashMapNonCap.get(key);
this.concurrentHashMapWithoutCap.get(key);
}

@Benchmark
@Threads(THREAD_COUNT)
public void randomGetPutOfConcurrentHashMapWithInitCap(ThreadState state) {
int key = state.next() & (MAP_CAPACITY - 1);
if (!this.concurrentHashMap.containsKey(key)) {
this.concurrentHashMap.put(key, state.next());
if (!this.concurrentHashMapWithCap.containsKey(key)) {
this.concurrentHashMapWithCap.put(key, state.next());
}
this.concurrentHashMap.get(key);
this.concurrentHashMapWithCap.get(key);
}

@Benchmark
@Threads(THREAD_COUNT)
public void randomGetPutOfIntMapBySegmentsWithInitCap(ThreadState state) {
int key = state.next() & (MAP_CAPACITY - 1);
if (!this.intMapBySegments.containsKey(key)) {
this.intMapBySegments.put(key, state.next());
if (!this.intMapBySegmentsWithCap.containsKey(key)) {
this.intMapBySegmentsWithCap.put(key, state.next());
}
this.intMapBySegments.get(key);
this.intMapBySegmentsWithCap.get(key);
}

@Benchmark
@Threads(THREAD_COUNT)
public void randomGetPutOfIntMapByDynamicHashWithNoneCap(ThreadState state) {
int key = state.next();
if (!this.intMapByDynamicHashNonCap.containsKey(key)) {
this.intMapByDynamicHashNonCap.put(key, state.next());
if (!this.intMapByDynamicHashWithoutCap.containsKey(key)) {
this.intMapByDynamicHashWithoutCap.put(key, state.next());
}
this.intMapByDynamicHashNonCap.get(key);
this.intMapByDynamicHashWithoutCap.get(key);
}

@Benchmark
@Threads(THREAD_COUNT)
public void randomGetPutOfIntMapByDynamicHashWithInitCap(ThreadState state) {
int key = state.next() & (MAP_CAPACITY - 1);
if (!this.intMapByDynamicHash.containsKey(key)) {
this.intMapByDynamicHash.put(key, state.next());
if (!this.intMapByDynamicHashWithCap.containsKey(key)) {
this.intMapByDynamicHashWithCap.put(key, state.next());
}
this.intMapByDynamicHash.get(key);
this.intMapByDynamicHashWithCap.get(key);
}

public static void main(String[] args) throws RunnerException {
Expand Down