Annotation of parser3/src/include/pa_db_connection.h, revision 1.3

1.1       parser      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.3     ! parser      7:        $Id: pa_db_connection.h,v 1.2 2001/10/23 12:41:05 parser Exp $
1.1       parser      8: */
                      9: 
                     10: #ifndef PA_DB_CONNECTION_H
                     11: #define PA_DB_CONNECTION_H
                     12: 
                     13: #include "pa_config_includes.h"
                     14: #include "pa_pool.h"
                     15: #include "pa_db_manager.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: 
1.2       parser     26: // forwards
                     27: 
                     28: class Auto_transaction;
1.3     ! parser     29: class DB_Cursor;
1.2       parser     30: 
1.1       parser     31: // class
                     32: 
1.3     ! parser     33: /// DB connection. handy wrapper around low level <db.h> calls
1.1       parser     34: class DB_Connection : public Pooled {
1.2       parser     35:        friend Auto_transaction;
1.3     ! parser     36:        friend DB_Cursor;
1.1       parser     37: public:
                     38: 
                     39:        DB_Connection(Pool& pool, const String& afile_spec, DB_ENV& adbenv);
                     40:        
                     41:        //const String& url() { return ffile_spec; }
                     42: 
                     43:        void set_services(Pool *aservices_pool) {
                     44:                time_used=time(0); // they started to use at this time
                     45:                fservices_pool=aservices_pool;
                     46:        }
                     47:        bool expired(time_t older_dies) {
                     48:                return time_used<older_dies;
                     49:        }
                     50: 
                     51:        void close() {
                     52:                DB_manager->close_connection(ffile_spec, *this);
                     53:        }
                     54: 
                     55:        bool connected() { return db!=0; }
                     56:        void connect();
1.2       parser     57:        void disconnect();      
1.1       parser     58:        bool ping() { return !needs_recovery; }
                     59: 
                     60:        void put(const String& key, const String& data);
                     61:        String *get(const String& key);
1.2       parser     62:        void _delete(const String& key);
1.1       parser     63: 
1.3     ! parser     64:        DB_Cursor cursor(const String *source);
        !            65: 
1.1       parser     66: private:
                     67: 
                     68:        DB_ENV& fdbenv;
1.3     ! parser     69:        const String& ffile_spec; const char *file_spec_cstr;
1.1       parser     70:        Pool *fservices_pool;
                     71:        DB *db;
                     72:        bool needs_recovery;
1.2       parser     73:        DB_TXN *ftid;
1.1       parser     74:        time_t time_used;
                     75: 
1.2       parser     76: private: // transaction
                     77: 
                     78:        /// commits current transaction, restores previous transaction handle
                     79:        void commit_restore(DB_TXN *atid) { 
                     80:                if(ftid)
                     81:                        check("txn_commit", &ffile_spec, txn_commit(ftid)); 
                     82: 
                     83:                ftid=atid;
                     84:        }
                     85:        
                     86:        /// rolls current transaction back, restores previous transaction handle
                     87:        void rollback_restore(DB_TXN *atid) {
                     88:                if(ftid)
                     89:                        check("txn_abort", &ffile_spec, txn_abort(ftid));
                     90: 
                     91:                ftid=atid;
                     92:        }
                     93:        
                     94:        /// stars new current trunsaction @returns previous transaction handle
                     95:        DB_TXN *transaction_begin_save() {
                     96:                DB_TXN *result=ftid;
                     97:                check("txn_begin", &ffile_spec, ::txn_begin(fdbenv.tx_info, 0/*parent*/, &ftid));
                     98: 
                     99:                return result;
                    100:        }
                    101:        
1.1       parser    102: private:
                    103: 
                    104:        void check(const char *operation, const String *source, int error);
                    105:        void *malloc(size_t size) { return fservices_pool->malloc(size); }
                    106:        void *calloc(size_t size) { return fservices_pool->calloc(size); }
1.3     ! parser    107:        void dbt_to_string(DBT& dbt, String& result);
1.2       parser    108: 
                    109: };
                    110: 
                    111: ///    Auto-object used for temporary changing DB_Connection::tid.
                    112: class Auto_transaction {
                    113:        DB_Connection& fconnection;
                    114:        bool marked_to_rollback;
                    115:        DB_TXN *saved_tid;
                    116: public:
                    117:        Auto_transaction(DB_Connection& aconnection) : 
                    118:                fconnection(aconnection), marked_to_rollback(false),
                    119:                saved_tid(aconnection.transaction_begin_save()) {
                    120:        }
                    121:        ~Auto_transaction() { 
                    122:                if(marked_to_rollback)
                    123:                        fconnection.rollback_restore(saved_tid);
                    124:                else
                    125:                        fconnection.commit_restore(saved_tid);
                    126:        }
                    127:        void mark_to_rollback() {
                    128:                marked_to_rollback=true;
1.3     ! parser    129:        }
        !           130: };
        !           131: 
        !           132: /// DB cursor. handy wrapper around low level <db.h> calls
        !           133: class DB_Cursor {
        !           134:        friend DB_Connection;
        !           135: private:
        !           136:        DB_Cursor(DB_Connection& aconnection, const String *asource);
        !           137: public:
        !           138:        ~DB_Cursor();
        !           139:        bool get(String& key, String& data, u_int32_t flags);
        !           140: private:
        !           141:        const String *fsource;
        !           142:        DB_Connection& fconnection;
        !           143:        DBC *cursor;
        !           144: private:
        !           145:        void check(const char *operation, const String *source, int error) {
        !           146:                fconnection.check(operation, source, error);
        !           147:        }
        !           148:        void dbt_to_string(DBT& dbt, String& result) {
        !           149:                fconnection.dbt_to_string(dbt, result);
1.2       parser    150:        }
1.1       parser    151: };
                    152: 
                    153: #endif

E-mail: