JTemporal Home
Tutorial

net.sf.jtemporal.spi
Class LazyTemporalAttributeStorage<V>

java.lang.Object
  extended by net.sf.jtemporal.spi.LazyTemporalAttributeStorage<V>
Type Parameters:
V - the type of the value
All Implemented Interfaces:
TemporalAttributeStorage

public class LazyTemporalAttributeStorage<V>
extends java.lang.Object
implements TemporalAttributeStorage

This storage is based on a cache whose size grows lazily, until all the underlying data is in the cache. This caching strategy is appropriate for applications handling primarily recent data, which is the case of most applications

It loads lazily the data between the positive infinity and the requested instant. If subsequently you ask an instant that precedes the previous request, it loads then the gap between both requested instants. This continues until the whole history is in cache.
Unlike usual caching APIs, these methods intentionally return void, in order to give you full freedom in implementing prefetching strategies, where on a call you decide to load more data than requested, possibly in other instances of LazyTemporalAttributeStorage.
Warning: the methods with no instant or period as a parameter usually cause the entire underlying data to be loaded. For example, isEmpty() will load all the underlying data.

Version:
$Id: LazyTemporalAttributeStorage.java,v 1.17 2007/09/14 20:03:11 tabeck Exp $

Nested Class Summary
static interface LazyTemporalAttributeStorage.ChangeHandler<V>
          Applies the changes on the underlying data source.
static interface LazyTemporalAttributeStorage.MissingRangeHandler<V>
          Loads the data from the real physical storage into the the LazyTemporalAttributeStorage cache, when additional data is requested.
 
Constructor Summary
LazyTemporalAttributeStorage(LazyTemporalAttributeStorage.MissingRangeHandler<V> loader)
          Instantiates a read only LazyTemporalAttributeStorage.
LazyTemporalAttributeStorage(LazyTemporalAttributeStorage.MissingRangeHandler<V> loader, LazyTemporalAttributeStorage.ChangeHandler<V> writer)
          Instantiates a read-write LazyTemporalAttributeStorage.
 
Method Summary
 void beginAtomicOperation()
          Tells the storage that the JTemporal client is calling a method that can result in multiples updates in the storage.
 void clear()
          Removes all the stored mappings.
 void clearCache()
           
 boolean containsValue(java.lang.Object value)
          Returns true if this map maps one or more Periods to the specified value.
 boolean containsValue(java.lang.Object value, Period range)
          Returns true if this map maps one or more Periods to the specified value, during the specified period.
 void endAtomicOperation()
          Tells the storage that an atomic operation has finished.
 CloseableIterator entryIterator()
          Iterates over all the entries of the storage.
 CloseableIterator entryIterator(Period p)
          Iterates over all the entries of the storage that intersects the period P.
 Period firstPeriod()
          Returns the first (lowest) period currently in this storage.
 Period firstPeriod(Period p)
          Returns the first (lowest) period currently in this storage, that intersects the given period.
 TimedObject getEntry(Instant instant)
          Returns the entry valid at the given instant, if any is found.
 TimedObject getEntryEndingAt(Instant instant)
          Returns the entry finishing exactly at the given instant, if any is found.
 TimedObject getEntryStartingAt(Instant instant)
          Returns the entry starting exactly at the given instant, if any is found.
 Period getPeriod(Instant instant)
          Returns the period that encloses the given instant, if any is found.
 java.lang.Object getValue(Instant instant)
          Returns the value to which this map maps the specified Instant.
 boolean isEmpty()
          Returns true if this storage does not contain objects.
 boolean isEmpty(Period p)
          Returns true if this storage does not contain objects intersecting the given period.
 Period lastPeriod()
          Returns the last (highest) period currently in this storage.
 Period lastPeriod(Period p)
          Returns the last (highest) period currently in this storage, that intersects the given period.
 CloseableIterator periodIterator()
          Iterates over all the periods of the storage.
 CloseableIterator periodIterator(Period p)
          Iterates over all the periods of the storage that intersects the period P.
 void populated(Period p)
          Called by the implementation of LazyTemporalAttributeStorage.MissingRangeHandler.populateRange(LazyTemporalAttributeStorage, Period) to to tell which Period (or additional Period) can now considered as complete in the cache.
 void put(Period p, java.lang.Object value)
          Stores a mapping (entry, tuple, row) composed by period and value.
 void putInCache(Period p, V o)
          Called by the implementation of LazyTemporalAttributeStorage.MissingRangeHandler.populateRange(LazyTemporalAttributeStorage, Period) to add entries in the cache.
 void removeEntry(Period p)
          Removes an entry composed by period and value previously stored.
 int removeRange(Period range)
          Removes all the entries whose period indersects the given period.
 int size()
          Returns the number of Period-value mappings in this map.
 int size(Period p)
          Returns the number of Period-value mappings in this map, for the given priod.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LazyTemporalAttributeStorage

public LazyTemporalAttributeStorage(LazyTemporalAttributeStorage.MissingRangeHandler<V> loader)
Instantiates a read only LazyTemporalAttributeStorage.

Parameters:
loader -

LazyTemporalAttributeStorage

public LazyTemporalAttributeStorage(LazyTemporalAttributeStorage.MissingRangeHandler<V> loader,
                                    LazyTemporalAttributeStorage.ChangeHandler<V> writer)
Instantiates a read-write LazyTemporalAttributeStorage.

Parameters:
loader -
Method Detail

clearCache

public void clearCache()

putInCache

public void putInCache(Period p,
                       V o)
Called by the implementation of LazyTemporalAttributeStorage.MissingRangeHandler.populateRange(LazyTemporalAttributeStorage, Period) to add entries in the cache.
The semantics is the same as put(Period, Object), except that the entries are loaded in the cache only.

Parameters:
p - period with which the specified value is associated. Cannot be null.
o - value to be associated with the specified key. Cannot be null.
See Also:
put(Period, Object)

populated

public void populated(Period p)
Called by the implementation of LazyTemporalAttributeStorage.MissingRangeHandler.populateRange(LazyTemporalAttributeStorage, Period) to to tell which Period (or additional Period) can now considered as complete in the cache.


lastPeriod

public Period lastPeriod()
Description copied from interface: TemporalAttributeStorage
Returns the last (highest) period currently in this storage.

Specified by:
lastPeriod in interface TemporalAttributeStorage
Returns:
the last (highest) period currently in this storage.

lastPeriod

public Period lastPeriod(Period p)
Description copied from interface: TemporalAttributeStorage
Returns the last (highest) period currently in this storage, that intersects the given period.
Note the the returned period may be larger than p.

Specified by:
lastPeriod in interface TemporalAttributeStorage
Parameters:
p - the intersecting period
Returns:
the last (highest) period currently in this storage.

firstPeriod

public Period firstPeriod()
Description copied from interface: TemporalAttributeStorage
Returns the first (lowest) period currently in this storage.

Specified by:
firstPeriod in interface TemporalAttributeStorage
Returns:
the first (lowest) period currently in this storage.

firstPeriod

public Period firstPeriod(Period p)
Description copied from interface: TemporalAttributeStorage
Returns the first (lowest) period currently in this storage, that intersects the given period.
Note the the returned period may be larger than p.

Specified by:
firstPeriod in interface TemporalAttributeStorage
Parameters:
p - the intersecting period
Returns:
the first (lowest) period currently in this storage.

isEmpty

public boolean isEmpty()
Description copied from interface: TemporalAttributeStorage
Returns true if this storage does not contain objects.

Specified by:
isEmpty in interface TemporalAttributeStorage
Returns:
true if this storage does not contain objects..

isEmpty

public boolean isEmpty(Period p)
Description copied from interface: TemporalAttributeStorage
Returns true if this storage does not contain objects intersecting the given period.

Specified by:
isEmpty in interface TemporalAttributeStorage
Parameters:
p - the intersecting period
Returns:
true if this storage does not contain objects intersecting the given period.

containsValue

public boolean containsValue(java.lang.Object value)
Description copied from interface: TemporalAttributeStorage
Returns true if this map maps one or more Periods to the specified value.

Specified by:
containsValue in interface TemporalAttributeStorage
Parameters:
value - value whose presence in this map is to be tested.
Returns:
true if this map maps one or more keys to the specified value.

containsValue

public boolean containsValue(java.lang.Object value,
                             Period range)
Description copied from interface: TemporalAttributeStorage
Returns true if this map maps one or more Periods to the specified value, during the specified period.

Specified by:
containsValue in interface TemporalAttributeStorage
Parameters:
value - value whose presence in this map is to be tested.
range - the period intersecting the value mappings
Returns:
true if this map maps one or more keys to the specified value.

getValue

public java.lang.Object getValue(Instant instant)
Description copied from interface: TemporalAttributeStorage
Returns the value to which this map maps the specified Instant. Returns null if the map contains no mapping at this Instant. It is not possible to map to a null Instant.

Specified by:
getValue in interface TemporalAttributeStorage
Parameters:
instant - instant whose associated value is to be returned
Returns:
the value to which this map maps the specified instant. returns null if this map does not contain a mapping for the specified instant.

getPeriod

public Period getPeriod(Instant instant)
Description copied from interface: TemporalAttributeStorage
Returns the period that encloses the given instant, if any is found.

Specified by:
getPeriod in interface TemporalAttributeStorage
Parameters:
instant - instant whose associated value is to be returned
Returns:
the value to which this map maps the specified instant. returns null if this map does not contain a mapping for the specified instant.

getEntry

public TimedObject getEntry(Instant instant)
Description copied from interface: TemporalAttributeStorage
Returns the entry valid at the given instant, if any is found.

Specified by:
getEntry in interface TemporalAttributeStorage
Parameters:
instant - instant whose associated value is to be returned
Returns:
the period and value to which this map maps the specified instant. returns null if this map does not contain a mapping for the specified instant.

getEntryEndingAt

public TimedObject getEntryEndingAt(Instant instant)
Description copied from interface: TemporalAttributeStorage
Returns the entry finishing exactly at the given instant, if any is found.

Specified by:
getEntryEndingAt in interface TemporalAttributeStorage
Parameters:
instant - the instant the equals the entry end
Returns:
the entry ending exactly at the given instant, or null.

getEntryStartingAt

public TimedObject getEntryStartingAt(Instant instant)
Description copied from interface: TemporalAttributeStorage
Returns the entry starting exactly at the given instant, if any is found.

Specified by:
getEntryStartingAt in interface TemporalAttributeStorage
Parameters:
instant - the instant the equals the entry end
Returns:
the entry starting exactly at the given instant, or null.

beginAtomicOperation

public void beginAtomicOperation()
Description copied from interface: TemporalAttributeStorage
Tells the storage that the JTemporal client is calling a method that can result in multiples updates in the storage.
This is not exactly a transaction.
If the storage supports transactions, this method suggests to perform all the following operations inside the same transaction.
For example, this method is called by TemporalAttributeImpl at the beginning of the put(Period, Object) method, then TemporalAttributeImpl calls some times the put and remove methods, then finally it calls TemporalAttributeStorage.endAtomicOperation().
If your storage supports nested transactions, this is the inner one. Non persistent implemetations just return with no action.
Must not throw any exception (including unchecked exceptions).

Specified by:
beginAtomicOperation in interface TemporalAttributeStorage
See Also:
TemporalAttributeStorage.endAtomicOperation()

endAtomicOperation

public void endAtomicOperation()
Description copied from interface: TemporalAttributeStorage
Tells the storage that an atomic operation has finished. If the storage supports transactions, it should commit if a transaction has been started by TemporalAttributeStorage.beginAtomicOperation().
Non persistent implemetations just return with no action.
Must not throw any exception (including unchecked exceptions).

Specified by:
endAtomicOperation in interface TemporalAttributeStorage
See Also:
TemporalAttributeStorage.beginAtomicOperation()

clear

public void clear()
Description copied from interface: TemporalAttributeStorage
Removes all the stored mappings.

Specified by:
clear in interface TemporalAttributeStorage

size

public int size()
Description copied from interface: TemporalAttributeStorage
Returns the number of Period-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface TemporalAttributeStorage
Returns:
the number of Instant-value mappings in this map.

size

public int size(Period p)
Description copied from interface: TemporalAttributeStorage
Returns the number of Period-value mappings in this map, for the given priod. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface TemporalAttributeStorage
Parameters:
p - the intersecting period
Returns:
the number of Instant-value mappings in this map.

put

public void put(Period p,
                java.lang.Object value)
Description copied from interface: TemporalAttributeStorage
Stores a mapping (entry, tuple, row) composed by period and value.
No particular check or rich semantics is expected, this method just stores.

Specified by:
put in interface TemporalAttributeStorage
Parameters:
p - period with which the specified value is associated. Cannot be null.
value - value to be associated with the specified key. Cannot be null.

removeEntry

public void removeEntry(Period p)
Description copied from interface: TemporalAttributeStorage
Removes an entry composed by period and value previously stored.

Specified by:
removeEntry in interface TemporalAttributeStorage
Parameters:
p - period whose mapping is to be removed from the storage. It must exactly match the key of an existing entry.

removeRange

public int removeRange(Period range)
Description copied from interface: TemporalAttributeStorage
Removes all the entries whose period indersects the given period.

Specified by:
removeRange in interface TemporalAttributeStorage
Parameters:
range - the period specifying that range whose interecting entries are to be deleted.
Returns:
the number of deleted entries (or Integer.MAX_VALUE if bigger than that).

entryIterator

public CloseableIterator entryIterator()
Description copied from interface: TemporalAttributeStorage
Iterates over all the entries of the storage.
The instances are sorted by Period.equals(Object)

Specified by:
entryIterator in interface TemporalAttributeStorage
Returns:
instances of TimedObject
See Also:
TimedObject

entryIterator

public CloseableIterator entryIterator(Period p)
Description copied from interface: TemporalAttributeStorage
Iterates over all the entries of the storage that intersects the period P.
Note that the CloseableIterator#remove() method, removes the whole entry, even if the entry has a period lager then the period p.
The instances are sorted by Period.equals(Object)

Specified by:
entryIterator in interface TemporalAttributeStorage
Parameters:
p - the intersecting period.
Returns:
instances of TimedObject
See Also:
TimedObject

periodIterator

public CloseableIterator periodIterator()
Description copied from interface: TemporalAttributeStorage
Iterates over all the periods of the storage.
The instances are sorted by Period.equals(Object)

Specified by:
periodIterator in interface TemporalAttributeStorage
Returns:
instances of Period
See Also:
Period

periodIterator

public CloseableIterator periodIterator(Period p)
Description copied from interface: TemporalAttributeStorage
Iterates over all the periods of the storage that intersects the period P.
Note that the CloseableIterator#remove() method, removes the whole entry, even if the entry has a period lager then the period p.
The instances are sorted by Period.equals(Object)

Specified by:
periodIterator in interface TemporalAttributeStorage
Parameters:
p - the intersecting period.
Returns:
instances of Period
See Also:
Period

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

JTemporal Home
Tutorial