Search in sources :

Example 96 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class TableReaderMetadataTest method assertThat.

private void assertThat(String expected, ColumnManipulator manipulator, int columnCount) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path().of(root).concat("all")) {
            int tableId;
            try (TableReaderMetadata metadata = new TableReaderMetadata(FilesFacadeImpl.INSTANCE, path.concat(TableUtils.META_FILE_NAME).$())) {
                tableId = metadata.getId();
                try (TableWriter writer = new TableWriter(configuration, "all")) {
                    manipulator.restructure(writer);
                }
                long pTransitionIndex = metadata.createTransitionIndex();
                StringSink sink = new StringSink();
                try {
                    metadata.applyTransitionIndex(pTransitionIndex);
                    Assert.assertEquals(columnCount, metadata.getColumnCount());
                    for (int i = 0; i < columnCount; i++) {
                        sink.put(metadata.getColumnName(i)).put(':').put(ColumnType.nameOf(metadata.getColumnType(i))).put('\n');
                    }
                    TestUtils.assertEquals(expected, sink);
                    if (expected.length() > 0) {
                        String[] lines = expected.split("\n");
                        Assert.assertEquals(columnCount, lines.length);
                        for (int i = 0; i < columnCount; i++) {
                            int p = lines[i].indexOf(':');
                            Assert.assertEquals(i, metadata.getColumnIndexQuiet(lines[i].substring(0, p)));
                        }
                    }
                } finally {
                    TableUtils.freeTransitionIndex(pTransitionIndex);
                }
            }
            // Check that table has same tableId.
            try (TableReaderMetadata metadata = new TableReaderMetadata(FilesFacadeImpl.INSTANCE, path.concat(TableUtils.META_FILE_NAME).$())) {
                Assert.assertEquals(tableId, metadata.getId());
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) StringSink(io.questdb.std.str.StringSink)

Example 97 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class TableReaderMetadataTest method testColumnIndex.

@Test
public void testColumnIndex() {
    ObjIntHashMap<String> expected = new ObjIntHashMap<>();
    expected.put("int", 0);
    expected.put("byte", 2);
    expected.put("bin", 9);
    expected.put("short", 1);
    expected.put("float", 4);
    expected.put("long", 5);
    expected.put("xyz", -1);
    expected.put("str", 6);
    expected.put("double", 3);
    expected.put("sym", 7);
    expected.put("bool", 8);
    expected.put("zall.sym", -1);
    try (Path path = new Path().of(root).concat("all").concat(TableUtils.META_FILE_NAME).$();
        TableReaderMetadata metadata = new TableReaderMetadata(FilesFacadeImpl.INSTANCE, path)) {
        for (ObjIntHashMap.Entry<String> e : expected) {
            Assert.assertEquals(e.value, metadata.getColumnIndexQuiet(e.key));
        }
    }
}
Also used : Path(io.questdb.std.str.Path) ObjIntHashMap(io.questdb.std.ObjIntHashMap) Test(org.junit.Test)

Example 98 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class TableReaderTest method testRemovePartitionByDayCannotDeleteDir.

@Test
public void testRemovePartitionByDayCannotDeleteDir() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        int N = 100;
        int N_PARTITIONS = 5;
        long timestampUs = TimestampFormatUtils.parseTimestamp("2017-12-11T00:00:00.000Z");
        long stride = 100;
        int bandStride = 1000;
        int totalCount = 0;
        FilesFacade ff = new FilesFacadeImpl() {

            @Override
            public int rmdir(Path name) {
                if (Chars.endsWith(name, "2017-12-14" + Files.SEPARATOR)) {
                    return 1;
                }
                return super.rmdir(name);
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // model table
        try (TableModel model = new TableModel(configuration, "w", PartitionBy.DAY).col("l", ColumnType.LONG).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (TableWriter writer = new TableWriter(configuration, "w")) {
            for (int k = 0; k < N_PARTITIONS; k++) {
                long band = k * bandStride;
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow(timestampUs);
                    row.putLong(0, band + i);
                    row.append();
                    writer.commit();
                    timestampUs += stride;
                }
                timestampUs = Timestamps.addDays(Timestamps.floorDD(timestampUs), 1);
            }
            Assert.assertEquals(N * N_PARTITIONS, writer.size());
            DateFormat fmt = TableWriter.selectPartitionDirFmt(PartitionBy.DAY);
            assert fmt != null;
            final long timestamp = fmt.parse("2017-12-14", null);
            Assert.assertTrue(writer.removePartition(timestamp));
            Assert.assertFalse(writer.removePartition(timestamp));
            Assert.assertEquals(N * (N_PARTITIONS - 1), writer.size());
        }
        // now open table reader having partition gap
        try (TableReader reader = new TableReader(configuration, "w")) {
            Assert.assertEquals(N * (N_PARTITIONS - 1), reader.size());
            int previousBand = -1;
            int bandCount = 0;
            RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                long value = record.getLong(0);
                int band = (int) ((value / bandStride) * bandStride);
                if (band != previousBand) {
                    // make sure we don#t pick up deleted partition
                    Assert.assertNotEquals(3000, band);
                    if (previousBand != -1) {
                        Assert.assertEquals(N, bandCount);
                    }
                    previousBand = band;
                    bandCount = 0;
                }
                bandCount++;
                totalCount++;
            }
            Assert.assertEquals(N, bandCount);
        }
        Assert.assertEquals(N * (N_PARTITIONS - 1), totalCount);
    });
}
Also used : Path(io.questdb.std.str.Path) RecordCursor(io.questdb.cairo.sql.RecordCursor) DateFormat(io.questdb.std.datetime.DateFormat) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 99 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class BitmapIndexTest method testBackwardCursorTimeout.

