Annotation of parser3/src/main/pa_db_connection.C, revision 1.14

1.1       parser      1: /** @file
1.11      paf         2:        Parser: sql driver connection implementation.
1.1       parser      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      6: 
1.14    ! paf         7:        $Id: pa_db_connection.C,v 1.13 2001/10/26 13:48:19 paf Exp $
1.1       parser      8: */
                      9: 
                     10: #include "pa_config_includes.h"
                     11: #ifdef HAVE_LIBDB
                     12: 
                     13: #include "pa_db_connection.h"
1.11      paf        14: #include "pa_db_table.h"
1.1       parser     15: #include "pa_exception.h"
1.11      paf        16: #include "pa_threads.h"
                     17: #include "pa_stack.h"
                     18: #include "pa_common.h"
1.1       parser     19: 
1.14    ! paf        20: // defines
        !            21: 
        !            22: #define DB_ERROR_PREFIX "db_err: "
        !            23: #define DB_EXCEPTION_NO_LOG_MESSAGE1 DB_ERROR_PREFIX"Ignoring log file"
        !            24: #define DB_EXCEPTION_NO_LOG_MESSAGE2 DB_ERROR_PREFIX"log_get: unable to find checkpoint record"
        !            25: 
1.11      paf        26: // consts
                     27: 
                     28: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
                     29: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
                     30: 
                     31: // callbacks
1.10      parser     32: 
1.11      paf        33: static void db_paniccall(DB_ENV *dbenv, int error) {
                     34:        throw Exception(0, 0, 
                     35:                0, 
                     36:                "db_panic: %s (%d)", 
                     37:                strerror(error), error);
                     38: }
                     39: 
                     40: static void db_errcall(const char *, char *buffer) {
                     41:        throw Exception(0, 0, 
                     42:                0, 
1.14    ! paf        43:                DB_ERROR_PREFIX "%s", 
1.11      paf        44:                buffer);
                     45: }
1.5       parser     46: 
1.13      paf        47: static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
                     48:        DB_Connection& table=*static_cast<DB_Connection *>(value);
1.11      paf        49:        time_t older_dies=reinterpret_cast<time_t>(info);
1.5       parser     50: 
1.13      paf        51:        if(table.expired(older_dies)) {
                     52:                table.~DB_Connection();  value=0;
                     53:        }
1.11      paf        54: }
1.5       parser     55: 
1.3       parser     56: // DB_Connection
                     57: 
1.13      paf        58: DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
                     59:        time_used(0),
                     60:        fdb_home(adb_home), 
                     61:        table_cache(apool),
1.11      paf        62:        prev_expiration_pass_time(0) {
                     63:        //_asm  int 3;
                     64:        char DB_DATA_DIR__VALUE[MAX_STRING];
                     65:        char DB_LOG_DIR__VALUE[MAX_STRING];
                     66:        char DB_TMP_DIR__VALUE[MAX_STRING];
                     67: 
                     68:        const char *db_home_cstr=fdb_home.cstr(String::UL_FILE_SPEC);
                     69: 
                     70:        snprintf(DB_DATA_DIR__VALUE, MAX_STRING, "DB_DATA_DIR %s", db_home_cstr);
                     71:        snprintf(DB_LOG_DIR__VALUE, MAX_STRING, "DB_LOG_DIR %s", db_home_cstr);
                     72:        snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
                     73: 
                     74:        char *db_config[] = {
                     75:                DB_DATA_DIR__VALUE,
                     76:                DB_LOG_DIR__VALUE,
                     77:                DB_TMP_DIR__VALUE, 
                     78:                0
                     79:        };
                     80: 
                     81:        u_int32_t flags=
1.13      paf        82:                DB_THREAD
                     83:                | DB_CREATE 
1.11      paf        84:                | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
                     85: 
1.14    ! paf        86:        try {
1.11      paf        87:                // 1: trying to open with SOFT RECOVER option set
1.14    ! paf        88:                memset(&dbenv, 0, sizeof(dbenv));
1.11      paf        89:                check("db_appinit soft recover", &fdb_home, db_appinit(
                     90:                        db_home_cstr,
                     91:                        db_config, 
                     92:                        &dbenv, 
                     93:                        flags | DB_RECOVER
                     94:                ));
1.14    ! paf        95:        } catch(const Exception& e) {
        !            96:                if(
        !            97:                        strncmp(
        !            98:                                e.comment(), 
        !            99:                                DB_EXCEPTION_NO_LOG_MESSAGE1, 
        !           100:                                strlen(DB_EXCEPTION_NO_LOG_MESSAGE1))==0 || 
        !           101:                        strncmp(
        !           102:                                e.comment(), 
        !           103:                                DB_EXCEPTION_NO_LOG_MESSAGE2, 
        !           104:                                strlen(DB_EXCEPTION_NO_LOG_MESSAGE2))==0) {
        !           105:                        // no log = no SOFT RECOVER
        !           106:                        // 2.1: trying to open with NO RECOVER option set
        !           107:                        memset(&dbenv, 0, sizeof(dbenv));
1.11      paf       108:                        check("db_appinit no recover", &fdb_home, db_appinit(
                    109:                                db_home_cstr,
                    110:                                db_config, 
                    111:                                &dbenv, 
                    112:                                flags
                    113:                        ));
1.14    ! paf       114:                } else {
        !           115:                        // some serious error
        !           116:                        // 2.2: trying to open with FATAL RECOVER option set
        !           117:                        memset(&dbenv, 0, sizeof(dbenv));
1.11      paf       118:                        check("db_appinit fatal recover", &fdb_home, db_appinit(
                    119:                                db_home_cstr,
                    120:                                db_config, 
                    121:                                &dbenv, 
                    122:                                flags | DB_RECOVER_FATAL
                    123:                        ));
                    124:                }
1.14    ! paf       125:        }
        !           126: 
        !           127:        // error handlers
        !           128:        dbenv.db_paniccall=db_paniccall;
        !           129:        dbenv.db_errcall=db_errcall;
1.11      paf       130: }
                    131: 
