Skip to content

Commit

Permalink
feat: add clear
Browse files Browse the repository at this point in the history
  • Loading branch information
conghuhu committed Aug 25, 2023
1 parent 0cd8ec0 commit ecf8dab
Showing 1 changed file with 120 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.apache.hugegraph.util.collection;

import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -29,44 +46,38 @@ public class IntMapByDynamicHash implements IntMap {

private volatile Entry[] table;

/**
* Partition counting to improve the concurrency performance of addToSize()
*/
private int[] partitionedSize;

private static final Entry RESIZING = new Entry(NULL_VALUE, NULL_VALUE, 1);
private static final Entry RESIZED = new Entry(NULL_VALUE, NULL_VALUE, 2);

private static final Entry RESIZE_SENTINEL = new Entry(NULL_VALUE, NULL_VALUE, 3);

private static final Unsafe UNSAFE = IntSet.UNSAFE;

private static final long ENTRY_ARRAY_BASE;

private static final int ENTRY_ARRAY_SHIFT;

private static final long INT_ARRAY_BASE;

private static final int INT_ARRAY_SHIFT;

private static final long SIZE_OFFSET;

/**
* must be 2^n - 1
*/
private static final int SIZE_BUCKETS = 7;


/* ---------------- Table element access -------------- */
private static Object arrayAt(Object[] array, int index) {
private static Object tableAt(Object[] array, int index) {
return UNSAFE.getObjectVolatile(array,
((long) index << ENTRY_ARRAY_SHIFT) +
ENTRY_ARRAY_BASE);
}

private static boolean casArrayAt(Object[] array, int index, Object expected, Object newValue) {
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);
}

private static void setArrayAt(Object[] array, int index, Object newValue) {
private static void setTableAt(Object[] array, int index, Object newValue) {
UNSAFE.putObjectVolatile(array, ((long) index << ENTRY_ARRAY_SHIFT) + ENTRY_ARRAY_BASE,
newValue);
}
Expand All @@ -81,31 +92,6 @@ private static int tableSizeFor(int c) {
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}

static {
try {
Class<?> tableClass = Entry[].class;
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");
}
ENTRY_ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(objectArrayScale);

Class<?> intArrayClass = int[].class;
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");
}
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);
}
}

@SuppressWarnings("UnusedDeclaration")
private volatile int size; // updated via atomic field updater

Expand Down Expand Up @@ -139,25 +125,25 @@ public IntMapByDynamicHash(int initialCapacity) {
public boolean put(int key, int value) {
int hash = this.hash(key);
Entry[] currentArray = this.table;
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, hash);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, hash);
if (o == null) {
Entry newEntry = new Entry(key, value);
this.addToSize(1);
if (IntMapByDynamicHash.casArrayAt(currentArray, hash, null, newEntry)) {
if (IntMapByDynamicHash.casTableAt(currentArray, hash, null, newEntry)) {
return true;
}
this.addToSize(-1);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L135 was not covered by tests
}

return this.slowPut(key, value, hash, currentArray);
return this.slowPut(key, value, currentArray);
}

