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

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)
1.28      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.32    ! paf         7:        $Id: pa_db_connection.C,v 1.31 2001/11/05 16:07:16 paf Exp $
1.20      paf         8: 
                      9:        developed with LIBDB 2.7.4
1.1       parser     10: */
                     11: 
                     12: #include "pa_config_includes.h"
1.19      paf        13: #ifdef DB2
1.1       parser     14: 
                     15: #include "pa_db_connection.h"
1.11      paf        16: #include "pa_db_table.h"
1.1       parser     17: #include "pa_exception.h"
1.11      paf        18: #include "pa_threads.h"
                     19: #include "pa_stack.h"
                     20: #include "pa_common.h"
1.28      paf        21: #include "pa_vtable.h"
1.1       parser     22: 
1.14      paf        23: // defines
                     24: 
1.11      paf        25: // consts
                     26: 
1.15      paf        27: /// @test increase
                     28: const int DB_CHECKPOINT_MINUTES=1; 
                     29: 
1.32    ! paf        30: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
1.11      paf        31: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
                     32: 
                     33: // callbacks
1.10      parser     34: 
1.11      paf        35: static void db_paniccall(DB_ENV *dbenv, int error) {
                     36:        throw Exception(0, 0, 
                     37:                0, 
                     38:                "db_panic: %s (%d)", 
                     39:                strerror(error), error);
                     40: }
                     41: 
                     42: static void db_errcall(const char *, char *buffer) {
                     43:        throw Exception(0, 0, 
                     44:                0, 
1.23      paf        45:                "db_err: %s", 
1.11      paf        46:                buffer);
                     47: }
1.5       parser     48: 
1.13      paf        49: static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
1.30      paf        50:        if(DB_Table *table=static_cast<DB_Table *>(value)) {
                     51:                time_t older_dies=reinterpret_cast<time_t>(info);
1.5       parser     52: 
1.30      paf        53:                if(table->expired(older_dies)) {
                     54:                        table->~DB_Table();  value=0;
                     55:                }
1.13      paf        56:        }
1.11      paf        57: }
1.5       parser     58: 
1.3       parser     59: // DB_Connection
                     60: 
1.13      paf        61: DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
                     62:        time_used(0),
1.28      paf        63:        fdb_home(adb_home), used(0),
1.13      paf        64:        table_cache(apool),
1.11      paf        65:        prev_expiration_pass_time(0) {
                     66:        //_asm  int 3;
                     67:        char DB_DATA_DIR__VALUE[MAX_STRING];
                     68:        char DB_LOG_DIR__VALUE[MAX_STRING];
                     69:        char DB_TMP_DIR__VALUE[MAX_STRING];
                     70: 
                     71:        const char *db_home_cstr=fdb_home.cstr(String::UL_FILE_SPEC);
                     72: 
                     73:        snprintf(DB_DATA_DIR__VALUE, MAX_STRING, "DB_DATA_DIR %s", db_home_cstr);
                     74:        snprintf(DB_LOG_DIR__VALUE, MAX_STRING, "DB_LOG_DIR %s", db_home_cstr);
                     75:        snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
                     76: 
                     77:        char *db_config[] = {
                     78:                DB_DATA_DIR__VALUE,
                     79:                DB_LOG_DIR__VALUE,
                     80:                DB_TMP_DIR__VALUE, 
                     81:                0
                     82:        };
                     83: 
                     84:        u_int32_t flags=
1.29      paf        85:                (parser_multithreaded?DB_THREAD:0)
1.13      paf        86:                | DB_CREATE 
1.11      paf        87:                | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
                     88: 
1.27      paf        89:        // prepare dbenv structure
1.16      paf        90:        memset(&dbenv, 0, sizeof(dbenv));
                     91:        
1.14      paf        92:        // error handlers
1.20      paf        93: #if DB_VERSION_MINOR >= 7
1.14      paf        94:        dbenv.db_paniccall=db_paniccall;
1.20      paf        95: #endif
1.14      paf        96:        dbenv.db_errcall=db_errcall;
1.26      paf        97: 
                     98:        // lockdetector flags
                     99:        dbenv.lk_detect=DB_LOCK_RANDOM;
1.16      paf       100: 
                    101:        // init
                    102:        check("db_appinit", &fdb_home, db_appinit(
                    103:                db_home_cstr,
                    104:                db_config, 
                    105:                &dbenv, 
                    106:                flags
                    107:                ));
1.28      paf       108: 
                    109:        // after some hang noticed this to be null
                    110:        if(!dbenv.tx_info) 
                    111:                throw Exception(0, 0,
                    112:                        &fdb_home,
                    113:                        "transaction system failed to initialize");
1.11      paf       114: }
                    115: 
1.13      paf       116: DB_Connection::~DB_Connection() {
1.11      paf       117:        // close tables
1.13      paf       118:        table_cache.for_each(expire_table, 
1.31      paf       119:                reinterpret_cast<void *>(time(0)/* =now= expire[close] all*/));
1.11      paf       120:        
                    121:        // destroy connection data
1.13      paf       122:        check("db_appexit", &fdb_home, db_appexit(&dbenv));
1.11      paf       123: }
                    124: 
                    125: 
