19 #include <sio/compression/zlib.h>
32 auto ra = std::make_shared<LCIORandomAccess>() ;
35 ra->_nRunHeaders =
_runEvtMap->getNumberOfRunRecords() ;
36 ra->_nEvents =
_runEvtMap->getNumberOfEventRecords() ;
37 ra->_recordsAreInOrder = true ;
38 ra->_indexLocation = 0 ;
39 ra->_prevLocation = 0 ;
40 ra->_nextLocation = 0 ;
41 ra->_firstRecordLocation = 0 ;
49 _fileRecord = std::make_shared<LCIORandomAccess>() ;
56 _fileRecord->_prevLocation = 9223372036854775807LL ;
69 if( ra->_nextLocation >
_fileRecord->_nextLocation ) {
72 if( 0 < ra->_prevLocation && ra->_prevLocation <
_fileRecord->_prevLocation ) {
95 sio::record_info recinfo ;
97 sio::api::read_record( stream, recinfo,
_rawBuffer ) ;
99 catch( sio::exception &e ) {
111 sio::block_list blocks {} ;
112 auto raBlock = std::make_shared<SIORandomAccessHandler>() ;
113 blocks.push_back( raBlock ) ;
115 auto recordData =
_rawBuffer.span( recinfo._header_length, recinfo._data_length ) ;
117 sio::api::read_blocks( recordData, blocks ) ;
135 SIO_DEBUG(
"LCIORandomAccessMgr::readLCIOIndex: Reading at " << stream.tellg() );
137 sio::record_info recinfo ;
139 sio::api::read_record( stream, recinfo,
_rawBuffer ) ;
141 catch( sio::exception &e ) {
154 sio::block_list blocks {} ;
155 auto indexBlock = std::make_shared<SIOIndexHandler>() ;
157 blocks.push_back( indexBlock ) ;
158 const bool compressed = sio::api::is_compressed( recinfo._options ) ;
160 sio::buffer compBuffer( recinfo._uncompressed_length ) ;
161 sio::zlib_compression compressor ;
162 compressor.uncompress(
_rawBuffer.span( recinfo._header_length, recinfo._data_length ), compBuffer ) ;
164 sio::api::read_blocks( compBuffer.span(), blocks ) ;
168 sio::api::read_blocks(
_rawBuffer.span( recinfo._header_length, recinfo._data_length ), blocks ) ;
205 raPos = ra->getPrevLocation() ;
206 auto indexPos = ra->getIndexLocation() ;
209 while( raPos != 0 ) {
212 raPos = ra->getPrevLocation() ;
213 auto idxPos = ra->getIndexLocation() ;
217 throw IO::IOException(
std::string(
"[LCIORandomAccessMgr::ReadEventMap()] Could not read previous LCIORandomAccess record" ) ) ;
228 std::cout <<
" LCIORandomAccessMgr::getEventMap() recreating event map for direct access (old file)" <<
std::endl ;
230 if( not stream.is_open() ) {
231 throw IO::IOException(
"LCIORandomAccessMgr::recreateEventMap: stream is not open" ) ;
237 sio::record_info recinfo {} ;
239 sio::api::skip_records( stream, [&](
const sio::record_info &ri ){
240 return ( recordlist.
find(ri._name) != recordlist.
end() ) ;
243 sio::api::read_record( stream, recinfo,
_rawBuffer ) ;
245 auto recdata =
_rawBuffer.span( recinfo._header_length, recinfo._data_length ) ;
247 const bool compressed = sio::api::is_compressed( recinfo._options ) ;
249 sio::zlib_compression compressor ;
257 auto event = std::make_shared<IOIMPL::LCEventIOImpl>() ;
261 _runEvtMap->add(
RunEvent(
event->getRunNumber() ,
event->getEventNumber() ) , static_cast<long64>(recinfo._file_start) ) ;
265 auto run = std::make_shared<IOIMPL::LCRunHeaderIOImpl>() ;
267 _runEvtMap->add(
RunEvent( run->getRunNumber() , -1 ) , static_cast<long64>(recinfo._file_start) ) ;
270 catch( sio::exception &e ) {
272 if( e.code() == sio::error_code::eof ) {
277 SIO_RETHROW( e, e.code(),
"Couldn't recreate event map !" ) ;
294 if( not stream.is_open() ) {
295 throw IO::IOException(
"[LCIORandomAccessMgr::writeRandomAccessRecords] stream not opened" ) ;
297 sio::block_list blocks {} ;
300 auto indexHandler = std::make_shared<SIOIndexHandler>() ;
302 blocks.push_back( indexHandler ) ;
305 sio::api::write_record( stream,
_rawBuffer.span(), recinfo ) ;
309 ra->setIndexLocation( recinfo._file_start ) ;
310 long64 thisPos = stream.tellp() ;
311 ra->setFirstRecordLocation( thisPos ) ;
313 SIO_DEBUG(
"LCIORandomAccessMgr::writeRandomAccessRecords: lRa is " << lRa );
314 long64 prevPos = ( lRa ? lRa->getFirstRecordLocation() : 0 ) ;
315 ra->setPreviousLocation( prevPos ) ;
320 auto raHandler = std::make_shared<SIORandomAccessHandler>() ;
322 blocks.push_back( raHandler ) ;
325 sio::api::write_record( stream,
_rawBuffer.span(), recinfo ) ;
340 sio::api::write_record( stream,
_rawBuffer.span(), recinfo ) ;
342 SIO_DEBUG(
"Random access manager: =====\n" << *
this );
348 if( not stream.is_open() ) {
349 throw IO::IOException(
"[LCIORandomAccessMgr::seekStream] Stream not open") ;
352 stream.seekg( 0 , std::ios_base::end ) ;
353 auto endg = stream.tellg() ;
355 std::stringstream s ; s <<
"[LCIORandomAccessMgr::seekStream] Can't seek stream to " << pos ;
360 stream.seekg( endg - tpos , std::ios_base::beg ) ;
363 stream.seekg( pos ) ;
365 if( not stream.good() ) {
366 std::stringstream s ; s <<
"[LCIORandomAccessMgr::seekStream] Can't seek stream to " << pos <<
". rdstate is: " << stream.
rdstate() ;
374 os <<
" LCIORandomAccessMgr: ----------------------- " <<
std::endl ;
Manager class for LCIO direct access.
std::shared_ptr< LCIORandomAccess > _fileRecord
The file record summary.
bool createEventMap(sio::ifstream &stream)
Get the run and event header map from the stream - either by reading the random access records or by ...
static constexpr int RandomAccessSize
static constexpr const char * RunRecordName
bool initAppend(sio::ifstream &stream)
Initialize random access for append mode: read last LCIORandomAccess record if it exists - recreate t...
std::shared_ptr< LCIORandomAccess > createFromEventMap() const
Prepare an LCIORandomAccess object from the current contents of RunEventMap (all file locations set t...
Helper struct that stores run and event positions in the file.
bool readLCIOIndex(sio::ifstream &stream)
Helper for reading the next LCIOIndex record (need preceeding call to LCSIO::seek() ) ...
static constexpr const char * AccessRecordName
void addLCIORandomAccess(std::shared_ptr< LCIORandomAccess > ra)
Add a new LCIORandomAccess object to the list.
std::ostream & operator<<(std::ostream &os, const LCIORandomAccess &ra)
bool recreateEventMap(sio::ifstream &stream)
Fill the RunEventMap from the event and run header records in the file.
bool readLCIORandomAccess(sio::ifstream &stream)
Helper for reading the next LCIORandomAccess record (need preceeding call to LCSIO::seek() ) ...
IOException used for reading/writing errors.
const std::shared_ptr< LCIORandomAccess > lastLCIORandomAccess() const
Pointer to the last LCIORandomAccess in the list.
bool readLCIORandomAccessAt(sio::ifstream &stream, long64 pos)
Read the LCIORandomAccess record at the specified position.
sio::buffer _compressBuffer
bool readLCIOIndexAt(sio::ifstream &stream, long64 pos)
Read the LCIOIndex record at the specified position.
void writeRandomAccessRecords(sio::ofstream &stream)
Write the current random access records LCIOIndex and LCIORandomAccess to the stream.
void createFileRecord()
Create file record from all LCIORandomAccess records.
LCIORandomAccessList _list
List of LCIORandomAccess objects.
static constexpr const char * IndexRecordName
std::shared_ptr< RunEventMap > _runEvtMap
Map with RunHeader and EventHeader record positions.
static constexpr const char * HeaderRecordName
void clear()
Clear all lists and maps before closing a file.
void seekStream(sio::ifstream &stream, long64 pos)
Seek the stream at the given position.