use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class $TabsRecordSource method sumSizes.
private static long sumSizes(LPSZ path) {
long total = 0;
long find = Files.findFirst(path);
if (find == 0) {
return 0;
}
try (Path cp = new Path()) {
NativeLPSZ file = tlNativeLpsz.get();
try {
do {
file.of(Files.findName(find));
if (Files.isDots(file)) {
continue;
}
LPSZ n = cp.of(path).concat(file).$();
if (Files.findType(find) == Files.DT_DIR) {
total += sumSizes(n);
} else {
total += Files.length(n);
}
} while (Files.findNext(find) > 0);
} finally {
Files.findClose(find);
}
}
return total;
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class $TabsRecordSource method prepareCursor.
@Override
public RecordCursor prepareCursor(ReaderFactory factory, CancellationHandler cancellationHandler) {
records.clear();
NativeLPSZ name = tlNativeLpsz.get();
int bufSz = metaSize;
long buf = Unsafe.malloc(bufSz);
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;
}
long lastModified = Files.getLastModified(compositePath.of(base).concat(name).$());
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;
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;
partitionBy = Unsafe.getUnsafe().getInt(readPtr);
columnCount = Unsafe.getUnsafe().getInt(readPtr + 4);
} finally {
Files.close(fd);
}
p = records.beginRecord(p);
// name
records.appendStr(name);
// partition type
records.appendInt(partitionBy);
// partition count
records.appendInt(countDirs(compositePath.of(base).concat(name).$()));
// column count
records.appendInt(columnCount);
// last modified
records.appendLong(lastModified);
// size
records.appendLong(sumSizes(compositePath));
}
}
} while (Files.findNext(find) > 0);
} finally {
Files.findClose(find);
}
}
} finally {
Unsafe.free(buf, bufSz);
}
records.toTop();
return records;
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class SqlLexerOptimiserTest method createModelsAndRun.
private void createModelsAndRun(CairoAware runnable, TableModel... tableModels) throws ParserException {
try {
for (int i = 0, n = tableModels.length; i < n; i++) {
CairoTestUtils.create(tableModels[i]);
}
runnable.run();
} finally {
Assert.assertTrue(engine.releaseAllReaders());
for (int i = 0, n = tableModels.length; i < n; i++) {
TableModel tableModel = tableModels[i];
Path path = tableModel.getPath().of(tableModel.getCairoCfg().getRoot()).concat(tableModel.getName()).put(Files.SEPARATOR).$();
Assert.assertTrue(configuration.getFilesFacade().rmdir(path));
tableModel.close();
}
}
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class ZipTest method testGzip.
@Test
public void testGzip() throws Exception {
try (Path path = new Path()) {
File outFile = temp.newFile("x");
File expected = new File(ZipTest.class.getResource("/large.csv").getFile());
final int available = 64 * 1024;
long in = Unsafe.malloc(available);
long out = Unsafe.malloc(available / 2);
try {
long strm = Zip.deflateInit();
try {
long pIn = 0;
long pOut = 0;
long fdIn = Files.openRO(path.of(expected.getAbsolutePath()).$());
try {
long fdOut = Files.openRW(path.of(outFile.getAbsolutePath()).$());
try {
// header
Files.write(fdOut, Zip.gzipHeader, Zip.gzipHeaderLen, pOut);
pOut += Zip.gzipHeaderLen;
int len;
int crc = 0;
while ((len = (int) Files.read(fdIn, in, available, pIn)) > 0) {
pIn += len;
Zip.setInput(strm, in, len);
crc = Zip.crc32(crc, in, len);
do {
int ret;
if ((ret = Zip.deflate(strm, out, available, false)) < 0) {
throw new FatalError("Error in deflator: " + ret);
}
int have = available - Zip.availOut(strm);
if (have > 0) {
Files.write(fdOut, out, have, pOut);
pOut += have;
}
} while (Zip.availIn(strm) > 0);
}
int ret;
do {
if ((ret = Zip.deflate(strm, out, available, true)) < 0) {
throw new FatalError("Error in deflator: " + ret);
}
int have = available - Zip.availOut(strm);
if (have > 0) {
Files.write(fdOut, out, have, pOut);
pOut += have;
}
} while (ret != 1);
// write trailer
Unsafe.getUnsafe().putInt(out, crc);
Unsafe.getUnsafe().putInt(out + 4, (int) pIn);
Files.write(fdOut, out, 8, pOut);
} finally {
Files.close(fdOut);
}
} finally {
Files.close(fdIn);
}
} finally {
Zip.deflateEnd(strm);
}
} finally {
Unsafe.free(in, available);
Unsafe.free(out, available / 2);
}
// ok. read what we produced
File actual = temp.newFile();
try (GZIPInputStream is = new GZIPInputStream(new FileInputStream(outFile));
FileOutputStream fos = new FileOutputStream(actual)) {
byte[] buf = new byte[16 * 1024];
int l;
while ((l = is.read(buf)) > 0) {
fos.write(buf, 0, l);
}
}
TestUtils.assertEquals(expected, actual);
}
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class JournalConfigurationImpl method exists.
public int exists(CharSequence location) {
Path path = tlPath.get();
String base = getJournalBase().getAbsolutePath();
if (!Files.exists(path.of(base).concat(location).$())) {
return JournalConfiguration.DOES_NOT_EXIST;
}
if (Files.exists(path.of(base).concat(location).concat(TxLog.FILE_NAME).$()) && Files.exists(path.of(base).concat(location).concat(JournalConfiguration.FILE_NAME).$())) {
return JournalConfiguration.EXISTS;
}
return JournalConfiguration.EXISTS_FOREIGN;
}
Aggregations