Search in sources :

Example 1 with DirectCharSequence

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

the class $ColsRecordSource method prepareCursor.

@Override
public RecordCursor prepareCursor(ReaderFactory factory, CancellationHandler cancellationHandler) {
    records.clear();
    NativeLPSZ name = tlNativeLpsz.get();
    int bufSz = metaSize;
    long buf = Unsafe.malloc(bufSz);
    DirectCharSequence dcs = tlDcs.get();
    try {
        String base = factory.getConfiguration().getJournalBase().getAbsolutePath();
        Path path = tlPath.get().of(base).$();
        long find = Files.findFirst(path);
        if (find > 0) {
            try {
                long p = -1;
                Path compositePath = tlCompositePath.get();
                do {
                    cancellationHandler.check();
                    if (Files.findType(find) == Files.DT_DIR) {
                        name.of(Files.findName(find));
                        if (Files.isDots(name)) {
                            continue;
                        }
                        compositePath.of(base).concat(name).concat(JournalConfiguration.FILE_NAME).$();
                        if (Files.exists(compositePath)) {
                            long fd = Files.openRO(compositePath);
                            if (fd < 0) {
                                LOG.error().$("Cannot open: ").$(compositePath).$(" [").$(Os.errno()).$(']').$();
                                continue;
                            }
                            int columnCount;
                            int partitionBy;
                            int timestampIndex;
                            try {
                                long len = Files.length(compositePath);
                                // don't bother with huge meta files. There is a high chance of them being fake.
                                if (len > maxMetaSize) {
                                    LOG.error().$("File : ").$(compositePath).$(" is too large [").$(len).$(']').$();
                                    continue;
                                }
                                if (len > bufSz) {
                                    Unsafe.free(buf, bufSz);
                                    buf = Unsafe.malloc(bufSz = (int) len);
                                }
                                Files.read(fd, buf, (int) len, 0);
                                // skip over append offset
                                long readPtr = buf + 8;
                                // skip over ID string
                                readPtr += Unsafe.getUnsafe().getInt(readPtr) * 2 + 4;
                                // // skip over location string
                                // readPtr += Unsafe.getUnsafe().getInt(readPtr) * 2 + 4;
                                partitionBy = Unsafe.getUnsafe().getInt(readPtr);
                                columnCount = Unsafe.getUnsafe().getInt(readPtr + 4);
                                timestampIndex = Unsafe.getUnsafe().getInt(readPtr + 8);
                                readPtr += 12;
                                for (int i = 0; i < columnCount; i++) {
                                    p = records.beginRecord(p);
                                    // table_name
                                    records.appendStr(name);
                                    // column_name
                                    int l = Unsafe.getUnsafe().getInt(readPtr) * 2;
                                    readPtr += 4;
                                    records.appendStr(dcs.of(readPtr, readPtr + l));
                                    readPtr += l;
                                    // column_type
                                    records.appendInt(Unsafe.getUnsafe().getInt(readPtr));
                                    // skip size and avgsize
                                    readPtr += 12;
                                    // timestamp
                                    records.appendBool(i == timestampIndex);
                                    // partition_by
                                    records.appendInt(i == timestampIndex ? partitionBy : SymbolTable.VALUE_IS_NULL);
                                    // indexed
                                    records.appendBool(Unsafe.getBool(readPtr));
                                    // skip 2 ints
                                    readPtr += 9;
                                    // index_buckets
                                    records.appendInt(Unsafe.getUnsafe().getInt(readPtr));
                                    readPtr += 4;
                                    l = Unsafe.getUnsafe().getInt(readPtr);
                                    readPtr += 4;
                                    if (l > -1) {
                                        readPtr += (l * 2);
                                    }
                                    readPtr += 1;
                                }
                            } finally {
                                Files.close(fd);
                            }
                        }
                    }
                } while (Files.findNext(find) > 0);
            } finally {
                Files.findClose(find);
            }
        }
    } finally {
        Unsafe.free(buf, bufSz);
    }
    records.toTop();
    return records;
}
Also used : Path(com.questdb.std.str.Path) DirectCharSequence(com.questdb.std.str.DirectCharSequence) NativeLPSZ(com.questdb.std.str.NativeLPSZ)

Example 2 with DirectCharSequence

use of com.questdb.std.str.DirectCharSequence 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 3 with DirectCharSequence

use of com.questdb.std.str.DirectCharSequence 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)

Aggregations

DirectCharSequence (com.questdb.std.str.DirectCharSequence)3 Path (com.questdb.std.str.Path)3 File (java.io.File)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 NativeLPSZ (com.questdb.std.str.NativeLPSZ)1