|
||||
File indexing completed on 2025-01-17 09:55:17
0001 #ifndef _DB_STL_COMMON_H 0002 #define _DB_STL_COMMON_H 0003 0004 #ifdef DBSTL_DEBUG_LEAK 0005 #include "vld.h" 0006 #endif 0007 0008 #include <assert.h> 0009 0010 #include "db_cxx.h" 0011 0012 // In release builds, the native assert will be disabled so we 0013 // can't use it in dbstl in cases where we rely on the expression being 0014 // evaluated to change the state of the application. 0015 // 0016 #if !defined(DEBUG) && !defined(_DEBUG) 0017 #undef dbstl_assert 0018 #define dbstl_assert(expression) 0019 #else 0020 #undef dbstl_assert 0021 #define dbstl_assert(expression) do { \ 0022 if (!(expression)) { \ 0023 FailedAssertionException ex(__FILE__, __LINE__, #expression);\ 0024 throw ex; } } while (0) 0025 #endif 0026 0027 #if defined( DB_WIN32) || defined(_WIN32) 0028 #include <windows.h> 0029 #include <tchar.h> 0030 #else 0031 #define TCHAR char 0032 #define _T(e) (e) 0033 #define _ftprintf fprintf 0034 #define _snprintf snprintf 0035 #define _tcschr strchr 0036 #define _tcscmp strcmp 0037 #define _tcscpy strcpy 0038 #define _tcslen strlen 0039 #define _tgetopt getopt 0040 #define _tmain main 0041 #define _tprintf printf 0042 #define _ttoi atoi 0043 #endif 0044 0045 #undef SIZE_T_MAX 0046 // The max value for size_t variables, one fourth of 2 powers 32. 0047 #define SIZE_T_MAX 1073741824 0048 0049 // Macro for HAVE_WSTRING (detected by configure) 0050 0051 0052 // Thread local storage modifier declaration. 0053 #define TLS_DECL_MODIFIER __thread 0054 #define TLS_DEFN_MODIFIER __thread 0055 0056 #if !defined(TLS_DECL_MODIFIER) && !defined(HAVE_PTHREAD_TLS) 0057 #error "No appropriate TLS modifier defined." 0058 #endif 0059 0060 ////////////////////////////////////////////////////////////////////////// 0061 ///////////////////////////////////////////////////////////////////////// 0062 // 0063 // C++ compiler portability control macro definitions. 0064 // If a C++ compiler does not support the following capabilities, disabling 0065 // these flags will remove usage of the feature from DB STL. 0066 // Where possible a DB STL has implemented work-arounds for the missing 0067 // functionality. 0068 // 0069 #define HAVE_EXPLICIT_KEYWORD 1 0070 #define HAVE_NAMESPACE 1 0071 #define HAVE_TYPENAME 1 0072 0073 // Platform specific compiler capability configuration. 0074 #ifdef WIN32 0075 #define CLS_SCOPE(clstmpl_name) 0076 #else 0077 0078 // C++ standard: It is not possible to define a full specialized version of 0079 // a member function of a class template inside the class body. It needs to 0080 // be defined outside the class template, and must be defined in the namespace 0081 // scope. 0082 #define CLS_SCOPE(clstmpl_name) clstmpl_name:: 0083 #define NO_IN_CLASS_FULL_SPECIALIZATION 1 0084 #define NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION 1 0085 #endif 0086 0087 #if HAVE_NAMESPACE 0088 #define START_NS(nsname) namespace nsname { 0089 #define END_NS } 0090 #else 0091 #define START_NS(nsname) struct nsname { 0092 #define END_NS }; 0093 #endif 0094 0095 #if HAVE_EXPLICIT_KEYWORD 0096 #define EXPLICIT explicit 0097 #else 0098 #define EXPLICIT 0099 #endif 0100 0101 #if HAVE_TYPENAME 0102 #define Typename typename 0103 #else 0104 #define Typename class 0105 #endif 0106 0107 ////////////////////////////////////////////////////////////////////////// 0108 // End of compiler portability control macro definitions. 0109 //////////////////////////////////////////////////////////////////////// 0110 0111 ////////////////////////////////////////////////////////////////////////// 0112 ///////////////////////////////////////////////////////////////////////// 0113 // 0114 // Iterator status macro definitions. 0115 // 0116 #define INVALID_ITERATOR_POSITION -1 // Iterator goes out of valid range. 0117 #define INVALID_ITERATOR_CURSOR -2 // The iterator's dbc cursor is invalid. 0118 #define ITERATOR_DUP_ERROR -3 // Failed to duplicate a cursor. 0119 0120 // Current cursor's key or data dbt has no data. 0121 #define INVALID_KEY_DATA -4 0122 #define EMPTY_DBT_DATA -5 // Current cursor's pointed data dbt has no data. 0123 #define ITERATOR_AT_END -6 0124 #define CURSOR_NOT_OPEN -7 0125 0126 /////////////////////////////////////////////////////////////////////// 0127 // End of iterator status macro definitions. 0128 ////////////////////////////////////////////////////////////////////// 0129 0130 ////////////////////////////////////////////////////////////////////// 0131 ////////////////////////////////////////////////////////////////////// 0132 // 0133 // Helper macros definitions. 0134 // 0135 // Use BDBOP and BDBOP2 to wrap Berkeley DB calls. The macros validate the 0136 // return value. On failure, the wrappers clean up, and generate the 0137 // expected exception. 0138 // 0139 #define BDBOP(bdb_call, ret) do { \ 0140 if ((ret = (bdb_call)) != 0) throw_bdb_exception(#bdb_call, ret);\ 0141 } while(0) 0142 #define BDBOP2(bdb_call, ret, cleanup) do { \ 0143 if ((ret = (bdb_call)) != 0) { (cleanup); \ 0144 throw_bdb_exception(#bdb_call, ret);} \ 0145 } while (0) 0146 // Do not throw the exception if bdb_call returned a specified error number. 0147 #define BDBOP3(bdb_call, ret, exception, cleanup) do { \ 0148 if (((ret = (bdb_call)) != 0) && (ret & exception) == 0) { \ 0149 (cleanup); throw_bdb_exception(#bdb_call, ret);} \ 0150 } while (0) 0151 0152 #define THROW(exception_type, arg_list) do { \ 0153 exception_type ex arg_list; throw ex; } while (0) 0154 0155 #define THROW0(exception_type) do { \ 0156 exception_type ex; throw ex; } while (0) 0157 0158 #define INVALID_INDEX ((index_type)-1) 0159 #define INVALID_DLEN ((u_int32_t)-1) 0160 0161 #define DBSTL_MAX_DATA_BUF_LEN 1024 * 4096 0162 #define DBSTL_MAX_KEY_BUF_LEN 1024 * 4096 0163 #define DBSTL_MAX_MTX_ENV_MUTEX 4096 * 4 0164 #define DBSTL_BULK_BUF_SIZE 256 * 1024 0165 0166 #define COMPARE_CHECK(obj) if (this == &obj) return true; 0167 #define ASSIGNMENT_PREDCOND(obj) if (this == &obj) return obj; 0168 ////////////////////////////////////////////////////////////////// 0169 // End of helper macro definitions. 0170 ////////////////////////////////////////////////////////////////// 0171 0172 ////////////////////////////////////////////////////////////////// 0173 ////////////////////////////////////////////////////////////////// 0174 // 0175 // Public global function declarations. 0176 // These functions are open/public functionalities of dbstl for 0177 // dbstl users to call. 0178 // 0179 START_NS(dbstl) 0180 // _exported is a macro we employ from db_cxx.h of Berkeley DB C++ 0181 // API. If we want to export the symbols it decorates on Windows, 0182 // we must define the macro "DB_CREATE_DLL", as is defined in dbstl 0183 // project property. 0184 /// \defgroup dbstl_global_functions dbstl global public functions 0185 //@{ 0186 0187 /// \name Functions to close database/environments. 0188 /// Normally you don't have to close any database 0189 /// or environment handles, they will be closed automatically. 0190 /// Though you still have the following API to close them. 0191 //@{ 0192 /// Close pdb regardless of reference count. You must make sure pdb 0193 /// is not used by others before calling this method. 0194 /// You can close the underlying database of a container and assign 0195 /// another database with right configurations to it, if the configuration 0196 /// is not suitable for the container, there will be an 0197 /// InvalidArgumentException type of exception thrown. 0198 /// You can't use the container after you called close_db and before setting 0199 /// another valid database handle to the container via 0200 /// db_container::set_db_handle() function. 0201 /// \param pdb The database handle to close. 0202 _exported void close_db(Db *pdb); 0203 0204 /// Close all open database handles regardless of reference count. 0205 /// You can't use any container after you called close_all_dbs and 0206 /// before setting another valid database handle to the 0207 /// container via db_container::set_db_handle() function. 0208 /// \sa close_db(Db *); 0209 _exported void close_all_dbs(); 0210 0211 /// \brief Close specified database environment handle regardless of reference 0212 /// count. 0213 /// 0214 /// Make sure the environment is not used by any other databases. 0215 /// \param pdbenv The database environment handle to close. 0216 _exported void close_db_env(DbEnv *pdbenv); 0217 0218 /// \brief Close all open database environment handles regardless of 0219 /// reference count. 0220 /// 0221 /// You can't use the container after you called close_db and before setting 0222 /// another valid database handle to the container via 0223 /// db_container::set_db_handle() function. \sa close_db_env(DbEnv *); 0224 _exported void close_all_db_envs(); 0225 //@} 0226 0227 /// \name Transaction control global functions. 0228 /// dbstl transaction API. You should call these API rather than DB C/C++ 0229 /// API to use Berkeley DB transaction features. 0230 //@{ 0231 /// Begin a new transaction from the specified environment "env". 0232 /// This function is called by dbstl user to begin an external transaction. 0233 /// The "flags" parameter is passed to DbEnv::txn_begin(). 0234 /// If a transaction created from 0235 /// the same database environment already exists and is unresolved, 0236 /// the new transaction is started as a child transaction of that transaction, 0237 /// and thus you can't specify the parent transaction. 0238 /// \param env The environment to start a transaction from. 0239 /// \param flags It is set to DbEnv::txn_begin() function. 0240 /// \return The newly created transaction. 0241 /// 0242 _exported DbTxn* begin_txn(u_int32_t flags, DbEnv *env); 0243 0244 /// Commit current transaction opened in the environment "env". 0245 /// This function is called by user to commit an external explicit transaction. 0246 /// \param env The environment whose current transaction is to be committed. 0247 /// \param flags It is set to DbTxn::commit() funcion. 0248 /// \sa commit_txn(DbEnv *, DbTxn *, u_int32_t); 0249 /// 0250 _exported void commit_txn(DbEnv *env, u_int32_t flags = 0); 0251 0252 /// Commit a specified transaction and all its child transactions. 0253 /// \param env The environment where txn is started from. 0254 /// \param txn The transaction to commit, can be a parent transaction of a 0255 /// nested transaction group, all un-aborted child transactions of 0256 /// it will be committed. 0257 /// \param flags It is passed to each DbTxn::commit() call. 0258 /// \sa commit_txn(DbEnv *, u_int32_t); 0259 _exported void commit_txn(DbEnv *env, DbTxn *txn, u_int32_t flags = 0); 0260 0261 /// Abort current transaction of environment "env". This function is called by 0262 /// dbstl user to abort an outside explicit transaction. 0263 /// \param env The environment whose current transaction is to be aborted. 0264 /// \sa abort_txn(DbEnv *, DbTxn *); 0265 _exported void abort_txn(DbEnv *env); 0266 0267 /// Abort specified transaction "txn" and all its child transactions. 0268 /// That is, "txn" can be a parent transaction of a nested transaction group. 0269 /// \param env The environment where txn is started from. 0270 /// \param txn The transaction to abort, can be a parent transaction of a 0271 /// nested transaction group, all child transactions of it will be aborted. 0272 /// \sa abort_txn(DbEnv *); 0273 /// 0274 _exported void abort_txn(DbEnv *env, DbTxn *txn); 0275 0276 /// Get current transaction of environment "env". 0277 /// \param env The environment whose current transaction we want to get. 0278 /// \return Current transaction of env. 0279 _exported DbTxn* current_txn(DbEnv *env); 0280 0281 /// Set environment env's current transaction handle to be newtxn. The original 0282 /// transaction handle returned without aborting or commiting. This function 0283 /// is used for users to use one transaction among multiple threads. 0284 /// \param env The environment whose current transaction to replace. 0285 /// \param newtxn The new transaction to be as the current transaction of env. 0286 /// \return The old current transaction of env. It is not resolved. 0287 _exported DbTxn* set_current_txn_handle(DbEnv *env, DbTxn *newtxn); 0288 //@} 0289 0290 /// \name Functions to open and register database/environment handles. 0291 //@{ 0292 /// Register a Db handle "pdb1". This handle and handles opened in it will be 0293 /// closed by ResourceManager, so application code must not try to close or 0294 /// delete it. Users can do enough configuration before opening the Db then 0295 /// register it via this function. 0296 /// All database handles should be registered via this function in each 0297 /// thread using the handle. The only exception is the database handle opened 0298 /// by dbstl::open_db should not be registered in the thread of the 0299 /// dbstl::open_db call. 0300 /// \param pdb1 The database handle to register into dbstl for current thread. 0301 /// 0302 _exported void register_db(Db *pdb1); 0303 0304 /// Register a DbEnv handle env1, this handle and handles opened in it will be 0305 /// closed by ResourceManager. Application code must not try to close or delete 0306 /// it. Users can do enough config before opening the DbEnv and then register 0307 /// it via this function. 0308 /// All environment handles should be registered via this function in each 0309 /// thread using the handle. The only exception is the environment handle 0310 /// opened by dbstl::open_db_env should not be registered in the thread of 0311 /// the dbstl::open_db_env call. 0312 /// \param env1 The environment to register into dbstl for current thread. 0313 /// 0314 _exported void register_db_env(DbEnv *env1); 0315 0316 /// Helper function to open a database and register it into dbstl for the 0317 /// calling thread. 0318 /// Users still need to register it in any other thread using it if it 0319 /// is shared by multiple threads, via register_db() function. 0320 /// Users don't need to delete or free the memory of the returned object, 0321 /// dbstl will take care of that. 0322 /// When you don't use dbstl::open_db() but explicitly call DB C++ API to 0323 /// open a database, you must new the Db object, rather than create it 0324 /// on stack, and you must delete the Db object by yourself. 0325 /// \param penv The environment to open the database from. 0326 /// \param cflags The create flags passed to Db class constructor. 0327 /// \param filename The database file name, passed to Db::open. 0328 /// \param dbname The database name, passed to Db::open. 0329 /// \param dbtype The database type, passed to Db::open. 0330 /// \param oflags The database open flags, passed to Db::open. 0331 /// \param mode The database open mode, passed to Db::open. 0332 /// \param txn The transaction to open the database from, passed to Db::open. 0333 /// \param set_flags The flags to be set to the created database handle. 0334 /// \return The opened database handle. 0335 /// \sa register_db(Db *); 0336 /// \sa open_db_env; 0337 /// 0338 _exported Db* open_db (DbEnv *penv, const char *filename, DBTYPE dbtype, 0339 u_int32_t oflags, u_int32_t set_flags, int mode = 0644, DbTxn *txn = NULL, 0340 u_int32_t cflags = 0, const char* dbname = NULL); 0341 0342 /// Helper function to open an environment and register it into dbstl for the 0343 /// calling thread. Users still need to register it in any other thread if it 0344 /// is shared by multiple threads, via register_db_env() function above. 0345 /// Users don't need to delete or free the memory of the returned object, 0346 /// dbstl will take care of that. 0347 /// 0348 /// When you don't use dbstl::open_env() but explicitly call DB C++ API to 0349 /// open an environment, you must new the DbEnv object, rather than create it 0350 /// on stack, and you must delete the DbEnv object by yourself. 0351 /// \param env_home Environment home directory, it must exist. Passed to 0352 /// DbEnv::open. 0353 /// \param cflags DbEnv constructor creation flags, passed to DbEnv::DbEnv. 0354 /// \param set_flags Flags to set to the created environment before opening it. 0355 /// \param oflags Environment open flags, passed to DbEnv::open. 0356 /// \param mode Environment region files mode, passed to DbEnv::open. 0357 /// \param cachesize Environment cache size, by default 4M bytes. 0358 /// \return The opened database environment handle. 0359 /// \sa register_db_env(DbEnv *); 0360 /// \sa open_db; 0361 /// 0362 _exported DbEnv* open_env(const char *env_home, u_int32_t set_flags, 0363 u_int32_t oflags = DB_CREATE | DB_INIT_MPOOL, 0364 u_int32_t cachesize = 4 * 1024 * 1024, 0365 int mode = 0644, 0366 u_int32_t cflags = 0/* Flags for DbEnv constructor. */); 0367 //@} 0368 0369 /// @name Mutex API based on Berkeley DB mutex. 0370 /// These functions are in-process mutex support which uses Berkeley DB 0371 /// mutex mechanisms. You can call these functions to do portable 0372 /// synchronization for your code. 0373 //@{ 0374 /// Allocate a Berkeley DB mutex. 0375 /// \return Berkeley DB mutex handle. 0376 _exported db_mutex_t alloc_mutex(); 0377 /// Lock a mutex, wait if it is held by another thread. 0378 /// \param mtx The mutex handle to lock. 0379 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message. 0380 _exported int lock_mutex(db_mutex_t mtx); 0381 /// Unlock a mutex, and return immediately. 0382 /// \param mtx The mutex handle to unlock. 0383 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message. 0384 _exported int unlock_mutex(db_mutex_t mtx); 0385 /// Free a mutex, and return immediately. 0386 /// \param mtx The mutex handle to free. 0387 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message. 0388 _exported void free_mutex(db_mutex_t mtx); 0389 //@} 0390 0391 /// Close cursors opened in dbp1. 0392 /// \param dbp1 The database handle whose active cursors to close. 0393 /// \return The number of cursors closed by this call. 0394 _exported size_t close_db_cursors(Db* dbp1); 0395 0396 /// \name Other global functions. 0397 //@{ 0398 /// If there are multiple threads within a process that make use of dbstl, then 0399 /// this function should be called in a single thread mutual exclusively before 0400 /// any use of dbstl in a process; Otherwise, you don't need to call it, but 0401 /// are allowed to call it anyway. 0402 _exported void dbstl_startup(); 0403 0404 /// This function releases any memory allocated in the heap by code of dbstl, 0405 /// and close all DB handles in the right order. 0406 /// So you can only call dbstl_exit() right before the entire process exits. 0407 /// It will release any memory allocated by dbstl that have to live during 0408 /// the entire process lifetime. 0409 _exported void dbstl_exit(); 0410 0411 /// This function release all DB handles in the right order. The environment 0412 /// and database handles are only closed when they are not used by other 0413 /// threads, otherwise the reference cout is decremented. 0414 _exported void dbstl_thread_exit(); 0415 0416 /// Operators to compare two Dbt objects. 0417 /// \param d1 Dbt object to compare. 0418 /// \param d2 Dbt object to compare. 0419 _exported bool operator==(const Dbt&d1, const Dbt&d2); 0420 /// Operators to compare two DBT objects. 0421 /// \param d1 DBT object to compare. 0422 /// \param d2 DBT object to compare. 0423 _exported bool operator==(const DBT&d1, const DBT&d2); 0424 0425 /// If exisiting random temporary database name generation mechanism is still 0426 /// causing name clashes, users can set this global suffix number which will 0427 /// be append to each temporary database file name and incremented after each 0428 /// append, and by default it is 0. 0429 /// \param num Starting number to append to each temporary db file name. 0430 _exported void set_global_dbfile_suffix_number(u_int32_t num); 0431 //@} 0432 0433 //@} // dbstl_global_functions 0434 0435 // Internally used memory allocation functions, they will throw an exception 0436 // of NotEnoughMemoryException if can't allocate memory. 0437 _exported void * DbstlReAlloc(void *ptr, size_t size); 0438 _exported void * DbstlMalloc(size_t size); 0439 0440 _exported u_int32_t hash_default(Db * /*dbp*/, const void *key, u_int32_t len); 0441 0442 // Default string manipulation callbacks. 0443 _exported u_int32_t dbstl_strlen(const char *str); 0444 _exported void dbstl_strcpy(char *dest, const char *src, size_t num); 0445 _exported int dbstl_strncmp(const char *s1, const char *s2, size_t num); 0446 _exported int dbstl_strcmp(const char *s1, const char *s2); 0447 _exported int dbstl_wcscmp(const wchar_t *s1, const wchar_t *s2); 0448 _exported int dbstl_wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t num); 0449 _exported u_int32_t dbstl_wcslen(const wchar_t *str); 0450 _exported void dbstl_wcscpy(wchar_t *dest, const wchar_t *src, size_t num); 0451 0452 END_NS 0453 0454 ////////////////////////////////////////////////////////////////// 0455 // End of public global function declarations. 0456 ////////////////////////////////////////////////////////////////// 0457 0458 #endif /* !_DB_STL_COMMON_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |