Search in sources :

Example 1 with MemoryCMARW

use of io.questdb.cairo.vm.api.MemoryCMARW in project questdb by bluestreak01.

the class TableReaderMetadataCorruptionTest method assertTransitionIndexValidation.

private void assertTransitionIndexValidation(int columnCount) throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path()) {
            CairoTestUtils.createAllTable(configuration, PartitionBy.NONE);
            path.of(root).concat("all").concat(TableUtils.META_FILE_NAME).$();
            long len = FilesFacadeImpl.INSTANCE.length(path);
            try (TableReaderMetadata metadata = new TableReaderMetadata(FilesFacadeImpl.INSTANCE, path)) {
                try (MemoryCMARW mem = Vm.getCMARWInstance()) {
                    mem.smallFile(FilesFacadeImpl.INSTANCE, path, MemoryTag.MMAP_DEFAULT);
                    mem.jumpTo(0);
                    mem.putInt(columnCount);
                    mem.skip(len - 4);
                }
                try {
                    metadata.createTransitionIndex();
                } catch (CairoException e) {
                    TestUtils.assertContains(e.getFlyweightMessage(), "Invalid metadata at ");
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW)

Example 2 with MemoryCMARW

use of io.questdb.cairo.vm.api.MemoryCMARW in project questdb by bluestreak01.

the class CairoMemoryTest method testWriteAndReadWithReadOnlyMem.

@Test
public void testWriteAndReadWithReadOnlyMem() throws Exception {
    long used = Unsafe.getMemUsed();
    try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
        try (MemoryCMARW mem = Vm.getCMARWInstance(FF, path, 2 * FF.getPageSize(), -1, MemoryTag.MMAP_DEFAULT)) {
            for (int i = 0; i < N; i++) {
                mem.putLong(i);
            }
            Assert.assertEquals(8L * N, mem.getAppendOffset());
        }
        try (MemoryMR mem = new MemoryCMRImpl(FF, path, 8L * N, MemoryTag.MMAP_DEFAULT)) {
            for (int i = 0; i < N; i++) {
                Assert.assertEquals(i, mem.getLong(i * 8));
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) MemoryMR(io.questdb.cairo.vm.api.MemoryMR) MemoryCMRImpl(io.questdb.cairo.vm.MemoryCMRImpl) Test(org.junit.Test)

Example 3 with MemoryCMARW

use of io.questdb.cairo.vm.api.MemoryCMARW in project questdb by bluestreak01.

the class CairoMemoryTest method testWriteAndRead.

@Test
public void testWriteAndRead() throws Exception {
    long used = Unsafe.getMemUsed();
    try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
        try (MemoryCMARW mem = Vm.getCMARWInstance(FF, path, 2 * FF.getPageSize(), -1, MemoryTag.MMAP_DEFAULT)) {
            for (int i = 0; i < N; i++) {
                mem.putLong(i);
            }
            // read in place
            for (int i = 0; i < N; i++) {
                Assert.assertEquals(i, mem.getLong(i * 8));
            }
            Assert.assertEquals(8L * N, mem.getAppendOffset());
        }
        try (MemoryCMARW mem = Vm.getSmallCMARWInstance(FF, path, MemoryTag.MMAP_DEFAULT)) {
            final int M = (int) (mem.size() / Long.BYTES);
            for (int i = 0; i < M; i++) {
                Assert.assertEquals(i, mem.getLong(i * 8L));
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) Test(org.junit.Test)

Example 4 with MemoryCMARW

use of io.questdb.cairo.vm.api.MemoryCMARW in project questdb by bluestreak01.

the class CairoMemoryTest method testReadWriteMemoryTruncate.

@Test
public void testReadWriteMemoryTruncate() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
            int pageSize = 1024 * 1024;
            try (MemoryCMARW mem = Vm.getSmallCMARWInstance(FF, path, MemoryTag.MMAP_DEFAULT)) {
                int count = 2 * pageSize / Long.BYTES;
                for (int i = 0; i < count; i++) {
                    mem.putLong(i);
                }
                long fileSize = FF.length(path);
                // read the whole file
                long addr = FF.mmap(mem.getFd(), fileSize, 0, Files.MAP_RO, MemoryTag.MMAP_DEFAULT);
                try {
                    for (int i = 0; i < count; i++) {
                        Assert.assertEquals(i, Unsafe.getUnsafe().getLong(addr + i * Long.BYTES));
                    }
                } finally {
                    FF.munmap(addr, fileSize, MemoryTag.MMAP_DEFAULT);
                }
                // truncate
                mem.truncate();
                // ensure that entire file is zeroed out
                fileSize = FF.length(path);
                addr = FF.mmap(mem.getFd(), fileSize, 0, Files.MAP_RO, MemoryTag.MMAP_DEFAULT);
                try {
                    for (int i = 0; i < fileSize / Long.BYTES; i++) {
                        Assert.assertEquals(0, Unsafe.getUnsafe().getLong(addr + i * 8L));
                    }
                } finally {
                    FF.munmap(addr, fileSize, MemoryTag.MMAP_DEFAULT);
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) Test(org.junit.Test)

Example 5 with MemoryCMARW

use of io.questdb.cairo.vm.api.MemoryCMARW in project questdb by bluestreak01.

the class CairoMemoryTest method testWriteOverMapFailuresAndRead.

@Test
public void testWriteOverMapFailuresAndRead() throws Exception {
    Rnd rnd = new Rnd();
    class X extends FilesFacadeImpl {

        @Override
        public long getMapPageSize() {
            return super.getPageSize();
        }

        @Override
        public long mremap(long fd, long addr, long previousSize, long newSize, long offset, int mode, int memoryTag) {
            if (rnd.nextBoolean()) {
                return -1;
            }
            return super.mremap(fd, addr, previousSize, newSize, offset, mode, memoryTag);
        }
    }
    final X ff = new X();
    TestUtils.assertMemoryLeak(() -> {
        int writeFailureCount = 0;
        try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
            try (MemoryCMARW mem = Vm.getSmallCMARWInstance(ff, path, MemoryTag.MMAP_DEFAULT)) {
                int i = 0;
                while (i < N) {
                    try {
                        mem.putLong(i);
                        i++;
                    } catch (CairoException ignore) {
                        writeFailureCount++;
                        break;
                    }
                }
            }
        }
        Assert.assertTrue(writeFailureCount > 0);
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryCMARW(io.questdb.cairo.vm.api.MemoryCMARW) Test(org.junit.Test)

Aggregations

MemoryCMARW (io.questdb.cairo.vm.api.MemoryCMARW)11 Path (io.questdb.std.str.Path)10 Test (org.junit.Test)8 MemoryMR (io.questdb.cairo.vm.api.MemoryMR)2 Gson (com.google.gson.Gson)1 Record (io.questdb.cairo.sql.Record)1 RecordCursor (io.questdb.cairo.sql.RecordCursor)1 MemoryCMRImpl (io.questdb.cairo.vm.MemoryCMRImpl)1 LPSZ (io.questdb.std.str.LPSZ)1