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

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.11    ! paf         7:        $Id: pa_db_connection.C,v 1.5 2001/10/25 09:48:18 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.11    ! paf        20: // consts
        !            21: 
        !            22: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
        !            23: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
        !            24: 
        !            25: const char *DB_WARNING1="Finding last valid log LSN";
1.10      parser     26: 
1.11    ! paf        27: // callbacks
1.10      parser     28: 
1.11    ! paf        29: static void db_paniccall(DB_ENV *dbenv, int error) {
        !            30:        throw Exception(0, 0, 
        !            31:                0, 
        !            32:                "db_panic: %s (%d)", 
        !            33:                strerror(error), error);
        !            34: }
        !            35: 
        !            36: static void db_errcall(const char *, char *buffer) {
        !            37:        // were it warning?
        !            38:        if(strncmp(buffer, DB_WARNING1, strlen(DB_WARNING1))==0)
        !            39:                return;
1.5       parser     40: 
1.11    ! paf        41:        throw Exception(0, 0, 
        !            42:                0, 
        !            43:                "db_err: %s", 
        !            44:                buffer);
        !            45: }
1.5       parser     46: 
1.11    ! paf        47: static void expire_table(Array::Item *value, void *info) {
        !            48:        DB_Table& table=*static_cast<DB_Table *>(value);
        !            49:        time_t older_dies=reinterpret_cast<time_t>(info);
1.5       parser     50: 
1.11    ! paf        51:        if(table.connected() && table.expired(older_dies))
        !            52:                table.disconnect();
        !            53: }
        !            54: static void expire_tables(const Hash::Key& key, Hash::Val *value, void *info) {
        !            55:        Stack& stack=*static_cast<Stack *>(value);
        !            56:        for(int i=0; i<=stack.top_index(); i++)
        !            57:                expire_table(stack.get(i), info);
        !            58: }
1.5       parser     59: 
1.3       parser     60: // DB_Connection
                     61: 
1.11    ! paf        62: DB_Connection::DB_Connection(Pool& pool, const String& adb_home) : Pooled(pool),
        !            63:        time_used(0), fservices_pool(0)        !            64:        fdb_home(adb_home), fconnected(false), errors(0),
        !            65:        table_cache(pool),
        !            66:        prev_expiration_pass_time(0) {
        !            67: }
        !            68: 
        !            69: void DB_Connection::connect() {
        !            70:        //_asm  int 3;
        !            71:        memset(&dbenv, 0, sizeof(dbenv));
        !            72:        dbenv.db_paniccall=db_paniccall;
        !            73:        dbenv.db_errcall=db_errcall;
        !            74: 
        !            75:        char DB_DATA_DIR__VALUE[MAX_STRING];
        !            76:        char DB_LOG_DIR__VALUE[MAX_STRING];
        !            77:        char DB_TMP_DIR__VALUE[MAX_STRING];
        !            78: 
        !            79:        const char *db_home_cstr=fdb_home.cstr(String::UL_FILE_SPEC);
        !            80: 
        !            81:        snprintf(DB_DATA_DIR__VALUE, MAX_STRING, "DB_DATA_DIR %s", db_home_cstr);
        !            82:        snprintf(DB_LOG_DIR__VALUE, MAX_STRING, "DB_LOG_DIR %s", db_home_cstr);
        !            83:        snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
        !            84: 
        !            85:        char *db_config[] = {
        !            86:                DB_DATA_DIR__VALUE,
        !            87:                DB_LOG_DIR__VALUE,
        !            88:                DB_TMP_DIR__VALUE, 
        !            89:                0
        !            90:        };
        !            91: 
        !            92:        u_int32_t flags=
        !            93:                DB_CREATE 
        !            94:                | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
        !            95: 
        !            96:        try {
        !            97:                // 1: trying to open with SOFT RECOVER option set
        !            98:                check("db_appinit soft recover", &fdb_home, db_appinit(
        !            99:                        db_home_cstr,
        !           100:                        db_config, 
        !           101:                        &dbenv, 
        !           102:                        flags | DB_RECOVER
        !           103:                ));
        !           104:        } catch(...) {
        !           105:                try {
        !           106:                        // 2: trying to open WITHOUT SOFT RECOVER option set
        !           107:                        check("db_appinit no recover", &fdb_home, db_appinit(
        !           108:                                db_home_cstr,
        !           109:                                db_config, 
        !           110:                                &dbenv, 
        !           111:                                flags
        !           112:                        ));
        !           113:                } catch(...) {
        !           114:                        // 3: trying to open with FATAL RECOVER option set
        !           115:                        check("db_appinit fatal recover", &fdb_home, db_appinit(
        !           116:                                db_home_cstr,
        !           117:                                db_config, 
        !           118:                                &dbenv, 
        !           119:                                flags | DB_RECOVER_FATAL
        !           120:                        ));
        !           121:                }
        !           122:        }
        !           123: 
        !           124:        fconnected=true;
        !           125: }
        !           126: 
        !           127: void DB_Connection::disconnect() {
        !           128:        // close tables
        !           129:        table_cache.for_each(expire_tables, 
        !           130:                reinterpret_cast<void *>(0/* =in the past = expire[close] all*/));
        !           131:        
        !           132:        // destroy connection data
        !           133:        check("db_appexit", &fdb_home, db_appexit(&dbenv));  fconnected=false;
        !           134: }
        !           135: 
        !           136: 
