Package org.h2.index

# Class Index

java.lang.Object
org.h2.engine.DbObject
org.h2.schema.SchemaObject
org.h2.index.Index

All Implemented Interfaces:
  `HasSQL`

Direct Known Subclasses:
  `LinkedIndex`, `MetaIndex`, `MVIndex`, `QueryExpressionIndex`, `VirtualTableIndex`

public abstract class Index extends SchemaObject
An index. Indexes are used to speed up searching data.

## Field Summary

Fields
Modifier and Type
Field
Description
`protected int[]`
`columnIds`
Identities of table columns.
`protected Column[]`
`columns`
Table columns used in this index.
`protected IndexColumn[]`
`indexColumns`
Columns of this index.
`protected final IndexType`
`indexType`
The index type.
`protected final Table`
`table`
The table.
`protected final int`
`uniqueColumnColumn`
Count of unique columns.

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

`AGGREGATE, comment, COMMENT, CONSTANT, CONSTRAINT, database, DOMAIN, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, SYNONYM, TABLE_OR_VIEW, trace, TRIGGER, USER`

### Fields inherited from interface org.h2.util.HasSQL

`ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS`

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`Index(Table newTable, int id, String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType)`
Initialize the index.

## Method Summary

Modifier and Type
Method
Description
`abstract void`
`add(SessionLocal session, Row row)`
Add a row to the index.
`boolean`
`canFindNext()`
Check if the index can get the next higher value.
`boolean`
`canGetFirstOrLast()`
Check if the index can directly look up the lowest or highest value of a column.
`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.
`abstract void`
`close(SessionLocal session)`
Close this index.
`final int`
`compareRows(SearchRow rowData, SearchRow compare)`
Compare two rows.
`abstract Cursor`
`find(SessionLocal session, SearchRow first, SearchRow last, boolean reverse)`
Find a row or a list of rows and create a cursor to iterate over the result.
`Cursor`
`findFirstOrLast(SessionLocal session, boolean first)`
Find the first (or last) value of this index.
`Cursor`
`findNext(SessionLocal 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
`final Column[]`
`getColumns()`
Get the indexed columns.
`abstract double`
`getCost(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)`
Estimate the cost to search for rows given the search mask.
`protected final long`
`getCostRangeIndex(int[] masks, long rowCount, TableFilter[] filters, int filter, SortOrder sortOrder, boolean isScanIndex, AllColumnsForPlan allColumnsSet)`
Calculate the cost for the given mask as if this index was a typical b-tree range index.
`String`
`getCreateSQL()`
Construct the CREATE ...
`String`
`getCreateSQLForCopy(Table targetTable, 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
`long`
`getDiskSpaceUsed(boolean approximate)`
Get the used disk space for this index.
`DbException`
`getDuplicateKeyException(String key)`
Create a duplicate key exception with a message that contains the index name.
`protected StringBuilder`
`getDuplicatePrimaryKeyMessage(int mainIndexColumn)`
Get "PRIMARY KEY ON <table> [(column)]".
`final IndexColumn[]`
`getIndexColumns()`
Get the indexed columns as index columns (with ordering information).
`final IndexType`
`getIndexType()`
Get the index type.
`String`
`getPlanSQL()`
Get the message to show in a EXPLAIN statement.
`Row`
`getRow(SessionLocal session, long key)`
Get the row with the given key.
`abstract long`
`getRowCount(SessionLocal session)`
Get the row count of this table, for the given session.
`abstract long`
`getRowCountApproximation(SessionLocal session)`
Get the approximated row count for this table.
`RowFactory`
`getRowFactory()`
`Table`
`getTable()`
Get the table on which this index is based.
`final int`
`getType()`
Get the object type.
`final int`
`getUniqueColumnCount()`
Returns count of unique columns.
`RowFactory`
`getUniqueRowFactory()`
`boolean`
`isFindUsingFullTableScan()`
Returns `true` if `find()` implementation performs scan over all index, `false` if `find()` performs the fast lookup.
`boolean`
`isFirstColumn(Column column)`
Check if the given column is the first for this index
`boolean`
`isRowIdIndex()`
Does this index support lookup by row id?
`abstract boolean`
`needRebuild()`
Check if the index needs to be rebuilt.
`final boolean`
`needsUniqueCheck(SearchRow searchRow)`
Check if this row needs to be checked for duplicates.
`abstract void`
`remove(SessionLocal session)`
Remove the index.
`abstract void`
`remove(SessionLocal session, Row row)`
Remove a row from the index.
`void`
`removeChildrenAndResources(SessionLocal session)`
Delete all dependent children objects and resources of this object.
`abstract void`
`truncate(SessionLocal session)`
Remove all rows from the index.
`void`
`update(SessionLocal session, Row oldRow, Row newRow)`
Update index after row change.

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

`getSchema, getSQL, getSQL`

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

`checkRename, getChildren, getComment, getCreateSQLForMeta, getDatabase, getDropSQL, getId, getModificationId, getName, 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.util.HasSQL

`getTraceSQL`

## Field Details

### indexColumns

protected IndexColumn[] indexColumns
Columns of this index.

### columns

protected Column[] columns
Table columns used in this index.

### columnIds

protected int[] columnIds
Identities of table columns.

### uniqueColumnColumn

protected final int uniqueColumnColumn
Count of unique columns. Unique columns, if any, are always first columns in the lists.

### table

protected final Table table
The table.

### indexType

protected final IndexType indexType
The index type.

## Constructor Details

### Index

protected Index(Table newTable, int id, String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType)
Initialize the 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
  `uniqueColumnCount` - count of unique columns
  `newIndexType` - the index type

## Method Details

### checkIndexColumnTypes

protected static void checkIndexColumnTypes(IndexColumn[] columns)
Check that the index columns are not CLOB or BLOB.

Parameters:
  `columns` - the columns

### getType

public final int getType()
Description copied from class: `DbObject`
Get the object type.

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

### removeChildrenAndResources

public void removeChildrenAndResources(SessionLocal session)
Description copied from class: `DbObject`
Delete all dependent children objects and resources of this object.

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

### getCreateSQLForCopy

public String getCreateSQLForCopy(Table targetTable, String quotedName)
Description copied from class: `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

Overrides:
  `getCreateSQLForCopy` in class `DbObject`
Parameters:
  `targetTable` - the new table
  `quotedName` - the quoted name
Returns:
  the SQL statement

### getCreateSQL

public String getCreateSQL()
Description copied from class: `DbObject`
Construct the CREATE ... SQL statement for this object.

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

### getPlanSQL

public String getPlanSQL()
Get the message to show in a EXPLAIN statement.

Returns:
  the plan

### close

public abstract void close(SessionLocal session)
Close this index.

Parameters:
  `session` - the session used to write data

### add

public abstract void add(SessionLocal session, Row row)
Add a row to the index.

Parameters:
  `session` - the session to use
  `row` - the row to add

### remove

public abstract void remove(SessionLocal session, Row row)
Remove a row from the index.

Parameters:
  `session` - the session
  `row` - the row

### update

public void update(SessionLocal session, Row oldRow, Row newRow)
Update index after row change.

Parameters:
  `session` - the session
  `oldRow` - row before the update
  `newRow` - row after the update

### isFindUsingFullTableScan

public boolean isFindUsingFullTableScan()
Returns `true` if `find()` implementation performs scan over all index, `false` if `find()` performs the fast lookup.

Returns:
  `true` if `find()` implementation performs scan over all index, `false` if `find()` performs the fast lookup

### find

public abstract Cursor find(SessionLocal session, SearchRow first, SearchRow last, boolean reverse)
Find a row or a list of rows and create a cursor to iterate over the result.

Parameters:
  `session` - the session
  `first` - the first row, or null for no limit
  `last` - the last row, or null for no limit
  `reverse` - if true, iterate in reverse (descending) order
Returns:
  the cursor to iterate over the results

### getCost

public abstract double getCost(SessionLocal session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, AllColumnsForPlan allColumnsSet)
Estimate the cost to search for rows given the search mask. There is one element per column in the search mask. For possible search masks, see IndexCondition.

Parameters:
  `session` - the session
  `masks` - per-column comparison bit masks, null means 'always false', see constants in IndexCondition
  `filters` - all joined table filters
  `filter` - the current table filter index
  `sortOrder` - the sort order
  `allColumnsSet` - the set of all columns
Returns:
  the estimated cost

### remove

public abstract void remove(SessionLocal session)
Remove the index.

Parameters:
  `session` - the session

### truncate

public abstract void truncate(SessionLocal session)
Remove all rows from the index.

Parameters:
  `session` - the session

### canGetFirstOrLast

public boolean canGetFirstOrLast()
Check if the index can directly look up the lowest or highest value of a column.

Returns:
  true if it can

### canFindNext

public boolean canFindNext()
Check if the index can get the next higher value.

Returns:
  true if it can

### findNext

public Cursor findNext(SessionLocal 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.

Parameters:
  `session` - the session
  `higherThan` - the lower limit (excluding)
  `last` - the last row, or null for no limit
Returns:
  the cursor

### findFirstOrLast

public Cursor findFirstOrLast(SessionLocal session, boolean first)
Find the first (or last) value of this index. The cursor returned is positioned on the correct row, or on null if no row has been found.

Parameters:
  `session` - the session
  `first` - true if the first (lowest for ascending indexes) or last value should be returned
Returns:
  a cursor (never null)

### needRebuild

public abstract boolean needRebuild()
Check if the index needs to be rebuilt. This method is called after opening an index.

Returns:
  true if a rebuild is required.

### getRowCount

public abstract long getRowCount(SessionLocal session)
Get the row count of this table, for the given session.

Parameters:
  `session` - the session
Returns:
  the row count

### getRowCountApproximation

public abstract long getRowCountApproximation(SessionLocal session)
Get the approximated row count for this table.

Parameters:
  `session` - the session
Returns:
  the approximated row count

### getDiskSpaceUsed

public long getDiskSpaceUsed(boolean approximate)
Get the used disk space for this index.

Parameters:
  `approximate` - `true` to return quick approximation
Returns:
  the estimated number of bytes

### compareRows

public final int compareRows(SearchRow rowData, SearchRow compare)
Compare two rows.

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

### getColumnIndex

public int getColumnIndex(Column col)
Get the index of a column in the list of index columns

Parameters:
  `col` - the column
Returns:
  the index (0 meaning first column)

### isFirstColumn

public boolean isFirstColumn(Column column)
Check if the given column is the first for this index

Parameters:
  `column` - the column
Returns:
  true if the given columns is the first

### getIndexColumns

public final IndexColumn[] getIndexColumns()
Get the indexed columns as index columns (with ordering information).

Returns:
  the index columns

### getColumns

public final Column[] getColumns()
Get the indexed columns.

Returns:
  the columns

### getUniqueColumnCount

public final int getUniqueColumnCount()
Returns count of unique columns. Unique columns, if any, are always first columns in the lists. Unique indexes may have additional indexed non-unique columns.

Returns:
  count of unique columns, or 0 if index isn't unique

### getIndexType

public final IndexType getIndexType()
Get the index type.

Returns:
  the index type

### getTable

public Table getTable()
Get the table on which this index is based.

Returns:
  the table

### getRow

public Row getRow(SessionLocal session, long key)
Get the row with the given key.

Parameters:
  `session` - the session
  `key` - the unique key
Returns:
  the row

### isRowIdIndex

public boolean isRowIdIndex()
Does this index support lookup by row id?

Returns:
  true if it does

### canScan

public boolean canScan()
Can this index iterate over all rows?

Returns:
  true if it can

### getDuplicateKeyException

public DbException getDuplicateKeyException(String key)
Create a duplicate key exception with a message that contains the index name.

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

### getDuplicatePrimaryKeyMessage

protected StringBuilder getDuplicatePrimaryKeyMessage(int mainIndexColumn)
Get "PRIMARY KEY ON <table> [(column)]".

Parameters:
  `mainIndexColumn` - the column index
Returns:
  the message

### getCostRangeIndex

protected final long getCostRangeIndex(int[] masks, long rowCount, TableFilter[] filters, int filter, SortOrder sortOrder, boolean isScanIndex, AllColumnsForPlan 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

### needsUniqueCheck

public final boolean needsUniqueCheck(SearchRow searchRow)
Check if this row needs to be checked for duplicates.

Parameters:
  `searchRow` - the row to check
Returns:
  `true` if check for duplicates is required, `false otherwise`

### getRowFactory

public RowFactory getRowFactory()

### getUniqueRowFactory

public RowFactory getUniqueRowFactory()