Annotation of parser3/src/main/pa_db_table.C, revision 1.7
1.1 paf 1: /** @file
2: Parser: Charset table implementation.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
6:
1.7 ! paf 7: $Id: pa_db_table.C,v 1.6 2001/10/28 11:40:48 paf Exp $
1.1 paf 8: */
9:
10: #include "pa_config_includes.h"
1.5 paf 11: #ifdef DB2
1.1 paf 12:
13: #include "pa_db_table.h"
14: #include "pa_exception.h"
1.3 paf 15: #include "pa_db_connection.h"
1.1 paf 16:
17: // defines
18:
1.7 ! paf 19: #ifdef DB_RMW
1.6 paf 20: # define DEADLOCK_POSSIBILITY_REDUCTION_FLAGS DB_RMW
21: #else
22: # define DEADLOCK_POSSIBILITY_REDUCTION_FLAGS 0
23: #endif
1.1 paf 24:
25: // consts
26:
27: const int DATA_STRING_SERIALIZED_VERSION=0x0100;
28:
29: // helper types
30:
31: #ifndef DOXYGEN
32: struct Data_string_serialized_prolog {
33: int version;
34: time_t time_to_die;
35: };
1.3 paf 36:
37: struct DBT_auto : public DBT {
38: DBT_auto() {
39: data=0;
40: size=ulen=dlen=doff=0;
41: flags=DB_DBT_MALLOC;
42: }
43:
44: ~DBT_auto() {
45: if(flags & DB_DBT_MALLOC)
46: free(data);
47: }
48: };
49:
1.1 paf 50: #endif
51:
52: // DB_Table
53:
1.3 paf 54: DB_Table::DB_Table(Pool& pool, const String& afile_name, DB_Connection& aconnection) : Pooled(pool),
1.1 paf 55: time_used(0), fservices_pool(0),
56: fconnection(aconnection),
57: dbenv(aconnection.dbenv),
1.3 paf 58: ffile_name(afile_name), file_name_cstr(afile_name.cstr(String::UL_FILE_SPEC)),
59: db(0) {
60:
61: // open
62: DB_INFO dbinfo;
63: memset(&dbinfo, 0, sizeof(dbinfo));
64: check("open/create", &ffile_name, db_open(
65: file_name_cstr,
66: PA_DB_ACCESS_METHOD,
67: DB_THREAD
68: | DB_CREATE /* used in single thread, no need for |DB_THREAD*/,
69: 0666,
70: &dbenv, &dbinfo, &db));
71: }
72: /// @todo this one of reasons of not having ^try for now
73: DB_Table::~DB_Table() {
74: // close
75: check("close", &ffile_name, db->close(db, 0/*flags*/)); db=0;
76: }
77:
78: void DB_Table::use() {
79: fconnection.use();
80:
81: time_used=time(0); // they started to use at this time
82: used++;
83: }
84: void DB_Table::unuse() {
85: used--;
86:
87: fconnection.unuse();
1.1 paf 88: }
89:
1.3 paf 90:
91:
1.1 paf 92: void DB_Table::check(const char *operation, const String *source, int error) {
93: switch(error) {
94: case 0:
95: // no error
96: break;
97:
98: case DB_KEYEXIST:
99: // DB_KEYEXIST is a "normal" return, so should not be
100: // thrown as an error
101: break;
102:
1.6 paf 103: #ifdef DB_RUNRECOVERY
1.1 paf 104: case DB_RUNRECOVERY:
105: throw Exception(0, 0,
106: source,
107: "action failed, RUN RECOVERY UTILITY. db %s error, real filename '%s'",
1.3 paf 108: operation, file_name_cstr);
1.6 paf 109: #endif
110:
1.1 paf 111: default:
112: throw Exception(0, 0,
113: source,
114: "action failed. db %s error: %s (%d), real filename '%s'",
1.3 paf 115: operation, strerror(error), error, file_name_cstr);
1.1 paf 116: }
117: }
118:
119: void DB_Table::key_string_to_dbt(const String& key_string, DBT& key_result) {
120: memset(&key_result, 0, sizeof(key_result));
121: key_result.data=key_string.cstr(String::UL_AS_IS);
122: key_result.size=key_string.size();
123: }
124:
1.3 paf 125: String& DB_Table::key_dbt_to_string(Pool& pool, const DBT& key_dbt) {
1.1 paf 126: String& result=*new(*fservices_pool) String(*fservices_pool);
127: if(key_dbt.size) {
1.3 paf 128: char *request_data=(char *)pool.malloc(key_dbt.size);
1.1 paf 129: memcpy(request_data, key_dbt.data, key_dbt.size);
1.3 paf 130: result.APPEND_TAINTED(request_data, key_dbt.size, file_name_cstr, 0/*line*/);
1.1 paf 131: }
132: return result;
133: }
134:
135: void DB_Table::data_string_to_dbt(const String& data_string, time_t time_to_die,
136: DBT& data_result) {
137: memset(&data_result, 0, sizeof(data_result));
138:
139: data_string.serialize(
140: sizeof(Data_string_serialized_prolog),
141: data_result.data, data_result.size);
142:
143: Data_string_serialized_prolog& prolog=
144: *static_cast<Data_string_serialized_prolog *>(data_result.data);
145:
146: prolog.version=DATA_STRING_SERIALIZED_VERSION;
147: prolog.time_to_die=time_to_die;
148: }
149:
1.3 paf 150: String *DB_Table::data_dbt_to_string(Pool& pool, const DBT& data_dbt) {
1.1 paf 151: Data_string_serialized_prolog& prolog=
152: *static_cast<Data_string_serialized_prolog *>(data_dbt.data);
153:
154: if(prolog.version!=DATA_STRING_SERIALIZED_VERSION)
155: throw Exception(0, 0,
1.3 paf 156: &ffile_name,
1.1 paf 157: "data string version 0x%04X not equal to 0x%04X, recreate file",
158: prolog.version, DATA_STRING_SERIALIZED_VERSION);
159:
160: if(prolog.time_to_die/*specified*/ && prolog.time_to_die <= time(0)/*expired*/)
161: return 0;
162:
1.3 paf 163: String& result=*new(pool) String(pool);
164: if(data_dbt.size) {
165: char *data=(char *)pool.malloc(data_dbt.size);
166: memcpy(data, data_dbt.data, data_dbt.size);
167: result.deserialize(
168: sizeof(Data_string_serialized_prolog),
169: data, data_dbt.size, file_name_cstr);
170: }
1.1 paf 171: return &result;
172: }
173:
1.3 paf 174: void DB_Table::put(DB_Transaction *t, const String& key, const String& data, time_t time_to_die) {
1.1 paf 175: DBT dbt_key; key_string_to_dbt(key, dbt_key);
176: DBT dbt_data; data_string_to_dbt(data, time_to_die, dbt_data);
1.3 paf 177: check("put", &key, db->put(db, t?t->id():0, &dbt_key, &dbt_data, 0/*flags*/));
1.1 paf 178: }
179:
1.3 paf 180: String *DB_Table::get(DB_Transaction *t, Pool& pool, const String& key) {
1.1 paf 181: DBT dbt_key; key_string_to_dbt(key, dbt_key);
1.3 paf 182: DBT_auto dbt_data;
183: int error=db->get(db, t?t->id():0, &dbt_key, &dbt_data,
184: DEADLOCK_POSSIBILITY_REDUCTION_FLAGS);
1.1 paf 185: if(error==DB_NOTFOUND)
186: return 0;
187: else {
188: check("get", &key, error);
1.3 paf 189: String *result=data_dbt_to_string(pool, dbt_data);
1.1 paf 190: if(!result) // save efforts by deleting expired keys
1.3 paf 191: check("del expired", &key, db->del(db, t?t->id():0, &dbt_key, 0/*flags*/));
1.1 paf 192: return result;
193: }
194: }
195:
1.3 paf 196: void DB_Table::remove(DB_Transaction *t, const String& key) {
1.1 paf 197: DBT dbt_key; key_string_to_dbt(key, dbt_key);
198:
1.3 paf 199: int error=db->del(db, t?t->id():0, &dbt_key, 0/*flags*/);
1.1 paf 200: if(error!=DB_NOTFOUND)
201: check("del", &key, error);
202: }
203:
1.3 paf 204: // DB_Transaction
205:
206: DB_Transaction::DB_Transaction(Pool& apool, DB_Table& atable, DB_Transaction *& aparent_ref) :
207: fpool(apool), ftable(atable), fparent_ref(aparent_ref),
208: parent(aparent_ref),
209: fid(0), marked_to_rollback(false) {
210:
211: check("txn_begin",
212: &ftable.file_name(), txn_begin(ftable.dbenv.tx_info, parent?parent->fid:0, &fid));
213:
214: aparent_ref=this;
215: }
216: DB_Transaction::~DB_Transaction() {
217: fparent_ref=parent;
218:
219: if(parent) // all in hands of our parent
220: return;
221:
222: if(marked_to_rollback)
223: check("txn_abort", &ftable.file_name(), txn_abort(fid));
224: else
225: check("txn_commit", &ftable.file_name(), txn_commit(fid));
226: }
227: void DB_Transaction::mark_to_rollback() {
228: if(parent)
229: parent->mark_to_rollback();
230: else
231: marked_to_rollback=true;
232: }
233:
1.1 paf 234: // DB_Cursor
235:
236: DB_Cursor::DB_Cursor(
1.3 paf 237: DB_Table& atable, DB_Transaction *transaction,
238: const String *asource) :
239: ftable(atable), fsource(asource),
240: cursor(0) {
241:
1.1 paf 242: check("cursor", fsource, ftable.db->cursor(ftable.db,
1.6 paf 243: transaction?transaction->id():0, &cursor
244: #if DB_VERSION_MINOR >= 7
245: , 0/*flags*/
246: #endif
247: ));
1.1 paf 248: }
249:
250: DB_Cursor::~DB_Cursor() {
1.3 paf 251: if(cursor)
252: check("c_close", fsource, cursor->c_close(cursor));
1.1 paf 253: }
254:
1.3 paf 255: bool DB_Cursor::get(Pool& pool, String *& key, String *& data, u_int32_t flags) {
1.1 paf 256: DBT dbt_key={0}; // must be zeroed
1.3 paf 257: DBT_auto dbt_data;
1.1 paf 258:
1.3 paf 259: int error=cursor->c_get(cursor, &dbt_key, &dbt_data,
260: DEADLOCK_POSSIBILITY_REDUCTION_FLAGS
261: | flags
262: );
1.1 paf 263: if(error==DB_NOTFOUND)
264: return false;
265:
266: check("c_get", fsource, error);
267:
1.3 paf 268: if(data=data_dbt_to_string(pool, dbt_data)) // not expired
269: key=&key_dbt_to_string(pool, dbt_key);
1.1 paf 270: else {
271: // save efforts by deleting expired keys
272: remove(0/*flags*/);
273: key=0;
274: }
1.4 paf 275: return true;
276: }
277:
278: bool DB_Cursor::move(u_int32_t flags) {
279: DBT dbt_key={0}; // must be zeroed
280: DBT dbt_data={0}; // must be zeroed
281: dbt_key.flags=dbt_data.flags=DB_DBT_PARTIAL; // just peep, not actually read [size=0]
282:
283: int error=cursor->c_get(cursor, &dbt_key, &dbt_data,
284: DEADLOCK_POSSIBILITY_REDUCTION_FLAGS
285: | flags
286: );
287: if(error==DB_NOTFOUND)
288: return false;
289:
290: check("c_get", fsource, error);
1.1 paf 291: return true;
292: }
293:
294: void DB_Cursor::remove(u_int32_t flags) {
295: check("c_del", fsource, cursor->c_del(cursor, flags));
296: }
297:
298: #endif
E-mail: