#include <ByteInputStream.h>
Inheritance diagram for ByteInputStream:
Public Member Functions | |
uint | readBytes (void *pData, uint cbRequested) |
Reads bytes from the stream. | |
template<class T> | |
uint | readValue (T &value) |
Reads a fixed-size value from the stream. | |
PConstBuffer | getReadPointer (uint cbRequested, uint *pcbActual=NULL) |
Copyless alternative for reading bytes from the stream. | |
void | consumeReadPointer (uint cbUsed) |
Advances stream position after a call to getReadPointer. | |
void | seekForward (uint cb) |
Skips forward in stream. | |
void | seekBackward (uint cb) |
Skips backward in stream. | |
virtual SharedByteStreamMarker | newMarker () |
Creates a new uninitialized marker for this stream. | |
virtual void | mark (ByteStreamMarker &marker) |
Marks the current stream position in preparation for a future call to reset(). | |
virtual void | reset (ByteStreamMarker const &marker) |
Resets stream to a previously marked position. | |
FileSize | getOffset () const |
| |
bool | isClosed () const |
| |
void | close () |
Closes this object, releasing any unallocated resources. | |
Protected Member Functions | |
ByteInputStream () | |
virtual void | readNextBuffer ()=0 |
Must be implemented by derived class by calling either setBuffer or nullifyBuffer. | |
virtual void | readPrevBuffer () |
Must be implemented by derived class if seekBackward is to be supported. | |
void | setBuffer (PConstBuffer pBuffer, uint cbBuffer) |
Sets the current buffer to be read. | |
void | nullifyBuffer () |
Nullifies the current buffer, indicating no more data is available. | |
uint | getBytesAvailable () const |
| |
uint | getBytesConsumed () const |
| |
virtual void | closeImpl ()=0 |
Must be implemented by derived class to release any resources. | |
Protected Attributes | |
FileSize | cbOffset |
Byte position in stream. | |
bool | needsClose |
Private Attributes | |
PConstBuffer | pFirstByte |
First buffered byte of data. | |
PConstBuffer | pNextByte |
Next buffered byte of data. | |
PConstBuffer | pEndByte |
End of buffer (one past last byte of data in buffer). |
Definition at line 34 of file ByteInputStream.h.
ByteInputStream::ByteInputStream | ( | ) | [explicit, protected] |
Definition at line 29 of file ByteInputStream.cpp.
References nullifyBuffer().
00030 { 00031 nullifyBuffer(); 00032 }
virtual void ByteInputStream::readNextBuffer | ( | ) | [protected, pure virtual] |
Must be implemented by derived class by calling either setBuffer or nullifyBuffer.
Implemented in ByteArrayInputStream, and SegInputStream.
Referenced by getReadPointer(), and readBytes().
void ByteInputStream::readPrevBuffer | ( | ) | [protected, virtual] |
Must be implemented by derived class if seekBackward is to be supported.
Reimplemented in ByteArrayInputStream, and SegInputStream.
Definition at line 66 of file ByteInputStream.cpp.
Referenced by seekBackward().
void ByteInputStream::setBuffer | ( | PConstBuffer | pBuffer, | |
uint | cbBuffer | |||
) | [inline, protected] |
Sets the current buffer to be read.
pBuffer | receives start address of new buffer | |
cbBuffer | number of bytes in buffer |
Definition at line 241 of file ByteInputStream.h.
References pEndByte, pFirstByte, and pNextByte.
Referenced by ByteArrayInputStream::ByteArrayInputStream(), SegInputStream::lockBuffer(), CrcSegInputStream::lockBufferParanoid(), nullifyBuffer(), and ByteArrayInputStream::resetArray().
00244 { 00245 pFirstByte = pBuffer; 00246 pEndByte = pBuffer + cbBuffer; 00247 pNextByte = pFirstByte; 00248 }
void ByteInputStream::nullifyBuffer | ( | ) | [inline, protected] |
Nullifies the current buffer, indicating no more data is available.
Definition at line 250 of file ByteInputStream.h.
References setBuffer().
Referenced by ByteInputStream(), CrcSegInputStream::lockBuffer(), SegInputStream::readNextBuffer(), ByteArrayInputStream::readNextBuffer(), SegInputStream::readPrevBuffer(), and ByteArrayInputStream::readPrevBuffer().
00251 { 00252 setBuffer(NULL,0); 00253 }
uint ByteInputStream::getBytesAvailable | ( | ) | const [inline, protected] |
Definition at line 199 of file ByteInputStream.h.
References pEndByte, and pNextByte.
Referenced by consumeReadPointer(), getReadPointer(), readBytes(), and SegInputStream::seekSegPos().
uint ByteInputStream::getBytesConsumed | ( | ) | const [inline, protected] |
Definition at line 204 of file ByteInputStream.h.
References pFirstByte, and pNextByte.
Referenced by SegInputStream::getSegPos(), and seekBackward().
00205 { 00206 return static_cast<uint>(pNextByte - pFirstByte); 00207 }
Reads bytes from the stream.
pData | target buffer to read into | |
cbRequested | number of bytes to read |
Definition at line 34 of file ByteInputStream.cpp.
References ByteStream::cbOffset, getBytesAvailable(), pEndByte, pNextByte, and readNextBuffer().
Referenced by FtrsTableWriter::redoLogicalAction(), and seekForward().
00036 { 00037 uint cbActual = 0; 00038 if (pNextByte == pEndByte) { 00039 readNextBuffer(); 00040 } 00041 for (;;) { 00042 uint cbAvailable = getBytesAvailable(); 00043 if (!cbAvailable) { 00044 break; 00045 } 00046 if (cbRequested <= cbAvailable) { 00047 if (pData) { 00048 memcpy(pData,pNextByte,cbRequested); 00049 } 00050 pNextByte += cbRequested; 00051 cbActual += cbRequested; 00052 break; 00053 } 00054 if (pData) { 00055 memcpy(pData,pNextByte,cbAvailable); 00056 pData = static_cast<char *>(pData) + cbAvailable; 00057 } 00058 cbRequested -= cbAvailable; 00059 cbActual += cbAvailable; 00060 readNextBuffer(); 00061 } 00062 cbOffset += cbActual; 00063 return cbActual; 00064 }
uint ByteInputStream::readValue | ( | T & | value | ) | [inline] |
Reads a fixed-size value from the stream.
value | value to write; type must be memcpy-safe |
Definition at line 112 of file ByteInputStream.h.
Referenced by FtrsTableWriterFactory::loadIndex(), LogicalTxnTest::loadParticipant(), FtrsTableWriterFactory::loadParticipant(), BTreeRecoveryFactory::loadParticipant(), TupleProjection::readPersistent(), TupleDescriptor::readPersistent(), LogicalTxnTest::redoLogicalAction(), DatabaseTest::redoLogicalAction(), LogicalTxnTest::undoLogicalAction(), and DatabaseTest::undoLogicalAction().
00113 { 00114 return readBytes(&value,sizeof(value)); 00115 }
PConstBuffer ByteInputStream::getReadPointer | ( | uint | cbRequested, | |
uint * | pcbActual = NULL | |||
) | [inline] |
Copyless alternative for reading bytes from the stream.
Provides direct access to the stream's internal buffer, but doesn't move the stream position (see consumeReadPointer).
Note that it is in general dangerous to assume that getReadPointer will be able to access desired data items contiguously. For example, a stream created by calling ByteOutputStream::write is likely to have data items split across buffers. The assumption MAY be valid for streams created by calling ByteOutputStream::getWritePointer with matching values for cbRequested; it depends on the stream implementation.
cbRequested | number of contiguous bytes to access; if a non-zero number of bytes are currently available in the buffer, cbRequested must be no greater than the number of available bytes (otherwise an assertion violation will result) | |
pcbActual | if non-NULL, receives actual number of contiguous bytes available, which will always be greater than or equal to cbRequested except 0 for end-of-stream |
Definition at line 209 of file ByteInputStream.h.
References getBytesAvailable(), pEndByte, pNextByte, and readNextBuffer().
Referenced by BTreeWriter::deleteLogged(), BTreeWriter::insertLogged(), and BTreeBuildLevel::processInput().
00211 { 00212 if (getBytesAvailable() < cbRequested) { 00213 assert(pNextByte == pEndByte); 00214 readNextBuffer(); 00215 if (pNextByte == pEndByte) { 00216 if (pcbActual) { 00217 *pcbActual = 0; 00218 } 00219 return NULL; 00220 } 00221 } 00222 if (pcbActual) { 00223 *pcbActual = getBytesAvailable(); 00224 } 00225 return pNextByte; 00226 }
void ByteInputStream::consumeReadPointer | ( | uint | cbUsed | ) | [inline] |
Advances stream position after a call to getReadPointer.
cbUsed | number of bytes to advance; must be less than or equal to the value of cbActual returned by the last call to getReadPointer |
Definition at line 234 of file ByteInputStream.h.
References ByteStream::cbOffset, getBytesAvailable(), and pNextByte.
Referenced by BTreeWriter::deleteLogged(), BTreeWriter::insertLogged(), BTreeBuildLevel::processInput(), and SegInputStream::seekSegPos().
00235 { 00236 assert(cbUsed <= getBytesAvailable()); 00237 pNextByte += cbUsed; 00238 cbOffset += cbUsed; 00239 }
void ByteInputStream::seekForward | ( | uint | cb | ) | [inline] |
Skips forward in stream.
cb | number of bytes to advance |
Definition at line 228 of file ByteInputStream.h.
References readBytes().
Referenced by reset().
void ByteInputStream::seekBackward | ( | uint | cb | ) |
Skips backward in stream.
Not all stream implementations support this behavior.
cb | number of bytes backward to seek |
Definition at line 71 of file ByteInputStream.cpp.
References ByteStream::cbOffset, getBytesConsumed(), pEndByte, pFirstByte, pNextByte, and readPrevBuffer().
Referenced by reset().
00072 { 00073 assert(cb <= cbOffset); 00074 cbOffset -= cb; 00075 if (pNextByte == pFirstByte) { 00076 readPrevBuffer(); 00077 pNextByte = pEndByte; 00078 } 00079 for (;;) { 00080 uint cbAvailable = getBytesConsumed(); 00081 assert(cbAvailable); 00082 if (cb <= cbAvailable) { 00083 pNextByte -= cb; 00084 break; 00085 } 00086 cb -= cbAvailable; 00087 readPrevBuffer(); 00088 pNextByte = pEndByte; 00089 } 00090 }
SharedByteStreamMarker ByteInputStream::newMarker | ( | ) | [virtual] |
Creates a new uninitialized marker for this stream.
The returned marker must be passed to mark() in order to initialize it.
Reimplemented in SegInputStream.
Definition at line 92 of file ByteInputStream.cpp.
00093 { 00094 return SharedByteStreamMarker(new SequentialByteStreamMarker(*this)); 00095 }
void ByteInputStream::mark | ( | ByteStreamMarker & | marker | ) | [virtual] |
Marks the current stream position in preparation for a future call to reset().
How long this marker remains valid depends upon the implementation of the ByteInputStream.
marker | memento object created with newMarker() on the same stream; receives the marked position (forgetting any previously marked position) |
Reimplemented in SegInputStream.
Definition at line 97 of file ByteInputStream.cpp.
References SequentialByteStreamMarker::cbOffset, ByteStream::getOffset(), and ByteStreamMarker::getStream().
00098 { 00099 assert(&(marker.getStream()) == this); 00100 00101 SequentialByteStreamMarker &seqMarker = 00102 dynamic_cast<SequentialByteStreamMarker &>(marker); 00103 seqMarker.cbOffset = getOffset(); 00104 }
void ByteInputStream::reset | ( | ByteStreamMarker const & | marker | ) | [virtual] |
Resets stream to a previously marked position.
The base implementation uses seekForward/seekBackward (i.e. sequential access), making it inefficient for large streams. Derived classes may override with more efficient implementations such as random access.
marker | memento previously memorized with mark() |
Reimplemented in SegInputStream.
Definition at line 106 of file ByteInputStream.cpp.
References ByteStream::cbOffset, SequentialByteStreamMarker::cbOffset, ByteStreamMarker::getStream(), isMAXU(), seekBackward(), and seekForward().
00107 { 00108 assert(&(marker.getStream()) == this); 00109 00110 SequentialByteStreamMarker const &seqMarker = 00111 dynamic_cast<SequentialByteStreamMarker const &>(marker); 00112 assert(!isMAXU(seqMarker.cbOffset)); 00113 if (cbOffset == seqMarker.cbOffset) { 00114 // expedite common case where stream has not moved since mark 00115 return; 00116 } else if (cbOffset > seqMarker.cbOffset) { 00117 seekBackward(cbOffset - seqMarker.cbOffset); 00118 } else { 00119 seekForward(seqMarker.cbOffset - cbOffset); 00120 } 00121 }
FileSize ByteStream::getOffset | ( | ) | const [inline, inherited] |
Definition at line 110 of file ByteStream.h.
References ByteStream::cbOffset.
Referenced by SpillOutputStream::getInputStream(), and mark().
00111 { 00112 return cbOffset; 00113 }
virtual void ClosableObject::closeImpl | ( | ) | [protected, pure virtual, inherited] |
Must be implemented by derived class to release any resources.
Implemented in CacheImpl< PageT, VictimPolicyT >, ByteArrayInputStream, ByteArrayOutputStream, ByteOutputStream, CheckpointThread, Database, BarrierExecStream, DoubleBufferExecStream, ExecStream, ExecStreamGraphImpl, MockResourceExecStream, ReshapeExecStream, ScratchBufferExecStream, SegBufferExecStream, SegBufferReader, SegBufferReaderExecStream, SegBufferWriter, SegBufferWriterExecStream, JavaSinkExecStream, JavaTransformExecStream, FlatFileBuffer, FlatFileExecStreamImpl, BTreeExecStream, BTreeInsertExecStream, BTreePrefetchSearchExecStream, BTreeReadExecStream, BTreeSearchExecStream, FtrsTableWriterExecStream, LhxAggExecStream, LhxJoinExecStream, LbmBitOpExecStream, LbmChopperExecStream, LbmGeneratorExecStream, LbmIntersectExecStream, LbmMinusExecStream, LbmSplicerExecStream, LbmUnionExecStream, LcsClusterAppendExecStream, LcsRowScanBaseExecStream, LcsRowScanExecStream, DelegatingSegment, DynamicDelegatingSegment, ScratchSegment, SegInputStream, Segment, SegOutputStream, SegPageBackupRestoreDevice, SegStream, SegStreamAllocation, SpillOutputStream, and ExternalSortExecStreamImpl.
Referenced by ClosableObject::close().
bool ClosableObject::isClosed | ( | ) | const [inline, inherited] |
Definition at line 58 of file ClosableObject.h.
00059 { 00060 return !needsClose; 00061 }
void ClosableObject::close | ( | ) | [inherited] |
Closes this object, releasing any unallocated resources.
Reimplemented in CollectExecStream, CorrelationJoinExecStream, LcsClusterAppendExecStream, and LcsClusterReplaceExecStream.
Definition at line 39 of file ClosableObject.cpp.
References ClosableObject::closeImpl(), and ClosableObject::needsClose.
Referenced by CacheImpl< PageT, VictimPolicyT >::allocatePages(), LcsRowScanBaseExecStream::closeImpl(), ExecStreamGraphImpl::closeImpl(), FlatFileBuffer::open(), ClosableObjectDestructor::operator()(), and Segment::~Segment().
00040 { 00041 if (!needsClose) { 00042 return; 00043 } 00044 needsClose = false; 00045 closeImpl(); 00046 }
PConstBuffer ByteInputStream::pFirstByte [private] |
First buffered byte of data.
Definition at line 39 of file ByteInputStream.h.
Referenced by getBytesConsumed(), SegInputStream::lockBuffer(), CrcSegInputStream::lockBufferParanoid(), seekBackward(), and setBuffer().
PConstBuffer ByteInputStream::pNextByte [private] |
Next buffered byte of data.
Definition at line 44 of file ByteInputStream.h.
Referenced by consumeReadPointer(), getBytesAvailable(), getBytesConsumed(), getReadPointer(), readBytes(), seekBackward(), and setBuffer().
PConstBuffer ByteInputStream::pEndByte [private] |
End of buffer (one past last byte of data in buffer).
Definition at line 49 of file ByteInputStream.h.
Referenced by getBytesAvailable(), getReadPointer(), readBytes(), seekBackward(), and setBuffer().
FileSize ByteStream::cbOffset [protected, inherited] |
Byte position in stream.
Definition at line 41 of file ByteStream.h.
Referenced by ByteStream::ByteStream(), consumeReadPointer(), ByteOutputStream::consumeWritePointer(), ByteStream::getOffset(), SegOutputStream::getSegPos(), SegInputStream::getSegPos(), readBytes(), reset(), ByteArrayInputStream::resetArray(), seekBackward(), SegInputStream::seekSegPos(), and ByteOutputStream::writeBytes().
bool ClosableObject::needsClose [protected, inherited] |
Definition at line 44 of file ClosableObject.h.
Referenced by SegStreamAllocation::beginWrite(), ExecStreamGraphImpl::clear(), ClosableObject::ClosableObject(), ClosableObject::close(), FlatFileBuffer::open(), ExecStreamGraphImpl::open(), ExecStream::open(), and ClosableObject::~ClosableObject().