--- parser3/src/include/Attic/pa_db_connection.h 2001/10/24 09:03:42 1.4 +++ parser3/src/include/Attic/pa_db_connection.h 2001/11/05 11:46:23 1.17 @@ -1,10 +1,11 @@ /** @file - Parser: sql db decl. + Parser: sql driver connection decl. + global sql driver connection, must be thread-safe Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: pa_db_connection.h,v 1.4 2001/10/24 09:03:42 parser Exp $ + $Id: pa_db_connection.h,v 1.17 2001/11/05 11:46:23 paf Exp $ */ #ifndef PA_DB_CONNECTION_H @@ -12,154 +13,107 @@ #include "pa_config_includes.h" #include "pa_pool.h" -#include "pa_db_manager.h" -#include "pa_globals.h" - -#ifdef HAVE_DB_H -# include -#endif +#include "pa_hash.h" +#include "pa_db_table.h" +#include "pa_value.h" // defines -#define PA_DB_ACCESS_METHOD DB_BTREE - // forwards -class Auto_transaction; -class DB_Cursor; - -// class +class DB_Table; +class DB_Connection_ptr; -/// DB connection. handy wrapper around low level calls +/// sql driver connection class DB_Connection : public Pooled { - friend Auto_transaction; - friend DB_Cursor; + friend class DB_Table; + friend class DB_Connection_ptr; public: - DB_Connection(Pool& pool, const String& afile_spec, DB_ENV& adbenv); - - //const String& url() { return ffile_spec; } + DB_Connection(Pool& apool, const String& db_home); + ~DB_Connection(); - void set_services(Pool *aservices_pool) { - time_used=time(0); // they started to use at this time - fservices_pool=aservices_pool; - } bool expired(time_t older_dies) { - return time_usedclose_connection(ffile_spec, *this); +private: // connection usage methods + + void use() { + time_used=time(0); // they started to use at this time + used++; + } + void unuse() { + used--; } - bool connected() { return db!=0; } - void connect(); - void disconnect(); - bool ping() { return !needs_recovery; } +private: // connection usage data - void put(const String& key, const String& data); - String *get(const String& key); - void _delete(const String& key); + int used; - DB_Cursor cursor(const String *source); +private: // table cache + DB_Table *get_table_from_cache(const String& file_name); + void put_table_to_cache(const String& file_name, DB_Table& table); + void maybe_expire_table_cache(); private: + time_t prev_expiration_pass_time; - DB_ENV& fdbenv; - const String& ffile_spec; const char *file_spec_cstr; - Pool *fservices_pool; - DB *db; - bool needs_recovery; - DB_TXN *ftid; - time_t time_used; +private: // for DB_Table -private: // transaction + /// caches table + void close_table(const String& file_name, DB_Table& table); - /// commits current transaction, restores previous transaction handle - void commit_restore(DB_TXN *atid) { - if(ftid) - check("txn_commit", &ffile_spec, txn_commit(ftid)); +private: - ftid=atid; - } - - /// rolls current transaction back, restores previous transaction handle - void rollback_restore(DB_TXN *atid) { - if(ftid) - check("txn_abort", &ffile_spec, txn_abort(ftid)); + time_t time_used; - ftid=atid; - } - - /// stars new current trunsaction @returns previous transaction handle - DB_TXN *transaction_begin_save() { - DB_TXN *result=ftid; - check("txn_begin", &ffile_spec, ::txn_begin(fdbenv.tx_info, 0/*parent*/, &ftid)); + const String& fdb_home; + DB_ENV dbenv; + Hash table_cache; - return result; - } - private: void check(const char *operation, const String *source, int error); - void *malloc(size_t size) { return fservices_pool->malloc(size); } - void *calloc(size_t size) { return fservices_pool->calloc(size); } - /// pass empty dbt, would fill it from string - void key_string_to_dbt(const String& key_string, DBT& key_result); - /// pass empty dbt, would fill it from string - void key_dbt_to_string(const DBT& key_dbt, String& key_result); - /// pass empty dbt, would fill it from string - void data_string_to_dbt(const String& data_string, DBT& data_result); - /// pass empty string, would fill it from dbt - void data_dbt_to_string(const DBT& data_dbt, String& data_result); +public: + + Value& get_status(Pool& pool, const String *source); }; -/// Auto-object used for temporary changing DB_Connection::tid. -class Auto_transaction { - DB_Connection& fconnection; - bool marked_to_rollback; - DB_TXN *saved_tid; +/// Auto-object used to track DB_Connection usage +class DB_Connection_ptr { + DB_Connection *fconnection; public: - Auto_transaction(DB_Connection& aconnection) : - fconnection(aconnection), marked_to_rollback(false), - saved_tid(aconnection.transaction_begin_save()) { - } - ~Auto_transaction() { - if(marked_to_rollback) - fconnection.rollback_restore(saved_tid); - else - fconnection.commit_restore(saved_tid); + DB_Connection_ptr(DB_Connection *aconnection) : fconnection(aconnection) { + fconnection->use(); } - void mark_to_rollback() { - marked_to_rollback=true; + ~DB_Connection_ptr() { + fconnection->unuse(); + } + DB_Connection* operator->() { + return fconnection; } -}; -/// DB cursor. handy wrapper around low level calls -class DB_Cursor { - friend DB_Connection; -private: - DB_Cursor(DB_Connection& aconnection, const String *asource); -public: - ~DB_Cursor(); - /// pass empty strings to key&data, would fill them - bool get(String& key, String& data, u_int32_t flags); -private: - const String *fsource; - DB_Connection& fconnection; - DBC *cursor; -private: - void check(const char *operation, const String *source, int error) { - fconnection.check(operation, source, error); + // copying + DB_Connection_ptr(const DB_Connection_ptr& src) : fconnection(src.fconnection) { + fconnection->use(); } - /// pass empty string, would fill it from dbt - void key_dbt_to_string(DBT& dbt, String& result) { - fconnection.key_dbt_to_string(dbt, result); - } - /// pass empty string, would fill it from dbt - void data_dbt_to_string(DBT& dbt, String& result) { - fconnection.data_dbt_to_string(dbt, result); + DB_Connection_ptr& operator =(const DB_Connection_ptr& src) { + // may do without this=src check + fconnection->unuse(); + fconnection=src.fconnection; + fconnection->use(); + + return *this; } };