Annotation of parser3/src/include/pa_db_table.h, revision 1.5
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.5 ! paf 7: $Id: pa_db_table.h,v 1.4 2001/10/27 10:14:45 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"
1.3 paf 14: #include "pa_globals.h"
1.1 paf 15: #include "pa_pool.h"
16:
1.5 ! paf 17: #ifdef DB2
1.1 paf 18: # include <db.h>
19: #endif
20:
21: // defines
22:
23: #define PA_DB_ACCESS_METHOD DB_BTREE
24:
25: // forwards
26:
1.3 paf 27: class DB_Connection;
28:
29: class DB_Table_ptr;
1.2 paf 30: class DB_Transaction;
1.1 paf 31: class DB_Cursor;
32:
33: // class
34:
35: /// DB table. handy wrapper around low level <db.h> calls
36: class DB_Table : public Pooled {
1.3 paf 37: friend DB_Table_ptr;
1.2 paf 38: friend DB_Transaction;
1.1 paf 39: friend DB_Cursor;
40: public:
41:
1.3 paf 42: DB_Table(Pool& apool, const String& afile_name, DB_Connection& aconnection);
43: ~DB_Table();
44:
45: const String& file_name() { return ffile_name; }
1.1 paf 46:
47: bool expired(time_t older_dies) {
1.3 paf 48: return !used && time_used<older_dies;
1.1 paf 49: }
50:
1.3 paf 51: void put(DB_Transaction *t, const String& key, const String& data, time_t time_to_die);
52: String *get(DB_Transaction *t, Pool& pool, const String& key);
53: void remove(DB_Transaction *t, const String& key);
54:
55: private: // table usage methods
56:
57: void use();
58: void unuse();
1.1 paf 59:
1.3 paf 60: private: // table usage data
1.1 paf 61:
1.3 paf 62: int used;
1.1 paf 63:
64: private:
65:
66: DB_Connection& fconnection;
67: DB_ENV& dbenv;
1.3 paf 68: const String& ffile_name; const char *file_name_cstr;
1.1 paf 69: Pool *fservices_pool;
70: DB *db;
71: time_t time_used;
72:
73: private:
74:
75: void check(const char *operation, const String *source, int error);
76: void *malloc(size_t size) { return fservices_pool->malloc(size); }
77: void *calloc(size_t size) { return fservices_pool->calloc(size); }
78: /// pass empty dbt, would fill it from string
79: void key_string_to_dbt(const String& key_string, DBT& key_result);
80: /// @returns new string
1.3 paf 81: String& key_dbt_to_string(Pool& pool, const DBT& key_dbt);
1.1 paf 82: /// pass empty dbt, would fill it from string
83: void data_string_to_dbt(const String& data_string, time_t time_to_die,
84: DBT& data_result);
85: /// @returns new string if it not expired
1.3 paf 86: String *data_dbt_to_string(Pool& pool, const DBT& data_dbt);
1.1 paf 87:
88: };
89:
1.3 paf 90: /// Auto-object used to track DB_Table usage
91: class DB_Table_ptr {
92: DB_Table *ftable;
93: public:
94: explicit DB_Table_ptr(DB_Table *atable) : ftable(atable) {
95: ftable->use();
96: }
97: ~DB_Table_ptr() {
98: ftable->unuse();
99: }
100: DB_Table* operator->() {
101: return ftable;
102: }
103: DB_Table& operator*() {
104: return *ftable;
105: }
106:
107: // copying
108: DB_Table_ptr(const DB_Table_ptr& src) : ftable(src.ftable) {
109: ftable->use();
110: }
111: DB_Table_ptr& operator =(const DB_Table_ptr& src) {
112: // may do without this=src check
113: ftable->unuse();
114: ftable=src.ftable;
115: ftable->use();
116:
117: return *this;
118: }
119: };
120:
1.1 paf 121: /// Auto-object used for temporary changing DB_Table::tid.
1.2 paf 122: class DB_Transaction {
1.1 paf 123: public:
1.3 paf 124:
125: DB_Transaction(Pool& apool, DB_Table& atable, DB_Transaction *& aparent_ref);
126: ~DB_Transaction();
127: DB_TXN *id() { return fid; }
128: void mark_to_rollback();
129:
130: void put(const String& key, const String& data, time_t time_to_die) {
131: ftable.put(this, key, data, time_to_die);
132: }
133: String *get(const String& key) {
134: return ftable.get(this, fpool, key);
1.1 paf 135: }
1.3 paf 136: void remove(const String& key) {
137: ftable.remove(this, key);
1.1 paf 138: }
1.3 paf 139:
140: private:
141:
142: void check(const char *operation, const String *source, int error) {
143: ftable.check(operation, source, error);
1.1 paf 144: }
1.3 paf 145:
146: private:
147:
148: Pool& fpool;
149: DB_Table& ftable;
150: DB_Transaction *parent;
151: DB_Transaction *& fparent_ref;
152: DB_TXN *fid;
153: bool marked_to_rollback;
154:
1.1 paf 155: };
156:
157: /// DB cursor. handy wrapper around low level <db.h> calls
158: class DB_Cursor {
159: public:
1.3 paf 160: DB_Cursor(DB_Table& atable, DB_Transaction *transaction, const String *asource);
1.1 paf 161: ~DB_Cursor();
162: /// pass empty strings to key&data, would fill them
1.3 paf 163: bool get(Pool& pool, String *& key, String *& data, u_int32_t flags);
1.4 paf 164: bool move(u_int32_t flags);
1.1 paf 165: void remove(u_int32_t flags);
166: private:
167: const String *fsource;
168: DB_Table& ftable;
169: DBC *cursor;
170: private:
171: void check(const char *operation, const String *source, int error) {
172: ftable.check(operation, source, error);
173: }
174: /// @returns new string
1.3 paf 175: String& key_dbt_to_string(Pool& pool, DBT& key_dbt) {
176: return ftable.key_dbt_to_string(pool, key_dbt);
1.1 paf 177: }
178: /// @returns new string if it not expired
1.3 paf 179: String *data_dbt_to_string(Pool& pool, const DBT& data_dbt) {
180: return ftable.data_dbt_to_string(pool, data_dbt);
1.1 paf 181: }
182: };
183:
184: #endif
E-mail: