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
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 IndexTypeThe index type.protected final TableThe table.protected final intCount 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, USERFields 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
ConstructorsModifierConstructorDescriptionprotectedIndex(Table newTable, int id, String name, IndexColumn[] newIndexColumns, int uniqueColumnCount, IndexType newIndexType) Initialize the index. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidadd(SessionLocal session, Row row) Add a row to the index.booleanCheck if the index can get the next higher value.booleanCheck if the index can directly look up the lowest or highest value of a column.booleancanScan()Can this index iterate over all rows?protected static voidcheckIndexColumnTypes(IndexColumn[] columns) Check that the index columns are not CLOB or BLOB.abstract voidclose(SessionLocal session) Close this index.final intcompareRows(SearchRow rowData, SearchRow compare) Compare two rows.abstract Cursorfind(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.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.intgetColumnIndex(Column col) Get the index of a column in the list of index columnsfinal Column[]Get the indexed columns.abstract doublegetCost(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 longgetCostRangeIndex(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 tablelonggetDiskSpaceUsed(boolean approximate) Get the used disk space for this index.Create a duplicate key exception with a message that contains the index name.protected StringBuildergetDuplicatePrimaryKeyMessage(int mainIndexColumn) Get "PRIMARY KEY ON <table> [(column)]".final IndexColumn[]Get the indexed columns as index columns (with ordering information).final IndexTypeGet 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 longgetRowCount(SessionLocal session) Get the row count of this table, for the given session.abstract longgetRowCountApproximation(SessionLocal session) Get the approximated row count for this table.getTable()Get the table on which this index is based.final intgetType()Get the object type.final intReturns count of unique columns.booleanReturnstrueiffind()implementation performs scan over all index,falseiffind()performs the fast lookup.booleanisFirstColumn(Column column) Check if the given column is the first for this indexbooleanDoes this index support lookup by row id?abstract booleanCheck if the index needs to be rebuilt.final booleanneedsUniqueCheck(SearchRow searchRow) Check if this row needs to be checked for duplicates.abstract voidremove(SessionLocal session) Remove the index.abstract voidremove(SessionLocal session, Row row) Remove a row from the index.voidremoveChildrenAndResources(SessionLocal session) Delete all dependent children objects and resources of this object.abstract voidtruncate(SessionLocal session) Remove all rows from the index.voidupdate(SessionLocal session, Row oldRow, Row newRow) Update index after row change.Methods inherited from class org.h2.schema.SchemaObject
getSchema, getSQL, getSQLMethods inherited from class org.h2.engine.DbObject
checkRename, getChildren, getComment, getCreateSQLForMeta, getDatabase, getDropSQL, getId, getModificationId, getName, invalidate, isTemporary, isValid, rename, setComment, setModified, setObjectName, setTemporary, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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.
-
-
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:DbObjectGet the object type. -
removeChildrenAndResources
Description copied from class:DbObjectDelete all dependent children objects and resources of this object.- Specified by:
removeChildrenAndResourcesin classDbObject- Parameters:
session- the session
-
getCreateSQLForCopy
Description copied from class:DbObjectBuild 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:
getCreateSQLForCopyin classDbObject- Parameters:
targetTable- the new tablequotedName- the quoted name- Returns:
- the SQL statement
-
getCreateSQL
Description copied from class:DbObjectConstruct the CREATE ... SQL statement for this object.- Specified by:
getCreateSQLin 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()Returnstrueiffind()implementation performs scan over all index,falseiffind()performs the fast lookup.- Returns:
trueiffind()implementation performs scan over all index,falseiffind()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 limitreverse- 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 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(boolean approximate) Get the used disk space for this index.- Parameters:
approximate-trueto return quick approximation- 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
-
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
-
needsUniqueCheck
Check if this row needs to be checked for duplicates.- Parameters:
searchRow- the row to check- Returns:
trueif check for duplicates is required,false otherwise
-
getRowFactory
-
getUniqueRowFactory
-