Search in sources :

Example 51 with Index

use of org.h2.index.Index in project h2database by h2database.

the class IndexCursor method prepare.

/**
 * Prepare this index cursor to make a lookup in index.
 *
 * @param s Session.
 * @param indexConditions Index conditions.
 */
public void prepare(Session s, ArrayList<IndexCondition> indexConditions) {
    this.session = s;
    alwaysFalse = false;
    start = end = null;
    inList = null;
    inColumn = null;
    inResult = null;
    inResultTested = null;
    intersects = null;
    // don't use enhanced for loop to avoid creating objects
    for (IndexCondition condition : indexConditions) {
        if (condition.isAlwaysFalse()) {
            alwaysFalse = true;
            break;
        }
        // lookups, each such lookup will perform an own table scan.
        if (index.isFindUsingFullTableScan()) {
            continue;
        }
        Column column = condition.getColumn();
        if (condition.getCompareType() == Comparison.IN_LIST) {
            if (start == null && end == null) {
                if (canUseIndexForIn(column)) {
                    this.inColumn = column;
                    inList = condition.getCurrentValueList(s);
                    inListIndex = 0;
                }
            }
        } else if (condition.getCompareType() == Comparison.IN_QUERY) {
            if (start == null && end == null) {
                if (canUseIndexForIn(column)) {
                    this.inColumn = column;
                    inResult = condition.getCurrentResult();
                }
            }
        } else {
            Value v = condition.getCurrentValue(s);
            boolean isStart = condition.isStart();
            boolean isEnd = condition.isEnd();
            boolean isIntersects = condition.isSpatialIntersects();
            int columnId = column.getColumnId();
            if (columnId >= 0) {
                IndexColumn idxCol = indexColumns[columnId];
                if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) {
                    // if the index column is sorted the other way, we swap
                    // end and start NULLS_FIRST / NULLS_LAST is not a
                    // problem, as nulls never match anyway
                    boolean temp = isStart;
                    isStart = isEnd;
                    isEnd = temp;
                }
            }
            if (isStart) {
                start = getSearchRow(start, columnId, v, true);
            }
            if (isEnd) {
                end = getSearchRow(end, columnId, v, false);
            }
            if (isIntersects) {
                intersects = getSpatialSearchRow(intersects, columnId, v);
            }
            // an X IN(..) condition, unless the X IN condition can use the index.
            if ((isStart || isEnd) && !canUseIndexFor(inColumn)) {
                inColumn = null;
                inList = null;
                inResult = null;
            }
            if (!session.getDatabase().getSettings().optimizeIsNull) {
                if (isStart && isEnd) {
                    if (v == ValueNull.INSTANCE) {
                        // join on a column=NULL is always false
                        alwaysFalse = true;
                    }
                }
            }
        }
    }
    if (inColumn != null) {
        start = table.getTemplateRow();
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Value(org.h2.value.Value) IndexColumn(org.h2.table.IndexColumn)

Example 52 with Index

use of org.h2.index.Index in project h2database by h2database.

the class PageBtree method find.

/**
 * Find an entry.
 *
 * @param compare the row
 * @param bigger if looking for a larger row
 * @param add if the row should be added (check for duplicate keys)
 * @param compareKeys compare the row keys as well
 * @return the index of the found row
 */
int find(SearchRow compare, boolean bigger, boolean add, boolean compareKeys) {
    if (compare == null) {
        return 0;
    }
    int l = 0, r = entryCount;
    int comp = 1;
    while (l < r) {
        int i = (l + r) >>> 1;
        SearchRow row = getRow(i);
        comp = index.compareRows(row, compare);
        if (comp == 0) {
            if (add && index.indexType.isUnique()) {
                if (!index.mayHaveNullDuplicates(compare)) {
                    throw index.getDuplicateKeyException(compare.toString());
                }
            }
            if (compareKeys) {
                comp = index.compareKeys(row, compare);
                if (comp == 0) {
                    return i;
                }
            }
        }
        if (comp > 0 || (!bigger && comp == 0)) {
            r = i;
        } else {
            l = i + 1;
        }
    }
    return l;
}
Also used : SearchRow(org.h2.result.SearchRow)

Example 53 with Index

use of org.h2.index.Index in project h2database by h2database.

the class PageBtreeLeaf method moveTo.

@Override
public void moveTo(Session session, int newPos) {
    PageStore store = index.getPageStore();
    readAllRows();
    PageBtreeLeaf p2 = PageBtreeLeaf.create(index, newPos, parentPageId);
    store.logUndo(this, data);
    store.logUndo(p2, null);
    p2.rows = rows;
    p2.entryCount = entryCount;
    p2.offsets = offsets;
    p2.onlyPosition = onlyPosition;
    p2.parentPageId = parentPageId;
    p2.start = start;
    store.update(p2);
    if (parentPageId == ROOT) {
        index.setRootPageId(session, newPos);
    } else {
        PageBtreeNode p = (PageBtreeNode) store.getPage(parentPageId);
        p.moveChild(getPos(), newPos);
    }
    store.free(getPos());
}
Also used : PageStore(org.h2.store.PageStore)

Example 54 with Index

use of org.h2.index.Index in project h2database by h2database.

the class PageBtreeNode method moveTo.

@Override
public void moveTo(Session session, int newPos) {
    PageStore store = index.getPageStore();
    store.logUndo(this, data);
    PageBtreeNode p2 = PageBtreeNode.create(index, newPos, parentPageId);
    readAllRows();
    p2.rowCountStored = rowCountStored;
    p2.rowCount = rowCount;
    p2.childPageIds = childPageIds;
    p2.rows = rows;
    p2.entryCount = entryCount;
    p2.offsets = offsets;
    p2.onlyPosition = onlyPosition;
    p2.parentPageId = parentPageId;
    p2.start = start;
    store.update(p2);
    if (parentPageId == ROOT) {
        index.setRootPageId(session, newPos);
    } else {
        Page p = store.getPage(parentPageId);
        if (!(p instanceof PageBtreeNode)) {
            throw DbException.throwInternalError();
        }
        PageBtreeNode n = (PageBtreeNode) p;
        n.moveChild(getPos(), newPos);
    }
    for (int i = 0; i < entryCount + 1; i++) {
        int child = childPageIds[i];
        PageBtree p = index.getPage(child);
        p.setParentPageId(newPos);
        store.update(p);
    }
    store.free(getPos());
}
Also used : PageStore(org.h2.store.PageStore) Page(org.h2.store.Page)

Example 55 with Index

use of org.h2.index.Index in project h2database by h2database.

the class Aggregate method getValue.

@Override
public Value getValue(Session session) {
    if (select.isQuickAggregateQuery()) {
        switch(type) {
            case COUNT:
            case COUNT_ALL:
                Table table = select.getTopTableFilter().getTable();
                return ValueLong.get(table.getRowCount(session));
            case MIN:
            case MAX:
                {
                    boolean first = type == AggregateType.MIN;
                    Index index = getMinMaxColumnIndex();
                    int sortType = index.getIndexColumns()[0].sortType;
                    if ((sortType & SortOrder.DESCENDING) != 0) {
                        first = !first;
                    }
                    Cursor cursor = index.findFirstOrLast(session, first);
                    SearchRow row = cursor.getSearchRow();
                    Value v;
                    if (row == null) {
                        v = ValueNull.INSTANCE;
                    } else {
                        v = row.getValue(index.getColumns()[0].getColumnId());
                    }
                    return v;
                }
            case MEDIAN:
                {
                    return AggregateDataMedian.getResultFromIndex(session, on, dataType);
                }
            default:
                DbException.throwInternalError("type=" + type);
        }
    }
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
    }
    AggregateData data = (AggregateData) group.get(this);
    if (data == null) {
        data = AggregateData.create(type);
    }
    if (type == AggregateType.GROUP_CONCAT) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        StatementBuilder buff = new StatementBuilder();
        String sep = groupConcatSeparator == null ? "," : groupConcatSeparator.getValue(session).getString();
        for (Value val : array) {
            String s;
            if (val.getType() == Value.ARRAY) {
                s = ((ValueArray) val).getList()[0].getString();
            } else {
                s = val.getString();
            }
            if (s == null) {
                continue;
            }
            if (sep != null) {
                buff.appendExceptFirst(sep);
            }
            buff.append(s);
        }
        return ValueString.get(buff.toString());
    } else if (type == AggregateType.ARRAY_AGG) {
        Value[] array = ((AggregateDataCollecting) data).getArray();
        if (array == null) {
            return ValueNull.INSTANCE;
        }
        if (orderByList != null || distinct) {
            sortWithOrderBy(array);
        }
        if (orderByList != null) {
            for (int i = 0; i < array.length; i++) {
                array[i] = ((ValueArray) array[i]).getList()[0];
            }
        }
        return ValueArray.get(array);
    }
    return data.getValue(session.getDatabase(), dataType, distinct);
}
Also used : Table(org.h2.table.Table) Index(org.h2.index.Index) ValueString(org.h2.value.ValueString) Cursor(org.h2.index.Cursor) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) SearchRow(org.h2.result.SearchRow)

Aggregations

ResultSet (java.sql.ResultSet)77 Index (org.h2.index.Index)75 Statement (java.sql.Statement)64 SimpleResultSet (org.h2.tools.SimpleResultSet)61 SQLException (java.sql.SQLException)60 Value (org.h2.value.Value)58 DbException (org.h2.message.DbException)57 Connection (java.sql.Connection)56 PreparedStatement (java.sql.PreparedStatement)56 Column (org.h2.table.Column)53 IndexColumn (org.h2.table.IndexColumn)45 ArrayList (java.util.ArrayList)31 ValueString (org.h2.value.ValueString)29 Constraint (org.h2.constraint.Constraint)25 Row (org.h2.result.Row)22 MultiVersionIndex (org.h2.index.MultiVersionIndex)19 JdbcConnection (org.h2.jdbc.JdbcConnection)19 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 Table (org.h2.table.Table)18 HashSet (java.util.HashSet)17