Class MemoryBasedCopyingRemoteIterator<T>
- java.lang.Object
-
- de.aristaflow.adept2.model.common.collection.MemoryBasedCopyingRemoteIterator<T>
-
- Type Parameters:
T
- The type of the elements that are iterated.
- All Implemented Interfaces:
RemoteIterator<List<T>>
,AutoCloseable
public class MemoryBasedCopyingRemoteIterator<T> extends Object implements RemoteIterator<List<T>>
A remote iterator based on a list of elements that are completely in memory. This is only useful for memory-based storages which are unable to provide a result partially.
Since all elements are copied when creating this iterator, it is a snapshot and always consistent.
-
-
Constructor Summary
Constructors Constructor Description MemoryBasedCopyingRemoteIterator(Collection<T> elements)
Creates a new caching remote iterator for the designated result set to be alive for the designated time.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
drop()
Explicitly drops this iterator which allows to release occupied memory and resources.List<T>
getAll()
Gets all elements of this iterator at once.List<T>
getNext(int count)
Gets the next specified amount of elements of this iterator.List<T>
getNext(int count, int start)
Gets the next specified amount of elements of this iterator starting at the designated index (0-based).List<T>
getPrevious(int count)
Gets the previous specified amount of elements of this iterator.long
getRemainingAliveTime()
Gets the time in milliseconds this iterator will still be valid and does allow access to the underlying data.boolean
isForwardOnly()
Gets whether this iterator only allows iterating in forward direction, that is calls toRemoteIterator.getNext(int)
but not toRemoteIterator.getNext(int, int)
andRemoteIterator.getPrevious(int)
.boolean
isSnapshot()
Gets whether this iterator is a snapshot and therefore its contents is not subject of change while iterating.long
keepAlive(long aliveTime)
Keeps this iterator alive for the designated time span (in milliseconds).-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.aristaflow.adept2.model.common.collection.RemoteIterator
close
-
-
-
-
Constructor Detail
-
MemoryBasedCopyingRemoteIterator
public MemoryBasedCopyingRemoteIterator(Collection<T> elements)
Creates a new caching remote iterator for the designated result set to be alive for the designated time. Note that this time is only for information purpose, the caller has to ensure to calldrop()
on the created iterator.- Parameters:
elements
- The elements of this iterator.
-
-
Method Detail
-
getNext
public List<T> getNext(int count) throws DataConsistencyException
Description copied from interface:RemoteIterator
Gets the next specified amount of elements of this iterator. The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.- Specified by:
getNext
in interfaceRemoteIterator<T>
- Parameters:
count
- The amount of elements to retrieve in forward direction. This has to be > 0.- Returns:
- The next n elements of this iterator, where n is the specified
count. In case of an insufficient amount of next elements remaining
only the available elements will be returned. If there are no more
next elements,
null
will be returned. - Throws:
DataConsistencyException
- If there are problems retrieving any of the next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyException
will be thrown.
-
getNext
public List<T> getNext(int count, int start) throws DataSourceException, DataConsistencyException
Description copied from interface:RemoteIterator
Gets the next specified amount of elements of this iterator starting at the designated index (0-based). The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.
Afterwards the cursor to the current element will be set to the index start + count, that is, getNext(n) will be the same as calling getNext(n, start + count).- Specified by:
getNext
in interfaceRemoteIterator<T>
- Parameters:
count
- The amount of elements to retrieve in forward direction. This has to be > 0.start
- The index of the element from which to retrieve the next count elements. 0 is the index of the first element. This has to be >= 0 and it must not be bigger than the amounts of elements in this iterator.- Returns:
- The next n elements of this iterator, where n is the specified
count. In case of an insufficient amount of next elements remaining
only the available elements will be returned. If there are no more
next elements,
null
will be returned. - Throws:
DataSourceException
- If there are problems accessing the underlying data source, aDataSourceException
will be thrown.DataConsistencyException
- If there are problems retrieving any of the next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyException
will be thrown.
-
getPrevious
public List<T> getPrevious(int count) throws DataConsistencyException
Description copied from interface:RemoteIterator
Gets the previous specified amount of elements of this iterator. The returned data will be consistent within the returned list (but not necessarily with respect to previous or later retrievals) due to the usage of synchronisation.- Specified by:
getPrevious
in interfaceRemoteIterator<T>
- Parameters:
count
- The amount of elements to retrieve in backward direction. This has to be > 0.- Returns:
- The previous n elements of this iterator, where n is the specified
count. The order corresponds to the one in the underlying data
structure, that is previous elements in the returned list are also
previous elements in the data structure. In case of an insufficient
amount of previous elements remaining only the available elements
will be returned. If there are no more previous elements,
null
will be returned. - Throws:
DataConsistencyException
- If there are problems retrieving any of the previous next elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more or if this iterator is forward only, aDataConsistencyException
will be thrown.
-
getAll
public List<T> getAll() throws DataConsistencyException
Description copied from interface:RemoteIterator
Gets all elements of this iterator at once. The returned data will be consistent (does not contain elements of different versions) due to the usage of synchronisation. Note that calling this method may take a lot of time.
Calling this method will reset the iterator before the first element if this iterator is not forward only. If this iterator is forward only and does not cache retrieved elements internally, this method will not work but throw aDataSourceException
since the already returned elements may not be retrieved any more.- Specified by:
getAll
in interfaceRemoteIterator<T>
- Returns:
- All elements of this iterator at once.
- Throws:
DataConsistencyException
- If there are problems retrieving any of the elements via the identifying attribute of the elements (for instance the primary key of a specific element identifying the next position) is not available any more, aDataConsistencyException
will be thrown.
-
isSnapshot
public boolean isSnapshot()
Description copied from interface:RemoteIterator
Gets whether this iterator is a snapshot and therefore its contents is not subject of change while iterating. However, the contents may be outdated while iterating.- Specified by:
isSnapshot
in interfaceRemoteIterator<T>
- Returns:
- Whether this iterator is a snapshot providing stable but maybe outdated data.
-
isForwardOnly
public boolean isForwardOnly()
Description copied from interface:RemoteIterator
Gets whether this iterator only allows iterating in forward direction, that is calls toRemoteIterator.getNext(int)
but not toRemoteIterator.getNext(int, int)
andRemoteIterator.getPrevious(int)
. This can be restricted by the underlying data structure, for instance aResultSet
.- Specified by:
isForwardOnly
in interfaceRemoteIterator<T>
- Returns:
- Whether this iterator only allows iterating in forward direction.
-
getRemainingAliveTime
public long getRemainingAliveTime()
Description copied from interface:RemoteIterator
Gets the time in milliseconds this iterator will still be valid and does allow access to the underlying data. Afterwards the iterator will bedropped
automatically to save memory and resources.
A negative value indicates a dropped iterator. Such an iterator cannot bekept alive
. Note that this alive time does not need to correspond to the original alive time requested- Specified by:
getRemainingAliveTime
in interfaceRemoteIterator<T>
- Returns:
- The time this iterator remains valid and can be used. Afterwards the iterator will be
dropped
.Long.MIN_VALUE
for an already dropped remote iterator.
-
keepAlive
public long keepAlive(long aliveTime)
Description copied from interface:RemoteIterator
Keeps this iterator alive for the designated time span (in milliseconds). This allows to continue using this iterator. However, note that extending the usage time significantly impacts resource occupation.
Note that extending the time need not be successful. The underlying iterator implementation may not be able to extend the alive time of all used resource, for instance pooled database connections.A 0 or a negative value is equal to
dropping
this iterator.- Specified by:
keepAlive
in interfaceRemoteIterator<T>
- Parameters:
aliveTime
- The time in milliseconds to keep this iterator alive that is not to drop it automatically.- Returns:
- The relative time this iterator will be kept alive longer. 0
indicates immediate dropping. A negative value represents the
failure of extending the alive time, check
RemoteIterator.getRemainingAliveTime()
in this case whether extending the keep alive time has been successful or not.
-
drop
public void drop()
Description copied from interface:RemoteIterator
Explicitly drops this iterator which allows to release occupied memory and resources. Dropping will be done implicitly after the alive time has elapsed.
This method may be called several times. Implementors have to take care of this.- Specified by:
drop
in interfaceRemoteIterator<T>
-
-