## org.h2.index
Class BtreeIndex

    java.lang.Object
      org.h2.engine.DbObjectBase
          org.h2.schema.SchemaObjectBase
              org.h2.index.BaseIndex
                  org.h2.index.BtreeIndex

All Implemented Interfaces:
  DbObject, Index, SchemaObject, RecordReader

    public class BtreeIndex

extends BaseIndex
implements RecordReader

This is the most common type of index, a b-tree index. The index structure is:

- There is one `BtreeHead` that points to the root page. The head always stays where it is.
- There is a number of `BtreePage`s. Each page is either a `BtreeNode` or a `BtreeLeaf`.
- A node page links to other leaf pages or to node pages. Leaf pages don't point to other pages (but may have a parent).
- The uppermost page is the root page. If pages are added or deleted, the root page may change.

Only the data of the indexed columns are stored in the index.

Field Summary

Fields inherited from class org.h2.index.BaseIndex
`columnIds, columns, indexColumns, indexType, isMultiVersion, table`

Fields inherited from class org.h2.engine.DbObjectBase
`comment, database, trace`

Fields inherited from interface org.h2.index.Index
`EMPTY_HEAD`

Fields inherited from interface org.h2.engine.DbObject
`AGGREGATE, COMMENT, CONSTANT, CONSTRAINT, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, TABLE_OR_VIEW, TRIGGER, USER, USER_DATATYPE`

Constructor Summary
`BtreeIndex(Session session, TableData table, int id, java.lang.String indexName, IndexColumn[] columns, IndexType indexType, int headPos)` Create a new b tree index with the given properties.

Method Summary
`void` `add(Session session, Row r)` Add a row to this index.
`boolean` `canFindNext()` Check if the index can get the next higher value.
`boolean` `canGetFirstOrLast()` Check if this index can quickly find the first or last value.
`void` `checkRename()` Check if this object can be renamed.
`void` `close(Session session)` Close this index.
`Cursor` `find(Session session, SearchRow first, SearchRow last)` Create a cursor to iterate over a number of rows.
`Cursor` `findFirstOrLast(Session session, boolean first)` Find the first (or last) value of this index.
`Cursor` `findNext(Session session, SearchRow first, SearchRow last)` Find a row or a list of rows that is larger and create a cursor to iterate over the result.
`void` `flush(Session session)` Write all changed paged to disk and mark the index as valid.
`double` `getCost(Session session, int[] masks)` Calculate the cost to find rows.
`int` `getHeadPos()` Get the position of the head record.
`long` `getLastChange()` Get the last change time or 0 if the index has not been changed.
`long` `getRowCount(Session session)` Get the row count of this table, for the given session.
`long` `getRowCountApproximation()` Get the approximated row count for this table.
`boolean` `needRebuild()` Check if this index needs to be re-built.
`Record` `read(Session session, DataPage s)` Read a record from the data page.
`void` `remove(Session session)` Remove the index.
`void` `remove(Session session, Row row)` Remove a row from the index.
`void` `truncate(Session session)` Truncate the index.

Methods inherited from class org.h2.index.BaseIndex
`commit, compareKeys, compareRows, containsNullAndAllowMultipleNull, getColumnIndex, getColumnListSQL, getColumns, getCostRangeIndex, getCreateSQL, getCreateSQLForCopy, getDropSQL, getDuplicateKeyException, getIndexColumns, getIndexType, getLookupCost, getPlanSQL, getRootPageId, getTable, getType, removeChildrenAndResources`

Methods inherited from class org.h2.schema.SchemaObjectBase
`getSchema, getSQL, initSchemaObjectBase`

Methods inherited from class org.h2.engine.DbObjectBase
`getChildren, getComment, getDatabase, getId, getModificationId, getName, initDbObjectBase, invalidate, isTemporary, 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.schema.SchemaObject
`getSchema`

Methods inherited from interface org.h2.engine.DbObject
`getChildren, getComment, getDatabase, getId, getModificationId, getName, getSQL, isTemporary, rename, setComment, setModified, setTemporary`

Constructor Detail

### BtreeIndex

    public BtreeIndex(Session session,
                      TableData table,
                      int id,
                      java.lang.String indexName,
                      IndexColumn[] columns,
                      IndexType indexType,
                      int headPos)
               throws java.sql.SQLException

  Create a new b tree index with the given properties. If the index does not yet exist, a new empty one is created.

  Parameters:
    `session` - the session
    `table` - the base table
    `id` - the object id
    `indexName` - the name of the index
    `columns` - the indexed columns
    `indexType` - the index type
    `headPos` - the position of the index header page, or Index.EMPTY_HEAD for a new index
  Throws:
    `java.sql.SQLException`

Method Detail

### getHeadPos

    public int getHeadPos()

  Description copied from interface: `DbObject`
  Get the position of the head record.

  Specified by:
    `getHeadPos` in interface `DbObject`
  Overrides:
    `getHeadPos` in class `DbObjectBase`
  Returns:
    the head position

### remove

    public void remove(Session session)
                throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Remove the index.

  Specified by:
    `remove` in interface `Index`
  Specified by:
    `remove` in class `BaseIndex`
  Parameters:
    `session` - the session
  Throws:
    `java.sql.SQLException`

### flush

    public void flush(Session session)
               throws java.sql.SQLException

  Write all changed paged to disk and mark the index as valid.

  Parameters:
    `session` - the session
  Throws:
    `java.sql.SQLException`

### close

    public void close(Session session)
               throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Close this index.

  Specified by:
    `close` in interface `Index`
  Specified by:
    `close` in class `BaseIndex`
  Parameters:
    `session` - the session
  Throws:
    `java.sql.SQLException`

### add

    public void add(Session session,
                    Row r)
             throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Add a row to this index.

  Specified by:
    `add` in interface `Index`
  Specified by:
    `add` in class `BaseIndex`
  Parameters:
    `session` - the session
    `r` - the row to add
  Throws:
    `java.sql.SQLException`

### remove

    public void remove(Session session,
                       Row row)
                throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Remove a row from the index.

  Specified by:
    `remove` in interface `Index`
  Specified by:
    `remove` in class `BaseIndex`
  Parameters:
    `session` - the session
    `row` - the row
  Throws:
    `java.sql.SQLException`

### canFindNext

    public boolean canFindNext()

  Description copied from interface: `Index`
  Check if the index can get the next higher value.

  Specified by:
    `canFindNext` in interface `Index`
  Overrides:
    `canFindNext` in class `BaseIndex`
  Returns:
    true if it can

### findNext

    public Cursor findNext(Session session,
                           SearchRow first,
                           SearchRow last)
                    throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Find a row or a list of rows that is larger and create a cursor to iterate over the result. The base implementation doesn't support this feature.

  Specified by:
    `findNext` in interface `Index`
  Overrides:
    `findNext` in class `BaseIndex`
  Parameters:
    `session` - the session
    `first` - the lower limit (excluding)
    `last` - the last row, or null for no limit
  Returns:
    the cursor
  Throws:
    `java.sql.SQLException`

### find

    public Cursor find(Session session,
                       SearchRow first,
                       SearchRow last)
                throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Create a cursor to iterate over a number of rows.

  Specified by:
    `find` in interface `Index`
  Specified by:
    `find` in class `BaseIndex`
  Parameters:
    `session` - the session
    `first` - the first row to return (null if no limit)
    `last` - the last row to return (null if no limit)
  Returns:
    the cursor to iterate over the results
  Throws:
    `java.sql.SQLException`

### getCost

    public double getCost(Session session,
                          int[] masks)

  Description copied from class: `BaseIndex`
  Calculate the cost to find rows.

  Specified by:
    `getCost` in interface `Index`
  Specified by:
    `getCost` in class `BaseIndex`
  Parameters:
    `session` - the session
    `masks` - the condition mask
  Returns:
    the cost

### read

    public Record read(Session session,
                       DataPage s)
                throws java.sql.SQLException

  Description copied from interface: `RecordReader`
  Read a record from the data page.

  Specified by:
    `read` in interface `RecordReader`
  Parameters:
    `session` - the session
    `s` - the data page
  Returns:
    the record
  Throws:
    `java.sql.SQLException`

### truncate

    public void truncate(Session session)
                  throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  Truncate the index.

  Specified by:
    `truncate` in interface `Index`
  Specified by:
    `truncate` in class `BaseIndex`
  Parameters:
    `session` - the session
  Throws:
    `java.sql.SQLException`

### checkRename

    public void checkRename()

  Description copied from class: `DbObjectBase`
  Check if this object can be renamed. System objects may not be renamed.

  Specified by:
    `checkRename` in interface `DbObject`
  Specified by:
    `checkRename` in class `DbObjectBase`

### needRebuild

    public boolean needRebuild()

  Description copied from class: `BaseIndex`
  Check if this index needs to be re-built.

  Specified by:
    `needRebuild` in interface `Index`
  Specified by:
    `needRebuild` in class `BaseIndex`
  Returns:
    true if it must be re-built.

### getLastChange

    public long getLastChange()

  Get the last change time or 0 if the index has not been changed.

  Returns:
    the last change time or 0

### canGetFirstOrLast

    public boolean canGetFirstOrLast()

  Description copied from class: `BaseIndex`
  Check if this index can quickly find the first or last value.

  Specified by:
    `canGetFirstOrLast` in interface `Index`
  Specified by:
    `canGetFirstOrLast` in class `BaseIndex`
  Returns:
    true if it can

### findFirstOrLast

    public Cursor findFirstOrLast(Session session,
                                  boolean first)
                           throws java.sql.SQLException

  Description copied from class: `BaseIndex`
  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.

  Specified by:
    `findFirstOrLast` in interface `Index`
  Specified by:
    `findFirstOrLast` in class `BaseIndex`
  Parameters:
    `session` - the session
    `first` - true for the first value, false for the last
  Returns:
    a cursor (never null)
  Throws:
    `java.sql.SQLException`

### getRowCount

    public long getRowCount(Session session)

  Description copied from interface: `Index`
  Get the row count of this table, for the given session.

  Specified by:
    `getRowCount` in interface `Index`
  Parameters:
    `session` - the session
  Returns:
    the row count

### getRowCountApproximation

    public long getRowCountApproximation()

  Description copied from interface: `Index`
  Get the approximated row count for this table.

  Specified by:
    `getRowCountApproximation` in interface `Index`
  Returns:
    the approximated row count