Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbBdbSSimpleClusterP.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSSimpleClusterP.cc,v 1.5 2004/08/06 05:54:25 bartoldu Exp $
00003 
00004 /// Implementation file for the CdbBdbSSimpleClusterP class
00005 /**
00006   * @see CdbBdbSSimpleClusterP
00007   */
00008 
00009 #include "BaBar/BaBar.hh"
00010 
00011 #include "CdbBdbShared/CdbBdbSSimpleClusterP.hh"
00012 #include "CdbBdbShared/CdbBdbSUtils.hh"
00013 #include "CdbBdbShared/CdbBdbSMetaDataP.hh"
00014 #include "CdbBdbShared/CdbBdbSConditionP.hh"
00015 
00016 #include "BdbClustering/BdbCondIndexClusteringHint.hh"
00017 
00018 #include <stdio.h>
00019 #include <iostream>
00020 
00021 #include <string>
00022 using std::cout;
00023 using std::endl;
00024 
00025 CdbBdbSSimpleClusterP::CdbBdbSSimpleClusterP( const char*    theName,
00026                                               d_UShort       theId,
00027                                               const char*    theDescription,
00028                                               const BdbTime& theCreationTime ) :
00029     CdbBdbSClusterP( theName,
00030                      theId,
00031                      theDescription,
00032                      theCreationTime )
00033 { }
00034 
00035 CdbBdbSSimpleClusterP::~CdbBdbSSimpleClusterP( ) 
00036 { }
00037 
00038 d_UShort
00039 CdbBdbSSimpleClusterP::numIncrements( ) const
00040 {
00041     return _increments.numIncrements( );
00042 }
00043 
00044 CdbStatus
00045 CdbBdbSSimpleClusterP::increment( d_UShort          theNumber,
00046                                   CdbBdbSIncrement& theIncrement ) const
00047 {
00048     return _increments.increment( theNumber,
00049                                   theIncrement );
00050 }
00051 
00052 CdbStatus
00053 CdbBdbSSimpleClusterP::createIncrement( const BdbTime& theSplitTime )
00054 {
00055     const char* errorStr = "CdbBdbSSimpleClusterP::createIncrement() -- ERROR";
00056 
00057     ooUpdate( );
00058 
00059     CdbStatus result = CdbStatus::Error;
00060     do {
00061 
00062       // Locate the highest old increment number
00063 
00064         d_UShort oldIncrementNumber = 0;
00065         {
00066             d_UShort num = _increments.numIncrements( );
00067 
00068             assert( num >= 1 );      // FATAL ERROR: There must be at least 1 increment
00069                                      //              in the cluster.
00070             oldIncrementNumber = num - 1;
00071         }
00072 
00073       // Create new increment number
00074 
00075         if( CdbStatus::Success != _increments.createIncrement( theSplitTime )) {
00076             cout << errorStr << endl
00077                  << "    Failed to create the new cluster increment." << endl;
00078             break;
00079         }
00080 
00081       // Locate the highest new increment number
00082 
00083         d_UShort newIncrementNumber = 0;
00084         {
00085             d_UShort num = _increments.numIncrements( );
00086 
00087             assert( num >= 2 );      // FATAL ERROR: There must be at least 1+1 increment
00088                                      //              in the cluster (the last one we've just
00089                                      //              created)
00090             newIncrementNumber = num - 1;
00091         }
00092 
00093       // Build the name of the new database for the increment
00094 
00095         std::string localRangeName;
00096         {
00097             CdbStatus returnStatus = CdbBdbSUtils::getLocalDbIdRange( localRangeName );
00098             assert( CdbStatus::Success == returnStatus );
00099         }
00100         std::string newDatabaseSuffix;
00101         {
00102             char clusterIdStr[12];
00103             char clusterNewIncrementStr[12];
00104 
00105             sprintf( clusterIdStr, "%u", id( ));
00106             sprintf( clusterNewIncrementStr, "%u", newIncrementNumber );
00107 
00108             newDatabaseSuffix = std::string( "c" )  + std::string( clusterIdStr ) +
00109                                 std::string( "_i" ) + std::string( clusterNewIncrementStr );
00110 
00111             newDatabaseSuffix = newDatabaseSuffix +
00112                                 std::string( "_" ) +
00113                                 localRangeName;
00114         }
00115 
00116       // Create the new database by asking to create the "System" container
00117       // in it.
00118 
00119         BdbCondIndexClusteringHint systemHint( "cdb",
00120                                                "System",
00121                                                newDatabaseSuffix.c_str( ));
00122 
00123         BdbHandle(BdbContObj) systemContH;
00124         systemContH = systemHint.updatedHint( );
00125 
00126         assert( ! BdbIsNull(systemContH));
00127         assert( systemHint.newContainerCreated( ));
00128 
00129       // For each condition in this cluster
00130 
00131         CdbItr< BdbRef(CdbBdbSConditionP) > itr;
00132         if( CdbStatus::Success != iterator( itr )) {
00133             cout << errorStr << endl
00134                  << "    Failed to obtain the conditions - members of the cluster." << endl;
00135             break;
00136         }
00137 
00138         bool failed = false;
00139 
00140         while( itr.next( )) {
00141 
00142           // Locate condition
00143 
00144             BdbRef(CdbBdbSConditionP) conditionRef = itr.value( );
00145             assert( !BdbIsNull(conditionRef));
00146 
00147             d_UShort conditionId = conditionRef->id( );
00148 
00149           // Locate topmost metadata of the old increment
00150 
00151             BdbRef(CdbBdbSMetaDataP) oldMetaDataRef;
00152             if( CdbStatus::Success != CdbBdbSUtils::findMetaData( oldMetaDataRef,
00153                                                                   conditionId,
00154                                                                   false,        // non-partitionable
00155                                                                   0,            // partition identifier
00156                                                                   id( ),        // current cluster identifier
00157                                                                   oldIncrementNumber,
00158                                                                   localRangeName )) {
00159 
00160                 cout << errorStr << endl
00161                      << "    Failed to find MetaData object for increment #" << oldIncrementNumber << endl;
00162 
00163                 failed = true;
00164                 break;
00165             }
00166 
00167           // Close  the found metadata object.
00168           //
00169           // NOTE: The closure procedure will also create a final revision in
00170           //       in the metadata container at the most recent modification
00171           //       time of that metadata container.
00172 
00173             BdbTime modificationTime = oldMetaDataRef->modified( );
00174             char finalRevisionNameStr[32];
00175 
00176             sprintf( finalRevisionNameStr,
00177                      "final_%u.%u",
00178                      modificationTime.getGmtSec( ),
00179                      modificationTime.getGmtNsec( ));
00180 
00181             if( CdbStatus::Success != oldMetaDataRef->close( theSplitTime,
00182                                                              finalRevisionNameStr,
00183                                                              theSplitTime,
00184                                                              "automatically created by the increment creation procedure" )) {
00185                 cout << errorStr << endl
00186                      << "    Failed to close the old MetaData object." << endl;
00187 
00188                 failed = true;
00189                 break;
00190             }
00191 
00192           // Create new metadata object for this condition in the new database
00193           //
00194           //   NOTE: We need to extend the API of MetaData object to pass it a reference onto
00195           //         the previous metadata object to be initialized from.
00196 
00197             char conditionIdStr[12];
00198             sprintf( conditionIdStr, "%u", conditionId );
00199 
00200             std::string mdContainerName = std::string( "MetaData." ) + std::string( conditionIdStr );
00201             std::string obContainerName = std::string( "Objects."  ) + std::string( conditionIdStr );
00202 
00203           // Create containers for metadata and condition objects at specified
00204           // database.
00205 
00206             BdbCondIndexClusteringHint mdHint( "cdb",
00207                                                mdContainerName.c_str( ),
00208                                                newDatabaseSuffix.c_str( ));
00209 
00210             BdbHandle(BdbContObj) mdContH;
00211             mdContH = mdHint.updatedHint( );
00212 
00213             assert( ! BdbIsNull(mdContH));
00214             assert( mdHint.newContainerCreated( ));
00215 
00216             BdbCondIndexClusteringHint obHint( "cdb",
00217                                                obContainerName.c_str( ),
00218                                                newDatabaseSuffix.c_str( ));
00219 
00220             BdbHandle(BdbContObj) obContH;
00221             obContH = obHint.updatedHint( );
00222 
00223             assert( ! BdbIsNull(obContH));
00224             assert( obHint.newContainerCreated( ));
00225 
00226           // Create initial infrastructure in the newely created
00227           // container for metadata. The new medatada will have the same validity interval
00228           // as the old, and its begin insertion time will be where the old one ends.
00229           //
00230           // NOTE: Remember, that intervals collections of the new metadata object
00231           //       will be incomplete since they will not include intervals from previous
00232           //       increments. It's up to the intervals locator algorithm running on the top
00233           //       of metadata objects to resolve this issue.
00234 
00235             BdbHandle(CdbBdbSMetaDataP) newMetaDataH;
00236             newMetaDataH = new( mdContH ) CdbBdbSMetaDataP( oldMetaDataRef->minValidity( ), // begin of the validity
00237                                                             oldMetaDataRef->maxValidity( ), // end   of the validity
00238                                                             theSplitTime );
00239             assert( ! BdbIsNull(newMetaDataH));
00240 
00241           //  Name the metadata structure to be able to discover it later.
00242 
00243             BdbStatus status;
00244             status = newMetaDataH.nameObj( mdContH,
00245                                           "MetaData" );
00246             assert( BdbcSuccess == status );
00247         }
00248         if( failed ) break;
00249 
00250       // Done
00251 
00252         result = CdbStatus::Success;
00253 
00254     } while( false );
00255 
00256     return result;
00257 }
00258 
00259 /////////////////
00260 // End Of File //
00261 /////////////////

Generated on Mon Dec 5 18:22:03 2005 for CDB by doxygen1.3-rc3