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