Diff for /parser3/src/main/Attic/pa_db_connection.C between versions 1.15 and 1.28

version 1.15, 2001/10/26 16:27:17 version 1.28, 2001/11/05 11:46:27
Line 2 Line 2
         Parser: sql driver connection implementation.          Parser: sql driver connection implementation.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
   
         $Id$          $Id$
   
           developed with LIBDB 2.7.4
 */  */
   
 #include "pa_config_includes.h"  #include "pa_config_includes.h"
 #ifdef HAVE_LIBDB  #ifdef DB2
   
 #include "pa_db_connection.h"  #include "pa_db_connection.h"
 #include "pa_db_table.h"  #include "pa_db_table.h"
Line 16 Line 18
 #include "pa_threads.h"  #include "pa_threads.h"
 #include "pa_stack.h"  #include "pa_stack.h"
 #include "pa_common.h"  #include "pa_common.h"
   #include "pa_vtable.h"
   
 // defines  // defines
   
 #define DB_ERROR_PREFIX "db_err: "  
 #define DB_EXCEPTION_NO_LOG_MESSAGE1 DB_ERROR_PREFIX"Ignoring log file"  
 #define DB_EXCEPTION_NO_LOG_MESSAGE2 DB_ERROR_PREFIX"log_get: unable to find checkpoint record"  
   
 // consts  // consts
   
 /// @test increase  /// @test increase
Line 43  static void db_paniccall(DB_ENV *dbenv, Line 42  static void db_paniccall(DB_ENV *dbenv,
 static void db_errcall(const char *, char *buffer) {  static void db_errcall(const char *, char *buffer) {
         throw Exception(0, 0,           throw Exception(0, 0, 
                 0,                   0, 
                 DB_ERROR_PREFIX "%s",                   "db_err: %s", 
                 buffer);                  buffer);
 }  }
   
 static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {  static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
         DB_Connection& table=*static_cast<DB_Connection *>(value);          DB_Table& table=*static_cast<DB_Table *>(value);
         time_t older_dies=reinterpret_cast<time_t>(info);          time_t older_dies=reinterpret_cast<time_t>(info);
   
         if(table.expired(older_dies)) {          if(table.expired(older_dies)) {
                 table.~DB_Connection();  value=0;                  table.~DB_Table();  value=0;
         }          }
 }  }
   
Line 60  static void expire_table(const Hash::Key Line 59  static void expire_table(const Hash::Key
   
 DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),  DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
         time_used(0),          time_used(0),
         fdb_home(adb_home),           fdb_home(adb_home), used(0),
         table_cache(apool),          table_cache(apool),
         prev_expiration_pass_time(0) {          prev_expiration_pass_time(0) {
         //_asm  int 3;          //_asm  int 3;
Line 82  DB_Connection::DB_Connection(Pool& apool Line 81  DB_Connection::DB_Connection(Pool& apool
         };          };
   
         u_int32_t flags=          u_int32_t flags=
                 DB_THREAD                  parser_multithreaded?DB_THREAD:0
                 | DB_CREATE                   | DB_CREATE 
                 | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;                  | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
   
         try {          // prepare dbenv structure
                 // 1: trying to open with SOFT RECOVER option set          memset(&dbenv, 0, sizeof(dbenv));
                 memset(&dbenv, 0, sizeof(dbenv));          
                 check("db_appinit soft recover", &fdb_home, db_appinit(  
                         db_home_cstr,  
                         db_config,   
                         &dbenv,   
                         flags | DB_RECOVER  
                 ));  
         } catch(const Exception& e) {  
                 if(  
                         strncmp(  
                                 e.comment(),   
                                 DB_EXCEPTION_NO_LOG_MESSAGE1,   
                                 strlen(DB_EXCEPTION_NO_LOG_MESSAGE1))==0 ||   
                         strncmp(  
                                 e.comment(),   
                                 DB_EXCEPTION_NO_LOG_MESSAGE2,   
                                 strlen(DB_EXCEPTION_NO_LOG_MESSAGE2))==0) {  
                         // no log = no SOFT RECOVER  
                         // 2.1: trying to open with NO RECOVER option set  
                         memset(&dbenv, 0, sizeof(dbenv));  
                         check("db_appinit no recover", &fdb_home, db_appinit(  
                                 db_home_cstr,  
                                 db_config,   
                                 &dbenv,   
                                 flags  
                         ));  
                 } else {  
                         // some serious error  
                         // 2.2: trying to open with FATAL RECOVER option set  
                         memset(&dbenv, 0, sizeof(dbenv));  
                         check("db_appinit fatal recover", &fdb_home, db_appinit(  
                                 db_home_cstr,  
                                 db_config,   
                                 &dbenv,   
                                 flags | DB_RECOVER_FATAL  
                         ));  
                 }  
         }  
   
         // error handlers          // error handlers
   #if DB_VERSION_MINOR >= 7
         dbenv.db_paniccall=db_paniccall;          dbenv.db_paniccall=db_paniccall;
   #endif
         dbenv.db_errcall=db_errcall;          dbenv.db_errcall=db_errcall;
   
           // lockdetector flags
           dbenv.lk_detect=DB_LOCK_RANDOM;
   
           // init
           check("db_appinit", &fdb_home, db_appinit(
                   db_home_cstr,
                   db_config, 
                   &dbenv, 
                   flags
                   ));
   
           // after some hang noticed this to be null
           if(!dbenv.tx_info) 
                   throw Exception(0, 0,
                           &fdb_home,
                           "transaction system failed to initialize");
 }  }
   
 DB_Connection::~DB_Connection() {  DB_Connection::~DB_Connection() {
Line 149  void DB_Connection::check(const char *op Line 129  void DB_Connection::check(const char *op
                 break;                   break; 
                   
         default:          default:
                 throw Exception(0, 0,                   if(error<0)
                         source,                           throw Exception(0, 0, 
                         "db %s error: %s (%d)",                                   source, 
                         operation, strerror(error), error);                                  "db %s error: %d", 
                                   operation, error);
                   else
                           throw Exception(0, 0, 
                                   source, 
                                   "db %s error: %s (%d)", 
                                   operation, strerror(error), error);
         }          }
 }  }
   
 DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {  DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {
         // checkpoint          // checkpoint
         check("checkpoint", source,           { 
                 txn_checkpoint(dbenv.tx_info, 0/*kbyte*/, DB_CHECKPOINT_MINUTES/*min*/));                  int error=txn_checkpoint(dbenv.tx_info, 0/*kbyte*/, DB_CHECKPOINT_MINUTES/*min*/);
                   /*
                   DB_INCOMPLETE if there were pages that needed to be written 
                   but that memp_sync was unable to write immediately. 
                   In this case, the txn_checkpoint call should be retried. 
   
                   we'll just ignore that
                   */
                   if(error!=DB_INCOMPLETE)
                           check("checkpoint", source, error);
           }
   
         // first trying to get cached table          // first trying to get cached table
         DB_Table *result=get_table_from_cache(request_file_name);          DB_Table *result=get_table_from_cache(request_file_name);
         if(!result) { // no cached table          if(!result) { // no cached table
                 // make global_file_name C-string on global pool                  // make global_file_name C-string on global pool
                 const char *request_file_name_cstr=request_file_name.cstr(String::UL_AS_IS);                  const char *request_file_name_cstr=request_file_name.cstr();
                 char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);                  char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
                 strcpy(global_file_name_cstr, request_file_name_cstr);                  strcpy(global_file_name_cstr, request_file_name_cstr);
                 // make global_file_name string on global pool                  // make global_file_name string on global pool
Line 182  DB_Table_ptr DB_Connection::get_table_pt Line 178  DB_Table_ptr DB_Connection::get_table_pt
         // return auto-it          // return auto-it
         return DB_Table_ptr(result);          return DB_Table_ptr(result);
 }  }
 void DB_Connection::clear_dbfile(const String& file_name) {  
         // open&clear  
         DB *db;  
         DB_INFO dbinfo;  
         memset(&dbinfo, 0, sizeof(dbinfo));  
         check("open(clear)", &file_name, db_open(  
                 file_name.cstr(String::UL_FILE_SPEC),   
                 PA_DB_ACCESS_METHOD,   
                 DB_CREATE | DB_TRUNCATE/* used in single thread, no need for |DB_THREAD*/,  
                 0666,   
                 &dbenv, &dbinfo, &db));  
   
         check("close", &file_name, db->close(db, 0/*flags*/));  db=0;   
 }  
   
 // table cache  // table cache
 /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]  /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
 DB_Table *DB_Connection::get_table_from_cache(const String& file_name) {   DB_Table *DB_Connection::get_table_from_cache(const String& file_name) { 
         SYNCHRONIZED;          SYNCHRONIZED;
   
           maybe_expire_table_cache();
   
         return static_cast<DB_Table *>(table_cache.get(file_name));          return static_cast<DB_Table *>(table_cache.get(file_name));
 }  }
   
Line 222  void DB_Connection::maybe_expire_table_c Line 206  void DB_Connection::maybe_expire_table_c
         }          }
 }  }
   
   static void add_table_to_status_table(const Hash::Key& key, Hash::Val *& value, void *info) {
           DB_Table& db_table=*static_cast<DB_Table *>(value);
           Table& status_table=*static_cast<Table *>(info);
           Pool& pool=status_table.pool();
   
           Array& row=*new(pool) Array(pool);
   
           // name
           row+=&db_table.file_name();
           // time
           time_t time_stamp=db_table.get_time_used();
           const char *unsafe_time_cstr=ctime(&time_stamp);
           int time_buf_size=strlen(unsafe_time_cstr);
           char *safe_time_buf=(char *)pool.malloc(time_buf_size);
           memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
           row+=new(pool) String(pool, safe_time_buf, time_buf_size);
           // users
           char *users_buf=(char *)pool.malloc(MAX_NUMBER);
           row+=new(pool) String(pool, 
                   users_buf, snprintf(users_buf, MAX_NUMBER, "%d", db_table.get_users_count()));
   
           status_table+=&row;
   }
   Value& DB_Connection::get_status(Pool& pool, const String *source) {
           Array& columns=*new(pool) Array(pool);
           columns+=new(pool) String(pool, "name");
           columns+=new(pool) String(pool, "time");
           columns+=new(pool) String(pool, "users");
           Table& table=*new(pool) Table(pool, 0, &columns, table_cache.size());
   
           table_cache.for_each(add_table_to_status_table, &table);
   
           return *new(pool) VTable(pool, &table);
   }
   
 #endif  #endif

Removed from v.1.15  
changed lines
  Added in v.1.28


E-mail: