Search in sources :

Example 6 with AtomicLongArray

use of java.util.concurrent.atomic.AtomicLongArray in project ignite by apache.

the class HadoopConcurrentHashMultimap method visit.

/**
     * Incrementally visits all the keys and values in the map.
     *
     * @param ignoreLastVisited Flag indicating that visiting must be started from the beginning.
     * @param v Visitor.
     * @return {@code false} If visiting was impossible due to rehashing.
     */
@Override
public boolean visit(boolean ignoreLastVisited, Visitor v) throws IgniteCheckedException {
    if (!state.compareAndSet(State.READING_WRITING, State.VISITING)) {
        assert state.get() != State.CLOSING;
        // Can not visit while rehashing happens.
        return false;
    }
    AtomicLongArray tbl0 = oldTbl;
    for (int i = 0; i < tbl0.length(); i++) {
        long meta = tbl0.get(i);
        while (meta != 0) {
            long valPtr = value(meta);
            long lastVisited = ignoreLastVisited ? 0 : lastVisitedValue(meta);
            if (valPtr != lastVisited) {
                v.onKey(key(meta), keySize(meta));
                // Set it to the first value in chain.
                lastVisitedValue(meta, valPtr);
                do {
                    v.onValue(valPtr + 12, valueSize(valPtr));
                    valPtr = nextValue(valPtr);
                } while (valPtr != lastVisited);
            }
            meta = collision(meta);
        }
    }
    state(State.VISITING, State.READING_WRITING);
    return true;
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 7 with AtomicLongArray

use of java.util.concurrent.atomic.AtomicLongArray in project ignite by apache.

the class BPlusTreeSelfTest method doTestMassivePut.

/**
     * @param canGetRow Can get row in inner page.
     * @throws Exception If failed.
     */
private void doTestMassivePut(final boolean canGetRow) throws Exception {
    final int threads = 16;
    // We may fail to insert more on small pages size because of tree height.
    final int keys = 26;
    final TestTree tree = createTestTree(canGetRow);
    info("Put...");
    final AtomicLongArray k = new AtomicLongArray(keys);
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Random rnd = new GridRandom();
            for (; ; ) {
                int idx = 0;
                boolean found = false;
                for (int i = 0, shift = rnd.nextInt(keys); i < keys; i++) {
                    idx = (i + shift) % keys;
                    if (k.get(idx) == 0 && k.compareAndSet(idx, 0, 1)) {
                        found = true;
                        break;
                    }
                }
                if (!found)
                    break;
                assertNull(tree.put((long) idx));
                assertNoLocks();
            }
            return null;
        }
    }, threads, "put");
    assertEquals(keys, tree.size());
    tree.validateTree();
    GridCursor<Long> c = tree.find(null, null);
    long x = 0;
    while (c.next()) assertEquals(Long.valueOf(x++), c.get());
    assertEquals(keys, x);
    assertNoLocks();
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 8 with AtomicLongArray

use of java.util.concurrent.atomic.AtomicLongArray in project ignite by apache.

the class BPlusTreeSelfTest method doTestMassiveRemove.

/**
     * @param canGetRow Can get row in inner page.
     * @throws Exception If failed.
     */
private void doTestMassiveRemove(final boolean canGetRow) throws Exception {
    final int threads = 64;
    final int keys = 3000;
    final AtomicLongArray rmvd = new AtomicLongArray(keys);
    final TestTree tree = createTestTree(canGetRow);
    // Put keys in reverse order to have a better balance in the tree (lower height).
    for (long i = keys - 1; i >= 0; i--) {
        tree.put(i);
    //            X.println(tree.printTree());
    }
    assertEquals(keys, tree.size());
    tree.validateTree();
    info("Remove...");
    try {
        GridTestUtils.runMultiThreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new GridRandom();
                for (; ; ) {
                    int idx = 0;
                    boolean found = false;
                    for (int i = 0, shift = rnd.nextInt(keys); i < keys; i++) {
                        idx = (i + shift) % keys;
                        if (rmvd.get(idx) == 0 && rmvd.compareAndSet(idx, 0, 1)) {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        break;
                    assertEquals(Long.valueOf(idx), tree.remove((long) idx));
                    if (canGetRow)
                        rmvdIds.add((long) idx);
                }
                return null;
            }
        }, threads, "remove");
        assertEquals(0, tree.size());
        tree.validateTree();
    } finally {
        rmvdIds.clear();
    }
}
Also used : GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) IgniteCheckedException(org.apache.ignite.IgniteCheckedException)

Example 9 with AtomicLongArray

use of java.util.concurrent.atomic.AtomicLongArray in project intellij-community by JetBrains.

the class ConcurrentBitSet method nextSetBit.

/**
  * Returns the index of the first bit that is set to {@code true}
  * that occurs on or after the specified starting index. If no such
  * bit exists then {@code -1} is returned.
  * <p/>
  * <p>To iterate over the {@code true} bits,
  * use the following loop:
  * <p/>
  * <pre> {@code
  * for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
  *     // operate on index i here
  * }}</pre>
  *
  * @param fromIndex the index to start checking from (inclusive)
  * @return the index of the next set bit, or {@code -1} if there
  * is no such bit
  * @throws IndexOutOfBoundsException if the specified index is negative
  */
public int nextSetBit(int fromIndex) {
    if (fromIndex < 0) {
        throw new IndexOutOfBoundsException("bitIndex < 0: " + fromIndex);
    }
    int arrayIndex;
    AtomicLongArray array = null;
    for (arrayIndex = arrayIndex(fromIndex); arrayIndex < arrays.length() && (array = arrays.get(arrayIndex)) == null; arrayIndex++) ;
    if (array == null) {
        return -1;
    }
    int wordIndexInArray = wordIndexInArray(fromIndex);
    long word = array.get(wordIndexInArray) & (WORD_MASK << fromIndex);
    while (true) {
        if (word != 0) {
            return ((1 << arrayIndex) - 1 + wordIndexInArray) * BITS_PER_WORD + Long.numberOfTrailingZeros(word);
        }
        if (++wordIndexInArray == array.length()) {
            wordIndexInArray = 0;
            for (++arrayIndex; arrayIndex != arrays.length() && (array = arrays.get(arrayIndex)) == null; arrayIndex++) ;
            if (array == null) {
                return -1;
            }
        }
        word = array.get(wordIndexInArray);
    }
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray)

Example 10 with AtomicLongArray

use of java.util.concurrent.atomic.AtomicLongArray in project intellij-community by JetBrains.

the class ConcurrentBitSet method toLongArray.

@NotNull
public long[] toLongArray() {
    int bits = size();
    long[] result = new long[bits / BITS_PER_WORD];
    int i = 0;
    for (int b = 0; b < bits; b += BITS_PER_WORD) {
        AtomicLongArray array = arrays.get(arrayIndex(b));
        long word = array == null ? 0 : array.get(wordIndexInArray(b));
        result[i++] = word;
    }
    return result;
}
Also used : AtomicLongArray(java.util.concurrent.atomic.AtomicLongArray) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)22 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Random (java.util.Random)2 AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)2 JobGraph (org.apache.flink.runtime.jobgraph.JobGraph)2 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridRandom (org.apache.ignite.internal.util.GridRandom)2 NotNull (org.jetbrains.annotations.NotNull)2 Test (org.junit.Test)2 Deadline (scala.concurrent.duration.Deadline)2 ActorRef (akka.actor.ActorRef)1 ActorSystem (akka.actor.ActorSystem)1 JSONArray (com.alibaba.fastjson.JSONArray)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1