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);
}
}
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;
}
}
}
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();
}
}
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;
}
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);
}
Aggregations