org.h2.index

## Class BaseIndex

- java.lang.Object
- - org.h2.engine.DbObjectBase
  - - org.h2.schema.SchemaObjectBase
    - - org.h2.index.BaseIndex

All Implemented Interfaces:
  DbObject, Index, SchemaObject

Direct Known Subclasses:
  FunctionIndex, HashIndex, LinkedIndex, MetaIndex, MVDelegateIndex, MVPrimaryIndex, MVSecondaryIndex, MVSpatialIndex, NonUniqueHashIndex, PageIndex, RangeIndex, ScanIndex, SpatialTreeIndex, TreeIndex, ViewIndex

    public abstract class BaseIndex
    extends SchemaObjectBase
    implements Index

Most index implementations extend the base index.

### Field Summary

Fields
`protected int[]` `columnIds`
`protected Column[]` `columns`
`protected IndexColumn[]` `indexColumns`
`protected IndexType` `indexType`
`protected boolean` `isMultiVersion`
`protected Table` `table`

### Fields inherited from class org.h2.engine.DbObjectBase

`comment, database, trace`

### Fields inherited from interface org.h2.engine.DbObject

`AGGREGATE, COMMENT, CONSTANT, CONSTRAINT, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, TABLE_OR_VIEW, TRIGGER, USER, USER_DATATYPE`

### Constructor Summary

Constructors
Constructor and Description
`BaseIndex()`

### Method Summary

Methods
`boolean` `canFindNext()` Check if the index can get the next higher value.
`boolean` `canScan()` Can this index iterate over all rows?
`protected static void` `checkIndexColumnTypes(IndexColumn[] columns)` Check that the index columns are not CLOB or BLOB.
`void` `commit(int operation, Row row)` Commit the operation for a row.
`int` `compareRows(SearchRow rowData, SearchRow compare)` Compare two rows.
`protected boolean` `containsNullAndAllowMultipleNull(SearchRow newRow)` Check if one of the columns is NULL and multiple rows with NULL are allowed using the current compatibility mode for unique indexes.
`IndexLookupBatch` `createLookupBatch(TableFilter[] filters, int filter)` Creates new lookup batch.
`Cursor` `find(TableFilter filter, SearchRow first, SearchRow last)` Find a row or a list of rows and create a cursor to iterate over the result.
`Cursor` `findNext(Session session, SearchRow higherThan, SearchRow last)` Find a row or a list of rows that is larger and create a cursor to iterate over the result.
`int` `getColumnIndex(Column col)` Get the index of a column in the list of index columns
`Column[]` `getColumns()` Get the indexed columns.
`protected long` `getCostRangeIndex(int[] masks, long rowCount, TableFilter[] filters, int filter, SortOrder sortOrder, boolean isScanIndex, java.util.HashSet<Column> allColumnsSet)` Calculate the cost for the given mask as if this index was a typical b-tree range index.
`java.lang.String` `getCreateSQL()` Build a SQL statement to re-create this object.
`java.lang.String` `getCreateSQLForCopy(Table targetTable, java.lang.String quotedName)` Build a SQL statement to re-create the object, or to create a copy of the object with a different name or referencing a different table
`java.lang.String` `getDropSQL()` Build a SQL statement to drop this object.
`protected DbException` `getDuplicateKeyException(java.lang.String key)` Create a duplicate key exception with a message that contains the index name.
`IndexColumn[]` `getIndexColumns()` Get the indexed columns as index columns (with ordering information).
`IndexType` `getIndexType()` Get the index type.
`java.lang.String` `getPlanSQL()` Get the message to show in a EXPLAIN statement.
`Row` `getRow(Session session, long key)` Get the row with the given key.
`Table` `getTable()` Get the table on which this index is based.
`int` `getType()` Get the object type.
`protected void` `initBaseIndex(Table newTable, int id, java.lang.String name, IndexColumn[] newIndexColumns, IndexType newIndexType)` Initialize the base index.
`boolean` `isFirstColumn(Column column)` Check if the given column is the first for this index
`boolean` `isHidden()` Check whether this is a hidden object that doesn't appear in the meta data and in the script, and is not dropped on DROP ALL OBJECTS.
`boolean` `isRowIdIndex()` Does this index support lookup by row id?
`void` `removeChildrenAndResources(Session session)` Remove all dependent objects and free all resources (files, blocks in files) of this object.
`void` `setSortedInsertMode(boolean sortedInsertMode)` Enable or disable the 'sorted insert' optimizations (rows are inserted in ascending or descending order) if applicable for this index implementation.

### Methods inherited from class org.h2.schema.SchemaObjectBase

`getSchema, getSQL, initSchemaObjectBase`

### Methods inherited from class org.h2.engine.DbObjectBase

`checkRename, getChildren, getComment, getDatabase, getId, getModificationId, getName, initDbObjectBase, invalidate, isTemporary, isValid, rename, setComment, setModified, setObjectName, setTemporary, toString`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.h2.index.Index

