Search in sources :

Example 21 with Path

use of com.questdb.std.str.Path 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 (ReadWriteMemory mem = new ReadWriteMemory(FF, path, 2 * FF.getPageSize())) {
            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 (ReadWriteMemory mem = new ReadWriteMemory(FF, path, FF.getPageSize())) {
            for (int i = 0; i < N; i++) {
                Assert.assertEquals(i, mem.getLong(i * 8));
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
}
Also used : Path(com.questdb.std.str.Path) Test(org.junit.Test)

Example 22 with Path

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

the class CairoMemoryTest method testAppendCannotOpenFile.

@Test
public void testAppendCannotOpenFile() {
    long used = Unsafe.getMemUsed();
    class X extends FilesFacadeImpl {

        @Override
        public long openRW(LPSZ name) {
            int n = name.length();
            if (n > 5 && Chars.equals(".fail", name, n - 5, n)) {
                return -1;
            }
            return super.openRW(name);
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int successCount = 0;
    int failCount = 0;
    try (Path path = new Path()) {
        path.of(temp.getRoot().getAbsolutePath());
        int prefixLen = path.length();
        try (AppendMemory mem = new AppendMemory()) {
            Rnd rnd = new Rnd();
            for (int k = 0; k < 10; k++) {
                path.trimTo(prefixLen).concat(rnd.nextString(10));
                boolean fail = rnd.nextBoolean();
                if (fail) {
                    path.put(".fail").$();
                    failCount++;
                } else {
                    path.put(".data").$();
                    successCount++;
                }
                if (fail) {
                    try {
                        mem.of(ff, path, 2 * ff.getPageSize());
                        Assert.fail();
                    } catch (CairoException ignored) {
                    }
                } else {
                    mem.of(ff, path, 2 * ff.getPageSize());
                    for (int i = 0; i < N; i++) {
                        mem.putLong(i);
                    }
                    Assert.assertEquals(N * 8, mem.getAppendOffset());
                }
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
    Assert.assertTrue(failCount > 0);
    Assert.assertTrue(successCount > 0);
}
Also used : Path(com.questdb.std.str.Path) LPSZ(com.questdb.std.str.LPSZ) Test(org.junit.Test)

Example 23 with Path

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

the class CairoMemoryTest method testAppendAndCannotMap.

@Test
public void testAppendAndCannotMap() throws Exception {
    long used = Unsafe.getMemUsed();
    Rnd rnd = new Rnd();
    class X extends FilesFacadeImpl {

        @Override
        public long mmap(long fd, long len, long offset, int mode) {
            if (rnd.nextBoolean()) {
                return -1;
            }
            return super.mmap(fd, len, offset, mode);
        }
    }
    X ff = new X();
    try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
        try (AppendMemory mem = new AppendMemory(FF, path, 2 * FF.getPageSize())) {
            for (int i = 0; i < N; i++) {
                mem.putLong(i);
            }
            Assert.assertEquals(8L * N, mem.getAppendOffset());
        }
        int failureCount = 0;
        try (ReadOnlyMemory mem = new ReadOnlyMemory()) {
            mem.of(ff, path, ff.getPageSize(), 8L * N);
            int i = 0;
            while (i < N) {
                try {
                    Assert.assertEquals(i, mem.getLong(i * 8));
                    i++;
                } catch (CairoException ignore) {
                    failureCount++;
                }
            }
            Assert.assertTrue(failureCount > 0);
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
}
Also used : Path(com.questdb.std.str.Path) Test(org.junit.Test)

Example 24 with Path

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

the class CairoMemoryTest method testAppendAfterMMapFailure.

@Test
public void testAppendAfterMMapFailure() throws Exception {
    long used = Unsafe.getMemUsed();
    Rnd rnd = new Rnd();
    class X extends FilesFacadeImpl {

        boolean force = true;

        @Override
        public long mmap(long fd, long len, long offset, int mode) {
            if (force || rnd.nextBoolean()) {
                force = false;
                return super.mmap(fd, len, offset, mode);
            } else {
                return -1;
            }
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int failureCount = 0;
    try (Path path = new Path()) {
        path.of(temp.newFile().getAbsolutePath());
        try (AppendMemory mem = new AppendMemory()) {
            mem.of(ff, path.$(), ff.getPageSize() * 2);
            int i = 0;
            while (i < N) {
                try {
                    mem.putLong(i);
                    i++;
                } catch (CairoException ignore) {
                    failureCount++;
                }
            }
            Assert.assertEquals(N * 8, mem.getAppendOffset());
        }
    }
    Assert.assertTrue(failureCount > 0);
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
}
Also used : Path(com.questdb.std.str.Path) Test(org.junit.Test)

Example 25 with Path

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

the class CairoMemoryTest method testReadWriteCannotOpenFile.

@Test
public void testReadWriteCannotOpenFile() {
    long used = Unsafe.getMemUsed();
    class X extends FilesFacadeImpl {

        @Override
        public long openRW(LPSZ name) {
            int n = name.length();
            if (n > 5) {
                if (Chars.equals(".fail", name, n - 5, n)) {
                    return -1;
                }
            }
            return super.openRW(name);
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int successCount = 0;
    int failCount = 0;
    try (Path path = new Path()) {
        path.of(temp.getRoot().getAbsolutePath());
        int prefixLen = path.length();
        try (ReadWriteMemory mem = new ReadWriteMemory()) {
            Rnd rnd = new Rnd();
            for (int k = 0; k < 10; k++) {
                path.trimTo(prefixLen).concat(rnd.nextString(10));
                boolean fail = rnd.nextBoolean();
                if (fail) {
                    path.put(".fail").$();
                    failCount++;
                } else {
                    path.put(".data").$();
                    successCount++;
                }
                if (fail) {
                    try {
                        mem.of(ff, path, 2 * ff.getPageSize());
                        Assert.fail();
                    } catch (CairoException ignored) {
                    }
                } else {
                    mem.of(ff, path, 2 * ff.getPageSize());
                    for (int i = 0; i < N; i++) {
                        mem.putLong(i);
                    }
                    Assert.assertEquals(N * 8, mem.getAppendOffset());
                }
            }
        }
    }
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
    Assert.assertTrue(failCount > 0);
    Assert.assertTrue(successCount > 0);
}
Also used : Path(com.questdb.std.str.Path) LPSZ(com.questdb.std.str.LPSZ) Test(org.junit.Test)

Aggregations

Path (com.questdb.std.str.Path)74 Test (org.junit.Test)46 File (java.io.File)8 Rnd (com.questdb.std.Rnd)7 LPSZ (com.questdb.std.str.LPSZ)6 NativeLPSZ (com.questdb.std.str.NativeLPSZ)5 NumericException (com.questdb.common.NumericException)3 RecordColumnMetadata (com.questdb.common.RecordColumnMetadata)3 DirectCharSequence (com.questdb.std.str.DirectCharSequence)3 StringSink (com.questdb.std.str.StringSink)3 RowCursor (com.questdb.common.RowCursor)2 CreateTableModel (com.questdb.griffin.lexer.model.CreateTableModel)2 ObjList (com.questdb.std.ObjList)2 JournalMetadata (com.questdb.store.factory.configuration.JournalMetadata)2 TestMicroClock (com.questdb.test.tools.TestMicroClock)2 ByteBuffer (java.nio.ByteBuffer)2 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1 Chars (com.questdb.std.Chars)1 FilesFacade (com.questdb.std.FilesFacade)1