org.cruxframework.crux.core.client.db
Class Index<K,I,V>

java.lang.Object
  extended by org.cruxframework.crux.core.client.db.DBObject
      extended by org.cruxframework.crux.core.client.db.Index<K,I,V>
Type Parameters:
K - object key type
I - index key type. The type of the indexed column
V - object type
Direct Known Subclasses:
IDXIndex, WSQLIndex

public 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.Key
    public 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.Key
    public 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){
      // ...
    }
 });
 

Author:
Thiago da Rosa de Bustamante

Field Summary
 
Fields inherited from class org.cruxframework.crux.core.client.db.DBObject
db, logger
 
Constructor Summary
protected Index(AbstractDatabase db)
          Constructor
 
Method Summary
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.
 
Methods inherited from class org.cruxframework.crux.core.client.db.DBObject
reportError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Index

protected Index(AbstractDatabase db)
Constructor

Parameters:
db - database
Method Detail

getName

public abstract String getName()
Retrieve the index name

Returns:
index name

isUnique

public abstract boolean isUnique()
Retrieve true if each key inside the index must be unique. If an unique index exists for an object store, any add or put operation that violates this index constraints will fail.

Returns:
true if index is unique

isMultiEntry

public abstract boolean isMultiEntry()
This flag affects how the index behaves when the result of evaluating the index's key path yields an Array. If the multiEntry flag is false, then a single record whose key is an Array is added to the index. If the multiEntry flag is true, then the one record is added to the index for each item in the Array.

Returns:
true if this is a multi entry index.

count

public abstract void count(DatabaseCountCallback callback)
Return the number of items referenced by the index.

Parameters:
callback - called when the operation complete

count

public abstract void count(KeyRange<I> range,
                           DatabaseCountCallback callback)
Return the number of items referenced by the index in the given range.

Parameters:
keyRange - an object specifying the indexed property range to count
callback - called when the operation complete

get

public abstract void get(I key,
                         DatabaseRetrieveCallback<V> callback)
Retrieve the object associated with the given key from the index. To read the object, use the method onSuccess from DatabaseRetrieveCallback.

Parameters:
key - index key
callback - called when the operation complete

get

public abstract void get(KeyRange<I> keyRange,
                         DatabaseRetrieveCallback<V> callback)
Retrieve the object in the given keyRange from the index. To read the object, use the method onSuccess from DatabaseRetrieveCallback.

Parameters:
keyRange - an object specifying the indexed property range. The first item from the range will be retrieved.
callback - called when the operation complete

getKey

public abstract void getKey(I key,
                            DatabaseRetrieveCallback<K> callback)
Retrieve the object key associated with the given key from the index. To read the object, use the method onSuccess from DatabaseRetrieveCallback.

Parameters:
key - index key
callback - called when the operation complete

getKey

public abstract void getKey(KeyRange<I> keyRange,
                            DatabaseRetrieveCallback<K> callback)
Retrieve the object key in the given keyRange from the index. To read the object, use the method onSuccess from DatabaseRetrieveCallback.

Parameters:
keyRange - an object specifying the indexed property range. The first item from the range will be retrieved.
callback - called when the operation complete

openCursor

public abstract void openCursor(DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.

Parameters:
callback - called when the operation complete

openCursor

public abstract void openCursor(KeyRange<I> keyRange,
                                DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.

Parameters:
keyRange - an object specifying the indexed property range, used to filter the query.
callback - called when the operation complete

openCursor

public abstract void openCursor(KeyRange<I> keyRange,
                                Cursor.CursorDirection direction,
                                DatabaseCursorCallback<I,V> callback)
Open a cursor to iterate over the object store.

Parameters:
keyRange - an object specifying the indexed property range, used to filter the query.
direction - specifies the cursor iteration direction
callback - called when the operation complete

openKeyCursor

public abstract void openKeyCursor(DatabaseCursorCallback<I,K> callback)
Open a cursor to iterate over the objects keys into the store.

Parameters:
callback - called when the operation complete

openKeyCursor

public abstract void openKeyCursor(KeyRange<I> keyRange,
                                   DatabaseCursorCallback<I,K> callback)
Open a cursor to iterate over the objects keys into the store.

Parameters:
keyRange - an object specifying the indexed property range, used to filter the query.
callback - called when the operation complete

openKeyCursor

public 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.

Parameters:
keyRange - an object specifying the indexed property range, used to filter the query.
direction - specifies the cursor iteration direction
callback - called when the operation complete

getKeyRangeFactory

public abstract KeyRangeFactory<I> getKeyRangeFactory()
Creates a factory for KeyRange objects used by this index.

Returns:
the factory


Copyright © 2014. All rights reserved.