`add, canGetFirstOrLast, close, find, findFirstOrLast, getCost, getDiskSpaceUsed, getRowCount, getRowCountApproximation, needRebuild, remove, remove, truncate`

### Methods inherited from interface org.h2.schema.SchemaObject

`getSchema`

### Methods inherited from interface org.h2.engine.DbObject

`checkRename, getChildren, getComment, getDatabase, getId, getName, getSQL, isTemporary, rename, setComment, setTemporary`

### Field Detail

#### indexColumns

    protected IndexColumn[] indexColumns

#### columns

    protected Column[] columns

#### columnIds

    protected int[] columnIds

#### table

    protected Table table

#### indexType

    protected IndexType indexType

#### isMultiVersion

    protected boolean isMultiVersion

### Constructor Detail

#### BaseIndex

    public BaseIndex()

### Method Detail

#### initBaseIndex

    protected void initBaseIndex(Table newTable,
                     int id,
                     java.lang.String name,
                     IndexColumn[] newIndexColumns,
                     IndexType newIndexType)

Initialize the base index.

Parameters:
  `newTable` - the table
  `id` - the object id
  `name` - the index name
  `newIndexColumns` - the columns that are indexed or null if this is not yet known
  `newIndexType` - the index type

#### checkIndexColumnTypes

    protected static void checkIndexColumnTypes(IndexColumn[] columns)

Check that the index columns are not CLOB or BLOB.

Parameters:
  `columns` - the columns

#### getDropSQL

    public java.lang.String getDropSQL()

Description copied from class: `DbObjectBase`
Build a SQL statement to drop this object.

Specified by:
  `getDropSQL` in interface `DbObject`
Specified by:
  `getDropSQL` in class `DbObjectBase`
Returns:
  the SQL statement

#### getDuplicateKeyException

    protected DbException getDuplicateKeyException(java.lang.String key)

Create a duplicate key exception with a message that contains the index name.

Parameters:
  `key` - the key values
Returns:
  the exception

#### getPlanSQL

    public java.lang.String getPlanSQL()

Description copied from interface: `Index`
Get the message to show in a EXPLAIN statement.

Specified by:
  `getPlanSQL` in interface `Index`
Returns:
  the plan

#### removeChildrenAndResources

    public void removeChildrenAndResources(Session session)

Description copied from class: `DbObjectBase`
Remove all dependent objects and free all resources (files, blocks in files) of this object.

Specified by:
  `removeChildrenAndResources` in interface `DbObject`
Specified by:
  `removeChildrenAndResources` in class `DbObjectBase`
Parameters:
  `session` - the session

#### canFindNext

    public boolean canFindNext()

Description copied from interface: `Index`
Check if the index can get the next higher value.

Specified by:
  `canFindNext` in interface `Index`
Returns:
  true if it can

#### find

    public Cursor find(TableFilter filter,
              SearchRow first,
              SearchRow last)

Description copied from interface: `Index`
Find a row or a list of rows and create a cursor to iterate over the result.

Specified by:
  `find` in interface `Index`
Parameters:
  `filter` - the table filter (which possibly knows about additional conditions)
  `first` - the first row, or null for no limit
  `last` - the last row, or null for no limit
Returns:
  the cursor to iterate over the results

#### findNext

    public Cursor findNext(Session session,
                  SearchRow higherThan,
                  SearchRow last)

Find a row or a list of rows that is larger and create a cursor to iterate over the result. The base implementation doesn't support this feature.

Specified by:
  `findNext` in interface `Index`
Parameters:
  `session` - the session
  `higherThan` - the lower limit (excluding)
  `last` - the last row, or null for no limit
Returns:
  the cursor
Throws:
  `DbException` - always

#### getCostRangeIndex

    protected final long getCostRangeIndex(int[] masks,
                         long rowCount,
                         TableFilter[] filters,
                         int filter,
                         SortOrder sortOrder,
                         boolean isScanIndex,
                         java.util.HashSet<Column> allColumnsSet)

Calculate the cost for the given mask as if this index was a typical b-tree range index. This is the estimated cost required to search one row, and then iterate over the given number of rows.

Parameters:
  `masks` - the IndexCondition search masks, one for each column in the table
  `rowCount` - the number of rows in the index
  `filters` - all joined table filters
  `filter` - the current table filter index
  `sortOrder` - the sort order
  `isScanIndex` - whether this is a "table scan" index
  `allColumnsSet` - the set of all columns
Returns:
  the estimated cost

#### compareRows

    public int compareRows(SearchRow rowData,
                  SearchRow compare)

Description copied from interface: `Index`
Compare two rows.

Specified by:
  `compareRows` in interface `Index`
Parameters:
  `rowData` - the first row
  `compare` - the second row
Returns:
  0 if both rows are equal, -1 if the first row is smaller, otherwise 1

