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();
}
}
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);
}
}
}
}
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());
}
}
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").$()));
}
}
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));
}
}
Aggregations