00001 #include "MemoryBackingStore.h"
00002 #include "AIDA_Dev/IDevTupleFactory.h"
00003 #include "AIDA_Dev/IDevTuple.h"
00004 #include "AIDA_Dev/ITupleHeader.h"
00005 #include "AIDA_Dev/ITupleVariableDescription.h"
00006 #include "AIDA_Dev/ITupleVariableStatistics.h"
00007 #include "AIDA/IAnnotation.h"
00008 #include <set>
00009
00010 namespace Anaphe {
00011 namespace AIDA_MemoryStore {
00012
00013 class MemoryStoreSupportedTypes
00014 {
00015 public:
00016 MemoryStoreSupportedTypes()
00017 {
00018 m_supportedTypes.insert( "double" );
00019 m_supportedTypes.insert( "float" );
00020 m_supportedTypes.insert( "long" );
00021 m_supportedTypes.insert( "int" );
00022 m_supportedTypes.insert( "short" );
00023 m_supportedTypes.insert( "char" );
00024 m_supportedTypes.insert( "bool" );
00025 m_supportedTypes.insert( "std::string" );
00026 m_supportedTypes.insert( "AIDA::ITuple" );
00027 }
00028 ~MemoryStoreSupportedTypes()
00029 {}
00030 bool isTypeSupported( const std::string& type ) const
00031 {
00032 return ( m_supportedTypes.find( type ) != m_supportedTypes.end() );
00033 }
00034 private:
00035 std::set< std::string > m_supportedTypes;
00036 };
00037
00038 }
00039 };
00040
00041 static const Anaphe::AIDA_MemoryStore::MemoryStoreSupportedTypes supportedTypes;
00042
00043 Anaphe::AIDA_MemoryStore::MemoryBackingStore::MemoryBackingStore( AIDA::Dev::IDevTupleFactory& factory ):
00044 m_factory( factory )
00045 {}
00046
00047
00048 Anaphe::AIDA_MemoryStore::MemoryBackingStore::~MemoryBackingStore()
00049 {
00050 m_tuples.clear();
00051 for ( std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator i = m_tupleData.begin();
00052 i != m_tupleData.end(); ++i ) {
00053 delete i->second;
00054 }
00055 m_tupleData.clear();
00056 }
00057
00058
00059 bool
00060 Anaphe::AIDA_MemoryStore::MemoryBackingStore::writeTupleHeader( AIDA::Dev::ITupleHeader& header )
00061 {
00062 const std::string& localPath = header.pathInStore();
00063 if ( m_tupleData.find( localPath ) != m_tupleData.end() ) return false;
00064 Anaphe::AIDA_MemoryStore::MemoryTupleData* tuple = new Anaphe::AIDA_MemoryStore::MemoryTupleData;
00065 for ( int i = 0; i < header.numberOfVariables(); ++i ) {
00066 AIDA::Dev::ITupleVariableDescription* description = header.variableDescription( i );
00067 const std::string& type = description->variableType();
00068 if ( ! supportedTypes.isTypeSupported( type ) ) return false;
00069 tuple->variables().push_back( m_factory.createDescription( *description ) );
00070 if ( type == "double" ) tuple->doubleVariableData().insert( std::make_pair( i, std::vector< double >() ) );
00071 else if ( type == "float" ) tuple->floatVariableData().insert( std::make_pair( i, std::vector< float >() ) );
00072 else if ( type == "long" ) tuple->longVariableData().insert( std::make_pair( i, std::vector< long >() ) );
00073 else if ( type == "int" ) tuple->intVariableData().insert( std::make_pair( i, std::vector< int >() ) );
00074 else if ( type == "short" ) tuple->shortVariableData().insert( std::make_pair( i, std::vector< short >() ) );
00075 else if ( type == "char" ) tuple->charVariableData().insert( std::make_pair( i, std::vector< char >() ) );
00076 else if ( type == "bool" ) tuple->boolVariableData().insert( std::make_pair( i, std::vector< bool >() ) );
00077 else if ( type == "std::string" ) tuple->stringVariableData().insert( std::make_pair( i, std::vector< std::string >() ) );
00078 else if ( type == "AIDA::ITuple" ) tuple->tupleVariableData().insert( std::make_pair( i, std::vector< Anaphe::AIDA_MemoryStore::MemoryTupleData * >() ) );
00079 }
00080 tuple->title( header.annotation().value( titleKey ) );
00081 m_tupleData.insert( std::make_pair( localPath, tuple ) );
00082 m_tuples.insert( std::make_pair( localPath, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple( static_cast<Anaphe::AIDA_MemoryStore::IMemoryBackingStore*>(this),
00083 m_factory,
00084 tuple,
00085 localPath ) ) );
00086 return true;
00087 }
00088
00089 bool
00090 Anaphe::AIDA_MemoryStore::MemoryBackingStore::readTupleHeader( AIDA::Dev::ITupleHeader& header )
00091 {
00092 const std::string& localPath = header.pathInStore();
00093 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator iTupleData = m_tupleData.find( localPath );
00094 if ( iTupleData == m_tupleData.end() ) return false;
00095 Anaphe::AIDA_MemoryStore::MemoryTupleData* tuple = iTupleData->second;
00096 header.annotation().setValue( titleKey, tuple->title() );
00097 if ( m_tuples.find( localPath ) == m_tuples.end() ) {
00098 m_tuples.insert( std::make_pair( localPath,
00099 Anaphe::AIDA_MemoryStore::MemoryPersistentTuple( static_cast<Anaphe::AIDA_MemoryStore::IMemoryBackingStore*>(this),
00100 m_factory,
00101 tuple,
00102 localPath ) ) );
00103 }
00104
00105 for ( unsigned int i = 0; i < tuple->variables().size(); ++i ) {
00106 header.setVariableDescription( tuple->variables()[i], false );
00107 }
00108
00109 unsigned int nRows = 0;
00110 if ( tuple->doubleVariableData().size() > 0 ) nRows = tuple->doubleVariableData().begin()->second.size();
00111 else if ( tuple->floatVariableData().size() > 0 ) nRows = tuple->floatVariableData().begin()->second.size();
00112 else if ( tuple->longVariableData().size() > 0 ) nRows = tuple->longVariableData().begin()->second.size();
00113 else if ( tuple->intVariableData().size() > 0 ) nRows = tuple->intVariableData().begin()->second.size();
00114 else if ( tuple->shortVariableData().size() > 0 ) nRows = tuple->shortVariableData().begin()->second.size();
00115 else if ( tuple->charVariableData().size() > 0 ) nRows = tuple->charVariableData().begin()->second.size();
00116 else if ( tuple->boolVariableData().size() > 0 ) nRows = tuple->boolVariableData().begin()->second.size();
00117 else if ( tuple->stringVariableData().size() > 0 ) nRows = tuple->stringVariableData().begin()->second.size();
00118 else if ( tuple->tupleVariableData().size() > 0 ) nRows = tuple->tupleVariableData().begin()->second.size();
00119 header.setNumberOfRows( static_cast< int >( nRows ) );
00120 header.setCurrentRowNumber( -1 );
00121 return true;
00122 }
00123
00124 bool
00125 Anaphe::AIDA_MemoryStore::MemoryBackingStore::removePersistentTuple( const std::string& path )
00126 {
00127 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( path );
00128 if ( iTuple == m_tuples.end() ) return false;
00129 m_tuples.erase( path );
00130 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator iTupleData = m_tupleData.find(path);
00131 if ( m_tupleData.find(path) != m_tupleData.end() ) {
00132 Anaphe::AIDA_MemoryStore::MemoryTupleData* p = iTupleData->second;
00133 if ( p ) delete p;
00134 m_tupleData.erase( path );
00135 }
00136 return true;
00137 }
00138
00139 bool
00140 Anaphe::AIDA_MemoryStore::MemoryBackingStore::setTupleData( const std::string& path, Anaphe::AIDA_MemoryStore::MemoryTupleData* data)
00141 {
00142 if ( ! data ) return false;
00143 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator iTupleData = m_tupleData.find(path);
00144 if ( m_tupleData.find(path) != m_tupleData.end() ) {
00145 iTupleData->second = data;
00146 return true;
00147 }
00148 else return false;
00149 }
00150
00151 Anaphe::AIDA_MemoryStore::IMemoryPersistentTuple*
00152 Anaphe::AIDA_MemoryStore::MemoryBackingStore::findPersistentTuple( const std::string& path )
00153 {
00154 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( path );
00155 if ( iTuple == m_tuples.end() ) return 0;
00156 else return &( iTuple->second );
00157 }
00158
00159
00160 bool
00161 Anaphe::AIDA_MemoryStore::MemoryBackingStore::bindVariable( AIDA::Dev::ITupleHeader& header, int variableIndex )
00162 {
00163 const std::string& localPath = header.pathInStore();
00164 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( localPath );
00165 if ( iTuple == m_tuples.end() ) return false;
00166 return iTuple->second.bindVariable( variableIndex );
00167 }
00168
00169 void*
00170 Anaphe::AIDA_MemoryStore::MemoryBackingStore::variableAddress( AIDA::Dev::ITupleHeader& header, int variableIndex )
00171 {
00172 const std::string& localPath = header.pathInStore();
00173 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( localPath );
00174 if ( iTuple == m_tuples.end() ) return 0;
00175 return iTuple->second.variableAddress( variableIndex );
00176 }
00177
00178 const void*
00179 Anaphe::AIDA_MemoryStore::MemoryBackingStore::variableAddress( const AIDA::Dev::ITupleHeader& header, int variableIndex ) const
00180 {
00181 const std::string& localPath = header.pathInStore();
00182 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::const_iterator iTuple = m_tuples.find( localPath );
00183 if ( iTuple == m_tuples.end() ) return 0;
00184 return iTuple->second.variableAddress( variableIndex );
00185 }
00186
00187 bool
00188 Anaphe::AIDA_MemoryStore::MemoryBackingStore::clearBindings( const AIDA::Dev::ITupleHeader& header )
00189 {
00190 const std::string& localPath = header.pathInStore();
00191 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( localPath );
00192 if ( iTuple == m_tuples.end() ) return false;
00193 return iTuple->second.clearBindings();
00194 }
00195
00196 bool
00197 Anaphe::AIDA_MemoryStore::MemoryBackingStore::writeTupleRow( AIDA::Dev::ITupleHeader& header )
00198 {
00199 const std::string& localPath = header.pathInStore();
00200 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( localPath );
00201 if ( iTuple == m_tuples.end() ) return false;
00202 if ( ! ( iTuple->second.writeTupleRow( header.numberOfRows() - 1 ) ) ) return false;
00203 for ( int i = 0; i < header.numberOfVariables(); ++i ) {
00204 const AIDA::Dev::ITupleVariableStatistics& tupleStatistics = header.variableDescription( i )->statistics();
00205 AIDA::Dev::ITupleVariableStatistics& storeStatistics = ( iTuple->second.tupleData()->variables() )[i]->statistics();
00206 storeStatistics.setStatistics( tupleStatistics.min(),
00207 tupleStatistics.max(),
00208 tupleStatistics.mean(),
00209 tupleStatistics.rms(),
00210 tupleStatistics.entries() );
00211 }
00212 return true;
00213 }
00214
00215 bool
00216 Anaphe::AIDA_MemoryStore::MemoryBackingStore::readTupleRow( AIDA::Dev::ITupleHeader& header )
00217 {
00218 const std::string& localPath = header.pathInStore();
00219 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryPersistentTuple >::iterator iTuple = m_tuples.find( localPath );
00220 if ( iTuple == m_tuples.end() ) return false;
00221 return iTuple->second.readTupleRow( header.currentRowNumber() );
00222 }
00223
00224 bool
00225 Anaphe::AIDA_MemoryStore::MemoryBackingStore::resetTuple( AIDA::Dev::ITupleHeader& header )
00226 {
00227 const std::string& localPath = header.pathInStore();
00228 std::map< std::string, Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator iTuple = m_tupleData.find( localPath );
00229 if ( iTuple == m_tupleData.end() ) return false;
00230 MemoryTupleData& tuple = *( iTuple->second );
00231 for( std::map< int, std::vector< double > >::iterator i = tuple.doubleVariableData().begin();
00232 i != tuple.doubleVariableData().end(); ++i ) i->second.clear();
00233 for( std::map< int, std::vector< float > >::iterator i = tuple.floatVariableData().begin();
00234 i != tuple.floatVariableData().end(); ++i ) i->second.clear();
00235 for( std::map< int, std::vector< long > >::iterator i = tuple.longVariableData().begin();
00236 i != tuple.longVariableData().end(); ++i ) i->second.clear();
00237 for( std::map< int, std::vector< int > >::iterator i = tuple.intVariableData().begin();
00238 i != tuple.intVariableData().end(); ++i ) i->second.clear();
00239 for( std::map< int, std::vector< short > >::iterator i = tuple.shortVariableData().begin();
00240 i != tuple.shortVariableData().end(); ++i ) i->second.clear();
00241 for( std::map< int, std::vector< char > >::iterator i = tuple.charVariableData().begin();
00242 i != tuple.charVariableData().end(); ++i ) i->second.clear();
00243 for( std::map< int, std::vector< bool > >::iterator i = tuple.boolVariableData().begin();
00244 i != tuple.boolVariableData().end(); ++i ) i->second.clear();
00245 for( std::map< int, std::vector< std::string > >::iterator i = tuple.stringVariableData().begin();
00246 i != tuple.stringVariableData().end(); ++i ) i->second.clear();
00247 for( std::map< int, std::vector< Anaphe::AIDA_MemoryStore::MemoryTupleData* > >::iterator i = tuple.tupleVariableData().begin();
00248 i != tuple.tupleVariableData().end(); ++i ) {
00249 for ( std::vector< Anaphe::AIDA_MemoryStore::MemoryTupleData* >::iterator j = i->second.begin(); j != i->second.end(); ++j ) {
00250 if ( *j ) delete *j;
00251 }
00252 i->second.clear();
00253 }
00254 return true;
00255 }