Search in sources :

Example 21 with LongList

use of io.questdb.std.LongList in project questdb by bluestreak01.

the class LongChainTest method testAll.

@Test
public void testAll() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (LongChain chain = new LongChain(1024 * 1024, Integer.MAX_VALUE)) {
            final int N = 1000;
            final int nChains = 10;
            final Rnd rnd = new Rnd();
            final LongList heads = new LongList(nChains);
            final ObjList<LongList> expectedValues = new ObjList<>();
            for (int i = 0; i < nChains; i++) {
                LongList expected = new LongList(N);
                heads.add(populateChain(chain, rnd, expected));
                expectedValues.add(expected);
                Assert.assertEquals(N, expected.size());
            }
            Assert.assertEquals(nChains, expectedValues.size());
            // values are be in reverse order
            for (int i = 0; i < nChains; i++) {
                LongChain.TreeCursor cursor = chain.getCursor(heads.getQuick(i));
                LongList expected = expectedValues.get(i);
                int count = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(expected.getQuick(count), cursor.next());
                    count++;
                }
                Assert.assertEquals(N, count);
            }
        }
    });
}
Also used : ObjList(io.questdb.std.ObjList) Rnd(io.questdb.std.Rnd) LongList(io.questdb.std.LongList) Test(org.junit.Test)

Example 22 with LongList

use of io.questdb.std.LongList in project questdb by bluestreak01.

the class LongTreeSetTest method testDuplicateValues.

@Test
public void testDuplicateValues() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (LongTreeSet set = new LongTreeSet(1024, 1)) {
            set.put(1);
            set.put(2);
            set.put(2);
            set.put(3);
            set.put(4);
            set.put(4);
            set.put(5);
            set.put(5);
            set.put(5);
            LongTreeSet.TreeCursor cursor = set.getCursor();
            LongList ll = new LongList();
            while (cursor.hasNext()) {
                ll.add(cursor.next());
            }
            TestUtils.assertEquals("[1,2,3,4,5]", ll.toString());
        }
    });
}
Also used : LongTreeSet(io.questdb.griffin.engine.table.LongTreeSet) LongList(io.questdb.std.LongList) Test(org.junit.Test)

Example 23 with LongList

use of io.questdb.std.LongList in project questdb by bluestreak01.

the class ConcurrentTest method testFanOutPingPongStableSequences.

