K - object key typeI - index key type. The type of the indexed columnV - object typepublic abstract class Index<K,I,V> extends DBObject
Represents an index into this database. Indexes increases searching performance considerably.
You can define an index for an object store, using three different strategies:
1) Through @DatabaseDef.IndexDef annotation os yor Database interface.
@DatabaseDef(name="CruxCompanyDatabase", version=1 objectStores={@DatabaseDef.ObjectStoreDef(targetClass=Person.class, indexes={@DatabaseDef.IndexDef(name="myIndex", keyPath={"name"}, unique=true)}) } ) public interface CompanyDatabase extends Database{ }
2) Through @DatabaseDef.IndexDef annotation on your Store object.
@Store(value=Person.STORE_NAME, indexes={@DatabaseDef.IndexDef(name="myIndex", keyPath={"name", "age"}, unique=true)}) public class Person { public static final String STORE_NAME = "Person"; private Integer id; private String name; private int age;@Store.Keypublic Integer getId(){return id;} public void setId(Integer id){this.id = id;} public String getName(){return name;} public void setName(String name){this.name = name;} public int getAge(){return age;} public void setAge(int age){this.age = age;} }
3) Through @Store.Indexed annotation on the property of your Store object.
@Store(Person.STORE_NAME)) public class Person { public static final String STORE_NAME = "Person"; private Integer id; private String name; private int age;@Store.Keypublic Integer getId(){return id;} public void setId(Integer id){this.id = id;}@Store.Indexed(unique=true) public String getName(){return name;} public void setName(String name){this.name = name;} public int getAge(){return age;} public void setAge(int age){this.age = age;} }
Once defined, you can access the index to perform faster search operations:
Transaction transaction = database.getTransaction(new String[]{Person.STORE_NAME}, Mode.readOnly);
ObjectStore<Integer, Person> personStore = transaction.getObjectStore(Person.STORE_NAME);
Index<Integer, String, Person> nameIndex = personStore.getIndex("name");
nameIndex.get(35, new DatabaseRetrieveCallback<Person>() {
@Override
public void onSuccess(Person person){
// ...
}
});
| Modifier | Constructor and Description |
|---|---|
protected |
Index(AbstractDatabase db)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
abstract void |
count(DatabaseCountCallback callback)
Return the number of items referenced by the index.
|
abstract void |
count(KeyRange<I> range,
DatabaseCountCallback callback)
Return the number of items referenced by the index in the given range.
|
abstract void |
get(I key,
DatabaseRetrieveCallback<V> callback)
Retrieve the object associated with the given key from the index.
|
abstract void |
get(KeyRange<I> keyRange,
DatabaseRetrieveCallback<V> callback)
Retrieve the object in the given keyRange from the index.
|
abstract void |
getKey(I key,
DatabaseRetrieveCallback<K> callback)
Retrieve the object key associated with the given key from the index.
|
abstract void |
getKey(KeyRange<I> keyRange,
DatabaseRetrieveCallback<K> callback)
Retrieve the object key in the given keyRange from the index.
|
abstract KeyRangeFactory<I> |
getKeyRangeFactory()
Creates a factory for
KeyRange objects used by this index. |
abstract String |
getName()
Retrieve the index name
|
abstract boolean |
isMultiEntry()
This flag affects how the index behaves when the result of evaluating the index's key path yields an Array.
|
abstract boolean |
isUnique()
Retrieve true if each key inside the index must be unique.
|
abstract void |
openCursor(DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.
|
abstract void |
openCursor(KeyRange<I> keyRange,
Cursor.CursorDirection direction,
DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.
|
abstract void |
openCursor(KeyRange<I> keyRange,
DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.
|
abstract void |
openKeyCursor(DatabaseCursorCallback<I,K> callback)
Open a cursor to iterate over the objects keys into the store.
|
abstract void |
openKeyCursor(KeyRange<I> keyRange,
Cursor.CursorDirection direction,
DatabaseCursorCallback<I,K> callback)
Open a cursor to iterate over the objects keys into the store.
|
abstract void |
openKeyCursor(KeyRange<I> keyRange,
DatabaseCursorCallback<I,K> callback)
Open a cursor to iterate over the objects keys into the store.
|
reportErrorprotected Index(AbstractDatabase db)
db - databasepublic abstract String getName()
public abstract boolean isUnique()
public abstract boolean isMultiEntry()
public abstract void count(DatabaseCountCallback callback)
callback - called when the operation completepublic abstract void count(KeyRange<I> range, DatabaseCountCallback callback)
keyRange - an object specifying the indexed property range to countcallback - called when the operation completepublic abstract void get(I key, DatabaseRetrieveCallback<V> callback)
DatabaseRetrieveCallback.key - index keycallback - called when the operation completepublic abstract void get(KeyRange<I> keyRange, DatabaseRetrieveCallback<V> callback)
DatabaseRetrieveCallback.keyRange - an object specifying the indexed property range. The first item from the range will be retrieved.callback - called when the operation completepublic abstract void getKey(I key, DatabaseRetrieveCallback<K> callback)
DatabaseRetrieveCallback.key - index keycallback - called when the operation completepublic abstract void getKey(KeyRange<I> keyRange, DatabaseRetrieveCallback<K> callback)
DatabaseRetrieveCallback.keyRange - an object specifying the indexed property range. The first item from the range will be retrieved.callback - called when the operation completepublic abstract void openCursor(DatabaseCursorCallback<I,V> callback)
callback - called when the operation completepublic abstract void openCursor(KeyRange<I> keyRange, DatabaseCursorCallback<I,V> callback)
keyRange - an object specifying the indexed property range, used to filter the query.callback - called when the operation completepublic abstract void openCursor(KeyRange<I> keyRange, Cursor.CursorDirection direction, DatabaseCursorCallback<I,V> callback)
keyRange - an object specifying the indexed property range, used to filter the query.direction - specifies the cursor iteration directioncallback - called when the operation completepublic abstract void openKeyCursor(DatabaseCursorCallback<I,K> callback)
callback - called when the operation completepublic abstract void openKeyCursor(KeyRange<I> keyRange, DatabaseCursorCallback<I,K> callback)
keyRange - an object specifying the indexed property range, used to filter the query.callback - called when the operation completepublic abstract void openKeyCursor(KeyRange<I> keyRange, Cursor.CursorDirection direction, DatabaseCursorCallback<I,K> callback)
keyRange - an object specifying the indexed property range, used to filter the query.direction - specifies the cursor iteration directioncallback - called when the operation completepublic abstract KeyRangeFactory<I> getKeyRangeFactory()
KeyRange objects used by this index.Copyright © 2015. All rights reserved.