messif.buckets.index.impl
Class LongStorageMemoryIndex<K,T>

java.lang.Object
  extended by messif.utility.SortedArrayData<K,LongStorageMemoryIndex.KeyAddressPair<K>>
      extended by messif.buckets.index.impl.LongStorageMemoryIndex<K,T>
Type Parameters:
K - the type of keys this index is ordered by
T - the type of objects stored in this collection
All Implemented Interfaces:
java.io.Serializable, Addible<T>, Index<T>, ModifiableIndex<T>, ModifiableOrderedIndex<K,T>, OrderedIndex<K,T>

public class LongStorageMemoryIndex<K,T>
extends SortedArrayData<K,LongStorageMemoryIndex.KeyAddressPair<K>>
implements ModifiableOrderedIndex<K,T>, java.io.Serializable

Implementation of disk (long) index that stores the indexed data in a sorted array and keeps the keys to be compared always in memory.

All search methods are correctly implemented using binary search on the array whenever possible.

See Also:
Serialized Form

Nested Class Summary
protected static class LongStorageMemoryIndex.KeyAddressPair<K>
          Class encapsulating the key and long position in the storage.
 
Constructor Summary
LongStorageMemoryIndex(DiskStorage<T> storage, IndexComparator<K,T> comparator)
          Creates a new instance of LongStorageMemoryIndex for the specified storage.
 
Method Summary
 boolean add(T object)
          Adds the specified object to this instance.
 IndexComparator<K,T> comparator()
          Returns the comparator that defines order of this index.
protected  int compare(K key, LongStorageMemoryIndex.KeyAddressPair<K> object)
          Compares its two arguments for order.
 void destroy()
          Destroy this index.
 void finalize()
          Finalize this index.
protected  LongStorageMemoryIndex.KeyAddressPair<K> get(int i)
          Returns the element at the specified position in this collection.
protected  int insertionPoint(K key)
          Searches for the point where to insert the object object.
protected  Lock lock()
          Locks this index and returns a lock object if it is supported.
protected  boolean remove(int i)
          Removes the element at the specified position in this collection - from both index and storage.
 void reorderStorage(java.io.File newFile)
          Switches this index to a new storage in which the data are ordered according the this index's current order.
 StorageSearch<T> search()
          Returns a search for all objects in this index.
 StorageSearch<T> search(java.util.Collection<? extends K> keys)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
<C> StorageSearch<T>
search(IndexComparator<? super C,? super T> comparator, C key)
          Returns a search for objects in this index that have any of the specified keys.
<C> StorageSearch<T>
search(IndexComparator<? super C,? super T> comparator, C from, C to)
          Returns a search for objects in this index that are within the specified key-range.
<C> StorageSearch<T>
search(IndexComparator<? super C,? super T> comparator, java.util.Collection<? extends C> keys)
          Returns a search for objects in this index that have any of the specified keys.
 StorageSearch<T> search(K key, boolean restrictEqual)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 StorageSearch<T> search(K from, K to)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 StorageSearch<T> search(K startKey, K from, K to)
          Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index.
 int size()
          Returns the number of elements in this collection.
 
Methods inherited from class messif.utility.SortedArrayData
binarySearch, first, fullSearch, indexOf, last, mergeSort
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LongStorageMemoryIndex

public LongStorageMemoryIndex(DiskStorage<T> storage,
                              IndexComparator<K,T> comparator)
Creates a new instance of LongStorageMemoryIndex for the specified storage.

Parameters:
storage - the storage to associate with this index
comparator - the comparator imposing natural order of this index
Method Detail

finalize

public void finalize()
              throws java.lang.Throwable
Description copied from interface: ModifiableIndex
Finalize this index. All transient resources associated with this index are released. After this method is called, the store and retrieval methods' behavior is unpredictable.

Specified by:
finalize in interface ModifiableIndex<T>
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable - if there was an error while cleaning

destroy

public void destroy()
             throws java.lang.Throwable
Description copied from interface: ModifiableIndex
Destroy this index. This method releases all resources (transient and persistent) associated with this index. After this method is called, the store and retrieval methods' behavior is unpredictable.

Specified by:
destroy in interface ModifiableIndex<T>
Throws:
java.lang.Throwable - if there was an error while cleaning

comparator

public IndexComparator<K,T> comparator()
Description copied from interface: OrderedIndex
Returns the comparator that defines order of this index.

Specified by:
comparator in interface OrderedIndex<K,T>
Returns:
the comparator that defines order of this index

compare

protected int compare(K key,
                      LongStorageMemoryIndex.KeyAddressPair<K> object)
               throws java.lang.ClassCastException
Description copied from class: SortedArrayData
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Specified by:
compare in class SortedArrayData<K,LongStorageMemoryIndex.KeyAddressPair<K>>
Parameters:
key - the key to indexCompare
object - the object to be compared
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
java.lang.ClassCastException - if the arguments' types prevent them from being compared by this comparator.

reorderStorage

public void reorderStorage(java.io.File newFile)
                    throws java.io.IOException,
                           BucketStorageException
Switches this index to a new storage in which the data are ordered according the this index's current order.

Parameters:
newFile - the file where the new storage is created
Throws:
java.io.IOException - if there was a problem writing the the new storage
BucketStorageException - if there was a problem reading objects from the old storage or writing them to the new one

size

public int size()
Description copied from class: SortedArrayData
Returns the number of elements in this collection.

Specified by:
size in interface Index<T>
Specified by:
size in class SortedArrayData<K,LongStorageMemoryIndex.KeyAddressPair<K>>
Returns:
the number of elements in this collection

insertionPoint

protected int insertionPoint(K key)
                      throws BucketStorageException
Searches for the point where to insert the object object.

Parameters:
key - key of the object to be inserted
Returns:
the point in the array where to put the object
Throws:
BucketStorageException - if there was a problem determining the point

add

public boolean add(T object)
            throws BucketStorageException
Description copied from interface: Addible
Adds the specified object to this instance.

Specified by:
add in interface Addible<T>
Parameters:
object - the object to be added
Returns:
true if the addition was successful
Throws:
BucketStorageException - if there was an error adding the object

remove

protected boolean remove(int i)
Removes the element at the specified position in this collection - from both index and storage.

Parameters:
i - index of the element to remove
Returns:
false if the object was not removed (e.g. because there is no object with this index)

get

protected LongStorageMemoryIndex.KeyAddressPair<K> get(int i)
                                                throws java.lang.IndexOutOfBoundsException,
                                                       java.lang.IllegalStateException
Description copied from class: SortedArrayData
Returns the element at the specified position in this collection.

Specified by:
get in class SortedArrayData<K,LongStorageMemoryIndex.KeyAddressPair<K>>
Parameters:
i - index of the element to return
Returns:
the element at the specified position in this collection
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
java.lang.IllegalStateException - if the object at position index cannot be accessed

lock

protected Lock lock()
Locks this index and returns a lock object if it is supported. A null is returned otherwise. The called must call the Lock.unlock() method if this method has returned non-null.

Returns:
a lock on this index or null

search

public StorageSearch<T> search()
                        throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for all objects in this index.

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Returns:
a search for all objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public StorageSearch<T> search(K key,
                               boolean restrictEqual)
                        throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. If the restrictEqual is true, the search returns only objects that are comparator-equal to key.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
key - the key to search for
restrictEqual - if true, the search is restricted only to objects that are comparator-equal to key
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public StorageSearch<T> search(K from,
                               K to)
                        throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects from the interval [from, to] are returned. If a null value is specified as a boundary, that bound is not restricted. That means that search(x, null) will return all objects from this index that are bigger than or equal to x.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public StorageSearch<T> search(K startKey,
                               K from,
                               K to)
                        throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects from the interval [from, to] are returned. Search starts at the object nearest to the given startKey. If a null value is specified as a boundary, that bound is not restricted. That means that search(start, x, null) will return all objects from this index that are bigger than or equal to x starting at object with key start.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
startKey - the key from which to start the search
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public StorageSearch<T> search(java.util.Collection<? extends K> keys)
                        throws java.lang.IllegalStateException
Description copied from interface: OrderedIndex
Returns a search for objects in this index using the internal OrderedIndex.comparator() of this index. All objects that are comparator-equal to any of the given keys are returned.

Objects are returned in the order defined by this index.

Specified by:
search in interface ModifiableOrderedIndex<K,T>
Specified by:
search in interface OrderedIndex<K,T>
Parameters:
keys - the keys to search for
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public <C> StorageSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                   C key)
                        throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that have any of the specified keys. The equality is checked exclusively by using the specified comparator, thus key need not necessarily be of the same class as the objects stored in this index and also consistency with equals is not required.

Note that objects are not necessarily returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type of the key used by the search
Parameters:
comparator - compares the key with the stored objects
key - the key to search for
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public <C> StorageSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                   C from,
                                   C to)
                        throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that are within the specified key-range. The key boundaries [from, to] need not necessarily be of the same class as the objects stored in this index, however, the comparator must be able to compare the boundaries and the internal objects.

Note that objects are not returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type the boundaries used by the search
Parameters:
comparator - compares the boundaries [from, to] with the stored objects
from - the lower bound on the searched objects, i.e. objects greater or equal are returned
to - the upper bound on the searched objects, i.e. objects smaller or equal are returned
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index

search

public <C> StorageSearch<T> search(IndexComparator<? super C,? super T> comparator,
                                   java.util.Collection<? extends C> keys)
                        throws java.lang.IllegalStateException
Description copied from interface: Index
Returns a search for objects in this index that have any of the specified keys. The equality is checked exclusively by using the specified comparator, thus key need not necessarily be of the same class as the objects stored in this index and also consistency with equals is not required.

Note that objects are not necessarily returned in the order defined by the comparator

Specified by:
search in interface Index<T>
Specified by:
search in interface ModifiableIndex<T>
Type Parameters:
C - the type of the keys used by the search
Parameters:
comparator - compares the keys with the stored objects
keys - the keys to search for (at least one key must be given)
Returns:
a search for objects in this index
Throws:
java.lang.IllegalStateException - if there was an error initializing the search on this index