--- parser3/src/include/Attic/pa_db_table.h 2001/10/26 06:40:25 1.2 +++ parser3/src/include/Attic/pa_db_table.h 2001/10/29 08:23:49 1.7 @@ -4,19 +4,21 @@ Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: pa_db_table.h,v 1.2 2001/10/26 06:40:25 paf Exp $ + $Id: pa_db_table.h,v 1.7 2001/10/29 08:23:49 paf Exp $ */ #ifndef PA_DB_TABLE_H #define PA_DB_TABLE_H #include "pa_config_includes.h" -#include "pa_pool.h" -#include "pa_db_connection.h" #include "pa_globals.h" +#include "pa_pool.h" -#ifdef HAVE_DB_H +#ifdef DB2 # include +# if DB_VERSION_MAJOR != 2 +# error Parser needs DB 2.x.x version of libdb to compile +# endif #endif // defines @@ -25,6 +27,9 @@ // forwards +class DB_Connection; + +class DB_Table_ptr; class DB_Transaction; class DB_Cursor; @@ -32,72 +37,42 @@ class DB_Cursor; /// DB table. handy wrapper around low level calls class DB_Table : public Pooled { - friend DB_Transaction; - friend DB_Cursor; + friend class DB_Table_ptr; + friend class DB_Transaction; + friend class DB_Cursor; public: - DB_Table(Pool& pool, const String& afile_spec, DB_Connection& aconnection); + DB_Table(Pool& apool, const String& afile_name, DB_Connection& aconnection); + ~DB_Table(); + + const String& file_name() { return ffile_name; } - 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_useduse(); + } + ~DB_Table_ptr() { + ftable->unuse(); + } + DB_Table* operator->() { + return ftable; + } + DB_Table& operator*() { + return *ftable; + } + + // copying + DB_Table_ptr(const DB_Table_ptr& src) : ftable(src.ftable) { + ftable->use(); + } + DB_Table_ptr& operator =(const DB_Table_ptr& src) { + // may do without this=src check + ftable->unuse(); + ftable=src.ftable; + ftable->use(); + + return *this; + } +}; + /// Auto-object used for temporary changing DB_Table::tid. class DB_Transaction { - DB_Table& ftable; - bool marked_to_rollback; - DB_TXN *saved_tid; bool saved_tid_has_parent; public: - DB_Transaction(DB_Table& atable) : ftable(atable), marked_to_rollback(false) { - atable.transaction_begin_save(saved_tid, saved_tid_has_parent); + + DB_Transaction(Pool& apool, DB_Table& atable, DB_Transaction *& aparent_ref); + ~DB_Transaction(); + DB_TXN *id() { return fid; } + void mark_to_rollback(); + + void put(const String& key, const String& data, time_t time_to_die) { + ftable.put(this, key, data, time_to_die); + } + String *get(const String& key) { + return ftable.get(this, fpool, key); } - ~DB_Transaction() { - if(marked_to_rollback) - ftable.rollback_restore(saved_tid, saved_tid_has_parent); - else - ftable.commit_restore(saved_tid, saved_tid_has_parent); + void remove(const String& key) { + ftable.remove(this, key); } - void mark_to_rollback() { - marked_to_rollback=true; + +private: + + void check(const char *operation, const String *source, int error) { + ftable.check(operation, source, error); } + +private: + + Pool& fpool; + DB_Table& ftable; + DB_Transaction *parent; + DB_Transaction *& fparent_ref; + DB_TXN *fid; + bool marked_to_rollback; + }; /// DB cursor. handy wrapper around low level calls class DB_Cursor { - friend DB_Table; public: - DB_Cursor(DB_Table& atable, const String *asource); + DB_Cursor(DB_Table& atable, DB_Transaction *transaction, const String *asource); ~DB_Cursor(); /// pass empty strings to key&data, would fill them - bool get(String *& key, String *& data, u_int32_t flags); + bool get(Pool& pool, String *& key, String *& data, u_int32_t flags); + bool move(u_int32_t flags); void remove(u_int32_t flags); private: const String *fsource; @@ -153,12 +175,12 @@ private: ftable.check(operation, source, error); } /// @returns new string - String& key_dbt_to_string(DBT& key_dbt) { - return ftable.key_dbt_to_string(key_dbt); + String& key_dbt_to_string(Pool& pool, DBT& key_dbt) { + return ftable.key_dbt_to_string(pool, key_dbt); } /// @returns new string if it not expired - String *data_dbt_to_string(const DBT& data_dbt) { - return ftable.data_dbt_to_string(data_dbt); + String *data_dbt_to_string(Pool& pool, const DBT& data_dbt) { + return ftable.data_dbt_to_string(pool, data_dbt); } };