1.13      paf       132: DB_Connection::~DB_Connection() {
1.11      paf       133:        // close tables
1.13      paf       134:        table_cache.for_each(expire_table, 
1.11      paf       135:                reinterpret_cast<void *>(0/* =in the past = expire[close] all*/));
                    136:        
                    137:        // destroy connection data
1.13      paf       138:        check("db_appexit", &fdb_home, db_appexit(&dbenv));
1.11      paf       139: }
                    140: 
                    141: 
1.1       parser    142: void DB_Connection::check(const char *operation, const String *source, int error) {
                    143:        switch(error) {
                    144:        case 0: 
                    145:                // no error
                    146:                break; 
                    147:        
                    148:        default:
                    149:                throw Exception(0, 0, 
1.3       parser    150:                        source, 
1.11      paf       151:                        "db %s error: %s (%d)", 
                    152:                        operation, strerror(error), error);
1.1       parser    153:        }
                    154: }
                    155: 
1.13      paf       156: DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *origin) {
                    157:        if(!used)
                    158:                throw(0, 0,
                    159:                        origin,
                    160:                        "note to parser3 programmer: your forgot about DB_Connection_usage");
1.11      paf       161: 
                    162:        // first trying to get cached table
                    163:        DB_Table *result=get_table_from_cache(request_file_name);
1.13      paf       164:        if(!result) { // no cached table
1.11      paf       165:                // make global_file_name C-string on global pool
1.13      paf       166:                const char *request_file_name_cstr=request_file_name.cstr(String::UL_AS_IS);
1.11      paf       167:                char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
                    168:                strcpy(global_file_name_cstr, request_file_name_cstr);
                    169:                // make global_file_name string on global pool
                    170:                String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
                    171:                
                    172:                // allocate in global pool 
                    173:                // associate with services[request]
                    174:                // NOTE: never freed up!
                    175:                result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.13      paf       176:                // cache it
                    177:                put_table_to_cache(global_file_name, *result);
1.4       parser    178:        }
1.1       parser    179: 
1.13      paf       180:        // return auto-it
                    181:        return DB_Table_ptr(result);
1.11      paf       182: }
                    183: void DB_Connection::clear_dbfile(const String& file_name) {
                    184:        // open&clear
                    185:        DB *db;
1.1       parser    186:        DB_INFO dbinfo;
                    187:        memset(&dbinfo, 0, sizeof(dbinfo));
1.11      paf       188:        check("open(clear)", &file_name, db_open(
                    189:                file_name.cstr(String::UL_FILE_SPEC), 
1.1       parser    190:                PA_DB_ACCESS_METHOD, 
1.11      paf       191:                DB_CREATE | DB_TRUNCATE/* used in single thread, no need for |DB_THREAD*/,
1.1       parser    192:                0666, 
1.11      paf       193:                &dbenv, &dbinfo, &db));
                    194: 
                    195:        check("close", &file_name, db->close(db, 0/*flags*/));  db=0; 
1.1       parser    196: }
1.11      paf       197: 
                    198: // table cache
                    199: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
                    200: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) { 
                    201:        SYNCHRONIZED;
                    202: 
1.13      paf       203:        return static_cast<DB_Table *>(table_cache.get(file_name));
1.3       parser    204: }
                    205: 
1.13      paf       206: void DB_Connection::put_table_to_cache(const String& file_name, DB_Table& table) { 
1.11      paf       207:        SYNCHRONIZED;
1.3       parser    208: 
1.13      paf       209:        table_cache.put(file_name, &table);
1.3       parser    210: }
                    211: 
1.11      paf       212: void DB_Connection::maybe_expire_table_cache() {
                    213:        time_t now=time(0);
                    214: 
                    215:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
1.13      paf       216:                table_cache.for_each(expire_table, 
1.11      paf       217:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3       parser    218: 
1.11      paf       219:                prev_expiration_pass_time=now;
1.9       parser    220:        }
1.1       parser    221: }
                    222: 
                    223: #endif

E-mail: