Search in sources :

Example 16 with Path

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

the class FilesTest method testWrite.

@Test
public void testWrite() throws Exception {
    try (Path path = new Path()) {
        File f = temporaryFolder.newFile();
        long fd = Files.openRW(path.of(f.getAbsolutePath()).$());
        try {
            Assert.assertTrue(fd > 0);
            ByteBuffer buf = ByteBuffer.allocateDirect(1024).order(ByteOrder.LITTLE_ENDIAN);
            try {
                ByteBuffers.putStr(buf, "hello from java");
                int len = buf.position();
                Assert.assertEquals(len, Files.write(fd, ByteBuffers.getAddress(buf), len, 0));
                buf.clear();
                ByteBuffers.putStr(buf, ", awesome");
                Files.write(fd, ByteBuffers.getAddress(buf), buf.position(), len);
            } finally {
                ByteBuffers.release(buf);
            }
        } finally {
            Files.close(fd);
        }
        fd = Files.openRO(path);
        try {
            Assert.assertTrue(fd > 0);
            ByteBuffer buf = ByteBuffer.allocateDirect(1024).order(ByteOrder.LITTLE_ENDIAN);
            try {
                int len = (int) Files.length(path);
                long ptr = ByteBuffers.getAddress(buf);
                Assert.assertEquals(48, Files.read(fd, ptr, len, 0));
                DirectCharSequence cs = new DirectCharSequence().of(ptr, ptr + len);
                TestUtils.assertEquals("hello from java, awesome", cs);
            } finally {
                ByteBuffers.release(buf);
            }
        } finally {
            Files.close(fd);
        }
        Assert.assertTrue(Files.exists(path));
        Assert.assertFalse(Files.exists(path.of("/x/yz/1/2/3")));
    }
}
Also used : Path(com.questdb.std.str.Path) DirectCharSequence(com.questdb.std.str.DirectCharSequence) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 17 with Path

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

the class FilesTest method testMkdirs.

@Test
public void testMkdirs() throws Exception {
    File r = temporaryFolder.newFolder("to_delete");
    try (Path path = new Path().of(r.getAbsolutePath())) {
        path.concat("a").concat("b").concat("c").concat("f.text").$();
        Assert.assertEquals(0, Files.mkdirs(path, 509));
    }
    try (Path path = new Path().of(r.getAbsolutePath())) {
        path.concat("a").concat("b").concat("c").$();
        Assert.assertTrue(Files.exists(path));
    }
}
Also used : Path(com.questdb.std.str.Path) File(java.io.File) Test(org.junit.Test)

Example 18 with Path

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

the class BitmapIndexTest method testCursorTimeout.

@Test
public void testCursorTimeout() 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 (BitmapIndexBackwardReader reader = new BitmapIndexBackwardReader(configuration, path.trimTo(plen), "x", 0)) {
            try (ReadWriteMemory mem = new ReadWriteMemory()) {
                try (Path path = new Path()) {
                    path.of(configuration.getRoot()).concat("x").put(".k").$();
                    mem.of(configuration.getFilesFacade(), path, configuration.getFilesFacade().getPageSize());
                }
                long offset = BitmapIndexUtils.getKeyEntryOffset(0);
                mem.jumpTo(offset + BitmapIndexUtils.KEY_ENTRY_OFFSET_VALUE_COUNT);
                mem.putLong(10);
                try {
                    reader.getCursor(0, Long.MAX_VALUE);
                    Assert.fail();
                } catch (CairoException e) {
                    Assert.assertTrue(Chars.contains(e.getMessage(), "cursor failed"));
                }
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) Test(org.junit.Test)

Example 19 with Path

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

the class BitmapIndexTest method testConcurrentRW.

private void testConcurrentRW(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 (BitmapIndexBackwardReader reader1 = new BitmapIndexBackwardReader(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(key, 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(sz - k - 1), 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(com.questdb.std.str.Path) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowCursor(com.questdb.common.RowCursor)

Example 20 with Path

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

the class CairoMemoryTest method testSlidingWindowMemory.

@Test
public void testSlidingWindowMemory() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path()) {
            path.of(temp.getRoot().getAbsolutePath());
            final int N = 100000;
            final Rnd rnd = new Rnd();
            try (AppendMemory mem = new AppendMemory()) {
                mem.of(FF, path.concat("x.dat").$(), FF.getPageSize());
                for (int i = 0; i < N; i++) {
                    mem.putLong(rnd.nextLong());
                }
                try (SlidingWindowMemory mem2 = new SlidingWindowMemory()) {
                    mem2.of(mem);
                    rnd.reset();
                    for (int i = 0; i < N; i++) {
                        Assert.assertEquals(rnd.nextLong(), mem2.getLong(i * 8));
                    }
                }
            }
        }
    });
}
Also used : Path(com.questdb.std.str.Path) 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