Search in sources :

Example 31 with Cursor

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;
}
Also used : Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Example 32 with 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);
}
Also used : ResultInterface(org.h2.result.ResultInterface)

Example 33 with Cursor

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);
}
Also used : LocalResult(org.h2.result.LocalResult) SelectUnion(org.h2.command.dml.SelectUnion) ResultInterface(org.h2.result.ResultInterface) Query(org.h2.command.dml.Query) Value(org.h2.value.Value) Parser(org.h2.command.Parser)

Example 34 with Cursor

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);
}
Also used : SpatialKey(org.h2.mvstore.rtree.SpatialKey) Value(org.h2.value.Value) VersionedValue(org.h2.mvstore.db.TransactionStore.VersionedValue) Session(org.h2.engine.Session)

Example 35 with Cursor

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());
}
Also used : Random(java.util.Random) Index(org.h2.index.Index) H2PkHashIndex(org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex) GridCursor(org.apache.ignite.internal.util.lang.GridCursor) Cursor(org.h2.index.Cursor) UUID(java.util.UUID)

Aggregations

Cursor (org.h2.index.Cursor)24 Value (org.h2.value.Value)20 Index (org.h2.index.Index)11 Row (org.h2.result.Row)11 SearchRow (org.h2.result.SearchRow)11 ArrayList (java.util.ArrayList)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Constraint (org.h2.constraint.Constraint)5 SingleRowCursor (org.h2.index.SingleRowCursor)5 Column (org.h2.table.Column)5 Session (org.h2.engine.Session)4 MultiVersionIndex (org.h2.index.MultiVersionIndex)4 IndexColumn (org.h2.table.IndexColumn)4 H2PkHashIndex (org.apache.ignite.internal.processors.query.h2.database.H2PkHashIndex)3 GridH2Row (org.apache.ignite.internal.processors.query.h2.opt.GridH2Row)3 Database (org.h2.engine.Database)3 ValueLong (org.h2.value.ValueLong)3 PreparedStatement (java.sql.PreparedStatement)2 BitSet (java.util.BitSet)2 UUID (java.util.UUID)2