Annotation of parser3/src/main/pa_db_manager.C, revision 1.18

1.1       parser      1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.12      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.18    ! paf         7:        $Id: pa_db_manager.C,v 1.17 2001/11/08 14:47:32 paf Exp $
1.1       parser      8: */
                      9: 
1.3       parser     10: #include "pa_config_includes.h"
1.9       paf        11: #ifdef DB2
1.3       parser     12: 
1.1       parser     13: #include "pa_db_manager.h"
1.2       parser     14: #include "pa_db_connection.h"
1.1       parser     15: #include "pa_exception.h"
                     16: #include "pa_threads.h"
                     17: #include "pa_stack.h"
1.11      paf        18: #include "pa_vhash.h"
1.1       parser     19: 
                     20: // globals
                     21: 
                     22: DB_Manager *DB_manager;
                     23: 
                     24: // consts
                     25: 
1.15      paf        26: const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
1.1       parser     27: const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
                     28: 
1.4       parser     29: // callbacks
                     30: 
1.7       paf        31: static void expire_connection(const Hash::Key& key, Hash::Val *& value, void *info) {
1.18    ! paf        32:        DB_Connection *connection=static_cast<DB_Connection *>(value);
        !            33:        time_t older_dies=reinterpret_cast<time_t>(info);
1.6       paf        34: 
1.18    ! paf        35:        if(connection->expired(older_dies)) {
        !            36:                connection->~DB_Connection();  value=0;
1.7       paf        37:        }
1.4       parser     38: }
1.1       parser     39: 
                     40: // DB_Manager
                     41: 
1.17      paf        42: DB_Manager::DB_Manager(Pool& apool) : Cache_manager(apool),
1.11      paf        43:        connection_cache(apool),
1.1       parser     44:        prev_expiration_pass_time(0) {
                     45: }
                     46: 
                     47: DB_Manager::~DB_Manager() {
1.6       paf        48:        // close connections
1.7       paf        49:        connection_cache.for_each(expire_connection, 
1.14      paf        50:                reinterpret_cast<void *>(time(0)/* =now= expire[close] all*/));
1.1       parser     51: }
                     52: 
1.7       paf        53: /// @test subpools mechanizm. one connection, one subpool. ~connection destructs it
                     54: DB_Connection_ptr DB_Manager::get_connection_ptr(const String& request_db_home,
                     55:                                                                                                   const String *source) {
1.6       paf        56:        if(request_db_home.size()==0)
                     57:                throw Exception(0, 0,
1.7       paf        58:                        source,
1.6       paf        59:                        "empty DB_HOME specified");
1.1       parser     60: 
                     61:        // first trying to get cached connection
1.6       paf        62:        DB_Connection *result=get_connection_from_cache(request_db_home);
1.7       paf        63:        if(!result) { // no cached connection
1.6       paf        64:                // make global_db_home C-string on global pool
1.10      paf        65:                const char *request_db_home_cstr=request_db_home.cstr();
1.6       paf        66:                char *global_db_home_cstr=(char *)malloc(strlen(request_db_home_cstr)+1);
                     67:                strcpy(global_db_home_cstr, request_db_home_cstr);
                     68:                // make global_db_home string on global pool
                     69:                String& global_db_home=*new(this->pool()) String(this->pool(), global_db_home_cstr);
1.1       parser     70:                
                     71:                // allocate in global pool 
                     72:                // NOTE: never freed up!
1.6       paf        73:                result=new(this->pool()) DB_Connection(this->pool(), global_db_home);
1.7       paf        74:                // cache it
                     75:                put_connection_to_cache(global_db_home, *result);
1.1       parser     76:        }
                     77: 
1.7       paf        78:        // return auto-it
                     79:        return DB_Connection_ptr(result);
1.2       parser     80: }
                     81: 
1.1       parser     82: // connection cache
                     83: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.6       paf        84: DB_Connection *DB_Manager::get_connection_from_cache(const String& db_home) { 
1.1       parser     85:        SYNCHRONIZED;
1.8       paf        86: 
1.7       paf        87:        return static_cast<DB_Connection *>(connection_cache.get(db_home));
1.1       parser     88: }
                     89: 
1.6       paf        90: void DB_Manager::put_connection_to_cache(const String& db_home, 
1.1       parser     91:                                                                                                 DB_Connection& connection) { 
                     92:        SYNCHRONIZED;
                     93: 
1.7       paf        94:        connection_cache.put(db_home, &connection);
1.1       parser     95: }
                     96: 
1.16      paf        97: void DB_Manager::maybe_expire_cache() {
1.1       parser     98:        time_t now=time(0);
                     99: 
                    100:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
1.7       paf       101:                connection_cache.for_each(expire_connection, 
1.1       parser    102:                        reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
                    103: 
                    104:                prev_expiration_pass_time=now;
                    105:        }
1.11      paf       106: }
                    107: 
1.12      paf       108: static void add_connection_to_status_status_cache(const Hash::Key& key, Hash::Val *value, void *info) {
1.18    ! paf       109:        DB_Connection *connection=static_cast<DB_Connection *>(value);
        !           110:        VHash& status_cache=*static_cast<VHash *>(info);
        !           111:        Pool& pool=status_cache.pool();
        !           112: 
        !           113:        // file => tables table
        !           114:        status_cache.hash(0).put(connection->db_home(), &connection->get_status(pool, 0));
1.12      paf       115: }
1.11      paf       116: Value& DB_Manager::get_status(Pool& pool, const String *source) {
                    117:        VHash& result=*new(pool) VHash(pool);
1.12      paf       118:        
                    119:        // db_homes
1.11      paf       120:        {
1.12      paf       121:                VHash& status_cache=*new(pool) VHash(pool);
                    122:                connection_cache.for_each(add_connection_to_status_status_cache, &status_cache);
1.11      paf       123: 
1.12      paf       124:                result.hash(source).put(*new(pool) String(pool, "cache"), &status_cache);
                    125:        }
1.11      paf       126: 
                    127:        return result;
1.1       parser    128: }
1.3       parser    129: 
                    130: #endif

E-mail: