public abstract class AbstractContextStreamView<T extends AbstractStreamContext> extends Object implements IStreamView, AutoCloseable
This implementation uses "contexts" to properly deal with copy-on-write streams. Every time a stream is copied, a new context is created which redirects requests to the source stream for the copy - each context contains its own queue and pointers. Implementers of fillReadQueue() and readAndUpdatePointers should be careful to use the id of the context, rather than that of the stream view itself.
Created by mwei on 1/6/17.
IStreamView.SearchDirection
Constructor and Description |
---|
AbstractContextStreamView(CorfuRuntime runtime,
UUID id,
BiFunction<UUID,Long,T> contextFactory)
Create a new abstract context stream view.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
protected boolean |
doesEntryUpdateContext(ILogData data)
Check whether the given entry updates the context.
|
protected T |
getCurrentContext()
Get the current context.
|
protected abstract boolean |
getHasNext(T context)
Return whether calling getNextEntry() may return more
entries, given the context.
|
protected List<ILogData> |
getNextEntries(T context,
long maxGlobal,
Function<ILogData,Boolean> contextCheckFn)
Retrieve the next entries in the stream, given the context.
|
protected abstract ILogData |
getNextEntry(T context,
long maxGlobal)
Retrieve the next entry in the stream, given the context.
|
boolean |
hasNext()
Returns whether or not there are potentially more entries in this
stream - this function may return true even if there are no entries
remaining, as addresses may have been acquired by other clients
but not written yet, or the addresses were hole-filled, or just failed.
|
ILogData |
nextUpTo(long maxGlobal)
Retrieve the next entry from this stream, up to the address given or the
tail of the stream.
|
protected void |
popContext() |
protected boolean |
processEntryForContext(ILogData data)
Check if the given entry adds a new context, and update
the global pointer.
|
protected void |
pushNewContext(UUID id,
long maxGlobal)
Add a new context.
|
List<ILogData> |
remainingUpTo(long maxGlobal)
Retrieve all of the entries from this stream, up to the address given or
the tail of the stream.
|
void |
reset()
Reset the state of this stream view, causing the next read to
start from the beginning of this stream.
|
void |
seek(long globalAddress)
Seek to the requested maxGlobal address.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
append, append, current, find, gc, getCurrentGlobalPosition, getId, getTotalUpdates, next, previous, remaining, spliterator, spliteratorUpTo, stream, streamUpTo
forEachRemaining, remove
public AbstractContextStreamView(CorfuRuntime runtime, UUID id, BiFunction<UUID,Long,T> contextFactory)
runtime
- The runtime.id
- The id of the stream.contextFactory
- A function which generates a context,
given the stream id and a maximum global
address.public void reset()
reset
in interface IStreamView
public void seek(long globalAddress)
seek
in interface IStreamView
globalAddress
- Address to seek topublic void close()
close
in interface AutoCloseable
public final ILogData nextUpTo(long maxGlobal)
nextUpTo
in interface IStreamView
maxGlobal
- The maximum global address to read up to.public final List<ILogData> remainingUpTo(long maxGlobal)
remainingUpTo
in interface IStreamView
maxGlobal
- The maximum global address to read up to.public boolean hasNext()
hasNext
in interface Iterator<ILogData>
hasNext
in interface IStreamView
protected abstract boolean getHasNext(T context)
context
- The context to retrieve the next entry from.protected abstract ILogData getNextEntry(T context, long maxGlobal)
context
- The context to retrieve the next entry from.maxGlobal
- The maximum global address to read to.protected List<ILogData> getNextEntries(T context, long maxGlobal, Function<ILogData,Boolean> contextCheckFn)
This function is designed to implement a bulk read. In a bulk read, one of the entries may cause the context to change - the implementation should check if the entry changes the context and stop reading if this occurs, returning the entry that caused contextCheckFn to return true.
The default implementation simply calls getNextEntry.
context
- The context to retrieve the next entry from.maxGlobal
- The maximum global address to read to.contextCheckFn
- A function which returns true if the entry changes the
stream context.protected boolean doesEntryUpdateContext(ILogData data)
data
- The entry to check.protected boolean processEntryForContext(ILogData data)
If it does, add it to the context stack. Otherwise, pop the context.
It is important that this method be called in order, since it updates the global pointer and can change the global pointer.
data
- The entry to process.protected T getCurrentContext()
Should never throw a NoSuchElement exception because streamContexts should always at least have one element.
protected void pushNewContext(UUID id, long maxGlobal)
protected void popContext()
Copyright © 2019 CorfuDB. All rights reserved.