1.1       parser    137: void DB_Connection::check(const char *operation, const String *source, int error) {
                    138:        switch(error) {
                    139:        case 0: 
                    140:                // no error
                    141:                break; 
                    142:        
                    143:        default:
1.11    ! paf       144:                errors++;
1.1       parser    145:                throw Exception(0, 0, 
1.3       parser    146:                        source, 
1.11    ! paf       147:                        "db %s error: %s (%d)", 
        !           148:                        operation, strerror(error), error);
1.1       parser    149:        }
                    150: }
                    151: 
1.11    ! paf       152: DB_Table& DB_Connection::get_table(const String& request_file_name,
        !           153:                                                                                                   const String& request_origin) {
        !           154:        Pool& pool=request_origin.pool(); // request pool                                                                                          
        !           155: 
        !           156:        // first trying to get cached table
        !           157:        DB_Table *result=get_table_from_cache(request_file_name);
        !           158:        if(result && !result->ping()) { // we have some cached table, is it pingable?
        !           159:                result->disconnect(); // kill unpingabe=dead table
        !           160:                result=0;
        !           161:        }
1.4       parser    162: 
1.11    ! paf       163:        char *request_file_name_cstr;
        !           164:        if(result)
        !           165:                request_file_name_cstr=0; // calm, compiler
        !           166:        else { // no cached table
        !           167:                // make global_file_name C-string on global pool
        !           168:                request_file_name_cstr=request_file_name.cstr(String::UL_AS_IS);
        !           169:                char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
        !           170:                strcpy(global_file_name_cstr, request_file_name_cstr);
        !           171:                // make global_file_name string on global pool
        !           172:                String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
        !           173:                
        !           174:                // allocate in global pool 
        !           175:                // associate with services[request]
        !           176:                // NOTE: never freed up!
        !           177:                result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.4       parser    178:        }
1.1       parser    179: 
1.11    ! paf       180:        // associate with services[request]  (deassociates at close)
        !           181:        result->set_services(&pool); 
        !           182:        // if not connected yet, do that now, when result has services
        !           183:        if(!result->connected())
        !           184:                result->connect();
        !           185:        // return it
        !           186:        return *result;
        !           187: }
        !           188: void DB_Connection::clear_dbfile(const String& file_name) {
        !           189:        // open&clear
        !           190:        DB *db;
1.1       parser    191:        DB_INFO dbinfo;
                    192:        memset(&dbinfo, 0, sizeof(dbinfo));
1.11    ! paf       193:        check("open(clear)", &file_name, db_open(
        !           194:                file_name.cstr(String::UL_FILE_SPEC), 
1.1       parser    195:                PA_DB_ACCESS_METHOD, 
1.11    ! paf       196:                DB_CREATE | DB_TRUNCATE/* used in single thread, no need for |DB_THREAD*/,
1.1       parser    197:                0666, 
1.11    ! paf       198:                &dbenv, &dbinfo, &db));
        !           199: 
        !           200:        check("close", &file_name, db->close(db, 0/*flags*/));  db=0; 
1.1       parser    201: }
1.11    ! paf       202: 
        !           203: void DB_Connection::close_table(const String& file_name, 
        !           204:                                                                                  DB_Table& table) {
        !           205:        // deassociate from services[request]
        !           206:        table.set_services(0);
        !           207:        put_table_to_cache(file_name, table);
1.2       parser    208: }
1.1       parser    209: 
                    210: 
1.11    ! paf       211: // table cache
        !           212: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
        !           213: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) { 
        !           214:        SYNCHRONIZED;
        !           215: 
        !           216:        maybe_expire_table_cache();
1.2       parser    217: 
1.11    ! paf       218:        if(Stack *tables=static_cast<Stack *>(table_cache.get(file_name)))
        !           219:                while(tables->top_index()>=0) { // there are cached tables to that 'file_name'
        !           220:                        DB_Table *result=static_cast<DB_Table *>(tables->pop());
        !           221:                        if(result->connected()) // not expired?
        !           222:                                return result;
        !           223:                }
1.1       parser    224: 
1.11    ! paf       225:        return 0;
1.3       parser    226: }
                    227: 
1.11    ! paf       228: void DB_Connection::put_table_to_cache(const String& file_name, 
        !           229:                                                                                                 DB_Table& table) { 
        !           230:        SYNCHRONIZED;
1.3       parser    231: 
1.11    ! paf       232:        Stack *tables=static_cast<Stack *>(table_cache.get(file_name));
        !           233:        if(!tables) { // there are no cached tables to that 'file_name' yet?
        !           234:                tables=NEW Stack(pool()); // NOTE: never freed up!
        !           235:                table_cache.put(file_name, tables);
        !           236:        }       
        !           237:        tables->push(&table);
1.3       parser    238: }
                    239: 
1.11    ! paf       240: void DB_Connection::maybe_expire_table_cache() {
        !           241:        time_t now=time(0);
        !           242: 
        !           243:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
        !           244:                table_cache.for_each(expire_tables, 
        !           245:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3       parser    246: 
1.11    ! paf       247:                prev_expiration_pass_time=now;
1.9       parser    248:        }
1.1       parser    249: }
                    250: 
                    251: #endif

E-mail: