Search in sources :

Example 41 with Path

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

the class JsonLexerTest method testParseLargeFile.

@Test
public void testParseLargeFile() throws Exception {
    String path = JsonLexerTest.class.getResource("/json/test.json").getPath();
    Path p = new Path();
    if (Os.type == Os.WINDOWS && path.startsWith("/")) {
        p.of(path.substring(1));
    } else {
        p.of(path);
    }
    try {
        long l = Files.length(p.$());
        long fd = Files.openRO(p);
        JsonParser listener = new NoOpParser();
        try {
            long buf = Unsafe.malloc(l);
            long bufA = Unsafe.malloc(l);
            long bufB = Unsafe.malloc(l);
            try {
                Assert.assertEquals(l, Files.read(fd, buf, (int) l, 0));
                long t = System.nanoTime();
                for (int i = 0; i < l; i++) {
                    try {
                        LEXER.clear();
                        Unsafe.getUnsafe().copyMemory(buf, bufA, i);
                        Unsafe.getUnsafe().copyMemory(buf + i, bufB, l - i);
                        LEXER.parse(bufA, i, listener);
                        LEXER.parse(bufB, l - i, listener);
                        LEXER.parseLast();
                    } catch (JsonException e) {
                        System.out.println(i);
                        throw e;
                    }
                }
                System.out.println((System.nanoTime() - t) / l);
            } finally {
                Unsafe.free(buf, l);
                Unsafe.free(bufA, l);
                Unsafe.free(bufB, l);
            }
        } finally {
            Files.close(fd);
        }
    } finally {
        p.close();
    }
}
Also used : Path(com.questdb.std.str.Path) Test(org.junit.Test)

Example 42 with Path

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

the class Factory method rename0.

private void rename0(CharSequence from, CharSequence to) throws JournalException {
    try (Path oldName = new Path()) {
        try (Path newName = new Path()) {
            String path = getConfiguration().getJournalBase().getAbsolutePath();
            oldName.of(path).concat(from).$();
            newName.of(path).concat(to).$();
            if (!Files.exists(oldName)) {
                LOG.error().$("Journal does not exist: ").$(oldName).$();
                throw JournalDoesNotExistException.INSTANCE;
            }
            if (Os.type == Os.WINDOWS) {
                oldName.of("\\\\?\\").concat(path).concat(from).$();
                newName.of("\\\\?\\").concat(path).concat(to).$();
            }
            String oname = oldName.toString();
            Lock lock = LockManager.lockExclusive(oname);
            try {
                if (lock == null || !lock.isValid()) {
                    LOG.error().$("Cannot obtain lock on ").$(oldName).$();
                    throw JournalWriterAlreadyOpenException.INSTANCE;
                }
                if (Files.exists(newName)) {
                    throw JournalExistsException.INSTANCE;
                }
                Lock writeLock = LockManager.lockExclusive(newName.toString());
                try {
                    // this should not happen because we checked for existence before
                    if (writeLock == null || !writeLock.isValid()) {
                        LOG.error().$("Cannot obtain lock on ").$(newName).$();
                        throw FactoryInternalException.INSTANCE;
                    }
                    metadataCache.remove(oname);
                    if (!Files.rename(oldName, newName)) {
                        LOG.error().$("Cannot rename ").$(oldName).$(" to ").$(newName).$(": ").$(Os.errno()).$();
                        throw SystemException.INSTANCE;
                    }
                } finally {
                    LockManager.release(writeLock);
                }
            } finally {
                LockManager.release(lock);
            }
        }
    }
}
Also used : Path(com.questdb.std.str.Path)

Example 43 with Path

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

the class FilesTest method testDeleteDir2.

@Test
public void testDeleteDir2() throws Exception {
    File r = temporaryFolder.newFolder("to_delete");
    Assert.assertTrue(new File(r, "a/b/c").mkdirs());
    Assert.assertTrue(new File(r, "d/e/f").mkdirs());
    touch(new File(r, "d/1.txt"));
    touch(new File(r, "a/b/2.txt"));
    try (Path path = new Path().of(r.getAbsolutePath()).$()) {
        Assert.assertTrue(Files.rmdir(path));
        Assert.assertFalse(r.exists());
    }
}
Also used : Path(com.questdb.std.str.Path) File(java.io.File) Test(org.junit.Test)

Example 44 with Path

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

the class FilesTest method testAppendAndSeqRead.

@Test
public void testAppendAndSeqRead() 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");
                Files.append(fd, ByteBuffers.getAddress(buf), buf.position());
                buf.clear();
                ByteBuffers.putStr(buf, ", awesome");
                Files.append(fd, ByteBuffers.getAddress(buf), buf.position());
            } 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.sequentialRead(fd, ptr, len));
                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 45 with Path

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

the class FilesTest method testRemove.

@Test
public void testRemove() throws Exception {
    try (Path path = new Path().of(temporaryFolder.newFile().getAbsolutePath()).$()) {
        Assert.assertTrue(Files.touch(path));
        Assert.assertTrue(Files.exists(path));
        Assert.assertTrue(Files.remove(path));
        Assert.assertFalse(Files.exists(path));
    }
}
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