@Test
public void testBackwardCursorTimeout() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        create(configuration, path.trimTo(plen), "x", 1024);
        try (BitmapIndexWriter w = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
            w.add(0, 10);
        }
        try (BitmapIndexBwdReader reader = new BitmapIndexBwdReader(configuration, path.trimTo(plen), "x", 0)) {
            try (MemoryMARW mem = Vm.getMARWInstance()) {
                try (Path path = new Path()) {
                    path.of(configuration.getRoot()).concat("x").put(".k").$();
                    mem.wholeFile(configuration.getFilesFacade(), path, MemoryTag.MMAP_DEFAULT);
                }
                mem.putLong(BitmapIndexUtils.getKeyEntryOffset(0) + BitmapIndexUtils.KEY_ENTRY_OFFSET_VALUE_COUNT, 10);
                try {
                    reader.getCursor(true, 0, 0, Long.MAX_VALUE);
                    Assert.fail();
                } catch (CairoException e) {
                    Assert.assertTrue(Chars.contains(e.getMessage(), "cursor could not consistently read index header"));
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) Test(org.junit.Test)

Example 100 with Path

use of io.questdb.std.str.Path in project questdb by bluestreak01.

the class BitmapIndexTest method testConcurrentForwardRW.

private void testConcurrentForwardRW(int N, int maxKeys) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        Rnd rnd = new Rnd();
        IntList keys = new IntList();
        IntObjHashMap<LongList> lists = new IntObjHashMap<>();
        // populate model for both reader and writer
        for (int i = 0; i < N; i++) {
            int key = rnd.nextPositiveInt() % maxKeys;
            LongList list = lists.get(key);
            if (list == null) {
                lists.put(key, list = new LongList());
                keys.add(key);
            }
            list.add(i);
        }
        final int threadCount = 3;
        CountDownLatch stopLatch = new CountDownLatch(threadCount);
        CyclicBarrier startBarrier = new CyclicBarrier(threadCount);
        AtomicInteger errors = new AtomicInteger();
        // create empty index
        create(configuration, path.trimTo(plen), "x", 1024);
        new Thread(() -> {
            try {
                startBarrier.await();
                try (Path path = new Path().of(configuration.getRoot())) {
                    try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
                        int pass = 0;
                        while (true) {
                            boolean added = false;
                            for (int i = 0, n = keys.size(); i < n; i++) {
                                int key = keys.getQuick(i);
                                LongList values = lists.get(key);
                                if (pass < values.size()) {
                                    writer.add(key, values.getQuick(pass));
                                    added = true;
                                }
                            }
                            pass++;
                            if (!added) {
                                break;
                            }
                        }
                    }
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            } finally {
                stopLatch.countDown();
            }
        }).start();
        class MyReader implements Runnable {

            @Override
            public void run() {
                try {
                    startBarrier.await();
                    try (Path path = new Path().of(configuration.getRoot())) {
                        try (BitmapIndexFwdReader reader1 = new BitmapIndexFwdReader(configuration, path, "x", 0)) {
                            LongList tmp = new LongList();
                            while (true) {
                                boolean keepGoing = false;
                                for (int i = keys.size() - 1; i > -1; i--) {
                                    int key = keys.getQuick(i);
                                    LongList values = lists.get(key);
                                    RowCursor cursor = reader1.getCursor(true, key, 0, Long.MAX_VALUE);
                                    tmp.clear();
                                    while (cursor.hasNext()) {
                                        tmp.add(cursor.next());
                                    }
                                    int sz = tmp.size();
                                    for (int k = 0; k < sz; k++) {
                                        Assert.assertEquals(values.getQuick(k), tmp.getQuick(k));
                                    }
                                    if (sz < values.size()) {
                                        keepGoing = true;
                                    }
                                }
                                if (!keepGoing) {
                                    break;
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    errors.incrementAndGet();
                    e.printStackTrace();
                } finally {
                    stopLatch.countDown();
                }
            }
        }
        new Thread(new MyReader()).start();
        new Thread(new MyReader()).start();
        Assert.assertTrue(stopLatch.await(20000, TimeUnit.SECONDS));
        Assert.assertEquals(0, errors.get());
    });
}
Also used : Path(io.questdb.std.str.Path) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowCursor(io.questdb.cairo.sql.RowCursor)

Aggregations

Path (io.questdb.std.str.Path)141 Test (org.junit.Test)89 File (java.io.File)14 FilesFacade (io.questdb.std.FilesFacade)13 MemoryCMARW (io.questdb.cairo.vm.api.MemoryCMARW)10 MemoryMR (io.questdb.cairo.vm.api.MemoryMR)10 Rnd (io.questdb.std.Rnd)10 AbstractCairoTest (io.questdb.cairo.AbstractCairoTest)7 MemoryMA (io.questdb.cairo.vm.api.MemoryMA)7 MemoryMARW (io.questdb.cairo.vm.api.MemoryMARW)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)6 NativeLPSZ (io.questdb.std.str.NativeLPSZ)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 SOCountDownLatch (io.questdb.mp.SOCountDownLatch)5 LPSZ (io.questdb.std.str.LPSZ)5 RecordCursor (io.questdb.cairo.sql.RecordCursor)4 RowCursor (io.questdb.cairo.sql.RowCursor)4 MemoryARW (io.questdb.cairo.vm.api.MemoryARW)4 RingQueue (io.questdb.mp.RingQueue)4