Top Description Fields Constructors Methods
org.h2.index

public abstract Class Index

extends SchemaObject
Class Inheritance
Known Direct Subclasses
org.h2.index.LinkedIndex, org.h2.index.MetaIndex, org.h2.index.QueryExpressionIndex, org.h2.index.VirtualTableIndex, org.h2.mvstore.db.MVIndex
Imports
java.util.ArrayList, .Arrays, org.h2.api.ErrorCode, org.h2.command.query.AllColumnsForPlan, org.h2.engine.Constants, .DbObject, .NullsDistinct, .SessionLocal, org.h2.message.DbException, .Trace, org.h2.result.Row, .RowFactory, .SearchRow, .SortOrder, org.h2.schema.SchemaObject, org.h2.table.Column, .IndexColumn, .Table, .TableFilter, org.h2.util.StringUtils, org.h2.value.CompareMode, .DataType, .Value, .ValueNull

An index. Indexes are used to speed up searching data.

Field Summary

Modifier and TypeField and 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.

private final RowFactory
protected final Table
table

The table.

protected final int
uniqueColumnColumn

Count of unique columns.

private final RowFactory

Constructor Summary

AccessConstructor and Description
protected
Index(Table
the table
newTable
,
int
the object id
id
,
String
the index name
name
,
IndexColumn[]
the columns that are indexed or null if this is not yet known
newIndexColumns
,
int
count of unique columns
uniqueColumnCount
,
IndexType
the index type
newIndexType
)

Initialize the index.

Method Summary

Modifier and TypeMethod and Description
public abstract void
add(SessionLocal
the session to use
session
,
Row
the row to add
row
)

Add a row to the index.

public boolean

Returns:

true if it can
canFindNext
()

Check if the index can get the next higher value.

public boolean

Returns:

true if it can
canGetFirstOrLast
()

Check if the index can directly look up the lowest or highest value of a column.

public boolean

Returns:

true if it can
canScan
()

Can this index iterate over all rows?

protected static void
checkIndexColumnTypes(IndexColumn[]
the columns
columns
)

Check that the index columns are not CLOB or BLOB.

public abstract void
close(SessionLocal
the session used to write data
session
)

Close this index.

public final int

Returns:

0 if both rows are equal, -1 if the first row is smaller, otherwise 1
compareRows
(SearchRow
the first row
rowData
,
SearchRow
the second row
compare
)

Compare two rows.

private int
compareValues(Value a, Value b, int sortType)

public abstract Cursor

Returns:

the cursor to iterate over the results
find
(SessionLocal
the session
session
,
SearchRow
the first row, or null for no limit
first
,
SearchRow
the last row, or null for no limit
last
,
boolean
if true, iterate in reverse (descending) order
reverse
)

Find a row or a list of rows and create a cursor to iterate over the result.

public Cursor

Returns:

a cursor (never null)
findFirstOrLast
(SessionLocal
the session
session
,
boolean
true if the first (lowest for ascending indexes) or last value should be returned
first
)

Find the first (or last) value of this index.

public Cursor

Returns:

the cursor
findNext
(SessionLocal
the session
session
,
SearchRow
the lower limit (excluding)
higherThan
,
SearchRow
the last row, or null for no limit
last
)

Find a row or a list of rows that is larger and create a cursor to iterate over the result.

public int

Returns:

the index (0 meaning first column)
getColumnIndex
(Column
the column
col
)

Get the index of a column in the list of index columns

private StringBuilder

Returns:

the list of columns
getColumnListSQL
(StringBuilder builder, int
formatting flags
sqlFlags
)

Get the list of columns as a string.

public final Column[]

Returns:

the columns
getColumns
()

Get the indexed columns.

public abstract double

Returns:

the estimated cost
getCost
(SessionLocal
the session
session
,
int[]
per-column comparison bit masks, null means 'always false', see constants in IndexCondition
masks
,
TableFilter[]
all joined table filters
filters
,
int
the current table filter index
filter
,
SortOrder
the sort order
sortOrder
,
AllColumnsForPlan
the set of all columns
allColumnsSet
)

Estimate the cost to search for rows given the search mask.

protected final long

Returns:

the estimated cost
getCostRangeIndex
(int[]
the IndexCondition search masks, one for each column in the table
masks
,
long
the number of rows in the index
rowCount
,
TableFilter[]
all joined table filters
filters
,
int
the current table filter index
filter
,
SortOrder
the sort order
sortOrder
,
boolean
whether this is a "table scan" index
isScanIndex
,
AllColumnsForPlan
the set of all columns
allColumnsSet
)

Calculate the cost for the given mask as if this index was a typical b-tree range index.

public String
getCreateSQL()

Implements abstract org.h2.engine.DbObject.getCreateSQL.

Construct the CREATE ...

public String
getCreateSQLForCopy(Table
the new table
targetTable
,
String
the quoted name
quotedName
)

Overrides org.h2.engine.DbObject.getCreateSQLForCopy.

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

public long

Returns:

the estimated number of bytes
getDiskSpaceUsed
(boolean
true to return quick approximation
approximate
)

Get the used disk space for this index.

public DbException

Returns:

the exception
getDuplicateKeyException
(String
the key values
key
)

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

protected StringBuilder

Returns:

the message
getDuplicatePrimaryKeyMessage
(int
the column index
mainIndexColumn
)

Get "PRIMARY KEY ON <table> [(column)]".

public final IndexColumn[]

Returns:

the index columns
getIndexColumns
()

Get the indexed columns as index columns (with ordering information).

public final IndexType

Returns:

the index type
getIndexType
()

Get the index type.

public String

Returns:

the plan
getPlanSQL
()

Get the message to show in a EXPLAIN statement.

public Row

Returns:

the row
getRow
(SessionLocal
the session
session
,
long
the unique key
key
)

Get the row with the given key.

public abstract long

Returns:

the row count
getRowCount
(SessionLocal
the session
session
)

Get the row count of this table, for the given session.

public abstract long

Returns:

the approximated row count
getRowCountApproximation
(SessionLocal
the session
session
)

Get the approximated row count for this table.

public RowFactory
public Table

Returns:

the table
getTable
()

Get the table on which this index is based.

public final int
getType()

Implements abstract org.h2.engine.DbObject.getType.

Get the object type.

public final int

Returns:

count of unique columns, or 0 if index isn't unique
getUniqueColumnCount
()

Returns count of unique columns.

public RowFactory
public boolean

Returns:

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

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

public boolean

Returns:

true if the given columns is the first
isFirstColumn
(Column
the column
column
)

Check if the given column is the first for this index

public boolean

Returns:

true if it does
isRowIdIndex
()

Does this index support lookup by row id?

public abstract boolean

Returns:

true if a rebuild is required.
needRebuild
()

Check if the index needs to be rebuilt.

public final boolean

Returns:

true if check for duplicates is required, false otherwise
needsUniqueCheck
(SearchRow
the row to check
searchRow
)

Check if this row needs to be checked for duplicates.

private boolean
needsUniqueCheck(SearchRow searchRow, boolean distinct)

public abstract void
remove(SessionLocal
the session
session
,
Row
the row
row
)

Remove a row from the index.

public abstract void
remove(SessionLocal
the session
session
)

Remove the index.

public void
removeChildrenAndResources(SessionLocal
the session
session
)

Implements abstract org.h2.engine.DbObject.removeChildrenAndResources.

Delete all dependent children objects and resources of this object.

public abstract void
truncate(SessionLocal
the session
session
)

Remove all rows from the index.

public void
update(SessionLocal
the session
session
,
Row
row before the update
oldRow
,
Row
row after the update
newRow
)

Update index after row change.

Inherited from org.h2.schema.SchemaObject:
getSchemagetSQLgetSQL

Field Detail

columnIdsback to summary
protected int[] columnIds

Identities of table columns.

columnsback to summary
protected Column[] columns

Table columns used in this index.

indexColumnsback to summary
protected IndexColumn[] indexColumns

Columns of this index.

indexTypeback to summary
protected final IndexType indexType

The index type.

rowFactoryback to summary
private final RowFactory rowFactory
tableback to summary
protected final Table table

The table.

uniqueColumnColumnback to summary
protected final int uniqueColumnColumn

Count of unique columns. Unique columns, if any, are always first columns in the lists.

uniqueRowFactoryback to summary
private final RowFactory uniqueRowFactory

Constructor Detail

Indexback to summary
protected Index(Table newTable, int id, String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType)

Initialize the index.

Parameters
newTable:Table

the table

id:int

the object id

name:String

the index name

newIndexColumns:IndexColumn[]

the columns that are indexed or null if this is not yet known

uniqueColumnCount:int

count of unique columns

newIndexType:IndexType

the index type

Method Detail

addback to summary
public abstract void add(SessionLocal session, Row row)

Add a row to the index.

Parameters
session:SessionLocal

the session to use

row:Row

the row to add

canFindNextback to summary
public boolean canFindNext()

Check if the index can get the next higher value.

Returns:boolean

true if it can

canGetFirstOrLastback to summary
public boolean canGetFirstOrLast()

Check if the index can directly look up the lowest or highest value of a column.

Returns:boolean

true if it can

canScanback to summary
public boolean canScan()

Can this index iterate over all rows?

Returns:boolean

true if it can

checkIndexColumnTypesback to summary
protected static void checkIndexColumnTypes(IndexColumn[] columns)

Check that the index columns are not CLOB or BLOB.

Parameters
columns:IndexColumn[]

the columns

closeback to summary
public abstract void close(SessionLocal session)

Close this index.

Parameters
session:SessionLocal

the session used to write data

compareRowsback to summary
public final int compareRows(SearchRow rowData, SearchRow compare)

Compare two rows.

Parameters
rowData:SearchRow

the first row

compare:SearchRow

the second row

Returns:int

0 if both rows are equal, -1 if the first row is smaller, otherwise 1

compareValuesback to summary
private int compareValues(Value a, Value b, int sortType)
findback to summary
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:SessionLocal

the session

first:SearchRow

the first row, or null for no limit

last:SearchRow

the last row, or null for no limit

reverse:boolean

if true, iterate in reverse (descending) order

Returns:Cursor

the cursor to iterate over the results

findFirstOrLastback to summary
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:SessionLocal

the session

first:boolean

true if the first (lowest for ascending indexes) or last value should be returned

Returns:Cursor

a cursor (never null)

findNextback to summary
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:SessionLocal

the session

higherThan:SearchRow

the lower limit (excluding)

last:SearchRow

the last row, or null for no limit

Returns:Cursor

the cursor

getColumnIndexback to summary
public int getColumnIndex(Column col)

Get the index of a column in the list of index columns

Parameters
col:Column

the column

Returns:int

the index (0 meaning first column)

getColumnListSQLback to summary
private StringBuilder getColumnListSQL(StringBuilder builder, int sqlFlags)

Get the list of columns as a string.

Parameters
sqlFlags:int

formatting flags

Returns:StringBuilder

the list of columns

getColumnsback to summary
public final Column[] getColumns()

Get the indexed columns.

Returns:Column[]

the columns

getCostback to summary
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:SessionLocal

the session

masks:int[]

per-column comparison bit masks, null means 'always false', see constants in IndexCondition

filters:TableFilter[]

all joined table filters

filter:int

the current table filter index

sortOrder:SortOrder

the sort order

allColumnsSet:AllColumnsForPlan

the set of all columns

Returns:double

the estimated cost

getCostRangeIndexback to summary
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:int[]

the IndexCondition search masks, one for each column in the table

rowCount:long

the number of rows in the index

filters:TableFilter[]

all joined table filters

filter:int

the current table filter index

sortOrder:SortOrder

the sort order

isScanIndex:boolean

whether this is a "table scan" index

allColumnsSet:AllColumnsForPlan

the set of all columns

Returns:long

the estimated cost

getCreateSQLback to summary
public String getCreateSQL()

Implements abstract org.h2.engine.DbObject.getCreateSQL.

Doc from org.h2.engine.DbObject.getCreateSQL.

Construct the CREATE ... SQL statement for this object.

Returns:String

the SQL statement

Annotations
@Override
getCreateSQLForCopyback to summary
public String getCreateSQLForCopy(Table targetTable, String quotedName)

Overrides org.h2.engine.DbObject.getCreateSQLForCopy.

Doc from org.h2.engine.DbObject.getCreateSQLForCopy.

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

Parameters
targetTable:Table

the new table

quotedName:String

the quoted name

Returns:String

the SQL statement

Annotations
@Override
getDiskSpaceUsedback to summary
public long getDiskSpaceUsed(boolean approximate)

Get the used disk space for this index.

Parameters
approximate:boolean

true to return quick approximation

Returns:long

the estimated number of bytes

getDuplicateKeyExceptionback to summary
public DbException getDuplicateKeyException(String key)

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

Parameters
key:String

the key values

Returns:DbException

the exception

getDuplicatePrimaryKeyMessageback to summary
protected StringBuilder getDuplicatePrimaryKeyMessage(int mainIndexColumn)

Get "PRIMARY KEY ON <table> [(column)]".

Parameters
mainIndexColumn:int

the column index

Returns:StringBuilder

the message

getIndexColumnsback to summary
public final IndexColumn[] getIndexColumns()

Get the indexed columns as index columns (with ordering information).

Returns:IndexColumn[]

the index columns

getIndexTypeback to summary
public final IndexType getIndexType()

Get the index type.

Returns:IndexType

the index type

getPlanSQLback to summary
public String getPlanSQL()

Get the message to show in a EXPLAIN statement.

Returns:String

the plan

getRowback to summary
public Row getRow(SessionLocal session, long key)

Get the row with the given key.

Parameters
session:SessionLocal

the session

key:long

the unique key

Returns:Row

the row

getRowCountback to summary
public abstract long getRowCount(SessionLocal session)

Get the row count of this table, for the given session.

Parameters
session:SessionLocal

the session

Returns:long

the row count

getRowCountApproximationback to summary
public abstract long getRowCountApproximation(SessionLocal session)

Get the approximated row count for this table.

Parameters
session:SessionLocal

the session

Returns:long

the approximated row count

getRowFactoryback to summary
public RowFactory getRowFactory()
getTableback to summary
public Table getTable()

Get the table on which this index is based.

Returns:Table

the table

getTypeback to summary
public final int getType()

Implements abstract org.h2.engine.DbObject.getType.

Doc from org.h2.engine.DbObject.getType.

Get the object type.

Returns:int

the object type

Annotations
@Override
getUniqueColumnCountback to summary
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:int

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

getUniqueRowFactoryback to summary
public RowFactory getUniqueRowFactory()
isFindUsingFullTableScanback to summary
public boolean isFindUsingFullTableScan()

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

Returns:boolean

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

isFirstColumnback to summary
public boolean isFirstColumn(Column column)

Check if the given column is the first for this index

Parameters
column:Column

the column

Returns:boolean

true if the given columns is the first

isRowIdIndexback to summary
public boolean isRowIdIndex()

Does this index support lookup by row id?

Returns:boolean

true if it does

needRebuildback to summary
public abstract boolean needRebuild()

Check if the index needs to be rebuilt. This method is called after opening an index.

Returns:boolean

true if a rebuild is required.

needsUniqueCheckback to summary
public final boolean needsUniqueCheck(SearchRow searchRow)

Check if this row needs to be checked for duplicates.

Parameters
searchRow:SearchRow

the row to check

Returns:boolean

true if check for duplicates is required, false otherwise

needsUniqueCheckback to summary
private boolean needsUniqueCheck(SearchRow searchRow, boolean distinct)
removeback to summary
public abstract void remove(SessionLocal session, Row row)

Remove a row from the index.

Parameters
session:SessionLocal

the session

row:Row

the row

removeback to summary
public abstract void remove(SessionLocal session)

Remove the index.

Parameters
session:SessionLocal

the session

removeChildrenAndResourcesback to summary
public void removeChildrenAndResources(SessionLocal session)

Implements abstract org.h2.engine.DbObject.removeChildrenAndResources.

Doc from org.h2.engine.DbObject.removeChildrenAndResources.

Delete all dependent children objects and resources of this object.

Parameters
session:SessionLocal

the session

Annotations
@Override
truncateback to summary
public abstract void truncate(SessionLocal session)

Remove all rows from the index.

Parameters
session:SessionLocal

the session

updateback to summary
public void update(SessionLocal session, Row oldRow, Row newRow)

Update index after row change.

Parameters
session:SessionLocal

the session

oldRow:Row

row before the update

newRow:Row

row after the update