1.1       parser    126: void DB_Connection::check(const char *operation, const String *source, int error) {
                    127:        switch(error) {
                    128:        case 0: 
                    129:                // no error
                    130:                break; 
                    131:        
                    132:        default:
1.21      paf       133:                if(error<0)
                    134:                        throw Exception(0, 0, 
                    135:                                source, 
                    136:                                "db %s error: %d", 
                    137:                                operation, error);
                    138:                else
                    139:                        throw Exception(0, 0, 
                    140:                                source, 
                    141:                                "db %s error: %s (%d)", 
                    142:                                operation, strerror(error), error);
1.1       parser    143:        }
                    144: }
                    145: 
1.15      paf       146: DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {
                    147:        // checkpoint
1.28      paf       148:        { 
1.21      paf       149:                int error=txn_checkpoint(dbenv.tx_info, 0/*kbyte*/, DB_CHECKPOINT_MINUTES/*min*/);
                    150:                /*
                    151:                DB_INCOMPLETE if there were pages that needed to be written 
                    152:                but that memp_sync was unable to write immediately. 
                    153:                In this case, the txn_checkpoint call should be retried. 
                    154: 
                    155:                we'll just ignore that
                    156:                */
                    157:                if(error!=DB_INCOMPLETE)
                    158:                        check("checkpoint", source, error);
                    159:        }
1.11      paf       160: 
                    161:        // first trying to get cached table
                    162:        DB_Table *result=get_table_from_cache(request_file_name);
1.13      paf       163:        if(!result) { // no cached table
1.11      paf       164:                // make global_file_name C-string on global pool
1.24      paf       165:                const char *request_file_name_cstr=request_file_name.cstr();
1.11      paf       166:                char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
                    167:                strcpy(global_file_name_cstr, request_file_name_cstr);
                    168:                // make global_file_name string on global pool
                    169:                String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
                    170:                
                    171:                // allocate in global pool 
                    172:                // associate with services[request]
                    173:                // NOTE: never freed up!
                    174:                result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.13      paf       175:                // cache it
                    176:                put_table_to_cache(global_file_name, *result);
1.4       parser    177:        }
1.1       parser    178: 
1.13      paf       179:        // return auto-it
                    180:        return DB_Table_ptr(result);
1.1       parser    181: }
1.11      paf       182: 
                    183: // table cache
                    184: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
                    185: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) { 
                    186:        SYNCHRONIZED;
1.17      paf       187: 
                    188:        maybe_expire_table_cache();
1.11      paf       189: 
1.13      paf       190:        return static_cast<DB_Table *>(table_cache.get(file_name));
1.3       parser    191: }
                    192: 
1.13      paf       193: void DB_Connection::put_table_to_cache(const String& file_name, DB_Table& table) { 
1.11      paf       194:        SYNCHRONIZED;
1.3       parser    195: 
1.13      paf       196:        table_cache.put(file_name, &table);
1.3       parser    197: }
                    198: 
1.11      paf       199: void DB_Connection::maybe_expire_table_cache() {
                    200:        time_t now=time(0);
                    201: 
                    202:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
1.13      paf       203:                table_cache.for_each(expire_table, 
1.11      paf       204:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3       parser    205: 
1.11      paf       206:                prev_expiration_pass_time=now;
1.9       parser    207:        }
1.28      paf       208: }
                    209: 
                    210: static void add_table_to_status_table(const Hash::Key& key, Hash::Val *& value, void *info) {
1.30      paf       211:        if(DB_Table *db_table=static_cast<DB_Table *>(value)) {
                    212:                Table& status_table=*static_cast<Table *>(info);
                    213:                Pool& pool=status_table.pool();
                    214: 
                    215:                Array& row=*new(pool) Array(pool);
                    216: 
                    217:                // name
                    218:                row+=&db_table->file_name();
                    219:                // time
                    220:                time_t time_stamp=db_table->get_time_used();
                    221:                const char *unsafe_time_cstr=ctime(&time_stamp);
                    222:                int time_buf_size=strlen(unsafe_time_cstr);
                    223:                char *safe_time_buf=(char *)pool.malloc(time_buf_size);
                    224:                memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
                    225:                row+=new(pool) String(pool, safe_time_buf, time_buf_size);
                    226:                // users
                    227:                char *users_buf=(char *)pool.malloc(MAX_NUMBER);
                    228:                row+=new(pool) String(pool, 
                    229:                        users_buf, snprintf(users_buf, MAX_NUMBER, "%d", db_table->get_users_count()));
1.28      paf       230: 
1.30      paf       231:                status_table+=&row;
                    232:        }
1.28      paf       233: }
                    234: Value& DB_Connection::get_status(Pool& pool, const String *source) {
                    235:        Array& columns=*new(pool) Array(pool);
                    236:        columns+=new(pool) String(pool, "name");
                    237:        columns+=new(pool) String(pool, "time");
                    238:        columns+=new(pool) String(pool, "users");
                    239:        Table& table=*new(pool) Table(pool, 0, &columns, table_cache.size());
                    240: 
                    241:        table_cache.for_each(add_table_to_status_table, &table);
                    242: 
                    243:        return *new(pool) VTable(pool, &table);
1.1       parser    244: }
                    245: 
                    246: #endif

E-mail: