Annotation of parser3/src/include/pa_db_table.h, revision 1.15
1.1 paf 1: /** @file
2: Parser: sql db decl.
3:
1.14 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.15 ! paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 paf 6:
1.15 ! paf 7: $Id: pa_db_table.h,v 1.14 2002/02/08 07:27:43 paf Exp $
1.1 paf 8: */
9:
10: #ifndef PA_DB_TABLE_H
11: #define PA_DB_TABLE_H
12:
1.3 paf 13: #include "pa_globals.h"
1.1 paf 14: #include "pa_pool.h"
15:
1.5 paf 16: #ifdef DB2
1.1 paf 17: # include <db.h>
1.6 paf 18: # if DB_VERSION_MAJOR != 2
19: # error Parser needs DB 2.x.x version of libdb to compile
20: # endif
1.1 paf 21: #endif
22:
23: // defines
24:
25: #define PA_DB_ACCESS_METHOD DB_BTREE
26:
27: // forwards
28:
1.3 paf 29: class DB_Connection;
30:
31: class DB_Table_ptr;
1.1 paf 32: class DB_Cursor;
33:
34: // class
35:
36: /// DB table. handy wrapper around low level <db.h> calls
37: class DB_Table : public Pooled {
1.7 paf 38: friend class DB_Table_ptr;
39: friend class DB_Cursor;
1.1 paf 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: }
1.8 paf 50: time_t get_time_used() { return time_used; }
51: int get_users_count() { return used; }
1.1 paf 52:
1.13 paf 53: void put(const String& key, const String& data, time_t lifespan);
54: String *get(Pool& pool, const String& key, time_t lifespan);
55: void remove(const String& key);
1.3 paf 56:
57: private: // table usage methods
58:
59: void use();
60: void unuse();
1.1 paf 61:
1.3 paf 62: private: // table usage data
1.1 paf 63:
1.3 paf 64: int used;
1.1 paf 65:
66: private:
67:
68: DB_Connection& fconnection;
69: DB_ENV& dbenv;
1.3 paf 70: const String& ffile_name; const char *file_name_cstr;
1.1 paf 71: DB *db;
72: time_t time_used;
73:
74: private:
75:
76: void check(const char *operation, const String *source, int error);
77: /// pass empty dbt, would fill it from string
78: void key_string_to_dbt(const String& key_string, DBT& key_result);
79: /// @returns new string
1.3 paf 80: String& key_dbt_to_string(Pool& pool, const DBT& key_dbt);
1.1 paf 81: /// pass empty dbt, would fill it from string
1.9 paf 82: void data_string_to_dbt(const String& data_string, time_t lifespan,
1.1 paf 83: DBT& data_result);
84: /// @returns new string if it not expired
1.9 paf 85: String *data_dbt_to_string(Pool& pool, const DBT& data_dbt, time_t lifespan);
1.1 paf 86:
87: };
88:
1.3 paf 89: /// Auto-object used to track DB_Table usage
90: class DB_Table_ptr {
91: DB_Table *ftable;
92: public:
93: explicit DB_Table_ptr(DB_Table *atable) : ftable(atable) {
94: ftable->use();
95: }
96: ~DB_Table_ptr() {
97: ftable->unuse();
98: }
99: DB_Table* operator->() {
100: return ftable;
101: }
102: DB_Table& operator*() {
103: return *ftable;
104: }
105:
106: // copying
107: DB_Table_ptr(const DB_Table_ptr& src) : ftable(src.ftable) {
108: ftable->use();
109: }
110: DB_Table_ptr& operator =(const DB_Table_ptr& src) {
111: // may do without this=src check
112: ftable->unuse();
113: ftable=src.ftable;
114: ftable->use();
115:
116: return *this;
117: }
118: };
119:
1.1 paf 120: /// DB cursor. handy wrapper around low level <db.h> calls
121: class DB_Cursor {
122: public:
1.13 paf 123: DB_Cursor(DB_Table& atable, const String *asource);
1.1 paf 124: ~DB_Cursor();
125: /// pass empty strings to key&data, would fill them
1.3 paf 126: bool get(Pool& pool, String *& key, String *& data, u_int32_t flags);
1.4 paf 127: bool move(u_int32_t flags);
1.1 paf 128: void remove(u_int32_t flags);
129: private:
130: const String *fsource;
131: DB_Table& ftable;
132: DBC *cursor;
133: private:
134: void check(const char *operation, const String *source, int error) {
135: ftable.check(operation, source, error);
136: }
137: /// @returns new string
1.3 paf 138: String& key_dbt_to_string(Pool& pool, DBT& key_dbt) {
139: return ftable.key_dbt_to_string(pool, key_dbt);
1.1 paf 140: }
141: /// @returns new string if it not expired
1.9 paf 142: String *data_dbt_to_string(Pool& pool, const DBT& data_dbt, time_t lifespan) {
143: return ftable.data_dbt_to_string(pool, data_dbt, lifespan);
1.1 paf 144: }
145: };
146:
147: #endif
E-mail: