Annotation of parser3/src/include/pa_db_table.h, revision 1.2

1.1       paf         1: /** @file
                      2:        Parser: sql db decl.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      6: 
1.2     ! paf         7:        $Id: pa_db_table.h,v 1.1 2001/10/25 13:18:19 paf Exp $
1.1       paf         8: */
                      9: 
                     10: #ifndef PA_DB_TABLE_H
                     11: #define PA_DB_TABLE_H
                     12: 
                     13: #include "pa_config_includes.h"
                     14: #include "pa_pool.h"
                     15: #include "pa_db_connection.h"
                     16: #include "pa_globals.h"
                     17: 
                     18: #ifdef HAVE_DB_H
                     19: #      include <db.h>
                     20: #endif
                     21: 
                     22: // defines
                     23: 
                     24: #define PA_DB_ACCESS_METHOD DB_BTREE
                     25: 
                     26: // forwards
                     27: 
1.2     ! paf        28: class DB_Transaction;
1.1       paf        29: class DB_Cursor;
                     30: 
                     31: // class
                     32: 
                     33: /// DB table. handy wrapper around low level <db.h> calls
                     34: class DB_Table : public Pooled {
1.2     ! paf        35:        friend DB_Transaction;
1.1       paf        36:        friend DB_Cursor;
                     37: public:
                     38: 
                     39:        DB_Table(Pool& pool, const String& afile_spec, DB_Connection& aconnection);
                     40: 
                     41:        void set_services(Pool *aservices_pool) {
                     42:                time_used=time(0); // they started to use at this time
                     43:                fservices_pool=aservices_pool;
                     44:        }
                     45:        bool expired(time_t older_dies) {
                     46:                return time_used<older_dies;
                     47:        }
                     48: 
                     49:        void close() {
                     50:                fconnection.close_table(ffile_spec, *this);
                     51:        }
                     52: 
                     53:        bool connected() { return db!=0; }
                     54:        void connect();
                     55:        void disconnect();      
                     56:        bool ping() { return errors==0; }
                     57: 
                     58:        void put(const String& key, const String& data, time_t time_to_die);
                     59:        String *get(const String& key);
                     60:        void remove(const String& key);
                     61: 
                     62: private:
                     63: 
                     64:        DB_Connection& fconnection;
                     65:        DB_ENV& dbenv;
                     66:        const String& ffile_spec; const char *file_spec_cstr;
                     67:        Pool *fservices_pool;
                     68:        DB *db;
                     69:        int errors;
1.2     ! paf        70:        DB_TXN *ftid; bool ftid_has_parent;
1.1       paf        71:        time_t time_used;
                     72: 
                     73: private: // transaction
                     74: 
                     75:        /// commits current transaction, restores previous transaction handle
1.2     ! paf        76:        void commit_restore(DB_TXN *atid, bool atid_has_parent) { 
        !            77:                if(ftid && !atid_has_parent) // it's parent responsibility to do that
1.1       paf        78:                        check("txn_commit", &ffile_spec, txn_commit(ftid)); 
                     79: 
                     80:                ftid=atid;
1.2     ! paf        81:                ftid_has_parent=atid_has_parent;
1.1       paf        82:        }
                     83:        
                     84:        /// rolls current transaction back, restores previous transaction handle
1.2     ! paf        85:        void rollback_restore(DB_TXN *atid, bool atid_has_parent) {
        !            86:                if(ftid && !atid_has_parent) // it's parent responsibility to do that
1.1       paf        87:                        check("txn_abort", &ffile_spec, txn_abort(ftid));
                     88: 
                     89:                ftid=atid;
1.2     ! paf        90:                ftid_has_parent=atid_has_parent;
1.1       paf        91:        }
                     92:        
                     93:        /// stars new current trunsaction @returns previous transaction handle
1.2     ! paf        94:        void transaction_begin_save(DB_TXN *& rtid, bool& rtid_has_parent) {
        !            95:                rtid=ftid;
        !            96:                rtid_has_parent=ftid_has_parent;
        !            97:                check("txn_begin", &ffile_spec, ::txn_begin(dbenv.tx_info, rtid, &ftid));
        !            98:                ftid_has_parent=rtid!=0;
1.1       paf        99:        }
                    100:        
                    101: private:
                    102: 
                    103:        void check(const char *operation, const String *source, int error);
                    104:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    105:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
                    106:        /// pass empty dbt, would fill it from string
                    107:        void key_string_to_dbt(const String& key_string, DBT& key_result);
                    108:        /// @returns new string
                    109:        String& key_dbt_to_string(const DBT& key_dbt);
                    110:        /// pass empty dbt, would fill it from string
                    111:        void data_string_to_dbt(const String& data_string,  time_t time_to_die, 
                    112:                DBT& data_result);
                    113:        /// @returns new string if it not expired
                    114:        String *data_dbt_to_string(const DBT& data_dbt);
                    115: 
                    116: };
                    117: 
                    118: ///    Auto-object used for temporary changing DB_Table::tid.
1.2     ! paf       119: class DB_Transaction {
1.1       paf       120:        DB_Table& ftable;
                    121:        bool marked_to_rollback;
1.2     ! paf       122:        DB_TXN *saved_tid; bool saved_tid_has_parent;
1.1       paf       123: public:
1.2     ! paf       124:        DB_Transaction(DB_Table& atable) : ftable(atable), marked_to_rollback(false) {
        !           125:                atable.transaction_begin_save(saved_tid, saved_tid_has_parent);
1.1       paf       126:        }
1.2     ! paf       127:        ~DB_Transaction() { 
1.1       paf       128:                if(marked_to_rollback)
1.2     ! paf       129:                        ftable.rollback_restore(saved_tid, saved_tid_has_parent);
1.1       paf       130:                else
1.2     ! paf       131:                        ftable.commit_restore(saved_tid, saved_tid_has_parent);
1.1       paf       132:        }
                    133:        void mark_to_rollback() {
                    134:                marked_to_rollback=true;
                    135:        }
                    136: };
                    137: 
                    138: /// DB cursor. handy wrapper around low level <db.h> calls
                    139: class DB_Cursor {
                    140:        friend DB_Table;
                    141: public:
                    142:        DB_Cursor(DB_Table& atable, const String *asource);
                    143:        ~DB_Cursor();
                    144:        /// pass empty strings to key&data, would fill them
                    145:        bool get(String *& key, String *& data, u_int32_t flags);
                    146:        void remove(u_int32_t flags);
                    147: private:
                    148:        const String *fsource;
                    149:        DB_Table& ftable;
                    150:        DBC *cursor;
                    151: private:
                    152:        void check(const char *operation, const String *source, int error) {
                    153:                ftable.check(operation, source, error);
                    154:        }
                    155:        /// @returns new string
                    156:        String& key_dbt_to_string(DBT& key_dbt) {
                    157:                return ftable.key_dbt_to_string(key_dbt);
                    158:        }
                    159:        /// @returns new string if it not expired
                    160:        String *data_dbt_to_string(const DBT& data_dbt) {       
                    161:                return ftable.data_dbt_to_string(data_dbt);
                    162:        }
                    163: };
                    164: 
                    165: #endif

E-mail: