Class CachingRemoteIterator<T>
- java.lang.Object
-
- de.aristaflow.adept2.model.common.collection.AbstractRemoteIterator<T,T>
-
- de.aristaflow.adept2.model.common.collection.AbstractTimeTrackingRemoteIterator<T>
-
- de.aristaflow.adept2.model.common.collection.CachingRemoteIterator<T>
-
- Type Parameters:
T
- The type of the elements that are retrieved from the cached iterator.
- All Implemented Interfaces:
RemoteIterator<List<T>>
,de.aristaflow.adept2.model.communication.ServerSideProxy
,de.aristaflow.adept2.model.communication.TimeTrackingSsProxy
,Closeable
,AutoCloseable
public class CachingRemoteIterator<T> extends AbstractTimeTrackingRemoteIterator<T>
This iterator implements a cache for the underlying iterator, that is, when iterating viaAbstractRemoteIterator.getNext(int)
, all retrieved elements will be cached. Previous elements will always be retrieved from the cache. Note that apositioned next
will add enough elements to the cache to fulfil the request. As soon as all elements have been retrieved, the wrapped remote iterator will be closed.
It has an expiring alive time thus allowing to release the occupied memory even if no explicitclose()
occurs. This alive time is kept in sync with the underlying iterator until all elements have been retrieved (and are thus cached).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
CachingRemoteIterator.CachingRemoteIteratorCloseTask
The clean-up task for closing the wrappedRemoteIterator
.
-
Field Summary
Fields Modifier and Type Field Description protected int
current
The reference to the current element which is to be retrieved next.protected List<T>
elements
The elements that have been retrieved from the underlying remote iterator so far.protected int
last
The index of last element in case it has been reached in the result set, -1 otherwise.protected int
lastCached
The index of the last element that is in the cache.protected RemoteIterator<List<T>>
wrappedIterator
The wrapped iterator of which all elements will be cached.-
Fields inherited from class de.aristaflow.adept2.model.common.collection.AbstractTimeTrackingRemoteIterator
busyThreads, snapshot
-
Fields inherited from class de.aristaflow.adept2.model.common.collection.AbstractRemoteIterator
cleanup, closeables, logger, transformator
-
-
Constructor Summary
Constructors Constructor Description CachingRemoteIterator(RemoteIterator<List<T>> wrappedIterator)
Deprecated, for removal: This API element is subject to removal in a future version.CachingRemoteIterator(RemoteIterator<List<T>> wrappedIterator, long aliveTime, long aliveTimeExtension, Logger logger)
Creates a new caching remote iterator for the designated iterator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Explicitly closes this iterator which allows to release occupied memory and resources.protected void
closeWrappedDse()
closeWrappedException(boolean)
keeping this iterator alive and wrappingIOException
in aDataSourceException
or unwrapping aDataSourceException
from anIOException
if appropriate.protected void
closeWrappedIoe(boolean keepAlive)
closeWrappedException(boolean)
closing this iterator implicitly and wrapping aDataSourceException
in aIOException
or unwrapping aIOException
from anDataSourceException
if appropriate.protected Long
exit()
Tracks exiting a method reentrant which is removing an invocation of the current thread to properly setAbstractTimeTrackingRemoteIterator.isBusy()
.protected List<T>
getAllImpl()
ImplementsAbstractRemoteIterator.getAll()
without the need to consider result transformation, closeable and alive time tracking.protected List<T>
getNextImpl(int count)
ImplementsAbstractRemoteIterator.getNext(int)
without the need to consider result transformation, closeable and alive time tracking.protected List<T>
getNextImpl(int count, int start)
ImplementsAbstractRemoteIterator.getNext(int, int)
without the need to consider result transformation, closeable and alive time tracking.protected List<T>
getPreviousImpl(int count)
ImplementsAbstractRemoteIterator.getPrevious(int)
without the need to consider result transformation, closeable and alive time tracking.long
getRemainingAliveTime()
Gets the time in milliseconds this iterator will still be valid and does allow access to the underlying data.protected boolean
getSnapshotImpl()
Gets whether this iterator is a snapshot and therefore its contents is not subject of change while iterating.protected boolean
isClosed()
Gets whether this remote iterator has been closed and its data is not available any more.long
keepAlive(long aliveTime)
Keeps this iterator alive for the designated time span (in milliseconds).-
Methods inherited from class de.aristaflow.adept2.model.common.collection.AbstractTimeTrackingRemoteIterator
enter, getCloseTime, isBusy, isForwardOnly, isSnapshot, setCloseTime
-
Methods inherited from class de.aristaflow.adept2.model.common.collection.AbstractRemoteIterator
aboutToReturn, closeOnTraversal, getAll, getNext, getNext, getPrevious, setCloseOnTraversal
-
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
drop
-
-
-
-
Field Detail
-
wrappedIterator
protected RemoteIterator<List<T>> wrappedIterator
The wrapped iterator of which all elements will be cached. Note that the current cursor need not correspond to the current cursor of the underlying iterator. This will benull
when the wrapped iterator has been closed.
-
elements
protected List<T> elements
The elements that have been retrieved from the underlying remote iterator so far.null
indicates that this caching remote iterator has been closed.
-
lastCached
protected int lastCached
The index of the last element that is in the cache. This corresponds to the current index of the wrapped iterator.
-
last
protected int last
The index of last element in case it has been reached in the result set, -1 otherwise.
-
current
protected int current
The reference to the current element which is to be retrieved next.
-
-
Constructor Detail
-
CachingRemoteIterator
@Deprecated(since="15.0.0", forRemoval=true) public CachingRemoteIterator(RemoteIterator<List<T>> wrappedIterator)
Deprecated, for removal: This API element is subject to removal in a future version.Creates a new caching remote iterator for the designated iterator sending no close notification for the wrapped iterator.- Parameters:
wrappedIterator
- The iterator of which the elements should be cached as soon as they are iterated (withAbstractRemoteIterator.getNext(int)
).
-
CachingRemoteIterator
public CachingRemoteIterator(RemoteIterator<List<T>> wrappedIterator, long aliveTime, long aliveTimeExtension, Logger logger)
Creates a new caching remote iterator for the designated iterator.- Parameters:
wrappedIterator
- The iterator of which the elements should be cached as soon as they are iterated (withAbstractRemoteIterator.getNext(int)
).aliveTime
- The time in milliseconds this remote iterator is alive.aliveTimeExtension
- The time in milliseconds to extend the alive time when accessing this remote iterator.logger
- The logger for logging exceptions from the wrapped remote iterator that cannot be forwarded or are irrelevant.
-
-
Method Detail
-
exit
protected Long exit()
Tracks exiting a method reentrant which is removing an invocation of the current thread to properly setAbstractTimeTrackingRemoteIterator.isBusy()
. If the thread leaves completely, the alive time will be extended; it will not be shortened in case the extension is less than the already set close time. If this remote iterator has been closed, no extension will take place. As long as the wrapped remote iterator is alive, we need to keep it alive since we may need to retrieve data that is not in the cache yet. Otherwise going to previous elements will not extend the alive time of the wrapped iterator but extend the alive time of the caching remote iterator.- Overrides:
exit
in classAbstractTimeTrackingRemoteIterator<T>
- Returns:
- The possibly new remaining alive time in milliseconds or
null
in case the thread is still active and not exiting finally.
-
isClosed
protected boolean isClosed()
Description copied from class:AbstractRemoteIterator
Gets whether this remote iterator has been closed and its data is not available any more.- Specified by:
isClosed
in classAbstractRemoteIterator<T,T>
- Returns:
- Whether this remote iterator has been closed and its data is not available any more.
-
getNextImpl
protected List<T> getNextImpl(int count) throws DataSourceException, DataConsistencyException
Description copied from class:AbstractRemoteIterator
ImplementsAbstractRemoteIterator.getNext(int)
without the need to consider result transformation, closeable and alive time tracking.- Specified by:
getNextImpl
in classAbstractRemoteIterator<T,T>
- Throws:
DataSourceException
DataConsistencyException
-
getNextImpl
protected List<T> getNextImpl(int count, int start) throws DataSourceException, DataConsistencyException
Description copied from class:AbstractRemoteIterator
ImplementsAbstractRemoteIterator.getNext(int, int)
without the need to consider result transformation, closeable and alive time tracking.- Specified by:
getNextImpl
in classAbstractRemoteIterator<T,T>
- Throws:
DataSourceException
DataConsistencyException
-
getPreviousImpl
protected List<T> getPreviousImpl(int count) throws DataSourceException, DataConsistencyException
Description copied from class:AbstractRemoteIterator
ImplementsAbstractRemoteIterator.getPrevious(int)
without the need to consider result transformation, closeable and alive time tracking.- Specified by:
getPreviousImpl
in classAbstractRemoteIterator<T,T>
- Throws:
DataSourceException
DataConsistencyException
-
getAllImpl
protected List<T> getAllImpl() throws DataSourceException, DataConsistencyException
Description copied from class:AbstractRemoteIterator
ImplementsAbstractRemoteIterator.getAll()
without the need to consider result transformation, closeable and alive time tracking.- Specified by:
getAllImpl
in classAbstractRemoteIterator<T,T>
- Throws:
DataSourceException
DataConsistencyException
-
getSnapshotImpl
protected boolean getSnapshotImpl() throws DataSourceException
Description copied from class:AbstractTimeTrackingRemoteIterator
Gets whether this iterator is a snapshot and therefore its contents is not subject of change while iterating.
This method will need to be overridden if the snapshot status cannot be provided when creating this instance (AbstractTimeTrackingRemoteIterator(boolean, boolean, long, long, Logger)
).This implementation always throws an
UnsupportedOperationException
.- Overrides:
getSnapshotImpl
in classAbstractTimeTrackingRemoteIterator<T>
- Returns:
- Whether this iterator is a snapshot and therefore its contents is not subject of change while iterating.
- Throws:
DataSourceException
- If there are problems retrieving the snapshot status of this remote iterator, aDataSourceException
will be thrown.
-
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 beclosed
automatically to save memory and resources.
A negative value indicates a closed 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>
- Specified by:
getRemainingAliveTime
in interfacede.aristaflow.adept2.model.communication.ServerSideProxy
- Specified by:
getRemainingAliveTime
in interfacede.aristaflow.adept2.model.communication.TimeTrackingSsProxy
- Overrides:
getRemainingAliveTime
in classAbstractTimeTrackingRemoteIterator<T>
- Returns:
- The time this iterator remains valid and can be used. Afterwards the iterator will be
closed
.Long.MIN_VALUE
for an already closed remote iterator.
-
keepAlive
public long keepAlive(long aliveTime) throws IOException
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
closed
this iterator.- Specified by:
keepAlive
in interfaceRemoteIterator<T>
- Specified by:
keepAlive
in interfacede.aristaflow.adept2.model.communication.ServerSideProxy
- Specified by:
keepAlive
in interfacede.aristaflow.adept2.model.communication.TimeTrackingSsProxy
- Overrides:
keepAlive
in classAbstractTimeTrackingRemoteIterator<T>
- Parameters:
aliveTime
- The time in milliseconds to keep this iterator alive that is not to close it automatically.- Returns:
- The relative time this iterator will be kept alive longer. 0 indicates immediate
closing. 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. - Throws:
IOException
- If there are problems accessing the underlying data source, anIOException
will be thrown.
-
close
public void close() throws IOException
Description copied from interface:RemoteIterator
Explicitly closes this iterator which allows to release occupied memory and resources. Closing 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceRemoteIterator<T>
- Specified by:
close
in interfacede.aristaflow.adept2.model.communication.ServerSideProxy
- Overrides:
close
in classAbstractRemoteIterator<T,T>
- Throws:
IOException
- If there are problems accessing the underlying data source, anIOException
will be thrown.
-
closeWrappedIoe
protected void closeWrappedIoe(boolean keepAlive) throws IOException
closeWrappedException(boolean)
closing this iterator implicitly and wrapping aDataSourceException
in aIOException
or unwrapping aIOException
from anDataSourceException
if appropriate.- Parameters:
keepAlive
- Whether to keep this caching remote iterator alive despite the wrapped iterator being closed.- Throws:
IOException
- If there are problems accessing the underlying data source, anIOException
will be thrown.
-
closeWrappedDse
protected void closeWrappedDse() throws DataSourceException
closeWrappedException(boolean)
keeping this iterator alive and wrappingIOException
in aDataSourceException
or unwrapping aDataSourceException
from anIOException
if appropriate.- Throws:
DataSourceException
- If there are problems accessing the underlying data source, aDataSourceException
will be thrown.
-
-