Package org.h2.index

Class 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 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.
    • rowFactory

      private final RowFactory rowFactory
    • uniqueRowFactory

      private final 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 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
    • 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 class SchemaObject
      Returns:
      true if it is hidden
    • 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
      Specified by:
      getCreateSQLForCopy in class DbObject
      Parameters:
      targetTable - the new table
      quotedName - the quoted name
      Returns:
      the SQL statement
    • getColumnListSQL

      private StringBuilder getColumnListSQL(StringBuilder builder, int sqlFlags)
      Get the list of columns as a string.
      Parameters:
      sqlFlags - formatting flags
      Returns:
      the list of columns
    • 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)
      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
      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()
      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 row
      compare - the second row
      Returns:
      0 if both rows are equal, -1 if the first row is smaller, otherwise 1
    • compareValues

      private int compareValues(Value a, Value b, int sortType)
    • 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
    • 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 with NULL 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()