Search in sources :

Example 36 with IterationPointer

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;
}
Also used : IterationPointer(com.hazelcast.internal.iteration.IterationPointer)

Example 37 with IterationPointer

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;
}
Also used : IterationPointer(com.hazelcast.internal.iteration.IterationPointer)

Aggregations

IterationPointer (com.hazelcast.internal.iteration.IterationPointer)37 Data (com.hazelcast.internal.serialization.Data)9 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)4 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)4 ClientInvocationFuture (com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)4 AbstractMap (java.util.AbstractMap)4 ArrayList (java.util.ArrayList)4 MapOperationProvider (com.hazelcast.map.impl.operation.MapOperationProvider)3 Map (java.util.Map)3 CacheEntriesWithCursor (com.hazelcast.cache.impl.CacheEntriesWithCursor)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)2 SampleableConcurrentHashMap (com.hazelcast.internal.util.SampleableConcurrentHashMap)2 MapEntriesWithCursor (com.hazelcast.map.impl.iterator.MapEntriesWithCursor)2 CacheKeysWithCursor (com.hazelcast.cache.impl.CacheKeysWithCursor)1 CacheOperationProvider (com.hazelcast.cache.impl.CacheOperationProvider)1 CacheIterateCodec (com.hazelcast.client.impl.protocol.codec.CacheIterateCodec)1 CacheIterateEntriesCodec (com.hazelcast.client.impl.protocol.codec.CacheIterateEntriesCodec)1 MapFetchEntriesCodec (com.hazelcast.client.impl.protocol.codec.MapFetchEntriesCodec)1 MapFetchKeysCodec (com.hazelcast.client.impl.protocol.codec.MapFetchKeysCodec)1 MapFetchWithQueryCodec (com.hazelcast.client.impl.protocol.codec.MapFetchWithQueryCodec)1