use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.
the class BitmapIndexTest method testBeyondPageSize.
@Test
public void testBeyondPageSize() throws Exception {
// value count for single pass, there are two passes
final int N = 2_000_000;
final int K = 1000;
TestUtils.assertMemoryLeak(() -> {
// values array holds
IntList values = new IntList(N + N);
values.setPos(N + N);
final Rnd rnd = new Rnd();
create(configuration, path.trimTo(plen), "x", 1024);
long expectedSum = 0;
// current random value distribution ensures we have all K keys
try (BitmapIndexBwdReader reader = new BitmapIndexBwdReader(configuration, path.trimTo(plen), "x", 0)) {
try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
for (int i = 0; i < N; i++) {
final int k = rnd.nextInt(K);
writer.add(k, i);
expectedSum += i;
}
}
// reopen indexer and add more values
try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
for (int i = 0; i < N; i++) {
final int k = rnd.nextInt(K);
writer.add(k, i + N);
expectedSum += i + N;
}
}
// current random value distribution ensures we have all K keys
reader.updateKeyCount();
Assert.assertEquals(K, reader.getKeyCount());
// compute sum of all values
long sum = 0;
for (int i = 0; i < K; i++) {
RowCursor cursor = reader.getCursor(true, i, Long.MIN_VALUE, Long.MAX_VALUE);
while (cursor.hasNext()) {
sum += cursor.next();
}
}
Assert.assertEquals(expectedSum, sum);
}
// test branch new reader
try (BitmapIndexFwdReader reader = new BitmapIndexFwdReader(configuration, path.trimTo(plen), "x", 0)) {
long sum = 0;
for (int i = 0; i < K; i++) {
RowCursor cursor = reader.getCursor(true, i, Long.MIN_VALUE, Long.MAX_VALUE);
while (cursor.hasNext()) {
sum += cursor.next();
}
}
Assert.assertEquals(expectedSum, sum);
}
});
}
use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.
the class BitmapIndexTest method testSimpleRollback.
@Test
public void testSimpleRollback() throws Exception {
TestUtils.assertMemoryLeak(() -> {
Rnd modelRnd = new Rnd();
final int maxKeys = 1024;
final int N = 1000000;
final int CUTOFF = 60000;
// noinspection ConstantConditions
assert CUTOFF < N;
IntList keys = new IntList();
IntObjHashMap<LongList> lists = new IntObjHashMap<>();
// populate model for both reader and writer
for (int i = 0; i < N; i++) {
int key = modelRnd.nextPositiveInt() % maxKeys;
LongList list = lists.get(key);
if (list == null) {
lists.put(key, list = new LongList());
keys.add(key);
}
if (i > CUTOFF) {
continue;
}
list.add(i);
}
Rnd rnd = new Rnd();
create(configuration, path.trimTo(plen), "x", 1024);
try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
for (int i = 0; i < N; i++) {
writer.add(rnd.nextPositiveInt() % maxKeys, i);
}
writer.rollbackValues(CUTOFF);
}
try (BitmapIndexBwdReader reader = new BitmapIndexBwdReader(configuration, path.trimTo(plen), "x", 0)) {
for (int i = 0, n = keys.size(); i < n; i++) {
int key = keys.getQuick(i);
// do not limit reader, we have to read everything index has
RowCursor cursor = reader.getCursor(true, key, 0, Long.MAX_VALUE);
LongList list = lists.get(key);
int v = list.size();
while (cursor.hasNext()) {
Assert.assertEquals(list.getQuick(--v), cursor.next());
}
Assert.assertEquals(0, v);
}
}
try (BitmapIndexFwdReader reader = new BitmapIndexFwdReader(configuration, path.trimTo(plen), "x", 0)) {
for (int i = 0, n = keys.size(); i < n; i++) {
int key = keys.getQuick(i);
// do not limit reader, we have to read everything index has
RowCursor cursor = reader.getCursor(true, key, 0, Long.MAX_VALUE);
LongList list = lists.get(key);
int v = 0;
while (cursor.hasNext()) {
Assert.assertEquals(list.getQuick(v++), cursor.next());
}
Assert.assertEquals(list.size(), v);
}
}
// add more data to model
for (int i = 0; i < N; i++) {
int key = modelRnd.nextPositiveInt() % maxKeys;
LongList list = lists.get(key);
if (list == null) {
lists.put(key, list = new LongList());
keys.add(key);
}
list.add(i + N);
}
// add more date to index
try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path.trimTo(plen), "x")) {
for (int i = 0; i < N; i++) {
writer.add(rnd.nextPositiveInt() % maxKeys, i + N);
}
}
// assert against model again
try (BitmapIndexBwdReader reader = new BitmapIndexBwdReader(configuration, path.trimTo(plen), "x", 0)) {
for (int i = 0, n = keys.size(); i < n; i++) {
int key = keys.getQuick(i);
// do not limit reader, we have to read everything index has
RowCursor cursor = reader.getCursor(true, key, 0, Long.MAX_VALUE);
LongList list = lists.get(key);
int v = list.size();
while (cursor.hasNext()) {
Assert.assertEquals(list.getQuick(--v), cursor.next());
}
Assert.assertEquals(0, v);
}
}
try (BitmapIndexFwdReader reader = new BitmapIndexFwdReader(configuration, path.trimTo(plen), "x", 0)) {
for (int i = 0, n = keys.size(); i < n; i++) {
int key = keys.getQuick(i);
// do not limit reader, we have to read everything index has
RowCursor cursor = reader.getCursor(true, key, 0, Long.MAX_VALUE);
LongList list = lists.get(key);
int v = 0;
while (cursor.hasNext()) {
Assert.assertEquals(list.getQuick(v++), cursor.next());
}
Assert.assertEquals(list.size(), v);
}
}
});
}
use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.
the class BitmapIndexTest method testForwardReaderKeyUpdateFail.
@Test
public void testForwardReaderKeyUpdateFail() {
create(configuration, path.trimTo(plen), "x", 1024);
try (BitmapIndexWriter writer = new BitmapIndexWriter(configuration, path, "x")) {
writer.add(0, 1000);
}
try (BitmapIndexFwdReader reader = new BitmapIndexFwdReader(configuration, path.trimTo(plen), "x", 0)) {
// should have single value in cursor
RowCursor cursor = reader.getCursor(true, 0, 0, Long.MAX_VALUE);
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(1000, cursor.next());
Assert.assertFalse(cursor.hasNext());
try (Path path = new Path();
MemoryCMARW mem = Vm.getSmallCMARWInstance(configuration.getFilesFacade(), path.of(root).concat("x").put(".k").$(), MemoryTag.MMAP_DEFAULT)) {
// change sequence but not sequence check
long seq = mem.getLong(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE);
mem.putLong(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE, 22);
try {
reader.getCursor(true, 10, 0, Long.MAX_VALUE);
Assert.fail();
} catch (CairoException e) {
Assert.assertTrue(Chars.contains(e.getMessage(), "could not consistently"));
}
try {
reader.getCursor(true, 0, 0, Long.MAX_VALUE);
Assert.fail();
} catch (CairoException e) {
Assert.assertTrue(Chars.contains(e.getMessage(), "could not consistently"));
}
mem.putLong(BitmapIndexUtils.KEY_RESERVED_OFFSET_SEQUENCE, seq);
// test that index recovers
cursor = reader.getCursor(true, 0, 0, Long.MAX_VALUE);
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(1000, cursor.next());
Assert.assertFalse(cursor.hasNext());
}
}
}
use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.
the class SymbolMapWriter method lookupAndPut.
private int lookupAndPut(CharSequence symbol) {
int hash = Hash.boundedHash(symbol, maxHash);
RowCursor cursor = indexWriter.getCursor(hash);
while (cursor.hasNext()) {
long offsetOffset = cursor.next();
if (Chars.equals(symbol, charMem.getStr(offsetMem.getLong(offsetOffset)))) {
return offsetToKey(offsetOffset);
}
}
return put0(symbol, hash);
}
use of io.questdb.cairo.sql.RowCursor in project questdb by bluestreak01.
the class SymbolMapReaderImpl method keyOf.
@Override
public int keyOf(CharSequence value) {
if (value != null) {
int hash = Hash.boundedHash(value, maxHash);
final RowCursor cursor = indexReader.getCursor(true, hash, 0, maxOffset - Long.BYTES);
while (cursor.hasNext()) {
final long offsetOffset = cursor.next();
if (Chars.equals(value, charMem.getStr(offsetMem.getLong(offsetOffset)))) {
return SymbolMapWriter.offsetToKey(offsetOffset);
}
}
return SymbolTable.VALUE_NOT_FOUND;
}
return SymbolTable.VALUE_IS_NULL;
}
Aggregations