org.h2.index
Class MultiVersionIndex

java.lang.Object
  extended by org.h2.index.MultiVersionIndex
All Implemented Interfaces:
DbObject, Index, SchemaObject

public class MultiVersionIndex
extends java.lang.Object
implements Index

A multi-version index is a combination of a regular index, and a in-memory tree index that contains uncommitted changes. Uncommitted changes can include new rows, and deleted rows.


Field Summary
 
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
MultiVersionIndex(Index base, RegularTable table)
           
 
Method Summary
 void add(Session 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.
 void checkRename()
          Check if renaming is allowed.
 void close(Session session)
          Close this index.
 void commit(int operation, Row row)
          Commit the operation for a row.
 int compareKeys(SearchRow rowData, SearchRow compare)
          Compare the positions of two rows.
 int compareRows(SearchRow rowData, SearchRow compare)
          Compare two rows.
 boolean containsNullAndAllowMultipleNull(SearchRow newRow)
          Check if one of the columns is NULL and multiple rows with NULL are allowed using the current compatibility mode for unique indexes.
 Cursor find(Session session, SearchRow first, SearchRow last)
          Find a row or a list of rows and create a cursor to iterate over the result.
 Cursor findFirstOrLast(Session session, boolean first)
          Find the lowest or highest value of a column.
 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.
 Index getBaseIndex()
           
 java.util.ArrayList<DbObject> getChildren()
          Get the list of dependent children (for tables, this includes indexes and so on).
 int getColumnIndex(Column col)
          Get the index of a column in the list of index columns
 java.lang.String getColumnListSQL()
          Get the list of columns as a string.
 Column[] getColumns()
          Get the indexed columns.
 java.lang.String getComment()
          Get the current comment of this object.
 double getCost(Session session, int[] masks)
          Estimate the cost to search for rows given the search mask.
 long getCostRangeIndex(int[] masks, long rowCount)
          Estimate the cost required to search one row, and then iterate over the given number of rows.
 java.lang.String getCreateSQL()
          Construct the original CREATE ...
 java.lang.String getCreateSQLForCopy(Table forTable, java.lang.String quotedName)
          Construct a CREATE ...
 Database getDatabase()
          Get the database.
 java.lang.String getDropSQL()
          Construct a DROP ...
 DbException getDuplicateKeyException()
          Create a duplicate key exception with a message that contains the index name
 int getId()
          Get the unique object id.
 IndexColumn[] getIndexColumns()
          Get the indexed columns as index columns (with ordering information).
 IndexType getIndexType()
          Get the index type.
 int getLookupCost(long rowCount)
          Estimate the cost required to search a number of rows.
 long getModificationId()
          Get the last modification id.
 java.lang.String getName()
          Get the name.
 java.lang.String getPlanSQL()
          Get the message to show in a EXPLAIN statement.
 int getRootPageId()
          Get the root page of this index.
 Row getRow(Session session, long key)
          Get the row with the given key.
 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.
 Schema getSchema()
          Get the schema in which this object is defined
 java.lang.String getSQL()
          Get the SQL name of this object (may be quoted).
 Table getTable()
          Get the table on which this index is based.
 int getType()
          Get the object type.
 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 isTemporary()
          Check if this object is temporary (for example, a temporary table).
 boolean isUncommittedFromOtherSession(Session session, Row row)
          Check if there is an uncommitted row with the given key within a different session.
 boolean needRebuild()
          Check if the index needs to be rebuilt.
 void remove(Session session)
          Remove the index.
 void remove(Session session, Row row)
          Remove a row from the index.
 void removeChildrenAndResources(Session session)
          Delete all dependent children objects and resources of this object.
 void rename(java.lang.String newName)
          Rename the object.
 void setComment(java.lang.String comment)
          Change the comment of this object.
 void setModified()
          Tell the object that is was modified.
 void setTemporary(boolean temporary)
          Tell this object that it is temporary or not.
 void truncate(Session session)
          Remove all rows from the index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiVersionIndex

public MultiVersionIndex(Index base,
                         RegularTable table)
Method Detail

add

public void add(Session session,
                Row row)
Description copied from interface: Index
Add a row to the index.

Specified by:
add in interface Index
Parameters:
session - the session to use
row - the data

getRootPageId

public int getRootPageId()
Description copied from interface: Index
Get the root page of this index.

Specified by:
getRootPageId in interface Index
Returns:
the root page id

close

public void close(Session session)
Description copied from interface: Index
Close this index.

Specified by:
close in interface Index
Parameters:
session - the session used to write data

find

public Cursor find(Session session,
                   SearchRow first,
                   SearchRow last)
Description copied from interface: Index
Find a row or a list of rows and create a cursor to iterate over the result.

Specified by:
find in interface Index
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

findNext

public Cursor findNext(Session session,
                       SearchRow first,
                       SearchRow last)
Description copied from interface: Index
Find a row or a list of rows that is larger and create a cursor to iterate over the result.

Specified by:
findNext in interface Index
Parameters:
session - the session
first - the lower limit (excluding)
last - the last row, or null for no limit
Returns:
the cursor

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
Returns:
true if it can

canGetFirstOrLast

public boolean canGetFirstOrLast()
Description copied from interface: Index
Check if the index can directly look up the lowest or highest value of a column.

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

findFirstOrLast

public Cursor findFirstOrLast(Session session,
                              boolean first)
Description copied from interface: Index
Find the lowest or highest value of a column.

Specified by:
findFirstOrLast in interface Index
Parameters:
session - the session
first - true if the first (lowest for ascending indexes) or last value should be returned
Returns:
a cursor or null

getCost

public double getCost(Session session,
                      int[] masks)
Description copied from interface: Index
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.

Specified by:
getCost in interface Index
Parameters:
session - the session
masks - the search mask
Returns:
the estimated cost

needRebuild

public boolean needRebuild()
Description copied from interface: Index
Check if the index needs to be rebuilt. This method is called after opening an index.

Specified by:
needRebuild in interface Index
Returns:
true if a rebuild is required.

isUncommittedFromOtherSession

public boolean isUncommittedFromOtherSession(Session session,
                                             Row row)
Check if there is an uncommitted row with the given key within a different session.

Parameters:
session - the original session
row - the row (only the key is checked)
Returns:
true if there is an uncommitted row

remove

public void remove(Session session,
                   Row row)
Description copied from interface: Index
Remove a row from the index.

Specified by:
remove in interface Index
Parameters:
session - the session
row - the data

remove

public void remove(Session session)
Description copied from interface: Index
Remove the index.

Specified by:
remove in interface Index
Parameters:
session - the session

truncate

public void truncate(Session session)
Description copied from interface: Index
Remove all rows from the index.

Specified by:
truncate in interface Index
Parameters:
session - the session

commit

public void commit(int operation,
                   Row row)
Description copied from interface: Index
Commit the operation for a row. This is only important for multi-version indexes.

Specified by:
commit in interface Index
Parameters:
operation - the operation type
row - the row

compareKeys

public int compareKeys(SearchRow rowData,
                       SearchRow compare)
Description copied from interface: Index
Compare the positions of two rows.

Specified by:
compareKeys in interface Index
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

compareRows

public int compareRows(SearchRow rowData,
                       SearchRow compare)
Description copied from interface: Index
Compare two rows.

Specified by:
compareRows in interface Index
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

getColumnIndex

public int getColumnIndex(Column col)
Description copied from interface: Index
Get the index of a column in the list of index columns

Specified by:
getColumnIndex in interface Index
Parameters:
col - the column
Returns:
the index (0 meaning first column)

getColumnListSQL

public java.lang.String getColumnListSQL()
Description copied from interface: Index
Get the list of columns as a string.

Specified by:
getColumnListSQL in interface Index
Returns:
the list of columns

getColumns

public Column[] getColumns()
Description copied from interface: Index
Get the indexed columns.

Specified by:
getColumns in interface Index
Returns:
the columns

getIndexColumns

public IndexColumn[] getIndexColumns()
Description copied from interface: Index
Get the indexed columns as index columns (with ordering information).

Specified by:
getIndexColumns in interface Index
Returns:
the index columns

getCostRangeIndex

public long getCostRangeIndex(int[] masks,
                              long rowCount)
Description copied from interface: Index
Estimate the cost required to search one row, and then iterate over the given number of rows.

Specified by:
getCostRangeIndex in interface Index
Parameters:
masks - the search mask
rowCount - the row count
Returns:
the estimated cost

getCreateSQL

public java.lang.String getCreateSQL()
Description copied from interface: DbObject
Construct the original CREATE ... SQL statement for this object.

Specified by:
getCreateSQL in interface DbObject
Returns:
the SQL statement

getCreateSQLForCopy

public java.lang.String getCreateSQLForCopy(Table forTable,
                                            java.lang.String quotedName)
Description copied from interface: DbObject
Construct a CREATE ... SQL statement for this object when creating a copy of it.

Specified by:
getCreateSQLForCopy in interface DbObject
Parameters:
forTable - the new table
quotedName - the quoted name
Returns:
the SQL statement

getDropSQL

public java.lang.String getDropSQL()
Description copied from interface: DbObject
Construct a DROP ... SQL statement for this object.

Specified by:
getDropSQL in interface DbObject
Returns:
the SQL statement

getDuplicateKeyException

public DbException getDuplicateKeyException()
Description copied from interface: Index
Create a duplicate key exception with a message that contains the index name

Specified by:
getDuplicateKeyException in interface Index
Returns:
the exception

getIndexType

public IndexType getIndexType()
Description copied from interface: Index
Get the index type.

Specified by:
getIndexType in interface Index
Returns:
the index type

getLookupCost

public int getLookupCost(long rowCount)
Description copied from interface: Index
Estimate the cost required to search a number of rows.

Specified by:
getLookupCost in interface Index
Parameters:
rowCount - the row count
Returns:
the estimated cost

getPlanSQL

public java.lang.String getPlanSQL()
Description copied from interface: Index
Get the message to show in a EXPLAIN statement.

Specified by:
getPlanSQL in interface Index
Returns:
the plan

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

getTable

public Table getTable()
Description copied from interface: Index
Get the table on which this index is based.

Specified by:
getTable in interface Index
Returns:
the table

getType

public int getType()
Description copied from interface: DbObject
Get the object type.

Specified by:
getType in interface DbObject
Returns:
the object type

containsNullAndAllowMultipleNull

public boolean containsNullAndAllowMultipleNull(SearchRow newRow)
Description copied from interface: Index
Check if one of the columns is NULL and multiple rows with NULL are allowed using the current compatibility mode for unique indexes. Note: NULL behavior is complicated in SQL.

Specified by:
containsNullAndAllowMultipleNull in interface Index
Parameters:
newRow - the row to check
Returns:
true if one of the columns is null and multiple nulls in unique indexes are allowed

removeChildrenAndResources

public void removeChildrenAndResources(Session session)
Description copied from interface: DbObject
Delete all dependent children objects and resources of this object.

Specified by:
removeChildrenAndResources in interface DbObject
Parameters:
session - the session

getSQL

public java.lang.String getSQL()
Description copied from interface: DbObject
Get the SQL name of this object (may be quoted).

Specified by:
getSQL in interface DbObject
Returns:
the SQL name

getSchema

public Schema getSchema()
Description copied from interface: SchemaObject
Get the schema in which this object is defined

Specified by:
getSchema in interface SchemaObject
Returns:
the schema

checkRename

public void checkRename()
Description copied from interface: DbObject
Check if renaming is allowed. Does nothing when allowed.

Specified by:
checkRename in interface DbObject

getChildren

public java.util.ArrayList<DbObject> getChildren()
Description copied from interface: DbObject
Get the list of dependent children (for tables, this includes indexes and so on).

Specified by:
getChildren in interface DbObject
Returns:
the list of children

getComment

public java.lang.String getComment()
Description copied from interface: DbObject
Get the current comment of this object.

Specified by:
getComment in interface DbObject
Returns:
the comment, or null if not set

getDatabase

public Database getDatabase()
Description copied from interface: DbObject
Get the database.

Specified by:
getDatabase in interface DbObject
Returns:
the database

getId

public int getId()
Description copied from interface: DbObject
Get the unique object id.

Specified by:
getId in interface DbObject
Returns:
the object id

getModificationId

public long getModificationId()
Description copied from interface: DbObject
Get the last modification id.

Specified by:
getModificationId in interface DbObject
Returns:
the modification id

getName

public java.lang.String getName()
Description copied from interface: DbObject
Get the name.

Specified by:
getName in interface DbObject
Returns:
the name

isTemporary

public boolean isTemporary()
Description copied from interface: DbObject
Check if this object is temporary (for example, a temporary table).

Specified by:
isTemporary in interface DbObject
Returns:
true if is temporary

rename

public void rename(java.lang.String newName)
Description copied from interface: DbObject
Rename the object.

Specified by:
rename in interface DbObject
Parameters:
newName - the new name

setComment

public void setComment(java.lang.String comment)
Description copied from interface: DbObject
Change the comment of this object.

Specified by:
setComment in interface DbObject
Parameters:
comment - the new comment, or null for no comment

setModified

public void setModified()
Description copied from interface: DbObject
Tell the object that is was modified.

Specified by:
setModified in interface DbObject

setTemporary

public void setTemporary(boolean temporary)
Description copied from interface: DbObject
Tell this object that it is temporary or not.

Specified by:
setTemporary in interface DbObject
Parameters:
temporary - the new value

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

getBaseIndex

public Index getBaseIndex()

getRow

public Row getRow(Session session,
                  long key)
Description copied from interface: Index
Get the row with the given key.

Specified by:
getRow in interface Index
Parameters:
session - the session
key - the unique key
Returns:
the row

isHidden

public boolean isHidden()
Description copied from interface: 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.

Specified by:
isHidden in interface SchemaObject
Returns:
true if it is hidden