use of org.h2.mvstore.Cursor in project h2database by h2database.
the class TreeIndex method findFirstOrLast.
@Override
public Cursor findFirstOrLast(Session session, boolean first) {
if (closed) {
throw DbException.throwInternalError(toString());
}
if (first) {
// TODO optimization: this loops through NULL
Cursor cursor = find(session, null, null);
while (cursor.next()) {
SearchRow row = cursor.getSearchRow();
Value v = row.getValue(columnIds[0]);
if (v != ValueNull.INSTANCE) {
return cursor;
}
}
return cursor;
}
TreeNode x = root, n;
while (x != null) {
n = x.right;
if (n == null) {
break;
}
x = n;
}
TreeCursor cursor = new TreeCursor(this, x, null, null);
if (x == null) {
return cursor;
}
// TODO optimization: this loops through NULL elements
do {
SearchRow row = cursor.getSearchRow();
if (row == null) {
break;
}
Value v = row.getValue(columnIds[0]);
if (v != ValueNull.INSTANCE) {
return cursor;
}
} while (cursor.previous());
return cursor;
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class ViewIndex method find.
private Cursor find(Session session, SearchRow first, SearchRow last, SearchRow intersection) {
if (recursive) {
return findRecursive(first, last);
}
setupQueryParameters(session, first, last, intersection);
ResultInterface result = query.query(0);
return new ViewCursor(this, result, first, last);
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class ViewIndex method findRecursive.
private Cursor findRecursive(SearchRow first, SearchRow last) {
assert recursive;
ResultInterface recursiveResult = view.getRecursiveResult();
if (recursiveResult != null) {
recursiveResult.reset();
return new ViewCursor(this, recursiveResult, first, last);
}
if (query == null) {
Parser parser = new Parser(createSession);
parser.setRightsChecked(true);
parser.setSuppliedParameterList(originalParameters);
query = (Query) parser.prepare(querySQL);
query.setNeverLazy(true);
}
if (!query.isUnion()) {
throw DbException.get(ErrorCode.SYNTAX_ERROR_2, "recursive queries without UNION");
}
SelectUnion union = (SelectUnion) query;
Query left = union.getLeft();
left.setNeverLazy(true);
// to ensure the last result is not closed
left.disableCache();
ResultInterface resultInterface = left.query(0);
LocalResult localResult = union.getEmptyResult();
// ensure it is not written to disk,
// because it is not closed normally
localResult.setMaxMemoryRows(Integer.MAX_VALUE);
while (resultInterface.next()) {
Value[] cr = resultInterface.currentRow();
localResult.addRow(cr);
}
Query right = union.getRight();
right.setNeverLazy(true);
resultInterface.reset();
view.setRecursiveResult(resultInterface);
// to ensure the last result is not closed
right.disableCache();
while (true) {
resultInterface = right.query(0);
if (!resultInterface.hasNext()) {
break;
}
while (resultInterface.next()) {
Value[] cr = resultInterface.currentRow();
localResult.addRow(cr);
}
resultInterface.reset();
view.setRecursiveResult(resultInterface);
}
view.setRecursiveResult(null);
localResult.done();
return new ViewCursor(this, localResult, first, last);
}
use of org.h2.mvstore.Cursor in project h2database by h2database.
the class MVSpatialIndex method findByGeometry.
@Override
public Cursor findByGeometry(TableFilter filter, SearchRow first, SearchRow last, SearchRow intersection) {
Session session = filter.getSession();
if (intersection == null) {
return find(session, first, last);
}
Iterator<SpatialKey> cursor = spatialMap.findIntersectingKeys(getKey(intersection));
TransactionMap<SpatialKey, Value> map = getMap(session);
Iterator<SpatialKey> it = map.wrapIterator(cursor, false);
return new MVStoreCursor(session, it);
}
use of org.h2.mvstore.Cursor in project ignite by apache.
the class GridH2TableSelfTest method testIndexFindFirstOrLast.
/**
* @throws Exception If failed.
*/
public void testIndexFindFirstOrLast() throws Exception {
Index index = tbl.getIndexes().get(2);
assertTrue(index instanceof GridH2TreeIndex);
assertTrue(index.canGetFirstOrLast());
//find first on empty data
Cursor cursor = index.findFirstOrLast(null, true);
assertFalse(cursor.next());
assertNull(cursor.get());
//find last on empty data
cursor = index.findFirstOrLast(null, false);
assertFalse(cursor.next());
assertNull(cursor.get());
//fill with data
int rows = 100;
long t = System.currentTimeMillis();
Random rnd = new Random();
UUID min = null;
UUID max = null;
for (int i = 0; i < rows; i++) {
UUID id = UUID.randomUUID();
if (min == null || id.compareTo(min) < 0)
min = id;
if (max == null || id.compareTo(max) > 0)
max = id;
GridH2Row row = row(id, t++, id.toString(), rnd.nextInt(100));
((GridH2TreeIndex) index).put(row);
}
//find first
cursor = index.findFirstOrLast(null, true);
assertTrue(cursor.next());
assertEquals(min, cursor.get().getValue(0).getObject());
assertFalse(cursor.next());
//find last
cursor = index.findFirstOrLast(null, false);
assertTrue(cursor.next());
assertEquals(max, cursor.get().getValue(0).getObject());
assertFalse(cursor.next());
}
Aggregations