use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class CairoMemoryTest method testWriteAndRead.
@Test
public void testWriteAndRead() throws Exception {
long used = Unsafe.getMemUsed();
try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
try (ReadWriteMemory mem = new ReadWriteMemory(FF, path, 2 * FF.getPageSize())) {
for (int i = 0; i < N; i++) {
mem.putLong(i);
}
// read in place
for (int i = 0; i < N; i++) {
Assert.assertEquals(i, mem.getLong(i * 8));
}
Assert.assertEquals(8L * N, mem.getAppendOffset());
}
try (ReadWriteMemory mem = new ReadWriteMemory(FF, path, FF.getPageSize())) {
for (int i = 0; i < N; i++) {
Assert.assertEquals(i, mem.getLong(i * 8));
}
}
}
Assert.assertEquals(used, Unsafe.getMemUsed());
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class CairoMemoryTest method testAppendCannotOpenFile.
@Test
public void testAppendCannotOpenFile() {
long used = Unsafe.getMemUsed();
class X extends FilesFacadeImpl {
@Override
public long openRW(LPSZ name) {
int n = name.length();
if (n > 5 && Chars.equals(".fail", name, n - 5, n)) {
return -1;
}
return super.openRW(name);
}
}
X ff = new X();
long openFileCount = ff.getOpenFileCount();
int successCount = 0;
int failCount = 0;
try (Path path = new Path()) {
path.of(temp.getRoot().getAbsolutePath());
int prefixLen = path.length();
try (AppendMemory mem = new AppendMemory()) {
Rnd rnd = new Rnd();
for (int k = 0; k < 10; k++) {
path.trimTo(prefixLen).concat(rnd.nextString(10));
boolean fail = rnd.nextBoolean();
if (fail) {
path.put(".fail").$();
failCount++;
} else {
path.put(".data").$();
successCount++;
}
if (fail) {
try {
mem.of(ff, path, 2 * ff.getPageSize());
Assert.fail();
} catch (CairoException ignored) {
}
} else {
mem.of(ff, path, 2 * ff.getPageSize());
for (int i = 0; i < N; i++) {
mem.putLong(i);
}
Assert.assertEquals(N * 8, mem.getAppendOffset());
}
}
}
}
Assert.assertEquals(used, Unsafe.getMemUsed());
Assert.assertEquals(openFileCount, ff.getOpenFileCount());
Assert.assertTrue(failCount > 0);
Assert.assertTrue(successCount > 0);
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class CairoMemoryTest method testAppendAndCannotMap.
@Test
public void testAppendAndCannotMap() throws Exception {
long used = Unsafe.getMemUsed();
Rnd rnd = new Rnd();
class X extends FilesFacadeImpl {
@Override
public long mmap(long fd, long len, long offset, int mode) {
if (rnd.nextBoolean()) {
return -1;
}
return super.mmap(fd, len, offset, mode);
}
}
X ff = new X();
try (Path path = new Path().of(temp.newFile().getAbsolutePath()).$()) {
try (AppendMemory mem = new AppendMemory(FF, path, 2 * FF.getPageSize())) {
for (int i = 0; i < N; i++) {
mem.putLong(i);
}
Assert.assertEquals(8L * N, mem.getAppendOffset());
}
int failureCount = 0;
try (ReadOnlyMemory mem = new ReadOnlyMemory()) {
mem.of(ff, path, ff.getPageSize(), 8L * N);
int i = 0;
while (i < N) {
try {
Assert.assertEquals(i, mem.getLong(i * 8));
i++;
} catch (CairoException ignore) {
failureCount++;
}
}
Assert.assertTrue(failureCount > 0);
}
}
Assert.assertEquals(used, Unsafe.getMemUsed());
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class CairoMemoryTest method testAppendAfterMMapFailure.
@Test
public void testAppendAfterMMapFailure() throws Exception {
long used = Unsafe.getMemUsed();
Rnd rnd = new Rnd();
class X extends FilesFacadeImpl {
boolean force = true;
@Override
public long mmap(long fd, long len, long offset, int mode) {
if (force || rnd.nextBoolean()) {
force = false;
return super.mmap(fd, len, offset, mode);
} else {
return -1;
}
}
}
X ff = new X();
long openFileCount = ff.getOpenFileCount();
int failureCount = 0;
try (Path path = new Path()) {
path.of(temp.newFile().getAbsolutePath());
try (AppendMemory mem = new AppendMemory()) {
mem.of(ff, path.$(), ff.getPageSize() * 2);
int i = 0;
while (i < N) {
try {
mem.putLong(i);
i++;
} catch (CairoException ignore) {
failureCount++;
}
}
Assert.assertEquals(N * 8, mem.getAppendOffset());
}
}
Assert.assertTrue(failureCount > 0);
Assert.assertEquals(used, Unsafe.getMemUsed());
Assert.assertEquals(openFileCount, ff.getOpenFileCount());
}
use of com.questdb.std.str.Path in project questdb by bluestreak01.
the class CairoMemoryTest method testReadWriteCannotOpenFile.
@Test
public void testReadWriteCannotOpenFile() {
long used = Unsafe.getMemUsed();
class X extends FilesFacadeImpl {
@Override
public long openRW(LPSZ name) {
int n = name.length();
if (n > 5) {
if (Chars.equals(".fail", name, n - 5, n)) {
return -1;
}
}
return super.openRW(name);
}
}
X ff = new X();
long openFileCount = ff.getOpenFileCount();
int successCount = 0;
int failCount = 0;
try (Path path = new Path()) {
path.of(temp.getRoot().getAbsolutePath());
int prefixLen = path.length();
try (ReadWriteMemory mem = new ReadWriteMemory()) {
Rnd rnd = new Rnd();
for (int k = 0; k < 10; k++) {
path.trimTo(prefixLen).concat(rnd.nextString(10));
boolean fail = rnd.nextBoolean();
if (fail) {
path.put(".fail").$();
failCount++;
} else {
path.put(".data").$();
successCount++;
}
if (fail) {
try {
mem.of(ff, path, 2 * ff.getPageSize());
Assert.fail();
} catch (CairoException ignored) {
}
} else {
mem.of(ff, path, 2 * ff.getPageSize());
for (int i = 0; i < N; i++) {
mem.putLong(i);
}
Assert.assertEquals(N * 8, mem.getAppendOffset());
}
}
}
}
Assert.assertEquals(used, Unsafe.getMemUsed());
Assert.assertEquals(openFileCount, ff.getOpenFileCount());
Assert.assertTrue(failCount > 0);
Assert.assertTrue(successCount > 0);
}
Aggregations