Diff for /parser3/src/main/pa_sql_driver_manager.C between versions 1.22 and 1.53

version 1.22, 2001/06/28 07:44:17 version 1.53, 2001/11/11 10:52:50
Line 1 Line 1
 /** @file  /** @file
         Parser: sql driver manager implementation.          Parser: sql driver manager 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://paf.design.ru)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)  
 */          $Id$
 static const char *RCSId="$Id$";   */
   
 #include "pa_sql_driver_manager.h"  #include "pa_sql_driver_manager.h"
 #include "ltdl.h"  #include "ltdl.h"
 #include "pa_sql_connection.h"  #include "pa_sql_connection.h"
 #include "pa_exception.h"  #include "pa_exception.h"
 #include "pa_common.h"  #include "pa_common.h"
 #include "pa_threads.h"  #include "pa_threads.h"
   #include "pa_stack.h"
 #include "pa_sapi.h"  #include "pa_vhash.h"
   #include "pa_vtable.h"
 // globals  
   // globals
 SQL_Driver_manager *SQL_driver_manager;  
   SQL_Driver_manager *SQL_driver_manager;
 // consts  
   // consts
 const char *LIBRARY_CREATE_FUNC_NAME="create";  
 const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;  const int EXPIRE_UNUSED_CONNECTION_SECONDS=60;
 const int CHECK_EXPIRED_CONNECTIONS_SECONDS=60*2;  const int CHECK_EXPIRED_CONNECTIONS_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
   
   // helpers
 /// SQL_Driver_services Pooled implementation  
 class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {  const String& url_without_login(Pool& pool, const String& url) {
 public:          String& result=*new(pool) String(pool);
         SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),          result << url.mid(0, url.pos(":")) << "://****";
                 furl(aurl) {  
         }          int at_pos=url.pos("@");
           if(at_pos>0)
         /// allocates some bytes on pool                  result << url.mid(at_pos, url.size());
         void *malloc(size_t size) { return Pooled::malloc(size); }  
         /// allocates some bytes clearing them with zeros          return result;
         void *calloc(size_t size) { return Pooled::calloc(size); }  }
         /// throw exception  
         void _throw(const char *comment) {   /// SQL_Driver_services Pooled implementation
                 THROW(0, 0,   class SQL_Driver_services_impl : public SQL_Driver_services, public Pooled {
                         &furl,   public:
                         comment);           SQL_Driver_services_impl(Pool& apool, const String& aurl) : Pooled(apool),
         }                  furl(aurl) {
           }
 private:  
         const String& furl;          virtual void *malloc(size_t size) { return Pooled::malloc(size, 8); }
 };          virtual void *calloc(size_t size) { return Pooled::calloc(size); }
   
 // SQL_Driver_manager          /**
                   the idea is to #1 jump to C++ some function to main body, where
 /// @param request_url protocol://[driver-dependent]                  every function stack frame has exception unwind information
 SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,                   and from there... #2 propagate_exception()
                                                                                                    Table *protocol2driver_and_client) {          */
         Pool& pool=request_url.pool(); // request pool                                                                                               virtual void _throw(const char *comment) {
                   // hiding passwords and addresses from accidental show [imagine user forgot @exception]
         // we have table for locating protocol's library                  e=Exception(0, 0,
         if(!protocol2driver_and_client)                          &url_without_login(pool(), furl),
                 PTHROW(0, 0,                          comment);
                         &request_url,  
                         "$SQL:drivers table must be defined");                  longjmp(mark, 1);
           }
         // first trying to get cached connection          virtual void propagate_exception() {
         SQL_Connection *result=get_connection_from_cache(request_url);                  throw e;
         if(result && !result->ping()) { // we have some cached connection, is it pingable?          }
                 result->disconnect(); // kill unpingabe=dead connection  
                 result=0;  private:
         }          const String& furl;
           Exception e;
         char *request_url_cstr;  };
         if(result)  
                 request_url_cstr=0; // calm, compiler  // helpers
         else { // no cached connection or it were unpingabe: connect/reconnect  
                 int pos=request_url.pos("://", 3);  static void expire_connection(Array::Item *value, void *info) {
                 if(pos<0)          SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
                         PTHROW(0, 0,          time_t older_dies=reinterpret_cast<time_t>(info);
                                 &request_url,  
                                 "no protocol specified"); // NOTE: not THROW, but PTHROW          if(connection.connected() && connection.expired(older_dies))
                   connection.disconnect();
                 // make global_url C-string on global pool  }
                 request_url_cstr=request_url.cstr(String::UL_AS_IS);  static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {
                 char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);          Stack& stack=*static_cast<Stack *>(value);
                 strcpy(global_url_cstr, request_url_cstr);          for(int i=0; i<=stack.top_index(); i++)
                 // make global_url string on global pool                  expire_connection(stack.get(i), info);
                 String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);  }
                   
                 char *request_protocol_cstr=lsplit(&request_url_cstr, ':');  // SQL_Driver_manager
                 // skip "//" after ':'  
                 while(*request_url_cstr=='/')  SQL_Driver_manager::SQL_Driver_manager(Pool& apool) : Cache_manager(apool),
                         request_url_cstr++;                  driver_cache(apool),
                 // make global_protocol C-string on global pool                  connection_cache(apool),
                 char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);                  prev_expiration_pass_time(0) {
                 strcpy(global_protocol_cstr, request_protocol_cstr);  }
                 // make global_protocol string on global pool  
                 String& global_protocol=*new(this->pool()) String(this->pool(),   SQL_Driver_manager::~SQL_Driver_manager() {
                         global_protocol_cstr);          connection_cache.for_each(expire_connections,
                   reinterpret_cast<void *>((time_t)0/*=in past=expire all*/));
                 SQL_Driver *driver;  }
                 const String *dlopen_file_spec=0;  
                 // first trying to get cached driver  /// @param request_url protocol://[driver-dependent]
                 if(!(driver=get_driver_from_cache(global_protocol))) {  SQL_Connection& SQL_Driver_manager::get_connection(const String& request_url,
                         // no cached                                                                                                     const String& request_origin,
                         const String *library=0;                                                                                                     Table *protocol2driver_and_client) {
                         if(protocol2driver_and_client->locate(0, global_protocol)) {          Pool& pool=request_origin.pool(); // request pool                                                                                          
                                 if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)  
                                         PTHROW(0, 0,          // we have table for locating protocol's library
                                                 protocol2driver_and_client->origin_string(),          if(!protocol2driver_and_client)
                                                 "driver library column for protocol '%s' is empty",                   throw Exception(0, 0,
                                                         request_protocol_cstr);                          &request_url,
                                 if(!(dlopen_file_spec=protocol2driver_and_client->item(2)) || dlopen_file_spec->size()==0)                          "$"MAIN_SQL_NAME":"MAIN_SQL_DRIVERS_NAME" table must be defined");
                                         PTHROW(0, 0,  
                                                 protocol2driver_and_client->origin_string(),          // construct services[request]  (deassociates at close)
                                                 "client library column for protocol '%s' is empty",           SQL_Driver_services *services=new(pool) SQL_Driver_services_impl(pool, request_url);
                                                         request_protocol_cstr);  
                         } else          // first trying to get cached connection
                                 PTHROW(0, 0,          SQL_Connection *result=get_connection_from_cache(request_url);
                                         &request_url,          if(result) {
                                         "undefined protocol '%s'",                   result->set_services(services);
                                                 request_protocol_cstr);                  if(!result->ping()) { // we have some cached connection, is it pingable?
                           result->disconnect(); // kill unpingabe=dead connection
                         const char *filename=library->cstr(String::UL_FILE_NAME);                          result=0;
                         lt_dlhandle handle=lt_dlopen(filename);                  }
                         if (!handle)          }
                                 PTHROW(0, 0,  
                                         library,          char *request_url_cstr;
                                         "can not open the module, %s", lt_dlerror());          if(result)
                   request_url_cstr=0; // calm, compiler
                         SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,           else { // no cached connection or it were unpingabe: connect/reconnect
                                 LIBRARY_CREATE_FUNC_NAME);                    int pos=request_url.pos("://", 3);
                         if(!create)                  if(pos<0)
                                 PTHROW(0, 0,                          throw Exception(0, 0,
                                         library,                                  request_url.size()?&request_url:&request_origin,
                                         "function '%s' was not found", LIBRARY_CREATE_FUNC_NAME);                                  "connection string must start with protocol://"); // NOTE: not THROW, but PTHROW
   
                         // create library-driver!                  // make global_url C-string on global pool
                         driver=(*create)();                  request_url_cstr=request_url.cstr();
                   char *global_url_cstr=(char *)malloc(strlen(request_url_cstr)+1);
                         // validate driver api version                  strcpy(global_url_cstr, request_url_cstr);
                         int driver_api_version=driver->api_version();                  // make global_url string on global pool
                         if(driver_api_version!=SQL_DRIVER_API_VERSION)                  String& global_url=*new(this->pool()) String(this->pool(), global_url_cstr);
                                 PTHROW(0, 0,                 
                                         library,                  char *request_protocol_cstr=lsplit(&request_url_cstr, ':');
                                         "driver implements API version 0x%04X not equal to 0x%04X",                  // skip "//" after ':'
                                                 driver_api_version, SQL_DRIVER_API_VERSION);                  while(*request_url_cstr=='/')
                           request_url_cstr++;
                         // initialise by connecting to sql client dynamic link library                  // make global_protocol C-string on global pool
                         const char *dlopen_file_spec_cstr=dlopen_file_spec->cstr(String::UL_FILE_NAME);                  char *global_protocol_cstr=(char *)malloc(strlen(request_protocol_cstr)+1);
                         if(const char *error=driver->initialize(                  strcpy(global_protocol_cstr, request_protocol_cstr);
                                 dlopen_file_spec_cstr))                  // make global_protocol string on global pool
                                 PTHROW(0, 0,                  String& global_protocol=*new(this->pool()) String(this->pool(),
                                         library,                          global_protocol_cstr);
                                         "driver failed to initialize client library '%s', %s",  
                                                 dlopen_file_spec_cstr, error);                  SQL_Driver *driver;
                   // first trying to get cached driver
                         // cache it                  if(!(driver=get_driver_from_cache(global_protocol))) {
                         put_driver_to_cache(global_protocol, *driver);                          // no cached
                 }                          const String *library=0;
                                   const String *dlopen_file_spec=0;
                 // allocate in global pool                           if(protocol2driver_and_client->locate(0, global_protocol)) {
                 // associate with services[request]                                  if(!(library=protocol2driver_and_client->item(1)) || library->size()==0)
                 // NOTE: never freed up!                                          throw Exception(0, 0,
                 result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);                                                  protocol2driver_and_client->origin_string(),
         }                                                  "driver library column for protocol '%s' is empty",
                                                           request_protocol_cstr);
         // associate with services[request]  (deassociates at close)                                  dlopen_file_spec=protocol2driver_and_client->item(2);
         result->set_services(new(pool) SQL_Driver_services_impl(pool, request_url));                           } else
         // if not connected yet, do that now, when result has services                                  throw Exception(0, 0,
         if(!result->connected())                                          &request_url,
                 result->connect(request_url_cstr);                                          "undefined protocol '%s'",
         // return it                                                  request_protocol_cstr);
         return *result;  
 }                          if(lt_dlinit())
                                   throw Exception(0, 0,
 void SQL_Driver_manager::close_connection(const String& url,                                           library,
                                                                                   SQL_Connection& connection) {                                          "prepare to dynamic loading failed, %s", lt_dlerror());
         // deassociate from services[request]  
         connection.set_services(0);                          const char *filename=library->cstr(String::UL_FILE_SPEC);
         put_connection_to_cache(url, connection);                          lt_dlhandle handle=lt_dlopen(filename);
 }                          if (!handle)
                                   throw Exception(0, 0,
                                           library,
 // driver cache                                          "can not open the module, %s", lt_dlerror());
   
 SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {                          SQL_Driver_create_func create=(SQL_Driver_create_func)lt_dlsym(handle,
         SYNCHRONIZED;                                  SQL_DRIVER_CREATE_NAME);
                           if(!create)
         return static_cast<SQL_Driver *>(driver_cache.get(protocol));                                  throw Exception(0, 0,
 }                                          library,
                                           "function '"SQL_DRIVER_CREATE_NAME"' was not found");
 void SQL_Driver_manager::put_driver_to_cache(const String& protocol,   
                                                                                          SQL_Driver& driver) {                          // create library-driver!
         SYNCHRONIZED;                          driver=(*create)();
   
         driver_cache.put(protocol, &driver);                          // validate driver api version
 }                          int driver_api_version=driver->api_version();
                           if(driver_api_version!=SQL_DRIVER_API_VERSION)
 // connection cache                                  throw Exception(0, 0,
                                           library,
 SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {                                           "driver implements API version 0x%04X not equal to 0x%04X",
         SYNCHRONIZED;                                                  driver_api_version, SQL_DRIVER_API_VERSION);
   
         maybe_expire_connection_cache();                          // initialise by connecting to sql client dynamic link library
                           char *dlopen_file_spec_cstr=
         if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))                                  dlopen_file_spec && dlopen_file_spec->size()?
                 while(connections->top_index()>=0) { // there are cached connections to that 'url'                                  dlopen_file_spec->cstr(String::UL_AS_IS):0;
                         SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());                          if(const char *error=driver->initialize(
                         if(result->connected()) // not expired?                                  dlopen_file_spec_cstr))
                                 return result;                                  throw Exception(0, 0,
                 }                                          library,
                                           "driver failed to initialize client library '%s', %s",
         return 0;                                                  dlopen_file_spec_cstr?dlopen_file_spec_cstr:"unspecifed",
 }                                                  error);
   
 void SQL_Driver_manager::put_connection_to_cache(const String& url,                           // cache it
                                                                                                  SQL_Connection& connection) {                           put_driver_to_cache(global_protocol, *driver);
         SYNCHRONIZED;                  }
          
         Stack *connections=static_cast<Stack *>(connection_cache.get(url));                  // allocate in global pool
         if(!connections) { // there are no cached connections to that 'url' yet?                  // associate with services[request]
                 connections=NEW Stack(pool()); // NOTE: never freed up!                  // NOTE: never freed up!
                 connection_cache.put(url, connections);                  result=new(this->pool()) SQL_Connection(this->pool(), global_url, *driver);
         }                         // associate with services[request]  (deassociates at close)
         connections->push(&connection);                  result->set_services(services);
 }          }
   
 static void expire_connection(Array::Item *value, void *info) {          // if not connected yet, do that now, when result has services
         SQL_Connection& connection=*static_cast<SQL_Connection *>(value);          if(!result->connected())
         time_t older_dies=reinterpret_cast<time_t>(info);                  result->connect(request_url_cstr);
           // return it
         if(connection.expired(older_dies))          return *result;
                 connection.disconnect();  }
 }  
 static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) {  void SQL_Driver_manager::close_connection(const String& url,
         Stack& stack=*static_cast<Stack *>(value);                                                                                    SQL_Connection& connection) {
         for(int i=0; i<=stack.top_index(); i++)          // deassociate from services[request]
                 expire_connection(stack.get(i), info);          connection.set_services(0);
 }          put_connection_to_cache(url, connection);
 void SQL_Driver_manager::maybe_expire_connection_cache() {  }
         time_t now=time(0);  
   
         if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {  // driver cache
                 connection_cache.for_each(expire_connections,   
                         reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));  SQL_Driver *SQL_Driver_manager::get_driver_from_cache(const String& protocol) {
           SYNCHRONIZED;
                 prev_expiration_pass_time=now;  
         }          return static_cast<SQL_Driver *>(driver_cache.get(protocol));
 }  }
   
   void SQL_Driver_manager::put_driver_to_cache(const String& protocol,
                                                                                            SQL_Driver& driver) {
           SYNCHRONIZED;
   
           driver_cache.put(protocol, &driver);
   }
   
   // connection cache
   /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
   SQL_Connection *SQL_Driver_manager::get_connection_from_cache(const String& url) {
           SYNCHRONIZED;
   
           if(Stack *connections=static_cast<Stack *>(connection_cache.get(url)))
                   while(connections->top_index()>=0) { // there are cached connections to that 'url'
                           SQL_Connection *result=static_cast<SQL_Connection *>(connections->pop());
                           if(result->connected()) // not expired?
                                   return result;
                   }
   
           return 0;
   }
   
   void SQL_Driver_manager::put_connection_to_cache(const String& url,
                                                                                                    SQL_Connection& connection) {
           SYNCHRONIZED;
   
           Stack *connections=static_cast<Stack *>(connection_cache.get(url));
           if(!connections) { // there are no cached connections to that 'url' yet?
                   connections=NEW Stack(pool()); // NOTE: never freed up!
                   connection_cache.put(url, connections);
           }      
           connections->push(&connection);
   }
   
   void SQL_Driver_manager::maybe_expire_cache() {
           time_t now=time(0);
   
           if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTIONS_SECONDS) {
                   connection_cache.for_each(expire_connections,
                           reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS));
   
                   prev_expiration_pass_time=now;
           }
   }
   
   static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
           SQL_Connection& connection=*static_cast<SQL_Connection *>(value);
           Table& table=*static_cast<Table *>(info);
   
           if(connection.connected()) {
                   Pool& pool=table.pool();
                   Array& row=*new(pool) Array(pool);
   
                   // url
                   row+=&url_without_login(pool, connection.get_url());
                   // time
                   time_t time_stamp=connection.get_time_stamp();
                   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);
   
                   table+=&row;
           }
   }
   static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
           Stack& stack=*static_cast<Stack *>(value);
           Array_iter iter(stack);
           for(int countdown=stack.top_index(); countdown-->=0; )
                   add_connection_to_status_cache_table(iter.next(), info);
   }
   
   
   Value& SQL_Driver_manager::get_status(Pool& pool, const String *source) {
           VHash& result=*new(pool) VHash(pool);
          
           // cache
           {
                   Array& columns=*new(pool) Array(pool);
                   columns+=new(pool) String(pool, "url");
                   columns+=new(pool) String(pool, "time");
                   Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
   
                   connection_cache.for_each(add_connections_to_status_cache_table, &table);
   
                   result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
           }
   
           return result;
   }
   

Removed from v.1.22  
changed lines
  Added in v.1.53


E-mail: