Annotation of parser3/src/include/pa_db_table.h, revision 1.1
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:
! 7: $Id: pa_db_table.h,v 1.8 2001/10/25 09:48:18 paf Exp $
! 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:
! 28: class Auto_transaction;
! 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 {
! 35: friend Auto_transaction;
! 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;
! 70: DB_TXN *ftid;
! 71: time_t time_used;
! 72:
! 73: private: // transaction
! 74:
! 75: /// commits current transaction, restores previous transaction handle
! 76: void commit_restore(DB_TXN *atid) {
! 77: if(ftid)
! 78: check("txn_commit", &ffile_spec, txn_commit(ftid));
! 79:
! 80: ftid=atid;
! 81: }
! 82:
! 83: /// rolls current transaction back, restores previous transaction handle
! 84: void rollback_restore(DB_TXN *atid) {
! 85: if(ftid)
! 86: check("txn_abort", &ffile_spec, txn_abort(ftid));
! 87:
! 88: ftid=atid;
! 89: }
! 90:
! 91: /// stars new current trunsaction @returns previous transaction handle
! 92: DB_TXN *transaction_begin_save() {
! 93: DB_TXN *parent=ftid;
! 94: check("txn_begin", &ffile_spec, ::txn_begin(dbenv.tx_info, parent, &ftid));
! 95:
! 96: return parent;
! 97: }
! 98:
! 99: private:
! 100:
! 101: void check(const char *operation, const String *source, int error);
! 102: void *malloc(size_t size) { return fservices_pool->malloc(size); }
! 103: void *calloc(size_t size) { return fservices_pool->calloc(size); }
! 104: /// pass empty dbt, would fill it from string
! 105: void key_string_to_dbt(const String& key_string, DBT& key_result);
! 106: /// @returns new string
! 107: String& key_dbt_to_string(const DBT& key_dbt);
! 108: /// pass empty dbt, would fill it from string
! 109: void data_string_to_dbt(const String& data_string, time_t time_to_die,
! 110: DBT& data_result);
! 111: /// @returns new string if it not expired
! 112: String *data_dbt_to_string(const DBT& data_dbt);
! 113:
! 114: };
! 115:
! 116: /// Auto-object used for temporary changing DB_Table::tid.
! 117: class Auto_transaction {
! 118: DB_Table& ftable;
! 119: bool marked_to_rollback;
! 120: DB_TXN *saved_tid;
! 121: public:
! 122: Auto_transaction(DB_Table& atable) :
! 123: ftable(atable), marked_to_rollback(false),
! 124: saved_tid(atable.transaction_begin_save()) {
! 125: }
! 126: ~Auto_transaction() {
! 127: if(marked_to_rollback)
! 128: ftable.rollback_restore(saved_tid);
! 129: else
! 130: ftable.commit_restore(saved_tid);
! 131: }
! 132: void mark_to_rollback() {
! 133: marked_to_rollback=true;
! 134: }
! 135: };
! 136:
! 137: /// DB cursor. handy wrapper around low level <db.h> calls
! 138: class DB_Cursor {
! 139: friend DB_Table;
! 140: public:
! 141: DB_Cursor(DB_Table& atable, const String *asource);
! 142: ~DB_Cursor();
! 143: /// pass empty strings to key&data, would fill them
! 144: bool get(String *& key, String *& data, u_int32_t flags);
! 145: void remove(u_int32_t flags);
! 146: private:
! 147: const String *fsource;
! 148: DB_Table& ftable;
! 149: DBC *cursor;
! 150: private:
! 151: void check(const char *operation, const String *source, int error) {
! 152: ftable.check(operation, source, error);
! 153: }
! 154: /// @returns new string
! 155: String& key_dbt_to_string(DBT& key_dbt) {
! 156: return ftable.key_dbt_to_string(key_dbt);
! 157: }
! 158: /// @returns new string if it not expired
! 159: String *data_dbt_to_string(const DBT& data_dbt) {
! 160: return ftable.data_dbt_to_string(data_dbt);
! 161: }
! 162: };
! 163:
! 164: #endif
E-mail: