Search in sources :

Example 6 with IndexOperation

use of herddb.index.IndexOperation in project herddb by diennea.

the class CalcitePlanner method findSecondaryIndexOperation.

private static IndexOperation findSecondaryIndexOperation(AbstractIndexManager index, CompiledSQLExpression where, Table table) throws StatementExecutionException {
    IndexOperation secondaryIndexOperation = null;
    String[] columnsToMatch = index.getColumnNames();
    SQLRecordKeyFunction indexSeekFunction = findIndexAccess(where, columnsToMatch, index.getIndex(), "=", table);
    if (indexSeekFunction != null) {
        if (indexSeekFunction.isFullPrimaryKey()) {
            secondaryIndexOperation = new SecondaryIndexSeek(index.getIndexName(), columnsToMatch, indexSeekFunction);
        } else {
            secondaryIndexOperation = new SecondaryIndexPrefixScan(index.getIndexName(), columnsToMatch, indexSeekFunction);
        }
    } else {
        SQLRecordKeyFunction rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">=", table);
        if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
            rangeMin = null;
        }
        if (rangeMin == null) {
            rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">", table);
            if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
                rangeMin = null;
            }
        }
        SQLRecordKeyFunction rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<=", table);
        if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
            rangeMax = null;
        }
        if (rangeMax == null) {
            rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<", table);
            if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
                rangeMax = null;
            }
        }
        if (rangeMin != null || rangeMax != null) {
            secondaryIndexOperation = new SecondaryIndexRangeScan(index.getIndexName(), columnsToMatch, rangeMin, rangeMax);
        }
    }
    return secondaryIndexOperation;
}
Also used : IndexOperation(herddb.index.IndexOperation) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan)

Example 7 with IndexOperation

use of herddb.index.IndexOperation in project herddb by diennea.

the class SQLPlanner method findSecondaryIndexOperation.

private static IndexOperation findSecondaryIndexOperation(AbstractIndexManager index, Expression where, Table table) throws StatementExecutionException {
    IndexOperation secondaryIndexOperation = null;
    String[] columnsToMatch = index.getColumnNames();
    SQLRecordKeyFunction indexSeekFunction = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, EqualsTo.class);
    if (indexSeekFunction != null) {
        if (indexSeekFunction.isFullPrimaryKey()) {
            secondaryIndexOperation = new SecondaryIndexSeek(index.getIndexName(), columnsToMatch, indexSeekFunction);
        } else {
            secondaryIndexOperation = new SecondaryIndexPrefixScan(index.getIndexName(), columnsToMatch, indexSeekFunction);
        }
    } else {
        SQLRecordKeyFunction rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, GreaterThanEquals.class);
        if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
            rangeMin = null;
        }
        if (rangeMin == null) {
            rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, GreaterThan.class);
            if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
                rangeMin = null;
            }
        }
        SQLRecordKeyFunction rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, MinorThanEquals.class);
        if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
            rangeMax = null;
        }
        if (rangeMax == null) {
            rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), table.name, MinorThan.class);
            if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
                rangeMax = null;
            }
        }
        if (rangeMin != null || rangeMax != null) {
            secondaryIndexOperation = new SecondaryIndexRangeScan(index.getIndexName(), columnsToMatch, rangeMin, rangeMax);
        }
    }
    return secondaryIndexOperation;
}
Also used : MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) IndexOperation(herddb.index.IndexOperation) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan)

Example 8 with IndexOperation

use of herddb.index.IndexOperation in project herddb by diennea.

the class IndexUtils method scanForIndexAccess.

private static IndexOperation scanForIndexAccess(CompiledSQLExpression expressionWhere, Table table, TableSpaceManager tableSpaceManager) {
    SQLRecordKeyFunction keyFunction = findIndexAccess(expressionWhere, table.primaryKey, table, "=", table);
    IndexOperation result = null;
    if (keyFunction != null) {
        if (keyFunction.isFullPrimaryKey()) {
            result = new PrimaryIndexSeek(keyFunction);
        } else {
            result = new PrimaryIndexPrefixScan(keyFunction);
        }
    } else {
        SQLRecordKeyFunction rangeMin = findIndexAccess(expressionWhere, table.primaryKey, table, ">=", table);
        if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
            rangeMin = null;
        }
        if (rangeMin == null) {
            rangeMin = findIndexAccess(expressionWhere, table.primaryKey, table, ">", table);
            if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
                rangeMin = null;
            }
        }
        SQLRecordKeyFunction rangeMax = findIndexAccess(expressionWhere, table.primaryKey, table, "<=", table);
        if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
            rangeMax = null;
        }
        if (rangeMax == null) {
            rangeMax = findIndexAccess(expressionWhere, table.primaryKey, table, "<", table);
            if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
                rangeMax = null;
            }
        }
        if (rangeMin != null || rangeMax != null) {
            result = new PrimaryIndexRangeScan(table.primaryKey, rangeMin, rangeMax);
        }
    }
    if (result == null && tableSpaceManager != null) {
        Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(table.name);
        if (indexes != null) {
            // TODO: use some kind of statistics, maybe using an index is more expensive than a full table scan
            for (AbstractIndexManager index : indexes.values()) {
                if (!index.isAvailable()) {
                    continue;
                }
                IndexOperation secondaryIndexOperation = findSecondaryIndexOperation(index, expressionWhere, table);
                if (secondaryIndexOperation != null) {
                    result = secondaryIndexOperation;
                    break;
                }
            }
        }
    }
    return result;
}
Also used : IndexOperation(herddb.index.IndexOperation) PrimaryIndexSeek(herddb.index.PrimaryIndexSeek) AbstractIndexManager(herddb.core.AbstractIndexManager) PrimaryIndexPrefixScan(herddb.index.PrimaryIndexPrefixScan) PrimaryIndexRangeScan(herddb.index.PrimaryIndexRangeScan)

Example 9 with IndexOperation

use of herddb.index.IndexOperation in project herddb by diennea.

the class IndexUtils method discoverIndexOperations.

static void discoverIndexOperations(final String tableSpace, CompiledSQLExpression where, Table table, SQLRecordPredicate predicate, Object debug, TableSpaceManager tableSpaceManager) throws StatementExecutionException {
    IndexOperation op = scanForIndexAccess(where, table, tableSpaceManager);
    predicate.setIndexOperation(op);
    CompiledSQLExpression filterPk = findFiltersOnPrimaryKey(table, where);
    if (filterPk != null) {
        filterPk = remapPositionalAccessToToPrimaryKeyAccessor(filterPk, table, debug);
    }
    predicate.setPrimaryKeyFilter(filterPk);
}
Also used : IndexOperation(herddb.index.IndexOperation) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression)

Example 10 with IndexOperation

use of herddb.index.IndexOperation in project herddb by diennea.

the class IndexUtils method findSecondaryIndexOperation.

private static IndexOperation findSecondaryIndexOperation(AbstractIndexManager index, CompiledSQLExpression where, Table table) throws StatementExecutionException {
    IndexOperation secondaryIndexOperation = null;
    String[] columnsToMatch = index.getColumnNames();
    SQLRecordKeyFunction indexSeekFunction = findIndexAccess(where, columnsToMatch, index.getIndex(), "=", table);
    if (indexSeekFunction != null) {
        if (indexSeekFunction.isFullPrimaryKey()) {
            secondaryIndexOperation = new SecondaryIndexSeek(index.getIndexName(), columnsToMatch, indexSeekFunction);
        } else {
            secondaryIndexOperation = new SecondaryIndexPrefixScan(index.getIndexName(), columnsToMatch, indexSeekFunction);
        }
    } else {
        SQLRecordKeyFunction rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">=", table);
        if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
            rangeMin = null;
        }
        if (rangeMin == null) {
            rangeMin = findIndexAccess(where, columnsToMatch, index.getIndex(), ">", table);
            if (rangeMin != null && !rangeMin.isFullPrimaryKey()) {
                rangeMin = null;
            }
        }
        SQLRecordKeyFunction rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<=", table);
        if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
            rangeMax = null;
        }
        if (rangeMax == null) {
            rangeMax = findIndexAccess(where, columnsToMatch, index.getIndex(), "<", table);
            if (rangeMax != null && !rangeMax.isFullPrimaryKey()) {
                rangeMax = null;
            }
        }
        if (rangeMin != null || rangeMax != null) {
            secondaryIndexOperation = new SecondaryIndexRangeScan(index.getIndexName(), columnsToMatch, rangeMin, rangeMax);
        }
    }
    return secondaryIndexOperation;
}
Also used : IndexOperation(herddb.index.IndexOperation) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) SecondaryIndexRangeScan(herddb.index.SecondaryIndexRangeScan) SecondaryIndexPrefixScan(herddb.index.SecondaryIndexPrefixScan)

Aggregations

IndexOperation (herddb.index.IndexOperation)10 PrimaryIndexSeek (herddb.index.PrimaryIndexSeek)5 AbstractIndexManager (herddb.core.AbstractIndexManager)3 PrimaryIndexPrefixScan (herddb.index.PrimaryIndexPrefixScan)3 PrimaryIndexRangeScan (herddb.index.PrimaryIndexRangeScan)3 SecondaryIndexPrefixScan (herddb.index.SecondaryIndexPrefixScan)3 SecondaryIndexRangeScan (herddb.index.SecondaryIndexRangeScan)3 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)3 LogEntry (herddb.log.LogEntry)2 DataScanner (herddb.model.DataScanner)2 Predicate (herddb.model.Predicate)2 Record (herddb.model.Record)2 StatementExecutionException (herddb.model.StatementExecutionException)2 ScanStatement (herddb.model.commands.ScanStatement)2 DataStorageManagerException (herddb.storage.DataStorageManagerException)2 FullTableScanConsumer (herddb.storage.FullTableScanConsumer)2 BatchOrderedExecutor (herddb.utils.BatchOrderedExecutor)2 Bytes (herddb.utils.Bytes)2 DataAccessor (herddb.utils.DataAccessor)2 LockHandle (herddb.utils.LockHandle)2