@Test
public void testFanOutPingPongStableSequences() {
    final int threads = 2;
    final int iterations = 30;
    final int cycle = Numbers.ceilPow2(threads * iterations);
    // Requests inbox
    RingQueue<LongMsg> pingQueue = new RingQueue<>(LongMsg::new, cycle);
    MPSequence pingPubSeq = new MPSequence(cycle);
    FanOut pingSubFo = new FanOut();
    pingPubSeq.then(pingSubFo).then(pingPubSeq);
    // Request inbox hook for processor
    SCSequence pingSubSeq = new SCSequence();
    pingSubFo.and(pingSubSeq);
    // Response outbox
    RingQueue<LongMsg> pongQueue = new RingQueue<>(LongMsg::new, cycle);
    SPSequence pongPubSeq = new SPSequence(threads);
    FanOut pongSubFo = new FanOut();
    pongPubSeq.then(pongSubFo).then(pongPubSeq);
    CyclicBarrier start = new CyclicBarrier(threads);
    SOCountDownLatch latch = new SOCountDownLatch(threads + 1);
    AtomicLong doneCount = new AtomicLong();
    AtomicLong idGen = new AtomicLong();
    // Processor
    new Thread(() -> {
        try {
            LongList pingPong = new LongList();
            int i = 0;
            while (i < threads * iterations) {
                long pingCursor = pingSubSeq.next();
                if (pingCursor > -1) {
                    // Get next request
                    LongMsg msg = pingQueue.get(pingCursor);
                    long requestId = msg.correlationId;
                    pingSubSeq.done(pingCursor);
                    pingPong.add(pingCursor);
                    System.out.println("* ping " + requestId);
                    long pongCursor;
                    while ((pongCursor = pongPubSeq.next()) < 0) {
                        LockSupport.parkNanos(10);
                    }
                    pongQueue.get(pongCursor).correlationId = requestId;
                    pongPubSeq.done(pongCursor);
                    pingPong.add(pongCursor);
                    System.out.println("* pong " + requestId);
                    i++;
                } else {
                    LockSupport.parkNanos(10);
                }
            }
            doneCount.incrementAndGet();
        } finally {
            latch.countDown();
        }
    }).start();
    final SCSequence[] sequences = new SCSequence[threads];
    for (int i = 0; i < threads; i++) {
        SCSequence pongSubSeq = new SCSequence();
        pongSubFo.and(pongSubSeq);
        sequences[i] = pongSubSeq;
    }
    // Request threads
    for (int th = 0; th < threads; th++) {
        final int threadId = th;
        new Thread(() -> {
            final LongList pingPong = new LongList();
            try {
                start.await();
                final SCSequence pongSubSeq = sequences[threadId];
                for (int i = 0; i < iterations; i++) {
                    // Put local response sequence into response FanOut
                    System.out.println("thread:" + threadId + ", added at " + pingPubSeq.value);
                    // Send next request
                    long requestId = idGen.incrementAndGet();
                    long pingCursor;
                    while ((pingCursor = pingPubSeq.next()) < 0) {
                        LockSupport.parkNanos(10);
                    }
                    pingQueue.get(pingCursor).correlationId = requestId;
                    pingPubSeq.done(pingCursor);
                    pingPong.add(pingCursor);
                    System.out.println("thread:" + threadId + ", ask: " + requestId);
                    // Wait for response
                    long responseId, pongCursor;
                    do {
                        while ((pongCursor = pongSubSeq.next()) < 0) {
                            LockSupport.parkNanos(10);
                        }
                        pingPong.add(pongCursor);
                        responseId = pongQueue.get(pongCursor).correlationId;
                        pongSubSeq.done(pongCursor);
                        pingPong.add(pongCursor);
                        System.out.println("thread:" + threadId + ", ping: " + responseId + ", expected: " + requestId);
                    } while (responseId != requestId);
                    System.out.println("thread " + threadId + ", pong " + requestId);
                // Remove local response sequence from response FanOut
                }
                pongSubFo.remove(pongSubSeq);
                doneCount.incrementAndGet();
            } catch (BrokenBarrierException | InterruptedException e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
            }
        }).start();
    }
    latch.await();
    Assert.assertEquals(threads + 1, doneCount.get());
}
Also used : LongList(io.questdb.std.LongList) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 24 with LongList

use of io.questdb.std.LongList in project questdb by bluestreak01.

the class IntervalUtilsTest method testUnionEmpty1.

@Test
public void testUnionEmpty1() {
    LongList intervals = new LongList();
    // A
    add(intervals, -1, 1);
    add(intervals, 2, 3);
    runTestUnionInplace(intervals, 4, "[-1,1], [2,3]");
}
Also used : LongList(io.questdb.std.LongList) Test(org.junit.Test)

Example 25 with LongList

use of io.questdb.std.LongList in project questdb by bluestreak01.

the class IntervalUtilsTest method testUnionInplaceSimple1.

@Test
public void testUnionInplaceSimple1() {
    LongList intervals = new LongList();
    // A
    add(intervals, -1, 10);
    // B
    add(intervals, 1, 2);
    add(intervals, 3, 4);
    add(intervals, 15, 16);
    runTestUnionInplace(intervals, 2, "[-1,10], [15,16]");
}
Also used : LongList(io.questdb.std.LongList) Test(org.junit.Test)

Aggregations

LongList (io.questdb.std.LongList)30 Test (org.junit.Test)25 LongTreeSet (io.questdb.griffin.engine.table.LongTreeSet)3 Rnd (io.questdb.std.Rnd)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 GriffinParserTestUtils.intervalToString (io.questdb.griffin.GriffinParserTestUtils.intervalToString)1 SOCountDownLatch (io.questdb.mp.SOCountDownLatch)1 ObjList (io.questdb.std.ObjList)1 Random (java.util.Random)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1