FIX::FileStore Class Reference

File based implementation of MessageStore. More...

`#include <FileStore.h>`

Inheritance diagram for FIX::FileStore:
[legend]
Collaboration diagram for FIX::FileStore:
[legend]

## Public Member Functions

| FileStore (std::string, const SessionID &s)
virtual | ~FileStore ()
bool | set (int, const std::string &) throw ( IOException )
void | get (int, int, std::vector< std::string > &) const throw ( IOException )
int | getNextSenderMsgSeqNum () const throw ( IOException )
int | getNextTargetMsgSeqNum () const throw ( IOException )
void | setNextSenderMsgSeqNum (int value) throw ( IOException )
void | setNextTargetMsgSeqNum (int value) throw ( IOException )
void | incrNextSenderMsgSeqNum () throw ( IOException )
void | incrNextTargetMsgSeqNum () throw ( IOException )
UtcTimeStamp | getCreationTime () const throw ( IOException )
void | reset () throw ( IOException )
void | refresh () throw ( IOException )
Public Member Functions inherited from FIX::MessageStore
virtual | ~MessageStore ()

## Private Types

typedef std::pair< long, std::size_t > | OffsetSize
typedef std::map< int, OffsetSize > | NumToOffset

## Private Member Functions

void | open (bool deleteFile)
void | populateCache ()
bool | readFromFile (int offset, int size, std::string &msg)
void | setSeqNum ()
void | setSession ()
bool | get (int, std::string &) const throw ( IOException )

## Private Attributes

MemoryStore | m_cache
NumToOffset | m_offsets
std::string | m_msgFileName
std::string | m_headerFileName
std::string | m_seqNumsFileName
std::string | m_sessionFileName
FILE * | m_msgFile
FILE * | m_headerFile
FILE * | m_seqNumsFile
FILE * | m_sessionFile

## Detailed Description

File based implementation of MessageStore.

Four files are created by this implementation. One for storing outgoing messages, one for indexing message locations, one for storing sequence numbers, and one for storing the session creation time.

The formats of the files are:
[path]+[BeginString]-[SenderCompID]-[TargetCompID].body
[path]+[BeginString]-[SenderCompID]-[TargetCompID].header
[path]+[BeginString]-[SenderCompID]-[TargetCompID].seqnums
[path]+[BeginString]-[SenderCompID]-[TargetCompID].session

The messages file is a pure stream of FIX messages.
The sequence number file is in the format of
[SenderMsgSeqNum] : [TargetMsgSeqNum]
The session file is a UTC timestamp in the format of
YYYYMMDD-HH:MM:SS

Definition at line 98 of file FileStore.h.

## Member Typedef Documentation

## NumToOffset

typedef std::map< int, OffsetSize > FIX::FileStore::NumToOffset

private

Definition at line 125 of file FileStore.h.

## OffsetSize

typedef std::pair< long, std::size_t > FIX::FileStore::OffsetSize

private

Definition at line 123 of file FileStore.h.

## Constructor & Destructor Documentation

## FileStore()

FIX::FileStore::FileStore | ( | std::string | path,
|  | const SessionID & | s
| ) |  |

Definition at line 51 of file FileStore.cpp.

