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
,ViewIndex
,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 IndexType
indexType
The index type.private RowFactory
rowFactory
protected Table
table
The table.protected int
uniqueColumnColumn
Count of unique columns.private RowFactory
uniqueRowFactory
-
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, java.lang.String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType)
Initialize the index.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 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.int
compareRows(SearchRow rowData, SearchRow compare)
Compare two rows.private int
compareValues(Value a, Value b, int sortType)
abstract Cursor
find(SessionLocal session, SearchRow first, SearchRow last)
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 columnsprivate java.lang.StringBuilder
getColumnListSQL(java.lang.StringBuilder builder, int sqlFlags)
Get the list of columns as a string.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 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.java.lang.String
getCreateSQL()
Construct the CREATE ...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 tablelong
getDiskSpaceUsed()
Get the used disk space for this index.DbException
getDuplicateKeyException(java.lang.String key)
Create a duplicate key exception with a message that contains the index name.protected java.lang.StringBuilder
getDuplicatePrimaryKeyMessage(int mainIndexColumn)
Get "PRIMARY KEY ON <table> [(column)]".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(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.int
getType()
Get the object type.int
getUniqueColumnCount()
Returns count of unique columns.RowFactory
getUniqueRowFactory()
boolean
isFindUsingFullTableScan()
Returnstrue
iffind()
implementation performs scan over all index,false
iffind()
performs the fast lookup.boolean
isFirstColumn(Column column)
Check if the given column is the first for this indexboolean
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?boolean
mayHaveNullDuplicates(SearchRow searchRow)
Check if this row may have duplicates with the same indexed values in the current compatibility mode.abstract boolean
needRebuild()
Check if the index needs to be rebuilt.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 Detail
-
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.
-
rowFactory
private final RowFactory rowFactory
-
uniqueRowFactory
private final RowFactory uniqueRowFactory
-
-
Constructor Detail
-
Index
protected Index(Table newTable, int id, java.lang.String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType)
Initialize the index.- Parameters:
newTable
- the tableid
- the object idname
- the index namenewIndexColumns
- the columns that are indexed or null if this is not yet knownuniqueColumnCount
- count of unique columnsnewIndexType
- the index type
-
-
Method Detail
-
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.
-
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 classDbObject
- Parameters:
session
- the session
-
isHidden
public final boolean isHidden()
Description copied from class: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.- Overrides:
isHidden
in classSchemaObject
- Returns:
- true if it is hidden
-
getCreateSQLForCopy
public java.lang.String getCreateSQLForCopy(Table targetTable, java.lang.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- Specified by:
getCreateSQLForCopy
in classDbObject
- Parameters:
targetTable
- the new tablequotedName
- the quoted name- Returns:
- the SQL statement
-
getColumnListSQL
private java.lang.StringBuilder getColumnListSQL(java.lang.StringBuilder builder, int sqlFlags)
Get the list of columns as a string.- Parameters:
sqlFlags
- formatting flags- Returns:
- the list of columns
-
getCreateSQL
public java.lang.String getCreateSQL()
Description copied from class:DbObject
Construct the CREATE ... SQL statement for this object.- Specified by:
getCreateSQL
in classDbObject
- Returns:
- the SQL statement
-
getPlanSQL
public java.lang.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 userow
- the row to add
-
remove
public abstract void remove(SessionLocal session, Row row)
Remove a row from the index.- Parameters:
session
- the sessionrow
- the row
-
update
public void update(SessionLocal session, Row oldRow, Row newRow)
Update index after row change.- Parameters:
session
- the sessionoldRow
- row before the updatenewRow
- row after the update
-
isFindUsingFullTableScan
public boolean isFindUsingFullTableScan()
Returnstrue
iffind()
implementation performs scan over all index,false
iffind()
performs the fast lookup.- Returns:
true
iffind()
implementation performs scan over all index,false
iffind()
performs the fast lookup
-
find
public abstract Cursor find(SessionLocal session, SearchRow first, SearchRow last)
Find a row or a list of rows and create a cursor to iterate over the result.- Parameters:
session
- the sessionfirst
- the first row, or null for no limitlast
- the last row, or null for no limit- 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 sessionmasks
- per-column comparison bit masks, null means 'always false', see constants in IndexConditionfilters
- all joined table filtersfilter
- the current table filter indexsortOrder
- the sort orderallColumnsSet
- 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 sessionhigherThan
- 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 sessionfirst
- 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()
Get the used disk space for this index.- Returns:
- the estimated number of bytes
-
compareRows
public final int compareRows(SearchRow rowData, SearchRow compare)
Compare two rows.- Parameters:
rowData
- the first rowcompare
- 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 sessionkey
- 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(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
-
getDuplicatePrimaryKeyMessage
protected java.lang.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 tablerowCount
- the number of rows in the indexfilters
- all joined table filtersfilter
- the current table filter indexsortOrder
- the sort orderisScanIndex
- whether this is a "table scan" indexallColumnsSet
- the set of all columns- Returns:
- the estimated cost
-
mayHaveNullDuplicates
public final boolean mayHaveNullDuplicates(SearchRow searchRow)
Check if this row may have duplicates with the same indexed values in the current compatibility mode. Duplicates withNULL
values are allowed in some modes.- Parameters:
searchRow
- the row to check- Returns:
true
if specified row may have duplicates,false otherwise
-
getRowFactory
public RowFactory getRowFactory()
-
getUniqueRowFactory
public RowFactory getUniqueRowFactory()
-
-