Main Page   Namespace List   Alphabetical List   Compound List   File List   Compound Members   File Members  

AIDA_ObjectivityStore.cpp

Go to the documentation of this file.
00001 #include "AIDA_ObjectivityStore.h"
00002 #include "ObjectivityOptionParser.h"
00003 #include "ObjyPathFormater.h"
00004 #include "ObjyHistogramConverter.h"
00005 #include "ObjyTuple.h"
00006 
00007 #include "AIDA_Dev/IDevManagedObject.h"
00008 
00009 #include "AIDA_Dev/IDevAnalysisFactory.h"
00010 #include "AIDA_Dev/IHistogramFactoryProvider.h"
00011 #include "AIDA_Dev/IDevHistogramFactory.h"
00012 #include "AIDA_Dev/IDevHistogram1D.h"
00013 #include "AIDA_Dev/IDevHistogram2D.h"
00014 #include "AIDA_Dev/IDevHistogram3D.h"
00015 #include "AIDA_Dev/IDevProfile1D.h"
00016 
00017 #include "AIDA_Dev/ITupleFactoryProvider.h"
00018 #include "AIDA_Dev/IDevTupleFactory.h"
00019 #include "AIDA_Dev/IDevTuple.h"
00020 #include "AIDA_Dev/ITupleHeader.h"
00021 #include "AIDA_Dev/ITupleVariableDescription.h"
00022 #include "AIDA_Dev/ITupleVariableStatistics.h"
00023 
00024 #include <memory>
00025 #include <stdexcept>
00026 #include <cstdlib>
00027 
00028 #ifdef OLDSTREAMS
00029 # include <strstream>
00030 # define ostringstream ostrstream
00031 # define istringstream istrstream
00032 #else
00033 # include <sstream>
00034 #endif
00035 
00036 #ifdef OBJECTIVITY_STORE_NO_EXCEPTIONS_TO_USER
00037 #include <iostream>
00038 #define OBJECTIVITY_STORE_REPORT_ERROR( XXX ) std::cerr << XXX << std::endl
00039 #else
00040 #define OBJECTIVITY_STORE_REPORT_ERROR( XXX ) throw std::runtime_error( XXX )
00041 #endif
00042 
00043 #include "HepODBMS/goodies/ooSession.h"
00044 #include "HepODBMS/naming/HepMapTree.h"
00045 #include "HTL/PHistograms.h"
00046 #include "HepODBMS/tagdb/HepExplorableGenericTags.h"
00047 
00048 
00049 #ifdef NONUMERICLIMITS
00050 static const int very_big_integer = 2147483647;
00051 #else
00052 #include <limits>
00053 static const int very_big_integer = std::numeric_limits<int>::max();
00054 #endif
00055 
00056 
00057 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::AIDA_ObjyStore( const std::string& name,
00058                                                                bool readOnly,
00059                                                                bool createNew,
00060                                                                const std::string& options ):
00061   m_name( name ),
00062   m_isClosed( true ),
00063   m_optionParser( new Anaphe::AIDA_ObjectivityStore::ObjectivityOptionParser( options ) ),
00064   m_ooSession( new ooSession ),
00065   m_hepMapTree( 0 )
00066 {
00067   // Initialize OBJECTIVITY and start a transaction
00068   char * env = std::getenv( "OO_FD_BOOT" );
00069   if ( ! env ) {
00070     throw std::runtime_error( "The environment variable \"OO_FD_BOOT\" is not set!" );
00071   }
00072   const std::string oo_fd_boot = std::string( env );
00073 
00074   if ( readOnly && !createNew ) {
00075     m_ooSession->setRead();
00076   }
00077   else {
00078     m_ooSession->setUpdate();
00079   }
00080   m_ooSession->Init();
00081   m_ooSession->TransStart( m_ooSession->mrow() );
00082 
00083   // go/create to the top level database/container
00084   std::pair< std::string, std::string > topLevelDatabaseAndContainer = m_optionParser->topLevelDatabaseAndContainer( name );
00085   m_topLevelDatabase = topLevelDatabaseAndContainer.first;
00086   m_topLevelContainer = topLevelDatabaseAndContainer.second;
00087   ooHandle(ooDBObj) hDb = m_ooSession->db( m_topLevelDatabase.c_str() );
00088   if ( hDb.isNull() ) {
00089     m_ooSession->TransAbort();
00090     throw std::runtime_error( "Could not open database " + m_topLevelDatabase );
00091   }
00092   ooHandle(ooContObj) hCont = m_ooSession->container( m_topLevelContainer.c_str() );
00093   if ( hCont.isNull() ) {
00094     m_ooSession->TransAbort();
00095     throw std::runtime_error( "Could not open container " + m_topLevelContainer );
00096   }
00097 
00098   // Select the naming if requested.
00099   if ( m_optionParser->isNamingEnabled() ) {
00100     HepRef(HepMapNode) root;
00101     if (root.lookupObj(hDb,"NamingRoot") != oocSuccess) {
00102     }
00103     else {
00104       root = new( hDb, "HepMapNode" ) HepMapNode;
00105       if ( ! root.isValid() || root.nameObj(hDb, "NamingRoot") != oocSuccess ) {
00106         m_ooSession->TransAbort();
00107         throw std::runtime_error( "Could not create the naming root" );
00108       }
00109     }
00110     m_hepMapTree = new HepMapTree(root);
00111     // Create the top-level directories if not existing and change to this directory.
00112     // ND
00113   }
00114 
00115   // Cleanup the contents of the container or the tree in case a new store was created.
00116   if ( createNew ) {
00117     if ( m_optionParser->isNamingEnabled() ) {
00118       // ND
00119     }
00120     else {
00121       hCont.update();
00122 
00123       // Remove first the tuples
00124       HepExplorableDescr::clustering = hCont;
00125       std::vector<std::string> explorables;
00126       const char * c = HepExplorable::firstExplorableName();
00127       while ( c ) {
00128         explorables.push_back( std::string( c ) );
00129         c = HepExplorable::nextExplorableName();
00130       }
00131 
00132       for ( std::vector<std::string>::const_iterator iDescription = explorables.begin();
00133             iDescription != explorables.end(); ++iDescription ) {
00134         HepExplorable* p = HepExplorable::findExplorable( iDescription->c_str() );
00135         p->removeDescription();
00136         delete p;
00137       }
00138 
00139       // Remove then the rest of the objects.
00140       ooItr(ooObj) objI;
00141       if ( hCont.contains( objI ) ) {
00142         while ( objI.next() ) {
00143           const ooHandle(ooObj) & hObj = objI;
00144           ooDelete( hObj );
00145         }
00146       }
00147     }
00148   }
00149 
00150   m_isClosed = false;
00151 
00152   // Retrieve the headers of the objects
00153   collectObjectTypes( "/" );
00154 }
00155 
00156 
00157 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::~AIDA_ObjyStore()
00158 {
00159   close();
00160   if ( m_optionParser ) delete m_optionParser;
00161   for ( std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTuple = m_tupleData.begin();
00162         iTuple != m_tupleData.end(); ++iTuple ) if ( iTuple->second ) delete iTuple->second;
00163 
00164   for ( std::map< std::string, HepExplorable* >::iterator iExplorable = m_tuples.begin();
00165         iExplorable != m_tuples.end(); ++iExplorable ) if ( iExplorable->second ) delete iExplorable->second;
00166   if ( m_hepMapTree ) delete m_hepMapTree;
00167   if ( m_ooSession ) delete m_ooSession;
00168 }
00169 
00170 
00171 const std::string&
00172 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::name() const
00173 {
00174   return m_name;
00175 }
00176 
00177 
00178 bool
00179 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::writeObject( const AIDA::IManagedObject& dataObject,
00180                                                                    const std::string& path )
00181 {
00182   if ( m_objectTypes.find( path ) != m_objectTypes.end() ) return false;
00183   AIDA::Dev::IDevManagedObject* object =
00184     dynamic_cast< AIDA::Dev::IDevManagedObject* >( &( const_cast<AIDA::IManagedObject&>( dataObject ) ) );
00185   if ( ! object ) return false;
00186   const std::string& type = object->userLevelClassType();
00187   if ( ! ( type == "IHistogram1D" || type == "IHistogram2D" ||
00188            type == "IHistogram3D" || type == "IProfile1D" || type == "ITuple" ) ) {
00189     return false;
00190   }
00191   object->setUpToDate( false );
00192 
00193   m_objectTypes[ path ] = type;
00194   m_objectRefs[ path ] = object;
00195 
00196   if ( type != "ITuple" ) {
00197     m_objectsToAdd.insert( path );
00198   }
00199 
00200   return true;
00201 }
00202 
00203 
00204 AIDA::IManagedObject*
00205 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::copyAndWrite( const AIDA::IManagedObject& dataObject,
00206                                                                     const std::string& path )
00207 {
00208   if ( m_objectTypes.find( path ) != m_objectTypes.end() ) return 0;
00209   AIDA::Dev::IDevManagedObject* object =
00210     dynamic_cast< AIDA::Dev::IDevManagedObject* >( &( const_cast<AIDA::IManagedObject&>( dataObject ) ) );
00211   if ( ! object ) return 0;
00212   const std::string& type = object->userLevelClassType();
00213   if ( ! ( type == "IHistogram1D" || type == "IHistogram2D" ||
00214            type == "IHistogram3D" || type == "IProfile1D" ) ) {
00215     return 0;
00216   }
00217 
00218   std::auto_ptr<AIDA::Dev::IDevAnalysisFactory> af( dynamic_cast<AIDA::Dev::IDevAnalysisFactory*>( AIDA_createAnalysisFactory() ) );
00219   if ( ! af.get() ) {
00220     OBJECTIVITY_STORE_REPORT_ERROR( "The store could not instantiate an analysis factory" );
00221     return 0;
00222   }
00223 
00224   AIDA::Dev::IDevManagedObject* newObject = 0;
00225   AIDA::Dev::IDevHistogramFactory& hf = af->histogramFactoryProvider()->devHistogramFactory();
00226   if ( type == "IHistogram1D" ) {
00227     newObject = hf.createCopy( dynamic_cast<const AIDA::IHistogram1D&>( dataObject ) );
00228   }
00229   else if ( type == "IHistogram2D" ) {
00230     newObject = hf.createCopy( dynamic_cast<const AIDA::IHistogram2D&>( dataObject ) );
00231   }
00232   else if ( type == "IHistogram3D" ) {
00233     newObject = hf.createCopy( dynamic_cast<const AIDA::IHistogram2D&>( dataObject ) );
00234   }
00235   else if ( type == "IProfile1D" ) {
00236     newObject = hf.createCopy( dynamic_cast<const AIDA::IProfile1D&>( dataObject ) );
00237   }
00238 
00239   // Identify the name
00240   unsigned int pos = path.size();
00241   for ( unsigned int iChar = path.size() - 1; iChar >= 0; --iChar ) {
00242     if ( path[iChar] == '/' ) break;
00243     --pos;
00244   }
00245   const std::string name = path.substr( pos );
00246 
00247   // Register the new object
00248   if ( newObject ) {
00249     newObject->setName( name );
00250     writeObject( *newObject, path );
00251   }
00252   return newObject;
00253 }
00254 
00255 
00256 AIDA::IManagedObject*
00257 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::retrieveObject( const std::string & path )
00258 {
00259   std::map< std::string, AIDA::Dev::IDevManagedObject* >::iterator iObjectRef = m_objectRefs.find( path );
00260   if ( iObjectRef == m_objectRefs.end() ) return 0;
00261   if ( iObjectRef->second != 0 ) return iObjectRef->second;
00262 
00263   // Fetch the analysis factory
00264   std::auto_ptr<AIDA::Dev::IDevAnalysisFactory> af( dynamic_cast<AIDA::Dev::IDevAnalysisFactory*>( AIDA_createAnalysisFactory() ) );
00265   if ( ! af.get() ) {
00266     OBJECTIVITY_STORE_REPORT_ERROR( "The store could not instantiate an analysis factory" );
00267     return 0;
00268   }
00269 
00270   AIDA::Dev::IDevManagedObject* newObject = 0;
00271   const std::string& type = m_objectTypes[ iObjectRef->first ];
00272   if ( type == "ITuple" ) {
00273     AIDA::Dev::ITupleFactoryProvider* fp = af->tupleFactoryProvider();
00274     if ( ! fp ) {
00275       OBJECTIVITY_STORE_REPORT_ERROR( "The store could not retrieve the tuple factory provider from the analysis factory" );
00276       return 0;
00277     }
00278     AIDA::Dev::IDevTupleFactory& tf = fp->devTupleFactory();
00279     AIDA::Dev::IDevTuple* tuple = tf.create( this, "" );
00280     if ( ! tuple ) {
00281       OBJECTIVITY_STORE_REPORT_ERROR( "Could not create a tuple" );
00282       return 0;
00283     }
00284     tuple->header().setPathInStore( path );
00285     m_objectRefs[path] = tuple;
00286     if ( ! this->readTupleHeader( tuple->header() ) ) {
00287       m_objectRefs[path] = 0;
00288       OBJECTIVITY_STORE_REPORT_ERROR( "Could not read the header of the tuple from the store" );
00289       return 0;
00290     }
00291     else {
00292       tuple->setTitle( m_tuples.find( path)->second->getName() );
00293       newObject = tuple;
00294     }
00295   }
00296   else {
00297     AIDA::Dev::IHistogramFactoryProvider* fp = af->histogramFactoryProvider();
00298     if ( ! fp ) {
00299       OBJECTIVITY_STORE_REPORT_ERROR( "The store could not retrieve the histogram factory provider from the analysis factory" );
00300       return 0;
00301     }
00302     AIDA::Dev::IDevHistogramFactory& hf = fp->devHistogramFactory();
00303     Anaphe::AIDA_ObjectivityStore::ObjyHistogramConverter converter;
00304 
00305     if ( type == "IHistogram1D" ) {
00306       ooHandle(P_I_Histo_1D) handle( m_objectSysRefs[ path ] );
00307       if ( handle.typeN() == ooTypeN(PHisto1D) ) {
00308         const ooHandle(PHisto1D)& hh = handle;
00309         newObject = converter.convertFromObjy( hf, hh );
00310       }
00311       else if ( handle.typeN() == ooTypeN(PHisto1DVar) ) {
00312         const ooHandle(PHisto1DVar)& hh = handle;
00313         newObject = converter.convertFromObjy( hf, hh );
00314       }
00315     }
00316     else if ( type == "IHistogram2D" ) {
00317       ooHandle(P_I_Histo_2D) handle( m_objectSysRefs[ path ] );
00318       if ( handle.typeN() == ooTypeN(PHisto2D) ) {
00319         const ooHandle(PHisto2D)& hh = handle;
00320         newObject = converter.convertFromObjy( hf, hh );
00321       }
00322       else if ( handle.typeN() == ooTypeN(PHisto2DVar) ) {
00323         const ooHandle(PHisto2DVar)& hh = handle;
00324         newObject = converter.convertFromObjy( hf, hh );
00325       }
00326     }
00327     else if ( type == "IHistogram3D" ) {
00328       ooHandle(P_I_Histo_3D) handle( m_objectSysRefs[ path ] );
00329       if ( handle.typeN() == ooTypeN(PHisto3D) ) {
00330         const ooHandle(PHisto3D)& hh = handle;
00331         newObject = converter.convertFromObjy( hf, hh );
00332       }
00333       else if ( handle.typeN() == ooTypeN(PHisto3DVar) ) {
00334         const ooHandle(PHisto3DVar)& hh = handle;
00335         newObject = converter.convertFromObjy( hf, hh );
00336       }
00337     }
00338     else if ( type == "IProfile1D" ) {
00339       ooHandle(P_I_Histo_1D) handle( m_objectSysRefs[ path ] );
00340       if ( handle.typeN() == ooTypeN(PProfileHisto) ) {
00341         const ooHandle(PProfileHisto)& hh = handle;
00342         newObject = converter.convertFromObjy( hf, hh );
00343       }
00344       else if ( handle.typeN() == ooTypeN(PProfileHistoVar) ) {
00345         const ooHandle(PProfileHistoVar)& hh = handle;
00346         newObject = converter.convertFromObjy( hf, hh );
00347       }
00348     }
00349     m_objectRefs[path] = newObject;
00350   }
00351 
00352   // Register the new object
00353   if ( newObject ) {
00354     newObject->setUpToDate( true );
00355     newObject->setName( Anaphe::AIDA_ObjectivityStore::ObjyPathFormater::theFormater().formPathNames(path).back() );
00356   }
00357   return newObject;
00358 }
00359 
00360 
00361 bool
00362 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::removeObject( const std::string& path )
00363 {
00364   if ( m_objectTypes.find( path ) == m_objectTypes.end() ) return false;
00365 
00366   m_objectTypes.erase( path );
00367   m_objectRefs.erase( path );
00368 
00369   // Check if it is in the added histograms.
00370   std::set< std::string >::const_iterator iHistogram = m_objectsToAdd.find( path );
00371   if ( iHistogram != m_objectsToAdd.end() ) {
00372     m_objectsToAdd.erase( iHistogram );
00373     return true;
00374   }
00375 
00376   // Check if it is a tuple. Remove the description...
00377   std::map< std::string, HepExplorable* >::iterator iTuple = m_tuples.find( path );
00378   if ( iTuple != m_tuples.end() ) {
00379     HepExplorable* p = iTuple->second;
00380     p->removeDescription();
00381     delete p;
00382     m_tuples.erase( iTuple );
00383 
00384     std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00385       m_tupleData.find( iTuple->first );
00386     if ( iTupleData != m_tupleData.end() ) {
00387       if ( iTupleData->second ) delete iTupleData->second;
00388       m_tupleData.erase( iTupleData );
00389     }
00390 
00391     return true;
00392   }
00393 
00394   // Remove the underlying persistent histogram
00395   std::map< std::string, ooRef(ooObj) >::iterator iHisto = m_objectSysRefs.find( path );
00396   if ( iHisto != m_objectSysRefs.end() ) {
00397     ooHandle(ooObj) h( iHisto->second );
00398     if ( ! h.isNull() && h.isValid() ) ooDelete( h );
00399     m_objectSysRefs.erase( iHisto );
00400   }
00401 
00402   return true;
00403 }
00404 
00405 
00406 bool
00407 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::moveObject( const std::string& from,
00408                                                                   const std::string& to )
00409 {
00410   // Check if the target path exists
00411   if ( m_objectTypes.find( to ) != m_objectTypes.end() ) return false;
00412 
00413   // Check if it is in the histograms to add
00414   if ( m_objectsToAdd.find( from ) != m_objectsToAdd.end() ) {
00415     AIDA::Dev::IDevManagedObject* mo = m_objectRefs.find( from )->second;
00416     m_objectRefs.erase( from );
00417     m_objectRefs.insert( std::make_pair( to, mo ) );
00418     m_objectsToAdd.insert( to );
00419     m_objectsToAdd.erase( from );
00420     return true;
00421   }
00422 
00423   // Check if it is in the existing histograms
00424   std::map< std::string, ooRef(ooObj) >::const_iterator iObject = m_objectSysRefs.find( from );
00425   if ( iObject != m_objectSysRefs.end() ) {
00426     AIDA::Dev::IDevManagedObject* mo = m_objectRefs.find( from )->second;
00427     m_objectRefs.erase( from );
00428     m_objectRefs.insert( std::make_pair( to, mo ) );
00429     ooRef(ooObj) ref = iObject->second;
00430     m_objectSysRefs.erase( from );
00431     m_objectSysRefs.insert( std::make_pair( to, ref ) );
00432     m_ooSession->db( m_topLevelDatabase.c_str() );
00433     ooHandle(ooContObj) hCont = m_ooSession->container( m_topLevelContainer.c_str() );
00434     hCont.update();
00435     ref.unnameObj( hCont );
00436     ref.nameObj( hCont, to.c_str() );
00437     hCont.close();
00438   }
00439   return false;
00440 }
00441 
00442 
00443 std::vector< std::string >
00444 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::listObjectPaths( const std::string directory,
00445                                                                        bool recursive ) const
00446 {
00447   std::string dir = directory;
00448   if ( dir[ dir.size() - 1 ] != '/' ) dir += "/";
00449 
00450   std::vector< std::string > result;
00451 
00452   std::set< std::string > directorySet;
00453   for ( std::map< std::string, std::string >::const_iterator iObj = m_objectTypes.begin();
00454         iObj != m_objectTypes.end(); ++iObj ) {
00455     const std::string& object = iObj->first;
00456     std::string::size_type p = object.find( dir );
00457     if ( p == 0 ) {
00458       std::string complement = object.substr( dir.size() );
00459       // Remove extra "/" characters in the begining
00460       while ( complement.size() > 0 && complement[0] == '/' ) complement = complement.substr( 1 );
00461       if ( complement.size() == 0 ) continue;
00462 
00463       // Check if it is a leaf object
00464       std::string::size_type n = complement.find( "/" );
00465       if ( n == std::string::npos ) {
00466         result.push_back( object );
00467       }
00468       else { // update the daughter directory list
00469         directorySet.insert( dir + complement.substr( 0, n ) + "/" );
00470       }
00471     }
00472   }
00473 
00474   for ( std::set< std::string >::const_iterator iDir = directorySet.begin();
00475         iDir != directorySet.end(); ++iDir ) {
00476     result.push_back( *iDir );
00477     if ( recursive ) {
00478       std::vector< std::string > subDirResults = listObjectPaths( *iDir, recursive );
00479       for ( unsigned int i = 0; i < subDirResults.size(); ++i ) result.push_back( subDirResults[ i ] );
00480     }
00481   }
00482 
00483   return result;
00484 }
00485 
00486 
00487 std::vector< std::string > Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::listObjectTypes( const std::string directory,
00488                                                                                                   bool recursive ) const
00489 {
00490   std::string dir = directory;
00491   if ( dir[ dir.size() - 1 ] != '/' ) dir += "/";
00492 
00493   std::vector< std::string > result;
00494 
00495   std::set< std::string > directorySet;
00496   for ( std::map< std::string, std::string >::const_iterator iObj = m_objectTypes.begin();
00497         iObj != m_objectTypes.end(); ++iObj ) {
00498     const std::string& object = iObj->first;
00499     std::string::size_type p = object.find( dir );
00500     if ( p == 0 ) {
00501       std::string complement = object.substr( dir.size() );
00502       // Remove extra "/" characters in the begining
00503       while ( complement.size() > 0 && complement[0] == '/' ) complement = complement.substr( 1 );
00504       if ( complement.size() == 0 ) continue;
00505 
00506       // Check if it is a leaf object
00507       std::string::size_type n = complement.find( "/" );
00508       if ( n == std::string::npos ) {
00509         result.push_back( iObj->second );
00510       }
00511       else { // update the daughter directory list
00512         directorySet.insert( dir + complement.substr( 0, n ) + "/" );
00513       }
00514     }
00515   }
00516 
00517   for ( std::set< std::string >::const_iterator iDir = directorySet.begin();
00518         iDir != directorySet.end(); ++iDir ) {
00519     result.push_back( "dir" );
00520     if ( recursive ) {
00521       std::vector< std::string > subDirResults = listObjectTypes( *iDir, recursive );
00522       for ( unsigned int i = 0; i < subDirResults.size(); ++i ) result.push_back( subDirResults[ i ] );
00523     }
00524   }
00525 
00526   return result;
00527 }
00528 
00529 
00530 bool
00531 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::commit()
00532 {
00533   // Loop over the histograms that have been updated.
00534   for ( std::map< std::string, AIDA::Dev::IDevManagedObject* >::iterator iObj = m_objectRefs.begin();
00535         iObj != m_objectRefs.end(); ++iObj ) {
00536     if ( iObj->second && !( iObj->second->isUpToDate() ) ) {
00537       std::map< std::string, ooRef(ooObj) >::iterator iHisto = m_objectSysRefs.find( iObj->first );
00538       if ( iHisto != m_objectSysRefs.end() ) { // It is a histogram...
00539         ooHandle(ooObj) h( iHisto->second );
00540         if ( ! h.isNull() && h.isValid() ) ooDelete( h );
00541         m_objectSysRefs.erase( iHisto );
00542         m_objectsToAdd.insert( iObj->first );
00543       }
00544       else { // It is a tuple...
00545         iObj->second->setUpToDate( true );
00546       }
00547     }
00548   }
00549 
00550   // Insert the new histograms
00551   commitAdd();
00552 
00553   // Final commit
00554   if ( m_ooSession->in_trans() ) m_ooSession->TransCommit();
00555   return true;
00556 }
00557 
00558 
00559 bool
00560 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::close()
00561 {
00562   if ( m_ooSession->in_trans() ) m_ooSession->TransAbort();
00563   m_isClosed = true;
00564   return true;
00565 }
00566 
00567 
00568 bool Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::canMoveTuples() const {return false;}
00569 bool Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::canCopyTuples() const {return false;}
00570 
00571 
00572 bool
00573 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::writeTupleHeader( AIDA::Dev::ITupleHeader& header )
00574 {
00575   const std::string& path = header.pathInStore();
00576   if ( m_tuples.find( path ) != m_tuples.end() ) return false;
00577   const std::string& options = header.options();
00578   std::string dbForData = "";
00579   std::istringstream is( options.c_str() );
00580   if ( ! is.eof() ) {
00581     is >> dbForData;
00582   }
00583   if ( dbForData == "" ) dbForData = m_topLevelDatabase;
00584 
00585   m_ooSession->db( m_topLevelDatabase.c_str() );
00586   ooHandle(ooContObj) hDescCont = m_ooSession->container( m_topLevelContainer.c_str() );
00587   hDescCont.update();
00588   HepExplorableDescr::clustering = hDescCont;
00589   ooHandle(ooContObj) hGroupCont = m_ooSession->container( ( m_topLevelContainer + "_containerGroups").c_str() );
00590   hGroupCont.update();
00591   HepContainerGroup::clustering = hGroupCont;
00592 
00593   ooHandle(ooDBObj) hDbForData = m_ooSession->db( dbForData.c_str() );
00594   hDbForData.update();
00595   HepClusteringHint tagClusteringHint;
00596   tagClusteringHint.create( path.substr(1).c_str(), 1, hDbForData );
00597 
00598   HepExplorableGenericTags* myTag = new HepExplorableGenericTags;
00599   myTag->createDescription( path.substr(1).c_str(), &tagClusteringHint );
00600   hDescCont.close();
00601   hGroupCont.close();
00602   hDbForData.close();
00603   Anaphe::AIDA_ObjectivityStore::ObjyTuple* tupleData = new Anaphe::AIDA_ObjectivityStore::ObjyTuple( static_cast<HepExplorable&>(*myTag) );
00604   if ( ! tupleData->writeDescription( header ) ) {
00605     delete tupleData;
00606     myTag->removeDescription();
00607     delete myTag;
00608     OBJECTIVITY_STORE_REPORT_ERROR( "Could not write the tuple variable descriptions" );
00609     return false;
00610   }
00611   m_tuples.insert( std::make_pair( path, static_cast<HepExplorable*>( myTag ) ) );
00612   m_tupleData.insert( std::make_pair( path, tupleData ) );
00613   return true;
00614 }
00615 
00616 
00617 bool
00618 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::readTupleHeader( AIDA::Dev::ITupleHeader& header )
00619 {
00620   const std::string& path = header.pathInStore();
00621   std::map< std::string, HepExplorable* >::iterator iTuple = m_tuples.find( path );
00622   if ( iTuple == m_tuples.end() ) return false;
00623 
00624   std::auto_ptr<AIDA::Dev::IDevAnalysisFactory> af( dynamic_cast<AIDA::Dev::IDevAnalysisFactory*>( AIDA_createAnalysisFactory() ) );
00625   if ( ! af.get() ) {
00626     OBJECTIVITY_STORE_REPORT_ERROR( "The store could not instantiate an analysis factory" );
00627     return false;
00628   }
00629   AIDA::Dev::ITupleFactoryProvider* fp = af->tupleFactoryProvider();
00630   if ( ! fp ) {
00631     OBJECTIVITY_STORE_REPORT_ERROR( "The store could not retrieve the tuple factory provider from the analysis factory" );
00632     return false;
00633   }
00634 
00635   Anaphe::AIDA_ObjectivityStore::ObjyTuple* tupleData = new Anaphe::AIDA_ObjectivityStore::ObjyTuple( *( iTuple->second ) );
00636   if ( ! tupleData->readDescription( header, fp->devTupleFactory() ) ) {
00637     delete tupleData;
00638     OBJECTIVITY_STORE_REPORT_ERROR( "Could not read the tuple variable descriptions" );
00639     return false;
00640   }
00641   header.setNumberOfRows( very_big_integer );
00642   m_tupleData.insert( std::make_pair( path, tupleData ) );
00643   return true;
00644 }
00645 
00646 
00647 bool
00648 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::bindVariable( AIDA::Dev::ITupleHeader& header, int variableIndex )
00649 {
00650   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00651     m_tupleData.find( header.pathInStore() );
00652   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return 0;
00653   else return iTupleData->second->bindVariable( variableIndex );
00654 }
00655 
00656 
00657 void*
00658 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::variableAddress( AIDA::Dev::ITupleHeader& header, int variableIndex )
00659 {
00660   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00661     m_tupleData.find( header.pathInStore() );
00662   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return 0;
00663   else return iTupleData->second->variableAddress( variableIndex );
00664 }
00665 
00666 
00667 const void*
00668 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::variableAddress( const AIDA::Dev::ITupleHeader& header, int variableIndex ) const
00669 {
00670   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::const_iterator iTupleData =
00671     m_tupleData.find( header.pathInStore() );
00672   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return 0;
00673   else return iTupleData->second->variableAddress( variableIndex );
00674 }
00675 
00676 
00677 bool
00678 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::clearBindings( const AIDA::Dev::ITupleHeader& header )
00679 {
00680   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00681     m_tupleData.find( header.pathInStore() );
00682   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return false;
00683   else return iTupleData->second->clearBindings();
00684 }
00685 
00686 
00687 bool
00688 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::writeTupleRow( AIDA::Dev::ITupleHeader& header )
00689 {
00690   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00691     m_tupleData.find( header.pathInStore() );
00692   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return false;
00693   else return iTupleData->second->writeTupleRow( header.currentRowNumber() );
00694 }
00695 
00696 
00697 bool
00698 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::readTupleRow( AIDA::Dev::ITupleHeader& header )
00699 {
00700   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00701     m_tupleData.find( header.pathInStore() );
00702   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return false;
00703   else {
00704     int crn = header.currentRowNumber();
00705     bool foundRow = iTupleData->second->readTupleRow( crn );
00706     if ( foundRow ) return true;
00707     else {
00708       if ( crn < 0 ) return false;
00709       if ( header.numberOfRows() == very_big_integer ) {
00710         header.setNumberOfRows( crn );
00711       }
00712       return false;
00713     }
00714   }
00715 }
00716 
00717 
00718 bool
00719 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::resetTuple( AIDA::Dev::ITupleHeader& header )
00720 {
00721   std::map< std::string, Anaphe::AIDA_ObjectivityStore::ObjyTuple* >::iterator iTupleData =
00722     m_tupleData.find( header.pathInStore() );
00723   if ( iTupleData == m_tupleData.end() || iTupleData->second == 0 ) return false;
00724   else return iTupleData->second->reset();
00725 }
00726 
00727 
00728 void
00729 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::collectObjectTypes( const std::string& )
00730 {
00731   if ( m_optionParser->isNamingEnabled() ) {
00732     // ND
00733     return;
00734   }
00735 
00736   m_ooSession->db( m_topLevelDatabase.c_str() );
00737   ooItr(ooObj) objI;
00738   ooHandle(ooContObj) hCont = m_ooSession->container( m_topLevelContainer.c_str() ); // Retrieve histograms
00739   if ( hCont.contains(objI) ) {
00740     while ( objI.next() ) {
00741       if ( objI.typeN() == ooTypeN(PHisto1D) || objI.typeN() == ooTypeN(PHisto1DVar) ) {
00742         registerType( ooRef(ooObj)(objI), hCont, std::string( "IHistogram1D" ) );
00743       }
00744       else if ( objI.typeN() == ooTypeN(PHisto2D) || objI.typeN() == ooTypeN(PHisto2DVar) ) {
00745         registerType( ooRef(ooObj)(objI), hCont, std::string( "IHistogram2D" ) );
00746       }
00747       else if ( objI.typeN() == ooTypeN(PHisto3D) || objI.typeN() == ooTypeN(PHisto3DVar) ) {
00748         registerType( ooRef(ooObj)(objI), hCont, std::string( "IHistogram3D" ) );
00749       }
00750       else if ( objI.typeN() == ooTypeN(PProfileHisto) || objI.typeN() == ooTypeN(PProfileHistoVar) ) {
00751         registerType( ooRef(ooObj)(objI), hCont, std::string( "IProfile1D" ) );
00752       }
00753     }
00754   }
00755 
00756   HepExplorableDescr::clustering = hCont; // Retrieve the tuples
00757   std::vector<std::string> explorables;
00758   const char * c = HepExplorable::firstExplorableName();
00759   while ( c ) {
00760     explorables.push_back( std::string( c ) );
00761     c = HepExplorable::nextExplorableName();
00762   }
00763 
00764   const std::string tupleType = "ITuple";
00765   for ( std::vector<std::string>::const_iterator iDescription = explorables.begin();
00766         iDescription != explorables.end(); ++iDescription ) {
00767     std::string path = Anaphe::AIDA_ObjectivityStore::ObjyPathFormater::theFormater().formAbsolutePath( *iDescription );
00768     m_objectTypes.insert( std::make_pair( path, tupleType ) );
00769     m_tuples.insert( std::make_pair( path, HepExplorable::findExplorable( iDescription->c_str() ) ) );
00770   }
00771 
00772 }
00773 
00774 
00775 void
00776 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::commitAdd()
00777 {
00778   for ( std::set<std::string>::const_iterator iObj = m_objectsToAdd.begin();
00779         iObj != m_objectsToAdd.end(); ++iObj ) {
00780     AIDA::Dev::IDevManagedObject* object = m_objectRefs[ *iObj ];
00781 
00782     m_ooSession->db( m_topLevelDatabase.c_str() );
00783     ooHandle(ooContObj) hCont = m_ooSession->container( m_topLevelContainer.c_str() );
00784     hCont.update();
00785     ooHandle(ooObj) hHistogram;
00786     Anaphe::AIDA_ObjectivityStore::ObjyHistogramConverter converter;
00787     const std::string& type = object->userLevelClassType();
00788     if ( type == "IHistogram1D" ) {
00789       hHistogram = converter.convertToObjy( hCont,
00790                                             *( dynamic_cast< AIDA::IHistogram1D* >( object ) ) );
00791     }
00792     else if ( type == "IHistogram2D" ) {
00793       hHistogram = converter.convertToObjy( hCont,
00794                                             *( dynamic_cast< AIDA::IHistogram2D* >( object ) ) );
00795     }
00796     else if ( type == "IHistogram3D" ) {
00797       hHistogram = converter.convertToObjy( hCont,
00798                                             *( dynamic_cast< AIDA::IHistogram3D* >( object ) ) );
00799     }
00800     else if ( type == "IProfile1D" ) {
00801       hHistogram = converter.convertToObjy( hCont,
00802                                             *( dynamic_cast< AIDA::IProfile1D* >( object ) ) );
00803     }
00804     hHistogram.nameObj( hCont, iObj->c_str() );
00805     m_objectSysRefs.insert( std::make_pair( *iObj, ooRef(ooObj)( hHistogram ) ) );
00806 
00807     // add the object
00808     object->setUpToDate( true );
00809   }
00810   m_objectsToAdd.clear();
00811 }
00812 
00813 
00814 void
00815 Anaphe::AIDA_ObjectivityStore::AIDA_ObjyStore::registerType( ooRef(ooObj) ref, const ooHandle(ooContObj)& hCont,
00816                                                              const std::string& type )
00817 {
00818   AIDA::Dev::IDevManagedObject* dummyObject = 0;
00819   std::string sysName = ref.getObjName( hCont );
00820   if ( sysName == "" ) sysName = std::string( ref.sprint() );
00821   std::string path = Anaphe::AIDA_ObjectivityStore::ObjyPathFormater::theFormater().formAbsolutePath( sysName );
00822   m_objectTypes.insert( std::make_pair( path, type ) );
00823   m_objectRefs.insert( std::make_pair( path, dummyObject ) );
00824   m_objectSysRefs.insert( std::make_pair( path, ref ) );
00825 }
00826 

Generated on Tue Nov 19 12:32:33 2002 for AIDA_ObjectivityStore by doxygen1.2.16