private boolean slowPut(int key, int value, int hash, Entry[] currentArray) {
private boolean slowPut(int key, int value, Entry[] currentArray) {
outer:
while (true) {
int length = currentArray.length;
int index = hash;
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, index);
int index = hash(key, length);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
currentArray = this.helpWithResizeWhileCurrentIndex(currentArray, index);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L148 was not covered by tests
} else {
Expand All @@ -168,7 +154,7 @@ private boolean slowPut(int key, int value, int hash, Entry[] currentArray) {
Entry newEntry = new Entry(e.getKey(),

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L154 was not covered by tests
value,
this.createReplacementChainForRemoval(o, e));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L156 was not covered by tests
if (!IntMapByDynamicHash.casArrayAt(currentArray, index, o,
if (!IntMapByDynamicHash.casTableAt(currentArray, index, o,
newEntry)) {
//noinspection ContinueStatementWithLabel
continue outer;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L160 was not covered by tests
Expand All @@ -178,7 +164,7 @@ private boolean slowPut(int key, int value, int hash, Entry[] currentArray) {
e = e.getNext();
}
Entry newEntry = new Entry(key, value, o);
if (IntMapByDynamicHash.casArrayAt(currentArray, index, o, newEntry)) {
if (IntMapByDynamicHash.casTableAt(currentArray, index, o, newEntry)) {
this.incrementSizeAndPossiblyResize(currentArray, length, o);
return true;
}
Expand All @@ -191,7 +177,7 @@ public int get(int key) {
int hash = this.hash(key);
Entry[] currentArray = this.table;
int index = hash;
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, index);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
return this.slowGet(key, currentArray);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L182 was not covered by tests
}
Expand All @@ -209,7 +195,7 @@ private int slowGet(int key, Entry[] currentArray) {
int length = currentArray.length;
int hash = this.hash(key, length);
int index = hash;
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, index);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L195-L198

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L200 was not covered by tests
} else {
Expand Down Expand Up @@ -248,7 +234,36 @@ public IntIterator values() {

@Override
public void clear() {

Entry[] currentArray = this.table;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L237 was not covered by tests
ResizeContainer resizeContainer;
do {
resizeContainer = null;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L240 was not covered by tests
for (int i = 0; i < currentArray.length - 1; i++) {
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, i);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L242 was not covered by tests
if (o == RESIZED || o == RESIZING) {
resizeContainer = (ResizeContainer) IntMapByDynamicHash.tableAt(currentArray,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L244 was not covered by tests
currentArray.length -
1);
} else if (o != null) {
Entry e = o;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L248 was not covered by tests
if (IntMapByDynamicHash.casTableAt(currentArray, i, o, null)) {
int removedEntries = 0;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L250 was not covered by tests
while (e != null) {
removedEntries++;
e = e.getNext();

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L252-L253

Added lines #L252 - L253 were not covered by tests
}
this.addToSize(-removedEntries);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L255 was not covered by tests
}
}
}
if (resizeContainer != null) {
if (resizeContainer.isNotDone()) {
this.helpWithResize(currentArray);
resizeContainer.waitForAllResizers();

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L261-L262

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L264 was not covered by tests
}
} while (resizeContainer != null);
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L267 was not covered by tests

@Override
Expand Down Expand Up @@ -280,7 +295,7 @@ private Entry getEntry(int key) {
while (true) {
int length = currentArray.length;
int index = this.hash(key, length);
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, index);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
currentArray = this.helpWithResizeWhileCurrentIndex(currentArray, index);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L300 was not covered by tests
} else {
Expand Down Expand Up @@ -363,7 +378,7 @@ private void incrementSizeAndPossiblyResize(Entry[] currentArray, int length, En
private Entry[] helpWithResizeWhileCurrentIndex(Entry[] currentArray, int index) {
Entry[] newArray = this.helpWithResize(currentArray);
int helpCount = 0;

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L379-L380

Added lines #L379 - L380 were not covered by tests
while (IntMapByDynamicHash.arrayAt(currentArray, index) != RESIZED) {
while (IntMapByDynamicHash.tableAt(currentArray, index) != RESIZED) {
helpCount++;
newArray = this.helpWithResize(currentArray);

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L382-L383

Added lines #L382 - L383 were not covered by tests
if ((helpCount & 7) == 0) {
Expand All @@ -382,7 +397,7 @@ private void resize(Entry[] oldTable) {
private void resize(Entry[] oldTable, int newSize) {
int oldCapacity = oldTable.length;
int end = oldCapacity - 1;
Entry last = (Entry) IntMapByDynamicHash.arrayAt(oldTable, end);
Entry last = (Entry) IntMapByDynamicHash.tableAt(oldTable, end);
if (this.size() < end && last == RESIZE_SENTINEL) {
return;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L402 was not covered by tests
}
Expand All @@ -394,13 +409,13 @@ private void resize(Entry[] oldTable, int newSize) {
if (last == null || last == RESIZE_SENTINEL) {
// allocating a new array is too expensive to make this an atomic operation
synchronized (oldTable) {
if (IntMapByDynamicHash.arrayAt(oldTable, end) == null) {
IntMapByDynamicHash.setArrayAt(oldTable, end, RESIZE_SENTINEL);
if (IntMapByDynamicHash.tableAt(oldTable, end) == null) {
IntMapByDynamicHash.setTableAt(oldTable, end, RESIZE_SENTINEL);
if (this.partitionedSize == null && newSize >= PARTITIONED_SIZE_THRESHOLD) {
this.partitionedSize = new int[SIZE_BUCKETS * 16];

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L415 was not covered by tests
}
resizeContainer = new ResizeContainer(new Entry[newSize], oldTable.length - 1);
IntMapByDynamicHash.setArrayAt(oldTable, end, resizeContainer);
IntMapByDynamicHash.setTableAt(oldTable, end, resizeContainer);
ownResize = true;
}
}
Expand Down Expand Up @@ -428,9 +443,9 @@ private void transfer(Entry[] src, ResizeContainer resizeContainer) {
Entry[] dest = resizeContainer.nextArray;

for (int j = 0; j < src.length - 1; ) {
Entry o = (Entry) IntMapByDynamicHash.arrayAt(src, j);
Entry o = (Entry) IntMapByDynamicHash.tableAt(src, j);
if (o == null) {
if (IntMapByDynamicHash.casArrayAt(src, j, null, RESIZED)) {
if (IntMapByDynamicHash.casTableAt(src, j, null, RESIZED)) {
j++;
}
} else if (o == RESIZED || o == RESIZING) {
Expand All @@ -449,12 +464,12 @@ private void transfer(Entry[] src, ResizeContainer resizeContainer) {
}
} else {
Entry e = o;
if (IntMapByDynamicHash.casArrayAt(src, j, o, RESIZING)) {
if (IntMapByDynamicHash.casTableAt(src, j, o, RESIZING)) {
while (e != null) {
this.unconditionalCopy(dest, e);
e = e.getNext();
}
IntMapByDynamicHash.setArrayAt(src, j, RESIZED);
IntMapByDynamicHash.setTableAt(src, j, RESIZED);
j++;
}
}
Expand All @@ -468,7 +483,7 @@ private void transfer(Entry[] src, ResizeContainer resizeContainer) {
*/
private Entry[] helpWithResize(Entry[] currentArray) {
ResizeContainer resizeContainer =
(ResizeContainer) IntMapByDynamicHash.arrayAt(currentArray,
(ResizeContainer) IntMapByDynamicHash.tableAt(currentArray,

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L485-L486

Added lines #L485 - L486 were not covered by tests
currentArray.length - 1);
Entry[] newTable = resizeContainer.nextArray;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L488 was not covered by tests
if (resizeContainer.getQueuePosition() > ResizeContainer.QUEUE_INCREMENT) {
Expand All @@ -489,22 +504,22 @@ private void reverseTransfer(Entry[] src, ResizeContainer resizeContainer) {
start = 0;

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L507 was not covered by tests
if (o == null) {
if (IntMapByDynamicHash.casArrayAt(src, j, null, RESIZED)) {
if (IntMapByDynamicHash.casTableAt(src, j, null, RESIZED)) {
j--;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L510 was not covered by tests
}
} else if (o == RESIZED || o == RESIZING) {
resizeContainer.zeroOutQueuePosition();
return;

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L513-L514

Added lines #L513 - L514 were not covered by tests
} else {
Entry e = o;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L516 was not covered by tests
if (IntMapByDynamicHash.casArrayAt(src, j, o, RESIZING)) {
if (IntMapByDynamicHash.casTableAt(src, j, o, RESIZING)) {
while (e != null) {
this.unconditionalCopy(dest, e);
e = e.getNext();

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L519-L520

Added lines #L519 - L520 were not covered by tests
}
IntMapByDynamicHash.setArrayAt(src, j, RESIZED);
IntMapByDynamicHash.setTableAt(src, j, RESIZED);
j--;

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L522-L523

Added lines #L522 - L523 were not covered by tests
}
}
Expand All @@ -513,15 +528,14 @@ private void reverseTransfer(Entry[] src, ResizeContainer resizeContainer) {
}
}

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L528-L529

Added lines #L528 - L529 were not covered by tests

private void unconditionalCopy(Object[] dest, Entry toCopyEntry) {
int hash = this.hash(toCopyEntry.getKey());
Object[] currentArray = dest;
private void unconditionalCopy(Entry[] dest, Entry toCopyEntry) {
Entry[] currentArray = dest;
while (true) {
int length = currentArray.length;
int index = hash;
Entry o = (Entry) IntMapByDynamicHash.arrayAt(currentArray, index);
int index = this.hash(toCopyEntry.getKey(), length);
Entry o = (Entry) IntMapByDynamicHash.tableAt(currentArray, index);
if (o == RESIZED || o == RESIZING) {
currentArray = ((ResizeContainer) IntMapByDynamicHash.arrayAt(currentArray,
currentArray = ((ResizeContainer) IntMapByDynamicHash.tableAt(currentArray,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L538 was not covered by tests
length -
1)).nextArray;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L540 was not covered by tests
} else {
Expand All @@ -536,7 +550,7 @@ private void unconditionalCopy(Object[] dest, Entry toCopyEntry) {
newEntry =
new Entry(toCopyEntry.getKey(), toCopyEntry.getValue(), (Entry) o);
}
if (IntMapByDynamicHash.casArrayAt(currentArray, index, o, newEntry)) {
if (IntMapByDynamicHash.casTableAt(currentArray, index, o, newEntry)) {
return;
}
}
Expand Down Expand Up @@ -667,4 +681,37 @@ public String toString() {
return this.key + "=" + this.value;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L681 was not covered by tests
}
}

/* ---------------- Unsafe mechanics -------------- */
private static final Unsafe UNSAFE = IntSet.UNSAFE;
private static final long ENTRY_ARRAY_BASE;
private static final int ENTRY_ARRAY_SHIFT;
private static final long INT_ARRAY_BASE;
private static final int INT_ARRAY_SHIFT;
private static final long SIZE_OFFSET;

static {
try {
Class<?> tableClass = Entry[].class;
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 699 in hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

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

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

Class<?> intArrayClass = int[].class;
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 707 in hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L707 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 714 in hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-core/src/main/java/org/apache/hugegraph/util/collection/IntMapByDynamicHash.java#L713-L714

Added lines #L713 - L714 were not covered by tests
}
}
}

0 comments on commit ecf8dab

Please sign in to comment.