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
An index. Indexes are used to speed up searching data.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]
Identities of table columns.protected Column[]
Table columns used in this index.protected IndexColumn[]
Columns of this index.protected final IndexType
The index type.private final RowFactory
protected final Table
The table.protected final int
Count of unique columns.private final RowFactory
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
ConstructorsModifierConstructorDescriptionprotected
Index
(Table newTable, int id, String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType) Initialize the index. -
Method Summary
Modifier and TypeMethodDescriptionabstract void
add
(SessionLocal session, Row row) Add a row to the index.boolean
Check if the index can get the next higher value.boolean
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.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.findFirstOrLast
(SessionLocal session, boolean first) Find the first (or last) value of this index.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 StringBuilder
getColumnListSQL
(StringBuilder builder, int sqlFlags) Get the list of columns as a string.final Column[]
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.Construct the CREATE ...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 tablelong
Get the used disk space for this index.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[]
Get the indexed columns as index columns (with ordering information).final IndexType
Get the index type.Get the message to show in a EXPLAIN statement.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.getTable()
Get the table on which this index is based.final int
getType()
Get the object type.final int
Returns count of unique columns.boolean
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 indexfinal 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
Does this index support lookup by row id?final boolean
mayHaveNullDuplicates
(SearchRow searchRow) Check if this row may have duplicates with the same indexed values in the current compatibility mode.abstract boolean
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 Details
-
indexColumns
Columns of this index. -
columns
Table columns used in this index. -
columnIds
protected int[] columnIdsIdentities of table columns. -
uniqueColumnColumn
protected final int uniqueColumnColumnCount of unique columns. Unique columns, if any, are always first columns in the lists. -
table
The table. -
indexType
The index type. -
rowFactory
-
uniqueRowFactory
-
-
Constructor Details
-
Index
protected Index(Table newTable, int id, 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 Details
-
checkIndexColumnTypes
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
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
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
Get the list of columns as a string.- Parameters:
sqlFlags
- formatting flags- Returns:
- the list of columns
-
getCreateSQL
Description copied from class:DbObject
Construct the CREATE ... SQL statement for this object.- Specified by:
getCreateSQL
in classDbObject
- Returns:
- the SQL statement
-
getPlanSQL
Get the message to show in a EXPLAIN statement.- Returns:
- the plan
-
close
Close this index.- Parameters:
session
- the session used to write data
-
add
Add a row to the index.- Parameters:
session
- the session to userow
- the row to add
-
remove
Remove a row from the index.- Parameters:
session
- the sessionrow
- the row
-
update
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
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
Remove the index.- Parameters:
session
- the session
-
truncate
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
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
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
Get the row count of this table, for the given session.- Parameters:
session
- the session- Returns:
- the row count
-
getRowCountApproximation
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
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
-
compareValues
-
getColumnIndex
Get the index of a column in the list of index columns- Parameters:
col
- the column- Returns:
- the index (0 meaning first column)
-
isFirstColumn
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
Get the indexed columns as index columns (with ordering information).- Returns:
- the index columns
-
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
Get the index type.- Returns:
- the index type
-
getTable
Get the table on which this index is based.- Returns:
- the table
-
getRow
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
Create a duplicate key exception with a message that contains the index name.- Parameters:
key
- the key values- Returns:
- the exception
-
getDuplicatePrimaryKeyMessage
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
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
-
getUniqueRowFactory
-