Search in sources :

Example 6 with Cursor

use of org.h2.mvstore.Cursor in project ignite by apache.

the class H2TreeIndex method findFirstOrLast.

/**
 * {@inheritDoc}
 */
@Override
public Cursor findFirstOrLast(Session session, boolean b) {
    try {
        int seg = threadLocalSegment();
        H2Tree tree = treeForRead(seg);
        GridH2Row row = b ? tree.findFirst() : tree.findLast();
        return new SingleRowCursor(row);
    } catch (IgniteCheckedException e) {
        throw DbException.convert(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridH2Row(org.apache.ignite.internal.processors.query.h2.opt.GridH2Row) SingleRowCursor(org.h2.index.SingleRowCursor)

Example 7 with Cursor

use of org.h2.mvstore.Cursor in project h2database by h2database.

the class Select method queryDistinct.

private void queryDistinct(ResultTarget result, long limitRows) {
    // limitRows is never 0 here
    if (limitRows > 0 && offsetExpr != null) {
        int offset = offsetExpr.getValue(session).getInt();
        if (offset > 0) {
            limitRows += offset;
        }
    }
    int rowNumber = 0;
    setCurrentRowNumber(0);
    Index index = topTableFilter.getIndex();
    SearchRow first = null;
    int columnIndex = index.getColumns()[0].getColumnId();
    int sampleSize = getSampleSizeValue(session);
    while (true) {
        setCurrentRowNumber(rowNumber + 1);
        Cursor cursor = index.findNext(session, first, null);
        if (!cursor.next()) {
            break;
        }
        SearchRow found = cursor.getSearchRow();
        Value value = found.getValue(columnIndex);
        if (first == null) {
            first = topTableFilter.getTable().getTemplateSimpleRow(true);
        }
        first.setValue(columnIndex, value);
        Value[] row = { value };
        result.addRow(row);
        rowNumber++;
        if ((sort == null || sortUsingIndex) && limitRows > 0 && rowNumber >= limitRows) {
            break;
        }
        if (sampleSize > 0 && rowNumber >= sampleSize) {
            break;
        }
    }
}
Also used : Value(org.h2.value.Value) Index(org.h2.index.Index) Cursor(org.h2.index.Cursor)

Example 8 with Cursor

use of org.h2.mvstore.Cursor in project h2database by h2database.

the class Database method checkMetaFree.

private void checkMetaFree(Session session, int id) {
    SearchRow r = meta.getTemplateSimpleRow(false);
    r.setValue(0, ValueInt.get(id));
    Cursor cursor = metaIdIndex.find(session, r, r);
    if (cursor.next()) {
        DbException.throwInternalError();
    }
}
Also used : Cursor(org.h2.index.Cursor) SearchRow(org.h2.result.SearchRow)

Example 9 with Cursor

use of org.h2.mvstore.Cursor in project h2database by h2database.

the class AggregateDataMedian method getResultFromIndex.

/**
 * Get the result from the index.
 *
 * @param session the session
 * @param on the expression
 * @param dataType the data type
 * @return the result
 */
static Value getResultFromIndex(Session session, Expression on, int dataType) {
    Index index = getMedianColumnIndex(on);
    long count = index.getRowCount(session);
    if (count == 0) {
        return ValueNull.INSTANCE;
    }
    Cursor cursor = index.find(session, null, null);
    cursor.next();
    int columnId = index.getColumns()[0].getColumnId();
    ExpressionColumn expr = (ExpressionColumn) on;
    if (expr.getColumn().isNullable()) {
        boolean hasNulls = false;
        SearchRow row;
        // will be used to read values.
        while (count > 0) {
            row = cursor.getSearchRow();
            if (row == null) {
                return ValueNull.INSTANCE;
            }
            if (row.getValue(columnId) == ValueNull.INSTANCE) {
                count--;
                cursor.next();
                hasNulls = true;
            } else {
                break;
            }
        }
        if (count == 0) {
            return ValueNull.INSTANCE;
        }
        // cursor to count nulls at the end.
        if (!hasNulls && isNullsLast(index)) {
            TableFilter tableFilter = expr.getTableFilter();
            SearchRow check = tableFilter.getTable().getTemplateSimpleRow(true);
            check.setValue(columnId, ValueNull.INSTANCE);
            Cursor nullsCursor = index.find(session, check, check);
            while (nullsCursor.next()) {
                count--;
            }
            if (count <= 0) {
                return ValueNull.INSTANCE;
            }
        }
    }
    long skip = (count - 1) / 2;
    for (int i = 0; i < skip; i++) {
        cursor.next();
    }
    SearchRow row = cursor.getSearchRow();
    if (row == null) {
        return ValueNull.INSTANCE;
    }
    Value v = row.getValue(columnId);
    if (v == ValueNull.INSTANCE) {
        return v;
    }
    if ((count & 1) == 0) {
        cursor.next();
        row = cursor.getSearchRow();
        if (row == null) {
            return v;
        }
        Value v2 = row.getValue(columnId);
        if (v2 == ValueNull.INSTANCE) {
            return v;
        }
        return getMedian(v, v2, dataType, session.getDatabase().getCompareMode());
    }
    return v;
}
Also used : TableFilter(org.h2.table.TableFilter) Value(org.h2.value.Value) Index(org.h2.index.Index) Cursor(org.h2.index.Cursor) SearchRow(org.h2.result.SearchRow)

Example 10 with Cursor

use of org.h2.mvstore.Cursor in project h2database by h2database.

the class HashIndex method find.

@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
    if (first == null || last == null) {
        // TODO hash index: should additionally check if values are the same
        throw DbException.throwInternalError(first + " " + last);
    }
    Value v = first.getValue(indexColumn);
    /*
         * Sometimes the incoming search is a similar, but not the same type
         * e.g. the search value is INT, but the index column is LONG. In which
         * case we need to convert, otherwise the ValueHashMap will not find the
         * result.
         */
    v = v.convertTo(tableData.getColumn(indexColumn).getType());
    Row result;
    Long pos = rows.get(v);
    if (pos == null) {
        result = null;
    } else {
        result = tableData.getRow(session, pos.intValue());
    }
    return new SingleRowCursor(result);
}
Also used : Value(org.h2.value.Value) Row(org.h2.result.Row) SearchRow(org.h2.result.SearchRow)

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