{
open( false );
}
catch ( IOException & e )
{
throw ConfigError( e.what() );
}
}
FileStore::~FileStore()
{
if( m_msgFile ) fclose( m_msgFile );
if( m_headerFile ) fclose( m_headerFile );
if( m_seqNumsFile ) fclose( m_seqNumsFile );
if( m_sessionFile ) fclose( m_sessionFile );
}
void FileStore::open( bool deleteFile )
{
if ( m_msgFile ) fclose( m_msgFile );
if ( m_headerFile ) fclose( m_headerFile );
if ( m_seqNumsFile ) fclose( m_seqNumsFile );
if ( m_sessionFile ) fclose( m_sessionFile );
m_msgFile = 0;

## ~FileStore()

FIX::FileStore::~FileStore | ( |  | ) |

virtual

Definition at line 88 of file FileStore.cpp.

{
file_unlink( m_msgFileName.c_str() );
file_unlink( m_headerFileName.c_str() );

References FIX::file_unlink(), m_headerFileName, m_msgFileName, and m_seqNumsFileName.

## Member Function Documentation

## get() [1/2]

void FIX::FileStore::get | ( | int | begin,
|  | int | end,
|  | std::vector< std::string > & | result
| ) |  | const
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 240 of file FileStore.cpp.

{
return m_cache.getNextTargetMsgSeqNum();
}
void FileStore::setNextSenderMsgSeqNum( int value ) throw ( IOException )
{
m_cache.setNextSenderMsgSeqNum( value );
setSeqNum();
}

References FIX::MemoryStore::getNextTargetMsgSeqNum(), and m_cache.

## get() [2/2]

bool FIX::FileStore::get | ( | int | msgSeqNum,
|  | std::string & | msg
| ) |  | const
throw | ( | IOException
| ) |  |

private

Definition at line 341 of file FileStore.cpp.

## getCreationTime()

UtcTimeStamp FIX::FileStore::getCreationTime | ( |  | ) | const
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 287 of file FileStore.cpp.

{

## getNextSenderMsgSeqNum()

int FIX::FileStore::getNextSenderMsgSeqNum | ( |  | ) | const
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 253 of file FileStore.cpp.

{
m_cache.setNextTargetMsgSeqNum( value );
setSeqNum();
}

## getNextTargetMsgSeqNum()

int FIX::FileStore::getNextTargetMsgSeqNum | ( |  | ) | const
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 258 of file FileStore.cpp.

{
m_cache.incrNextSenderMsgSeqNum();
setSeqNum();

References FIX::MemoryStore::incrNextSenderMsgSeqNum(), m_cache, and setSeqNum().

## incrNextSenderMsgSeqNum()

void FIX::FileStore::incrNextSenderMsgSeqNum | ( |  | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 275 of file FileStore.cpp.

{
try
{
m_cache.reset();

References m_cache, open(), and FIX::MemoryStore::reset().

## incrNextTargetMsgSeqNum()

void FIX::FileStore::incrNextTargetMsgSeqNum | ( |  | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 281 of file FileStore.cpp.

{
throw IOException( e.what() );

## open()

void FIX::FileStore::open | ( | bool | deleteFile | ) |

private

Definition at line 96 of file FileStore.cpp.

: " + m_msgFileName );
m_headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
if ( !m_headerFile ) m_headerFile = file_fopen( m_headerFileName.c_str(), "w+" );
if ( !m_headerFile ) throw ConfigError( "Could not open header file: " + m_headerFileName );
m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "r+" );
if ( !m_seqNumsFile ) m_seqNumsFile = file_fopen( m_seqNumsFileName.c_str(), "w+" );
if ( !m_seqNumsFile ) throw ConfigError( "Could not open seqnums file: " + m_seqNumsFileName );
bool setCreationTime = false;
m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r" );
if ( !m_sessionFile ) setCreationTime = true;
else fclose( m_sessionFile );
m_sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
if ( !m_sessionFile ) m_sessionFile = file_fopen( m_sessionFileName.c_str(), "w+" );
if ( !m_sessionFile ) throw ConfigError( "Could not open session file" );
if ( setCreationTime ) setSession();
setNextSenderMsgSeqNum( getNextSenderMsgSeqNum() );
setNextTargetMsgSeqNum( getNextTargetMsgSeqNum() );
}
void FileStore::populateCache()
{
FILE* headerFile = file_fopen( m_headerFileName.c_str(), "r+" );
if ( headerFile )
{
int num;
long offset;
std::size_t size;
while (FILE_FSCANF(headerFile, "%d,%ld,%lu ", &num, &offset, &size) == 3)
{
std::pair<NumToOffset::iterator, bool> it =
m_offsets.insert(NumToOffset::value_type(num, std::make_pair(offset, size)));
//std::cout << it.first->second.first << " --- " << it.first->second.second << '\n';
if (it.second == false)
{

Referenced by incrNextSenderMsgSeqNum(), and reset().

## populateCache()

void FIX::FileStore::populateCache | ( |  | ) |

private

Definition at line 143 of file FileStore.cpp.

{
int sender, target;
if ( FILE_FSCANF( seqNumsFile, "%d : %d", &sender, &target ) == 2 )
{
m_cache.setNextSenderMsgSeqNum( sender );
m_cache.setNextTargetMsgSeqNum( target );
}
fclose( seqNumsFile );
}
FILE* sessionFile = file_fopen( m_sessionFileName.c_str(), "r+" );
if ( sessionFile )
{
char time[ 22 ];
#ifdef HAVE_FSCANF_S
int result = FILE_FSCANF( sessionFile, "%s", time, 22 );
#else
int result = FILE_FSCANF( sessionFile, "%s", time );
#endif
if( result == 1 )
{
m_cache.setCreationTime( UtcTimeStampConvertor::convert( time ) );
}
fclose( sessionFile );
}
}
MessageStore* FileStoreFactory::create( const SessionID& s )
{
if ( m_path.size() ) return new FileStore( m_path, s );
std::string path;
Dictionary settings = m_settings.get( s );
path = settings.getString( FILE_STORE_PATH );
return new FileStore( path, s );
}
void FileStoreFactory::destroy( MessageStore* pStore )
{
delete pStore;
}
bool FileStore::set( int msgSeqNum, const std::string& msg )

## readFromFile()

bool FIX::FileStore::readFromFile | ( | int | offset,
|  | int | size,
|  | std::string & | msg
| ) |  |

private

## refresh()

void FIX::FileStore::refresh | ( |  | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 306 of file FileStore.cpp.

{
rewind( m_sessionFile );
fprintf( m_sessionFile, "%s",
UtcTimeStampConvertor::convert( m_cache.getCreationTime() ).c_str() );

## reset()

void FIX::FileStore::reset | ( |  | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 292 of file FileStore.cpp.

{
m_cache.reset();
open( false );
}
catch( std::exception& e )
{
throw IOException( e.what() );
}
}
void FileStore::setSeqNum()
{
rewind( m_seqNumsFile );

References m_cache, open(), and FIX::MemoryStore::reset().

## set()

bool FIX::FileStore::set | ( | int | msgSeqNum,
|  | const std::string & | msg
| ) |  |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 209 of file FileStore.cpp.

{
it.first->second = std::make_pair(offset, size);
}
fwrite( msg.c_str(), sizeof( char ), msg.size(), m_msgFile );
if ( ferror( m_msgFile ) )
throw IOException( "Unable to write to file " + m_msgFileName );
if ( fflush( m_msgFile ) == EOF )
throw IOException( "Unable to flush file " + m_msgFileName );
if ( fflush( m_headerFile ) == EOF )
throw IOException( "Unable to flush file " + m_headerFileName );
return true;
}
void FileStore::get( int begin, int end,
std::vector < std::string > & result ) const
throw ( IOException )
{
result.clear();
std::string msg;
for ( int i = begin; i <= end; ++i )
{
if ( get( i, msg ) )
result.push_back( msg );
}
}
int FileStore::getNextSenderMsgSeqNum() const throw ( IOException )
{
return m_cache.getNextSenderMsgSeqNum();

## setNextSenderMsgSeqNum()

void FIX::FileStore::setNextSenderMsgSeqNum | ( | int | value | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 263 of file FileStore.cpp.

{
m_cache.incrNextTargetMsgSeqNum();
setSeqNum();

References FIX::MemoryStore::incrNextTargetMsgSeqNum(), m_cache, and setSeqNum().

## setNextTargetMsgSeqNum()

void FIX::FileStore::setNextTargetMsgSeqNum | ( | int | value | ) |
throw | ( | IOException
| ) |  |

virtual

Implements FIX::MessageStore.

Definition at line 269 of file FileStore.cpp.

{
return m_cache.getCreationTime();
}

References FIX::MemoryStore::getCreationTime(), and m_cache.

## setSeqNum()

void FIX::FileStore::setSeqNum | ( |  | ) |

private

Definition at line 319 of file FileStore.cpp.

{
NumToOffset::const_iterator find = m_offsets.find( msgSeqNum );
if ( find == m_offsets.end() ) return false;

Referenced by getNextTargetMsgSeqNum(), and setNextSenderMsgSeqNum().

## setSession()

void FIX::FileStore::setSession | ( |  | ) |

private

Definition at line 330 of file FileStore.cpp.

{
delete [] buffer;
throw IOException( "Unable to read from file " + m_msgFileName );
}
buffer[ offset.second ] = 0;

## Member Data Documentation

## m_cache

MemoryStore FIX::FileStore::m_cache

private

Definition at line 135 of file FileStore.h.

Referenced by get(), getNextTargetMsgSeqNum(), incrNextSenderMsgSeqNum(), reset(), setNextSenderMsgSeqNum(), and setNextTargetMsgSeqNum().

## m_headerFile

FILE* FIX::FileStore::m_headerFile

private

Definition at line 144 of file FileStore.h.

## m_headerFileName

std::string FIX::FileStore::m_headerFileName

private

Definition at line 139 of file FileStore.h.

Referenced by ~FileStore().

## m_msgFile

FILE* FIX::FileStore::m_msgFile

private

Definition at line 143 of file FileStore.h.

## m_msgFileName

std::string FIX::FileStore::m_msgFileName

private

Definition at line 138 of file FileStore.h.

Referenced by ~FileStore().

## m_offsets

NumToOffset FIX::FileStore::m_offsets

private

Definition at line 136 of file FileStore.h.

## m_seqNumsFile

FILE* FIX::FileStore::m_seqNumsFile

private

Definition at line 145 of file FileStore.h.

## m_seqNumsFileName

std::string FIX::FileStore::m_seqNumsFileName

private

Definition at line 140 of file FileStore.h.

Referenced by ~FileStore().

## m_sessionFile

FILE* FIX::FileStore::m_sessionFile

private

Definition at line 146 of file FileStore.h.

## m_sessionFileName

std::string FIX::FileStore::m_sessionFileName

private

Definition at line 141 of file FileStore.h.

The documentation for this class was generated from the following files:

- FileStore.h
- FileStore.cpp

FIX::FileStore::m_sessionFileName
std::string m_sessionFileName
Definition: FileStore.h:141
FIX::FileStore::m_msgFileName
std::string m_msgFileName
Definition: FileStore.h:138
FIX::FileStoreFactory::create
MessageStore * create(const SessionID &)
Definition: FileStore.cpp:194
FIX::FileStore::m_cache
MemoryStore m_cache
Definition: FileStore.h:135
FIX::FileStore::FileStore
FileStore(std::string, const SessionID &s)
Definition: FileStore.cpp:51
FIX::FileStore::m_msgFile
FILE * m_msgFile
Definition: FileStore.h:143
FIX::FileStore::~FileStore
virtual ~FileStore()
Definition: FileStore.cpp:88
FIX::MemoryStore::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: MessageStore.h:121
FIX::MemoryStore::setNextTargetMsgSeqNum
void setNextTargetMsgSeqNum(int value)
Definition: MessageStore.h:127
FIX::MemoryStore::getNextTargetMsgSeqNum
int getNextTargetMsgSeqNum() const
Definition: MessageStore.h:123
FIX::FileStore::getNextSenderMsgSeqNum
int getNextSenderMsgSeqNum() const
Definition: FileStore.cpp:253
FIX::MemoryStore::incrNextSenderMsgSeqNum
void incrNextSenderMsgSeqNum()
Definition: MessageStore.h:129
FIX::FILE_STORE_PATH
const char FILE_STORE_PATH[]
Definition: SessionSettings.h:97
FIX::FileStore::get
void get(int, int, std::vector< std::string > &) const
Definition: FileStore.cpp:240
FIX::MemoryStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: MessageStore.h:125
FIX::FileStore::m_seqNumsFile
FILE * m_seqNumsFile
Definition: FileStore.h:145
FIX::FileStore::m_headerFileName
std::string m_headerFileName
Definition: FileStore.h:139
FIX::FileStore::m_sessionFile
FILE * m_sessionFile
Definition: FileStore.h:146
FIX::UtcTimeStampConvertor::convert
static std::string convert(const UtcTimeStamp &value, int precision=0)
Definition: FieldConvertors.h:451
FIX::file_unlink
void file_unlink(const char *path)
Definition: Utility.cpp:554
FIX::MemoryStore::incrNextTargetMsgSeqNum
void incrNextTargetMsgSeqNum()
Definition: MessageStore.h:131
FIX::MemoryStore::getCreationTime
UtcTimeStamp getCreationTime() const
Definition: MessageStore.h:136
FIX::MemoryStore::reset
void reset()
Definition: MessageStore.h:139
FIX::FileStore::setSeqNum
void setSeqNum()
Definition: FileStore.cpp:319
FIX::file_fopen
FILE * file_fopen(const char *path, const char *mode)
Definition: Utility.cpp:526
FIX::FileStore::m_offsets
NumToOffset m_offsets
Definition: FileStore.h:136
FILE_FSCANF
#define FILE_FSCANF
Definition: Utility.h:215
FIX::MemoryStore::setCreationTime
void setCreationTime(const UtcTimeStamp &creationTime)
Definition: MessageStore.h:134
FIX::FileStore::open
void open(bool deleteFile)
Definition: FileStore.cpp:96
FIX::FileStore::set
bool set(int, const std::string &)
Definition: FileStore.cpp:209
FIX::FileStoreFactory::destroy
void destroy(MessageStore *)
Definition: FileStore.cpp:204
FIX::FileStore::setNextSenderMsgSeqNum
void setNextSenderMsgSeqNum(int value)
Definition: FileStore.cpp:263
FIX::FileStore::m_headerFile
FILE * m_headerFile
Definition: FileStore.h:144