#### containsNullAndAllowMultipleNull

    protected boolean containsNullAndAllowMultipleNull(SearchRow newRow)

Check if one of the columns is NULL and multiple rows with NULL are allowed using the current compatibility mode for unique indexes. Note: NULL behavior is complicated in SQL.

Parameters:
  `newRow` - the row to check
Returns:
  true if one of the columns is null and multiple nulls in unique indexes are allowed

#### getColumnIndex

    public int getColumnIndex(Column col)

Description copied from interface: `Index`
Get the index of a column in the list of index columns

Specified by:
  `getColumnIndex` in interface `Index`
Parameters:
  `col` - the column
Returns:
  the index (0 meaning first column)

#### isFirstColumn

    public boolean isFirstColumn(Column column)

Description copied from interface: `Index`
Check if the given column is the first for this index

Specified by:
  `isFirstColumn` in interface `Index`
Parameters:
  `column` - the column
Returns:
  true if the given columns is the first

#### getCreateSQLForCopy

    public java.lang.String getCreateSQLForCopy(Table targetTable,
                                       java.lang.String quotedName)

Description copied from interface: `DbObject`
Build a SQL statement to re-create the object, or to create a copy of the object with a different name or referencing a different table

Specified by:
  `getCreateSQLForCopy` in interface `DbObject`
Parameters:
  `targetTable` - the new table
  `quotedName` - the quoted name
Returns:
  the SQL statement

#### getCreateSQL

    public java.lang.String getCreateSQL()

Description copied from class: `DbObjectBase`
Build a SQL statement to re-create this object.

Specified by:
  `getCreateSQL` in interface `DbObject`
Specified by:
  `getCreateSQL` in class `DbObjectBase`
Returns:
  the SQL statement

#### getIndexColumns

    public IndexColumn[] getIndexColumns()

Description copied from interface: `Index`
Get the indexed columns as index columns (with ordering information).

Specified by:
  `getIndexColumns` in interface `Index`
Returns:
  the index columns

#### getColumns

    public Column[] getColumns()

Description copied from interface: `Index`
Get the indexed columns.

Specified by:
  `getColumns` in interface `Index`
Returns:
  the columns

#### getIndexType

    public IndexType getIndexType()

Description copied from interface: `Index`
Get the index type.

Specified by:
  `getIndexType` in interface `Index`
Returns:
  the index type

#### getType

    public int getType()

Description copied from interface: `DbObject`
Get the object type.

Specified by:
  `getType` in interface `DbObject`
Returns:
  the object type

#### getTable

    public Table getTable()

Description copied from interface: `Index`
Get the table on which this index is based.

Specified by:
  `getTable` in interface `Index`
Returns:
  the table

#### commit

    public void commit(int operation,
              Row row)

Description copied from interface: `Index`
Commit the operation for a row. This is only important for multi-version indexes. The method is only called if multi-version is enabled.

Specified by:
  `commit` in interface `Index`
Parameters:
  `operation` - the operation type
  `row` - the row

#### getRow

    public Row getRow(Session session,
             long key)

Description copied from interface: `Index`
Get the row with the given key.

Specified by:
  `getRow` in interface `Index`
Parameters:
  `session` - the session
  `key` - the unique key
Returns:
  the row

#### isHidden

    public boolean isHidden()

Description copied from interface: `SchemaObject`
Check whether this is a hidden object that doesn't appear in the meta data and in the script, and is not dropped on DROP ALL OBJECTS.

Specified by:
  `isHidden` in interface `SchemaObject`
Overrides:
  `isHidden` in class `SchemaObjectBase`
Returns:
  true if it is hidden

#### isRowIdIndex

    public boolean isRowIdIndex()

Description copied from interface: `Index`
Does this index support lookup by row id?

Specified by:
  `isRowIdIndex` in interface `Index`
Returns:
  true if it does

#### canScan

    public boolean canScan()

Description copied from interface: `Index`
Can this index iterate over all rows?

Specified by:
  `canScan` in interface `Index`
Returns:
  true if it can

#### setSortedInsertMode

    public void setSortedInsertMode(boolean sortedInsertMode)

Description copied from interface: `Index`
Enable or disable the 'sorted insert' optimizations (rows are inserted in ascending or descending order) if applicable for this index implementation.

Specified by:
  `setSortedInsertMode` in interface `Index`
Parameters:
  `sortedInsertMode` - the new value

#### createLookupBatch

    public IndexLookupBatch createLookupBatch(TableFilter[] filters,
                                     int filter)

Description copied from interface: `Index`
Creates new lookup batch. Note that returned `IndexLookupBatch` instance can be used multiple times.

Specified by:
  `createLookupBatch` in interface `Index`
Parameters:
  `filters` - the table filters
  `filter` - the filter index (0, 1,...)
Returns:
  created batch or `null` if batched lookup is not supported by this index.