Annotation of parser3/src/include/pa_db_connection.h, revision 1.4
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.4 ! parser 7: $Id: pa_db_connection.h,v 1.3 2001/10/23 14:43:44 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.4 ! parser 107: /// pass empty dbt, would fill it from string
! 108: void key_string_to_dbt(const String& key_string, DBT& key_result);
! 109: /// pass empty dbt, would fill it from string
! 110: void key_dbt_to_string(const DBT& key_dbt, String& key_result);
! 111: /// pass empty dbt, would fill it from string
! 112: void data_string_to_dbt(const String& data_string, DBT& data_result);
! 113: /// pass empty string, would fill it from dbt
! 114: void data_dbt_to_string(const DBT& data_dbt, String& data_result);
1.2 parser 115:
116: };
117:
118: /// Auto-object used for temporary changing DB_Connection::tid.
119: class Auto_transaction {
120: DB_Connection& fconnection;
121: bool marked_to_rollback;
122: DB_TXN *saved_tid;
123: public:
124: Auto_transaction(DB_Connection& aconnection) :
125: fconnection(aconnection), marked_to_rollback(false),
126: saved_tid(aconnection.transaction_begin_save()) {
127: }
128: ~Auto_transaction() {
129: if(marked_to_rollback)
130: fconnection.rollback_restore(saved_tid);
131: else
132: fconnection.commit_restore(saved_tid);
133: }
134: void mark_to_rollback() {
135: marked_to_rollback=true;
1.3 parser 136: }
137: };
138:
139: /// DB cursor. handy wrapper around low level <db.h> calls
140: class DB_Cursor {
141: friend DB_Connection;
142: private:
143: DB_Cursor(DB_Connection& aconnection, const String *asource);
144: public:
145: ~DB_Cursor();
1.4 ! parser 146: /// pass empty strings to key&data, would fill them
1.3 parser 147: bool get(String& key, String& data, u_int32_t flags);
148: private:
149: const String *fsource;
150: DB_Connection& fconnection;
151: DBC *cursor;
152: private:
153: void check(const char *operation, const String *source, int error) {
154: fconnection.check(operation, source, error);
155: }
1.4 ! parser 156: /// pass empty string, would fill it from dbt
! 157: void key_dbt_to_string(DBT& dbt, String& result) {
! 158: fconnection.key_dbt_to_string(dbt, result);
! 159: }
! 160: /// pass empty string, would fill it from dbt
! 161: void data_dbt_to_string(DBT& dbt, String& result) {
! 162: fconnection.data_dbt_to_string(dbt, result);
1.2 parser 163: }
1.1 parser 164: };
165:
166: #endif
E-mail: