Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

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

/BdbClusteringServer/BdbSrvUtil.cc

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: BdbSrvUtil.cc,v 1.19 2002/08/30 01:13:31 ryd Exp $
00004 //
00005 // Description:
00006 //      Transient class
00007 //      A set of utility functions used by the clustering server
00008 //
00009 // Environment:
00010 //      Software developed for the BaBar Detector at the SLAC B-Factory
00011 //
00012 // Author List:
00013 //      Jacek Becla                Original Author
00014 //
00015 // Copyright Information:
00016 //      Copyright (C) 2000      Stanford Linear Accelerator Center
00017 //
00018 //------------------------------------------------------------------------------
00019 
00020 
00021 
00022 #include "BdbClusteringServer/BdbSrvUtil.hh"
00023 
00024 
00025 #include "BdbUtil/BdbMutex.hh"
00026 #include "BdbApplication/BdbDomain.hh"
00027 #include "BdbAccess/BdbClustHintSetupMgr.hh"
00028 #include "BdbClusteringServer/BdbActiveDbSpec.hh"
00029 #include "BdbClusteringServer/BdbActiveDbsMgr.hh"
00030 #include "BdbClusteringServer/BdbClSrvCRef.hh"
00031 #include "BdbClusteringServer/BdbDbMetadataMgr.hh"
00032 #include "BdbClusteringServer/BdbFSSelector.hh"
00033 #include "BdbClusteringServer/BdbGroupDescr.hh"
00034 #include "BdbClusteringServer/BdbSrvLogMgr.hh"
00035 
00036 
00037 #include <iomanip.h>
00038 #include <iostream.h>
00039 #include <strstream.h>
00040 
00041 #include <set>
00042 using std::set;
00043 #include <map>
00044 using std::map;
00045 #include <string>
00046 using std::string;
00047 #include <vector>
00048 using std::vector;
00049 
00050 extern ooStatus BaBarObjyDummyErrorHandler(ooErrorLevel, ooError&, ooHandle(ooObj)*, char*);
00051 
00052 extern "C" {
00053 #include <unistd.h>     /* access */
00054 }
00055 
00056 
00057 unsigned long     BdbSrvUtil::_reqID           = 0;
00058 long              BdbSrvUtil::_lastReqTime     = 0;
00059 BdbClustHintSetupMgr* BdbSrvUtil::_chsMgr      = 0;
00060 BdbDbMetadataMgr* BdbSrvUtil::_dbmMgr          = 0;
00061 BdbMutex          BdbSrvUtil::_chsMutex("chsMgrMutex");
00062 BdbFSSelector*    BdbSrvUtil::_fsSelector      = 0;
00063 ooContext*        BdbSrvUtil::_exclLockCtx     = 0;
00064 ooTrans           BdbSrvUtil::_exclLockTrans;
00065 char*             BdbSrvUtil::_logDir          = 0;
00066 ooMode            BdbSrvUtil::_transMode       = oocNoOpen;
00067 const int         BdbSrvUtil::_noReqPerLog     = 100;
00068 unsigned long     BdbSrvUtil::_noCommits       = 0;
00069 int               BdbSrvUtil::_lockWait        = 30;// sec
00070 string         BdbSrvUtil::_lastErrorMsg    = "";
00071 const int         BdbSrvUtil::MAX_NO_CACHED_DBS = 5;
00072 BdbSrvUtil::DbName2Id* BdbSrvUtil::_dbs2ids     = new DbName2Id[MAX_NO_CACHED_DBS];
00073 BdbMutex          BdbSrvUtil::_dbCacheMutex("dbs2idsMutex");
00074 
00075 /**
00076  **  The function obtains an exclusive lock on a dummy database
00077  **  and holds that lock (in a separate context) as long as the 
00078  **  server is running.
00079  **/
00080 BdbStatus
00081 BdbSrvUtil::getExclusiveServerLock()
00082 {
00083     const char* dbName = "clustServerLock";    
00084 
00085     _exclLockCtx = new ooContext(6,10); // small cache ok
00086 
00087     ooContext* mainContext = ooContext::current();
00088     ooContext::setCurrent(_exclLockCtx);
00089     cout << "BdbSrvUtil::getExclusiveServerLock set ooContext to _exclLockCtx = " << _exclLockCtx << endl;
00090     ooRegErrorHandler(BaBarObjyDummyErrorHandler);
00091     
00092     _exclLockTrans.start(oocNoMROW, 1); // 3 sec - lock wait time (should be enough)
00093 
00094     BdbHandle(BdbFDObj)   fdH;
00095     BdbHandle(BdbDBObj)   dbH;
00096 
00097     BdbStatus status = BdbcSuccess;
00098     if ( BdbcSuccess != fdH.open(fdName(), oocUpdate) ) {
00099         cerr << "Unable to open fd " << fdName() << endl;
00100         _exclLockTrans.abort();
00101         status = BdbcError;
00102         goto end;
00103     }
00104     
00105     if ( ! dbH.exist(fdH, dbName, oocNoOpen) ) {
00106         dbH = new (fdH) ooDBObj (dbName, 1, 1);
00107         if ( BdbIsNull(dbH) ) {
00108             cerr << "Unable to create db " << dbName << endl;
00109             _exclLockTrans.abort();
00110             status = BdbcError;
00111             goto end;
00112         }        
00113     } else {
00114         if ( BdbcSuccess != dbH.exist(fdH, dbName, oocUpdate) ) {
00115             cerr << "Unable to get exclusive server lock (e)" << endl;
00116             _exclLockTrans.abort();
00117             status = BdbcError;
00118             goto end;
00119         }
00120     }
00121 
00122     if ( BdbcSuccess != dbH.lock(oocLockUpdate) ) {
00123         cerr << "Unable to get exclusive server lock (l)" << endl;
00124         _exclLockTrans.abort();
00125         status = BdbcError;
00126         goto end;
00127     }
00128 
00129 end:
00130     ooContext::setCurrent(mainContext);
00131     cout << "BdbSrvUtil::getExclusiveServerLock set ooContext to mainContext = " << mainContext << endl;
00132     if ( BdbcError == status ) {
00133         delete _exclLockCtx;
00134         _exclLockCtx = 0;
00135     }
00136     
00137     return status;
00138 }
00139 
00140 
00141 /**
00142  **   The function releases exclusive server lock
00143  **/
00144 BdbStatus
00145 BdbSrvUtil::releaseExclusiveServerLock()
00146 {
00147     ooContext* mainContext = ooContext::current();
00148     ooContext::setCurrent(_exclLockCtx);
00149     cout << "BdbSrvUtil::releaseExclusiveServerLock set ooContext to _exclLockCtx = " << _exclLockCtx << endl;
00150     BdbStatus status = _exclLockTrans.commit();
00151     ooContext::setCurrent(mainContext);
00152     cout << "BdbSrvUtil::releaseExclusiveServerLock set ooContext to mainContext = " << mainContext << endl;
00153     if ( 0 != _exclLockCtx ) {
00154         delete _exclLockCtx;
00155         _exclLockCtx = 0;
00156     }
00157     
00158     return status;
00159 }
00160 
00161 
00162 
00163 /**
00164  **  The function opens the log file (if path != 0 )
00165  **  or it binds the log stream to the bdbdebug
00166  **/ 
00167 BdbStatus
00168 BdbSrvUtil::initLogging(const char* logDir)
00169 {
00170     if ( 0 == logDir ) {
00171         cerr << "logDir = 0" << endl;
00172         return BdbcError;
00173     }
00174 
00175     if ( 0 != _logDir ) {
00176         cerr << "_logDir != 0" << endl;
00177         return BdbcError;
00178     }
00179     
00180     _logDir = new char [strlen(logDir)+1];
00181     strcpy(_logDir, logDir);
00182 
00183     // check if directory exists
00184     if ( 0 != access(_logDir, X_OK) ) {
00185         cerr << "Directory does not exist or wrong access rights" << endl;
00186         return BdbcError;
00187     }
00188     
00189     if ( BdbcSuccess != BdbSrvLogMgr::openLogFiles() ) {
00190         return BdbcError;
00191     }
00192 
00193     SRVLOG << PRINT_REQID << "Initialization" << endl;
00194     
00195     return BdbcSuccess;
00196 }
00197 
00198 
00199 BdbStatus
00200 BdbSrvUtil::shutdown()
00201 {
00202     if ( 0 != _logDir ) {
00203         delete [] _logDir;
00204         _logDir = 0;
00205     }
00206     if ( 0 != _chsMgr ) {
00207         delete _chsMgr;
00208         _chsMgr = 0;
00209     }
00210     if ( 0 != _dbmMgr ) {
00211         delete _dbmMgr;
00212         _dbmMgr = 0;
00213     }
00214     if ( 0 != _fsSelector ) {
00215         delete _fsSelector;
00216         _fsSelector = 0;
00217     }
00218 
00219     delete [] _dbs2ids;
00220     
00221     return BdbcSuccess;
00222 }
00223 
00224 
00225 
00226 
00227 BdbActiveDbSpec*
00228 BdbSrvUtil::initSpec(const BdbGroupDescr& descr)
00229 {
00230     BdbActiveDbSpec* spec = 0;
00231     if ( 0 == _chsMgr ) {
00232         SRVLOG << "_chsMgr not init" << endl;
00233         return spec;
00234     }
00235     
00236     const char* authLevelName = "User";
00237     if ( descr.authLevel() == 'g' ) {
00238         static const char* n = "Group";
00239         authLevelName = n;
00240     } else if ( descr.authLevel() == 's' ) {
00241         static const char* n = "System";
00242         authLevelName = n;
00243     }
00244 
00245     // retrieve all the info from transient cache maintained by BdbClustHintSetupMgr
00246     const  char* fullDName = BdbDomain::domainName(descr.sdName());
00247 
00248     // During parallel recovery many threads may call that at the same time.
00249     // Make sure the access is serialized.
00250     _chsMutex.lock();
00251 
00252     _chsMgr->setStrings(descr.clusterName(), fullDName, 
00253                         authLevelName, descr.authName(), descr.compName());
00254     
00255     d_ULong values[10];    
00256     _chsMgr->retrieveDomainSpec((unsigned*)values, 10);
00257 
00258     SRVLOG << "values for : " << fullDName << " "
00259            << descr.authLevel() << " "  << descr.authName()
00260            << " " << descr.compName() << "\n"
00261            << "   dirRange       = " << values[0] << "\n"
00262            << "   initNrPages    = " << values[1] << "\n"
00263            << "   percGrowth     = " << values[2] << "\n"
00264            << "   contHashing    = " << values[3] << "\n"
00265            << "   minNoPrecrDbs  = " << values[4] << "\n"
00266            << "   maxNoPrecrDbs  = " << values[5] << "\n"
00267            << "   precrNoConts   = " << values[6] << "\n"
00268            << "   maxDbSize      = " << values[7] << "\n"
00269            << "   prefContSize   = " << values[8] << "\n"
00270            << "   hardContLimit  = " << values[9] << endl;
00271 
00272     // convert all these values to what we really need
00273     // and use defaults if not defined
00274 
00275     spec = new BdbActiveDbSpec();
00276 
00277     int pageSize = BdbApplicationOrDomain::activeInstance()->fd().pageSize()/1024; // in KB
00278     spec->_hardPageLimit      = values[7] / pageSize;
00279     spec->_prefNoPagesPerCont = values[8] / pageSize;
00280     spec->_hardContLimit      = values[9];
00281     spec->_dirRange           = values[0];
00282     spec->_initNoPages        = values[1];
00283     spec->_percGrowth         = values[2];
00284     spec->_hashed             = values[3];
00285     spec->_minNoPrecrDbs      = values[4];
00286     spec->_maxNoPrecrDbs      = values[5];
00287 
00288     if ( values[7] < values[8] ) {
00289         SRVLOG << "WARNING: Incorrect values loaded via BdbFileConfigLoader: "
00290                << "(maxDbSize < prefContSize)" << endl;
00291         spec->_prefNoPagesPerCont = spec->_hardPageLimit;
00292     }
00293     spec->adjust();
00294 
00295     const char* cGroupName = _chsMgr->retrieveFSGroupName();
00296     char* groupName = 0;
00297     if ( ! _fsSelector->containsGroup(cGroupName) ) {
00298         vector<BdbTFileSystem*>* vector = 0;
00299         if ( BdbcSuccess != _chsMgr->retrieveFileSystems(groupName, vector) ) {
00300             SRVLOG << "ERR failed to retrieve file system info" << endl;
00301             delete spec;
00302             spec = 0;
00303             _chsMutex.unlock();
00304             return spec;
00305         }
00306         if ( BdbcSuccess != _fsSelector->addGroup(groupName, vector) ) {
00307             SRVLOG << "ERR failed to add group" << endl;
00308             delete spec;
00309             spec = 0;
00310             delete [] groupName;
00311             _chsMutex.unlock();
00312             return spec;
00313         }
00314     } else {
00315         groupName = new char [strlen(cGroupName)+1];
00316         strcpy(groupName, cGroupName);
00317     }
00318     
00319     spec->_fsGroupName = groupName;
00320     SRVLOG << "   fsGroupName    = " << groupName << endl;
00321     
00322     _chsMutex.unlock();
00323     return spec;
00324 }
00325 
00326 
00327 
00328 BdbStatus
00329 BdbSrvUtil::buildClientKey(const char* hostName, int pid,
00330                                 string& rwKey)
00331 {
00332     if ( hostName == 0 ) {
00333         rwKey = "in-process";
00334         return BdbcSuccess;
00335     }
00336     
00337     int length = strlen(hostName)+10;
00338     char* s = new char [length];
00339     ostrstream stream(s, length);
00340     stream << hostName << pid << ends;
00341 
00342     rwKey = s;
00343     delete [] s;
00344 
00345     return BdbcSuccess;
00346 }
00347 
00348 
00349 
00350 char*
00351 BdbSrvUtil::buildDbName(const char* sdName, 
00352                         const char* clusterName)
00353 {
00354     const char* fn = "BdbSrvUtil::buildDbName";
00355 
00356     if ( 0 == sdName ) {
00357         SRVLOG << fn << "sdName == 0" << endl;
00358         return 0;
00359     }
00360 
00361     const char* n = "_clustHintConfigSrv"; 
00362     int len = strlen(sdName) + strlen(n) + 3;
00363     if ( clusterName != 0 ) {
00364         len += strlen(clusterName);
00365     }
00366     
00367     char* dbName = new char [len];
00368     
00369     strcpy(dbName, sdName);
00370     strcat(dbName, n);
00371     if ( 0 != clusterName ) {
00372         strcat(dbName, "_");
00373         strcat(dbName, clusterName);
00374     }
00375     SRVLOG << "Db name is " << dbName << endl;
00376     return dbName;
00377 }
00378 
00379 
00380 
00381 /**
00382  **  The function scans the catalog, finds all
00383  **  clustering hint databases, and locates all
00384  **  registered groups. It fills the set, the set
00385  **  contains BdbClSrvCRef*
00386  **  Transaction is required, can be run in main thread only
00387  **  (used app->fd())
00388  **/
00389 BdbStatus
00390 BdbSrvUtil::findAllGroups(set<BdbClSrvCRef*,babar::Collection::PtrLess>& theset)
00391 {
00392     const char* fn = "BdbSrvUtil::findAllGroups";
00393     
00394     ooItr(ooDBObj) dbItr;
00395     if ( BdbcSuccess != BdbApplicationOrDomain::activeInstance()->fd().contains(dbItr) ) {
00396         SRVLOG << fn << " ERR Cannot initialize fd iterator" << endl;
00397         return BdbcError;
00398     }
00399 
00400     while ( dbItr.next() ) {
00401         const char* dbName = dbItr.name();
00402         if ( 0 != strstr(dbName, "clustHintConfigSrv") ) {
00403             // iterate over all containers
00404             ooRef(ooDBObj) dbR = dbItr;
00405             SRVLOG << "Iterating over all containers in db: " << dbItr.name() 
00406                    << ", oid = " << BdbPrintOID(dbR) << endl;
00407             ooItr(ooContObj) contItr;
00408             if ( dbR.contains(contItr, oocRead) != BdbcSuccess ) {
00409                 SRVLOG << "Cannot initialize container iterator in db " << dbName << endl;
00410                 return BdbcError;
00411             }
00412             // find all registered groups (one cont per group)
00413             while ( contItr.next() ) {
00414                 const char* contName = contItr.name();
00415 
00416                 ooRef(ooContObj) c = contItr;
00417                 SRVLOG << "   found container: oid = " << BdbPrintOID(c)
00418                        << ", name = \"" << contName << "\"" << endl;
00419                 
00420                 if ( 0 != strcmp(contName, "_ooDefaultContObj") ) {
00421                     ooRef(ooContObj) contR = contItr;
00422                     theset.insert(new BdbClSrvCRef(contR));
00423                 }
00424             }
00425         }
00426     }
00427     
00428     return BdbcSuccess;
00429 }
00430 
00431 
00432 /**
00433  **  Builds part of the full db path
00434  **  if event store: subDir     "evs/groups/isPhysicsEvents/aod/aod"
00435  **                  partDbName "evs_g_isPhysicsEvents_aod"
00436  **/
00437 BdbStatus
00438 BdbSrvUtil::constructPartDirAndDbName(const char* groupName, 
00439                                       char*& subDirs, 
00440                                       char*& partDbName)
00441 {
00442     BdbGroupDescr descr;
00443     if ( BdbcSuccess != splitName(groupName, descr) ) {
00444         SRVLOG << "splitName falied for groupName = " << groupName << endl;
00445         return BdbcError;
00446     }
00447     
00448     if ( strcmp(descr.sdName(), "evs") ) {
00449         SRVLOG << "constructPartDirAndDbName not implemented for domain: " 
00450                << descr.sdName() << endl;
00451         return BdbcError;
00452     }
00453     
00454     const char* fullDName = BdbDomain::domainName(descr.sdName());
00455     
00456     const char* authLevel = BdbDomain::authLevelName(descr.authLevel());
00457     int length = strlen(fullDName) + strlen(authLevel) + 
00458                  strlen(descr.authName())   + 2 * strlen(descr.compName()) + 8;
00459     
00460     subDirs = new char[length];
00461     ostrstream pStream(subDirs, length);
00462     pStream << fullDName << "/" << authLevel << "/" << descr.authName()
00463             << "/" << descr.compName() << "/" << descr.compName() << ends;
00464     
00465     SRVLOG << "Relative part of directory: " << subDirs << endl;
00466 
00467     length = strlen(descr.authName()) + 16;
00468     partDbName = new char [length];
00469     ostrstream dStream(partDbName, length);
00470     dStream << descr.sdName() << "_" << descr.authLevel() << "_" 
00471             << descr.authName() << "_" << descr.compName() << ends;
00472     SRVLOG << "Part of db name: " << partDbName << endl;
00473 
00474     return BdbcSuccess;
00475 }
00476 
00477 
00478 
00479 /**
00480  **  The function constructs database path and database name, eg
00481  ** subDir and partDbName
00482  **  if event store: "evs/groups/isPhysicsEvents/aod/aod" "evs_g_isPhysicsEvents_aod"
00483  **/
00484 BdbStatus
00485 BdbSrvUtil::constructFinalNamesAndPath(const char* baseDir,
00486                                        const char* subDirs,
00487                                        const char* partDbName,
00488                                        char*& fullPath,
00489                                        char*& dbSystemName,
00490                                        char*& dbFileName,
00491                                        d_ULong dbid, 
00492                                        d_ULong dirRange)
00493 {
00494     // build full path
00495     if ( baseDir == 0 || subDirs == 0 || partDbName == 0 ) {
00496         return BdbcError;
00497     }
00498     int length = strlen(baseDir) + strlen(subDirs) + 16;
00499     fullPath = new char [length];
00500 
00501     int leftRange  = ( dbid / dirRange ) * dirRange;
00502     int rightRange = leftRange + dirRange;
00503 #define HEXA_FORMAT setfill('0') << setw(6) << hex << setiosflags(ios::uppercase) 
00504     ostrstream pStream(fullPath, length);
00505     pStream << baseDir << "/" << subDirs 
00506             << HEXA_FORMAT << leftRange << "-" 
00507             << HEXA_FORMAT << rightRange << ends;
00508     SRVLOG << "Full path: " << fullPath << endl;
00509     
00510     // build db system name
00511     length = strlen(partDbName) + 8;
00512     dbSystemName = new char [length];
00513     ostrstream dName(dbSystemName, length);
00514     dName << partDbName << HEXA_FORMAT << dbid << ends;
00515     SRVLOG << "Db system name: " << dbSystemName << endl;
00516 #undef HEXA_FORMAT
00517     // build db file name
00518     length = strlen(dbSystemName) + 5;
00519     dbFileName = new char [length];
00520     ostrstream fName(dbFileName, length);
00521     fName << dbSystemName << ".bdb" << ends;
00522     SRVLOG << "Db file name: " << dbFileName << endl;
00523 
00524     return BdbcSuccess;
00525 }
00526 
00527 BdbStatus
00528 BdbSrvUtil::startTrans(ooMode mode)
00529 {
00530     if ( _transMode != oocNoOpen ) {
00531         SRVLOG << "ERR: trans already running" << endl;
00532         return BdbcError;
00533     }
00534     
00535     BdbStatus status;
00536     
00537     if ( mode == oocRead ) {
00538         status = BdbApplicationOrDomain::activeInstance()->startRead("ad");
00539     } else if ( mode == oocUpdate ) {
00540         status = BdbApplicationOrDomain::activeInstance()->startUpdate("ad");
00541     }
00542     
00543     if ( BdbcSuccess == status ) {
00544         _transMode = mode;
00545     }
00546     return status;
00547 }
00548 
00549     
00550 BdbStatus
00551 BdbSrvUtil::commitTrans()
00552 {
00553     if ( _transMode == oocNoOpen ) {
00554         SRVLOG << "ERR: trans not opened" << endl;
00555         return BdbcError;
00556     }
00557     
00558     BdbStatus status = BdbApplicationOrDomain::activeInstance()->commit("ad");
00559     if ( BdbcSuccess != status ) {
00560         SRVLOG << "ERR: cannot commit transaction" << endl;
00561     } else {
00562         _transMode = oocNoOpen;
00563     }
00564     _noCommits++;
00565     
00566     return status;
00567 }
00568 
00569 
00570 BdbStatus
00571 BdbSrvUtil::abortTrans()
00572 {
00573     if ( _transMode == oocNoOpen ) {
00574         SRVLOG << "ERR: trans not opened" << endl;
00575         return BdbcError;
00576     }
00577     
00578     BdbStatus status = BdbApplicationOrDomain::activeInstance()->commit("ad");
00579     if ( BdbcSuccess != status ) {
00580         SRVLOG << "ERR: cannot abort transaction" << endl;
00581     } else {
00582         SRVLOG << "Transaction aborted" << endl;
00583         _transMode = oocNoOpen;
00584     }
00585     return status;
00586 }
00587 
00588 
00589 
00590 unsigned long 
00591 BdbSrvUtil::getReqID()
00592 {
00593     return _reqID;
00594 }
00595 
00596 
00597 void 
00598 BdbSrvUtil::incrReqID()
00599 {
00600     _reqID++;
00601     _lastReqTime = time(0);
00602 }
00603 
00604 
00605 
00606 
00607 //raw_cl1_evs_g_isPhysicsEvents_stream
00608 BdbStatus
00609 BdbSrvUtil::splitName(const char* name, BdbGroupDescr& descr)
00610 {
00611     SRVLOG << "BdbSrvUtil::splitName: name = " << name << endl;
00612     
00613     if ( 0 == name ) {
00614         return BdbcError;
00615     }
00616     
00617     int pos = 0;
00618     // comp name
00619     const char* p1 = strchr(name, '_');
00620     if ( p1 <= 0  ) {
00621         SRVLOG << "Cannot find first _ in " << name << endl;
00622         return BdbcError;
00623     }
00624     int len = p1 - name;
00625     char* compName = new char [len+1];
00626     strncpy(compName, name, len);
00627     compName[len] = '\0';
00628     pos += len+1;
00629     // cluster name
00630     const char* p2 = strchr(name+pos, '_');
00631     len = p2 - p1 - 1;
00632     char* clusterName = 0;
00633     if ( len > 0 ) {
00634         clusterName = new char [len+1];
00635         strncpy(clusterName, name+pos, len);
00636         clusterName[len] = '\0';
00637     }
00638     pos += len + 1;
00639     // domain name
00640     const char* p3 =  strchr(name+pos, '_');
00641     len = p3 - p2 - 1;
00642     if ( len <= 0 ) {
00643         SRVLOG << "Cannot find third _ in " << name << endl;
00644         return BdbcError;
00645     }
00646     char* sdName = new char [len+1];
00647     strncpy(sdName, name+pos, len);
00648     sdName[len] = '\0';
00649     pos += len+1;
00650     // auth level
00651     p3 += 2;
00652     char authLevel = *(name+pos);
00653     pos += 2;
00654     // auth name
00655     const char* p4 =  strchr(name+pos, '_');
00656     len = p4 - p3 - 1;
00657     if ( len <= 0 ) {
00658         SRVLOG << "Cannot find 4th _ in " << name << endl;
00659         return BdbcError;
00660     }
00661     char* authName = new char [len+1];
00662     strncpy(authName, name+pos, len);
00663     authName[len] = '\0';
00664     pos += len+1;
00665     // stream name
00666     char* streamName = 0;
00667     if ( name+pos > 0 ) {
00668         len = strlen(name+pos);
00669         streamName = new char [len+1];
00670         strcpy(streamName, name+pos);
00671     }
00672     if ( BdbcSuccess != descr.set(clusterName, sdName, authLevel, 
00673                                   authName, compName, streamName) ) {
00674         return BdbcError;
00675     }
00676     if ( 0 != clusterName ) delete [] clusterName;
00677     if ( 0 != sdName      ) delete [] sdName;
00678     if ( 0 != authName    ) delete [] authName;
00679     if ( 0 != compName    ) delete [] compName;
00680     if ( 0 != streamName  ) delete [] streamName;
00681         
00682     return BdbcSuccess;
00683 }
00684 
00685 
00686 /**
00687  **  The function converts group name to short domain name.
00688  **  Function calling this one should deallocate the string
00689  **/
00690 char* 
00691 BdbSrvUtil::groupName2SDName(const char* groupName)
00692 {
00693     BdbGroupDescr descr;
00694     if ( BdbcSuccess != splitName(groupName, descr) ) {
00695         return 0;
00696     }
00697     
00698     char* s = new char [strlen(descr.sdName())+1];
00699     strcpy(s, descr.sdName());
00700     return s;
00701 }
00702 
00703 
00704 
00705 
00706     
00707 
00708 const char* 
00709 BdbSrvUtil::fdName()
00710 {
00711     return BdbApplicationOrDomain::activeInstance()->bootName();
00712 }
00713 
00714 
00715 
00716 
00717 void
00718 BdbSrvUtil::cacheDbInfo(const char* name, int id)
00719 {
00720     _dbCacheMutex.lock();
00721     
00722     int i;
00723     d_Boolean  full = d_True;
00724     for (i=0 ; i<MAX_NO_CACHED_DBS ; i++) {
00725         if ( (_dbs2ids[i]).id == id ) {
00726             _dbCacheMutex.unlock();
00727             cout << "entry already in (id = " << id << ")" << endl;
00728             return;
00729         }
00730         if ( (_dbs2ids[i]).id == 0 ) {
00731             full = d_False;
00732         }
00733         
00734     }
00735     if ( full ) {
00736         _dbCacheMutex.unlock();
00737         cout << "Cannot cache more than " << MAX_NO_CACHED_DBS << " (cache full)" << endl;
00738         return;
00739     }
00740     
00741     for (i=0 ; i<MAX_NO_CACHED_DBS ; i++) {
00742         if ( (_dbs2ids[i]).id == 0 ) {
00743             (_dbs2ids[i]).set(name, id);
00744             _dbCacheMutex.unlock();
00745             cout << "entry cached on pos " << i << endl;
00746             return;
00747         }
00748     }
00749     _dbCacheMutex.unlock();
00750 }
00751 
00752 int
00753 BdbSrvUtil::getDbId(const char* dbName)
00754 {
00755     int i, id = 0;
00756     _dbCacheMutex.lock();
00757     for (i=0 ; i<MAX_NO_CACHED_DBS ; i++) {
00758         if ( (_dbs2ids[i]).itIsMe(dbName) ) {
00759             id = (_dbs2ids[i]).id;
00760             break;
00761         }
00762     }
00763     _dbCacheMutex.unlock();
00764     return id;
00765 }
00766 
00767     

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002