use of com.hazelcast.internal.iteration.IterationPointer in project hazelcast by hazelcast.
the class SampleableConcurrentHashMap method hasNotBeenObserved.
/**
* Returns {@code true} if the given {@code key} has not been already observed
* (or should have been observed) with the iteration state provided by the
* {@code pointers}.
*
* @param key the key to check
* @param pointers the iteration state
* @return if the key should have already been observed by the iteration user
*/
private boolean hasNotBeenObserved(K key, IterationPointer[] pointers) {
if (pointers.length < 2) {
// there was no resize yet so we most definitely haven't observed the entry
return true;
}
int hash = hashOf(key);
// check only the pointers up to the last, we haven't observed it with the last pointer
for (int i = 0; i < pointers.length - 1; i++) {
IterationPointer iterationPointer = pointers[i];
int tableSize = iterationPointer.getSize();
int tableIndex = iterationPointer.getIndex();
int index = hash & (tableSize - 1);
if (index > tableIndex) {
// so we have observed it
return false;
}
}
return true;
}
use of com.hazelcast.internal.iteration.IterationPointer in project hazelcast by hazelcast.
the class SampleableConcurrentHashMap method checkPointers.
/**
* Checks the {@code pointers} to see if we need to restart iteration on the
* current table and returns the updated pointers if necessary.
*
* @param pointers the pointers defining the state of iteration
* @param currentTableSize the current table size
* @return the updated pointers, if necessary
*/
private IterationPointer[] checkPointers(IterationPointer[] pointers, int currentTableSize) {
IterationPointer lastPointer = pointers[pointers.length - 1];
boolean iterationStarted = lastPointer.getSize() == -1;
boolean tableResized = lastPointer.getSize() != currentTableSize;
// clone pointers to avoid mutating given reference
// add new pointer if resize happened during iteration
int newLength = !iterationStarted && tableResized ? pointers.length + 1 : pointers.length;
IterationPointer[] updatedPointers = new IterationPointer[newLength];
for (int i = 0; i < pointers.length; i++) {
updatedPointers[i] = new IterationPointer(pointers[i]);
}
// reset last pointer if we haven't started iteration or there was a resize
if (iterationStarted || tableResized) {
updatedPointers[updatedPointers.length - 1] = new IterationPointer(Integer.MAX_VALUE, currentTableSize);
}
return updatedPointers;
}
Aggregations