00001 #ifndef __cdb__CDB_h__ 00002 #define __cdb__CDB_h__ 00003 /******************************************************************************* 00004 * ALMA - Atacama Large Millimiter Array 00005 * Copyright (c) European Southern Observatory, 2011 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * "@(#) $Id: cdb.h,v 1.27 2011/10/28 15:05:05 hsommer Exp $" 00022 * 00023 * who when what 00024 * -------- -------- ---------------------------------------------- 00025 * almadev 2011-10-28 created 00026 */ 00027 00028 #include "cdbField.h" 00029 00030 #include <map> 00031 #include <set> 00032 00033 #include "acsutil.h" 00034 #include "cdbExport.h" 00035 #include "ace/Singleton.h" 00036 00037 namespace cdb { 00038 class cdb_EXPORT Table; 00039 00040 // 00041 // DESCRIPTION: A record in a configuration database. 00042 // 00043 // A record stores a plethora of fields, each of them identified by a name. 00044 // The fields are stored in memory as a STL map, which guarantees fast 00045 // O(log N) 00046 // 00047 typedef std::map<String, Field> MapStringToField; 00048 typedef std::set<String> SetOfStrings; 00049 typedef Table* (*TableFactory)(int, char**, CORBA::ORB_ptr); 00050 00051 class cdb_EXPORT Record : protected MapStringToField 00052 { 00053 friend class Table; 00054 00055 Record(const Record&); 00056 Record &operator=(const Record&); 00057 00058 public: 00059 00060 00061 typedef MapStringToField::const_iterator const_iterator; 00062 const_iterator begin() const { return MapStringToField::begin(); } 00063 const_iterator end() const { return MapStringToField::end(); } 00064 const_iterator find(const String &str) const 00065 { return MapStringToField::find(str); } 00066 00067 void Clear(); 00068 00069 Record(const String &strRecord = "", Table *pTable = 0); 00070 ~Record(); 00071 00072 void SetOrigin(const String &strRecord, Table *pTable); 00073 00074 void CommitOnClose(Boolean b) { m_bCommitOnClose = b; }; 00075 Boolean Commit(); 00076 00077 // 00078 // 00079 const Field& operator[](const String &strName) const; 00080 00081 // 00082 // DESCRIPTION: 00083 // Get a field from the record. If it doesn't exist, use a default 00084 // value. 00085 // 00086 // PARAMETERS: 00087 // strName Name of the field. 00088 // fldDefault Default value which is returned if the field does not 00089 // exist yet. 00090 // 00091 // RETURN VALUE: 00092 // Returns the requested field or default value if it does not exist. 00093 // 00094 const Field& GetField(const String &strName, 00095 const Field &fldDefault) const; 00096 00097 // 00098 // DESCRIPTION: 00099 // Set a value of the field. 00100 // 00101 Boolean SetField(const String &strName, 00102 const Field &fldValue, 00103 Boolean bCreate = TRUE); 00104 00105 00106 00107 Boolean RemoveField(const String &strName); 00108 00109 const SetOfStrings::const_iterator GetFirstDirty() const 00110 { return m_setDirty.begin(); } 00111 const SetOfStrings::const_iterator GetLastDirty() const 00112 { return m_setDirty.end(); } 00113 00114 SetOfStrings &Dirty() { return m_setDirty; } 00115 const SetOfStrings &Dirty() const { return m_setDirty; } 00116 00117 MapStringToField &Map() { return *this; } 00118 const MapStringToField &Map() const { return *this; } 00119 00120 protected: 00121 iterator begin() { return MapStringToField::begin(); } 00122 iterator end() { return MapStringToField::end(); } 00123 00124 private: 00125 00126 Table *m_pTable; 00127 String m_strRecord; 00128 00129 Boolean m_bCommitOnClose; 00130 00131 SetOfStrings m_setDirty; 00132 }; 00133 00134 // 00135 // Separator between name components of the record, so that the name can 00136 // imply a hierarchy. 00137 // 00138 // Alternatives: 00139 // / 00140 // : VLT CCS database 00141 // \ Windows 00142 // 00143 00144 #define CDB_HIERARCHY_SEPARATOR ':' 00145 00146 #define CDB_RECORD_READABLE 1 00147 #define CDB_RECORD_WRITABLE 2 00148 #define CDB_RECORD_REMOVABLE 4 00149 00150 // 00151 // DESCRIPTION: A table in the configuration database. 00152 // 00153 class cdb_EXPORT Table 00154 { 00155 int m_nRefCount; 00156 Boolean m_bWriteLock; 00157 public: 00158 int _add_ref(void) { return ++m_nRefCount; } 00159 int _rem_ref(void) { return --m_nRefCount; } 00160 00161 typedef std::pair<String, Field> NamedField; 00162 typedef std::vector<Field> NamedFieldArray; 00163 00164 Table(); 00165 virtual ~Table(); 00166 00167 virtual Boolean isInitialized() = 0; 00168 00169 00170 // 00171 // DESCRIPTION: Read/write lock the table. 00172 // 00173 // Before using a table, a read/write lock must be put in place to: 00174 // 00175 // 1. Prevent the table from being deleted while still in use. 00176 // 2. Prevent several processes from simultaneously modifying the 00177 // table. 00178 // 00179 // To gain an exclusive write lock, set bExclusiveWrite to a TRUE 00180 // value. Such an operation might block, waiting for the current 00181 // exclusive write lock to be released. To prevent blocking in such 00182 // cases, set bNonBlocking to TRUE as well. 00183 // 00184 // EXAMPLE: 00185 // 00186 // SomeTable tbl; 00187 // tbl.Lock(TRUE, FALSE); 00188 // // ... 00189 // tbl.Unlock(TRUE); // don't forget to unlock! 00190 // 00191 Boolean Lock(Boolean bExclusiveWrite = 0); 00192 00193 Boolean Unlock(Boolean bExclusiveWrite = 0); 00194 00195 // 00196 // DESCRIPTION: Create a record in the table. 00197 // 00198 // Create an empty record in the table. 00199 // 00200 // PARAMETERS: 00201 // strRecordName - The name by which the newly created record 00202 // will be identified. 00203 // 00204 // RETURN VALUE: Returns TRUE if the record can be used, and FALSE if the 00205 // creation had failed. 00206 // 00207 virtual Boolean CreateRecord(const String &strRecordName, 00208 Boolean bTruncate = FALSE) = 0; 00209 00210 // 00211 // Returns TRUE if the record already exists. 00212 // 00213 virtual ULong GetRecordState(const String &strRecordName) = 0; 00214 00215 virtual Boolean GetField(const String &strRecordName, 00216 const String &strFieldName, 00217 Field &fld) = 0; 00218 virtual Boolean SetField(const String &strRecordName, 00219 const String &strFieldName, 00220 const Field &fld, 00221 Boolean bCreate = TRUE) = 0; 00222 virtual Boolean RemoveField(const String &strRecordName, 00223 const String &strFieldName) = 0; 00224 00225 // 00226 // DESCRIPTION: Get a record from the database. 00227 // 00228 // PARAMETERS: 00229 // strRecordName - The name of the record to retrieve from the database. 00230 // rec - Reference of the record to fill-in. 00231 virtual Boolean GetRecord(const String &strRecordName, 00232 Record &rec, 00233 Boolean bCreate = FALSE, 00234 Boolean bAppend = FALSE) = 0; 00235 virtual Boolean SetRecord(const String &strRecordName, 00236 const Record &rec, 00237 Boolean bCreate = TRUE, 00238 Boolean bAll = TRUE) = 0; 00239 virtual Boolean RemoveRecord(const String &strRecordName) = 0; 00240 00241 // ---------------------------------------------------------------------- 00242 // GROUP = Navigation 00243 // ---------------------------------------------------------------------- 00244 00245 // 00246 // DESCRIPTION: Get the name of the root record. 00247 // 00248 // PARAMETERS: 00249 // strRoot - Reference of the string where the root record's name will 00250 // be placed. This name can be later used with other methods 00251 // of the table. 00252 // 00253 // RETURN VALUE: 00254 // FALSE if the table does not have any support for hierarchical 00255 // ordering of its records, TRUE otherwise. 00256 // 00257 virtual Boolean GetRoot(String &strRoot) 00258 { strRoot = CDB_HIERARCHY_SEPARATOR; return TRUE; } 00259 00260 // 00261 // DESCRIPTION: Get all child-records of a given record. 00262 // 00263 // PARAMETERS: 00264 // strRecordName 00265 // 00266 virtual Boolean GetChildren(const String &strRecordName, 00267 StringArray &astrChildren) = 0; 00268 00269 virtual Boolean GetParent(const String &strRecordName, 00270 String &strParent); 00271 }; 00272 00273 typedef struct _tagTableEntry 00274 { 00275 String name; 00276 Table* table; 00277 } TableEntry; 00278 00279 class TableStorage 00280 { 00281 public: 00282 Table* find(const char* name ); 00283 Table* first(void); 00284 Boolean bind(const char* name, Table* table ); 00285 Boolean unbind( Table* table ); 00286 const char* getDefault( void ); 00287 void setDefault( const char* name ); 00288 00289 // registered types 00290 Boolean bindType(const char* name, TableFactory pTf ); 00291 TableFactory findType(const char* name); 00292 00293 protected: 00294 TableStorage(); 00295 ~TableStorage(); 00296 00297 typedef std::map<String, TableFactory> DBTypes; 00298 00299 std::vector<TableEntry> m_dbMap; 00300 DBTypes m_dbTypes; 00301 ACE_CString m_defaultTable; 00302 friend class ACE_Singleton<TableStorage, ACE_Null_Mutex>; 00303 }; 00304 00305 cdb_EXPORT Table* getDatabase( int argc = 0, char** argv = NULL, CORBA::ORB_ptr orb = CORBA::ORB::_nil(), const char* defaultTable= NULL, int forceNew = 0 ); 00306 cdb_EXPORT void destroyDatabase( Table* table ); 00307 cdb_EXPORT void registerTable( const char* name, TableFactory pTf ); 00308 00309 #include "cdb.i" 00310 00311 }; 00312 00313 00314 00315 #endif // __cdb__CDB_h__ 00316 00317 // ************************************************************************ 00318 // 00319 // REVISION HISTORY: 00320 // 00321 // $Log: cdb.h,v $ 00322 // Revision 1.27 2011/10/28 15:05:05 hsommer 00323 // Manually fixed "no LGPL license text" issue reported by addCopyright.py 00324 // 00325 // Revision 1.26 2006/09/01 02:20:54 cparedes 00326 // small change, NAMESPACE_BEGIN / NAMESPACE_END / NAMESPACE_USE macross to clean up a little the cpp code 00327 // 00328 // Revision 1.25 2003/07/09 08:07:35 bjeram 00329 // ported to gcc 3.2 00330 // 00331 // Revision 1.24 2003/01/28 16:43:49 vltsccm 00332 // gchiozzi: patch for cdb module to create lib/endorsed directory, since CVS cannot restore empty directories 00333 // 00334 // Revision 1.23 2003/01/24 10:44:02 vltsccm 00335 // cdb1.23 00336 // 00337 // Revision 1.22 2003/01/20 15:12:18 vltsccm 00338 // cdb1.22 00339 // 00340 // Revision 1.21 2003/01/20 10:45:52 vltsccm 00341 // cdb1.21 00342 // 00343 // Revision 1.20 2002/12/05 16:03:57 vltsccm 00344 // cdb1.20 00345 // 00346 // Revision 1.19 2002/11/25 16:04:48 vltsccm 00347 // cdb1.19 00348 // 00349 // Revision 1.18 2002/11/13 14:53:03 vltsccm 00350 // cdb1.18 00351 // 00352 // Revision 1.17 2002/11/13 10:22:29 vltsccm 00353 // cdb1.17 00354 // 00355 // Revision 1.16 2002/11/06 08:37:03 vltsccm 00356 // cdb1.16 00357 // 00358 // Revision 1.15.1.23 2002/11/05 16:05:12 vltsccm 00359 // cdb1.15.1.23 00360 // 00361 // Revision 1.15.1.22 2002/11/05 13:46:30 vltsccm 00362 // cdb1.15.1.22 00363 // 00364 // Revision 1.15.1.21 2002/11/05 10:41:13 vltsccm 00365 // cdb1.15.1.21 00366 // 00367 // Revision 1.15.1.20 2002/11/01 12:49:01 vltsccm 00368 // cdb1.15.1.20 00369 // 00370 // Revision 1.15.1.19 2002/10/30 07:56:43 vltsccm 00371 // cdb1.15.1.19 00372 // 00373 // Revision 1.15.1.18 2002/10/25 12:44:22 vltsccm 00374 // cdb1.15.1.18 00375 // 00376 // Revision 1.15.1.17 2002/10/24 13:08:43 vltsccm 00377 // cdb1.15.1.17 00378 // 00379 // Revision 1.15.1.16 2002/10/16 11:43:44 vltsccm 00380 // cdb1.15.1.16 00381 // 00382 // Revision 1.15.1.15 2002/10/14 22:26:08 vltsccm 00383 // cdb1.15.1.15 00384 // 00385 // Revision 1.15.1.14 2002/10/14 12:18:31 vltsccm 00386 // cdb1.15.1.14 00387 // 00388 // Revision 1.15.1.13 2002/10/04 16:20:22 vltsccm 00389 // cdb1.15.1.13 00390 // 00391 // Revision 1.15.1.12 2002/10/02 12:54:13 vltsccm 00392 // cdb1.15.1.12 00393 // 00394 // Revision 1.15.1.11 2002/10/01 10:33:24 vltsccm 00395 // cdb1.15.1.11 00396 // 00397 // Revision 1.15.1.10 2002/09/30 13:56:50 vltsccm 00398 // cdb1.15.1.10 00399 // 00400 // Revision 1.15.1.9 2002/09/26 14:13:09 vltsccm 00401 // cdb1.15.1.9 00402 // 00403 // Revision 1.15.1.8 2002/09/26 07:45:45 vltsccm 00404 // cdb1.15.1.8 00405 // 00406 // Revision 1.15.1.7 2002/09/17 16:19:20 vltsccm 00407 // cdb1.15.1.7 00408 // 00409 // Revision 1.15.1.6 2002/09/17 11:15:46 vltsccm 00410 // cdb1.15.1.6 00411 // 00412 // Revision 1.15.1.5 2002/09/02 09:37:05 vltsccm 00413 // cdb1.15.1.5 00414 // 00415 // Revision 1.15.1.4 2002/08/09 09:35:22 vltsccm 00416 // cdb1.15.1.4 00417 // 00418 // Revision 1.15.1.3 2002/07/24 07:29:09 vltsccm 00419 // cdb1.15.1.3 00420 // 00421 // Revision 1.15.1.2 2002/07/12 09:58:15 vltsccm 00422 // cdb1.15.1.2 00423 // 00424 // Revision 1.15+.1.1 2002/07/09 09:40:08 vltsccm 00425 // cdb1.15.1 00426 // 00427 // Revision 1.15 2002/02/05 17:50:07 vltsccm 00428 // cdb1.15 00429 // 00430 // Revision 1.14 2002/01/14 21:14:17 vltsccm 00431 // cdb1.14 00432 // 00433 // Revision 1.13 2001/10/19 09:56:21 vltsccm 00434 // cdb1.13 00435 // 00436 // Revision 1.12 2001/09/18 10:07:11 vltsccm 00437 // cdb1.12 00438 // 00439 // Revision 1.11 2001/07/12 07:48:25 vltsccm 00440 // cdb1.11 00441 // 00442 // Revision 1.10 2001/07/11 09:16:09 vltsccm 00443 // cdb1.10 00444 // 00445 // Revision 1.6 2000/12/07 18:00:40 vltsccm 00446 // cdb1.6 00447 // 00448 // Revision 1.5 2000/11/17 13:14:57 vltsccm 00449 // cdb1.5 00450 // 00451 // Revision 1.4 2000/10/20 13:51:19 vltsccm 00452 // cdb1.4 00453 // 00454 // Revision 1.3 2000/10/20 13:51:19 vltsccm 00455 // cdb1.3 00456 // 00457 // Revision 1.2 2000/10/20 13:51:18 vltsccm 00458 // cdb1.2 00459 // 00460 // Revision 1.1 2000/10/20 13:51:18 vltsccm 00461 // cdb1.1 00462 // 00463 // Revision 1.0 2000/10/20 13:51:18 vltsccm 00464 // cdb1.0 00465 // 00466 // Revision 1.3 2000/10/13 16:03:01 vltsccm 00467 // cdb1.3 00468 // 00469 // Revision 1.2 2000/09/13 14:49:28 vltsccm 00470 // cdb1.2 00471 // 00472 // Revision 1.1 2000/09/06 15:42:10 vltsccm 00473 // cdb1.1 00474 // 00475 // Revision 1.1 2000/06/13 07:26:24 kzagar 00476 // CDB, initial commit. Documentation not yet finished. 00477 // 00478 // ************************************************************************