Annotation of parser3/src/main/pa_db_connection.C, revision 1.35
1.1 parser 1: /** @file
1.11 paf 2: Parser: sql driver connection implementation.
1.1 parser 3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.28 paf 5: Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1 parser 6:
1.35 ! paf 7: $Id: pa_db_connection.C,v 1.34.6.1 2002/01/24 17:05:59 paf Exp $
1.20 paf 8:
9: developed with LIBDB 2.7.4
1.1 parser 10: */
11:
12: #include "pa_config_includes.h"
1.19 paf 13: #ifdef DB2
1.1 parser 14:
15: #include "pa_db_connection.h"
1.11 paf 16: #include "pa_db_table.h"
1.1 parser 17: #include "pa_exception.h"
1.11 paf 18: #include "pa_threads.h"
19: #include "pa_stack.h"
20: #include "pa_common.h"
1.28 paf 21: #include "pa_vtable.h"
1.1 parser 22:
1.14 paf 23: // defines
24:
1.11 paf 25: // consts
26:
1.15 paf 27: /// @test increase
28: const int DB_CHECKPOINT_MINUTES=1;
29:
1.32 paf 30: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
1.11 paf 31: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
32:
33: // callbacks
1.10 parser 34:
1.11 paf 35: static void db_paniccall(DB_ENV *dbenv, int error) {
36: throw Exception(0, 0,
37: 0,
38: "db_panic: %s (%d)",
39: strerror(error), error);
40: }
41:
42: static void db_errcall(const char *, char *buffer) {
43: throw Exception(0, 0,
44: 0,
1.23 paf 45: "db_err: %s",
1.11 paf 46: buffer);
47: }
1.5 parser 48:
1.13 paf 49: static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
1.33 paf 50: DB_Table *table=static_cast<DB_Table *>(value);
51: time_t older_dies=reinterpret_cast<time_t>(info);
1.5 parser 52:
1.33 paf 53: if(table->expired(older_dies)) {
54: table->~DB_Table(); value=0;
1.13 paf 55: }
1.11 paf 56: }
1.5 parser 57:
1.3 parser 58: // DB_Connection
59:
1.13 paf 60: DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
61: time_used(0),
1.28 paf 62: fdb_home(adb_home), used(0),
1.13 paf 63: table_cache(apool),
1.11 paf 64: prev_expiration_pass_time(0) {
65: //_asm int 3;
66: char DB_DATA_DIR__VALUE[MAX_STRING];
67: char DB_TMP_DIR__VALUE[MAX_STRING];
68:
69: const char *db_home_cstr=fdb_home.cstr(String::UL_FILE_SPEC);
70:
71: snprintf(DB_DATA_DIR__VALUE, MAX_STRING, "DB_DATA_DIR %s", db_home_cstr);
72: snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
73:
74: char *db_config[] = {
75: DB_DATA_DIR__VALUE,
76: DB_TMP_DIR__VALUE,
77: 0
78: };
79:
80: u_int32_t flags=
1.29 paf 81: (parser_multithreaded?DB_THREAD:0)
1.13 paf 82: | DB_CREATE
1.35 ! paf 83: | DB_INIT_MPOOL;
1.11 paf 84:
1.27 paf 85: // prepare dbenv structure
1.16 paf 86: memset(&dbenv, 0, sizeof(dbenv));
87:
1.14 paf 88: // error handlers
1.20 paf 89: #if DB_VERSION_MINOR >= 7
1.14 paf 90: dbenv.db_paniccall=db_paniccall;
1.20 paf 91: #endif
1.14 paf 92: dbenv.db_errcall=db_errcall;
1.26 paf 93:
94: // lockdetector flags
95: dbenv.lk_detect=DB_LOCK_RANDOM;
1.16 paf 96:
97: // init
98: check("db_appinit", &fdb_home, db_appinit(
99: db_home_cstr,
100: db_config,
101: &dbenv,
102: flags
103: ));
1.11 paf 104: }
105:
1.13 paf 106: DB_Connection::~DB_Connection() {
1.11 paf 107: // close tables
1.13 paf 108: table_cache.for_each(expire_table,
1.34 paf 109: reinterpret_cast<void *>(time(0)+1/* =future= expire[close] all*/));
1.11 paf 110:
111: // destroy connection data
1.13 paf 112: check("db_appexit", &fdb_home, db_appexit(&dbenv));
1.11 paf 113: }
114:
115:
1.1 parser 116: void DB_Connection::check(const char *operation, const String *source, int error) {
117: switch(error) {
118: case 0:
119: // no error
120: break;
121:
122: default:
1.21 paf 123: if(error<0)
124: throw Exception(0, 0,
125: source,
126: "db %s error: %d",
127: operation, error);
128: else
129: throw Exception(0, 0,
130: source,
131: "db %s error: %s (%d)",
132: operation, strerror(error), error);
1.1 parser 133: }
134: }
135:
1.15 paf 136: DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {
1.11 paf 137: // first trying to get cached table
138: DB_Table *result=get_table_from_cache(request_file_name);
1.13 paf 139: if(!result) { // no cached table
1.11 paf 140: // make global_file_name C-string on global pool
1.24 paf 141: const char *request_file_name_cstr=request_file_name.cstr();
1.11 paf 142: char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
143: strcpy(global_file_name_cstr, request_file_name_cstr);
144: // make global_file_name string on global pool
145: String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
146:
147: // allocate in global pool
148: // associate with services[request]
149: // NOTE: never freed up!
150: result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.13 paf 151: // cache it
152: put_table_to_cache(global_file_name, *result);
1.4 parser 153: }
1.1 parser 154:
1.13 paf 155: // return auto-it
156: return DB_Table_ptr(result);
1.1 parser 157: }
1.11 paf 158:
159: // table cache
160: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
161: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) {
162: SYNCHRONIZED;
1.17 paf 163:
164: maybe_expire_table_cache();
1.11 paf 165:
1.13 paf 166: return static_cast<DB_Table *>(table_cache.get(file_name));
1.3 parser 167: }
168:
1.13 paf 169: void DB_Connection::put_table_to_cache(const String& file_name, DB_Table& table) {
1.11 paf 170: SYNCHRONIZED;
1.3 parser 171:
1.13 paf 172: table_cache.put(file_name, &table);
1.3 parser 173: }
174:
1.11 paf 175: void DB_Connection::maybe_expire_table_cache() {
176: time_t now=time(0);
177:
178: if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
1.13 paf 179: table_cache.for_each(expire_table,
1.11 paf 180: reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3 parser 181:
1.11 paf 182: prev_expiration_pass_time=now;
1.9 parser 183: }
1.28 paf 184: }
185:
186: static void add_table_to_status_table(const Hash::Key& key, Hash::Val *& value, void *info) {
1.33 paf 187: DB_Table *db_table=static_cast<DB_Table *>(value);
188: Table& status_table=*static_cast<Table *>(info);
189: Pool& pool=status_table.pool();
190:
191: Array& row=*new(pool) Array(pool);
192:
193: // name
194: row+=&db_table->file_name();
195: // time
196: time_t time_stamp=db_table->get_time_used();
197: const char *unsafe_time_cstr=ctime(&time_stamp);
198: int time_buf_size=strlen(unsafe_time_cstr);
199: char *safe_time_buf=(char *)pool.malloc(time_buf_size);
200: memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size);
201: row+=new(pool) String(pool, safe_time_buf, time_buf_size);
202: // users
203: char *users_buf=(char *)pool.malloc(MAX_NUMBER);
204: row+=new(pool) String(pool,
205: users_buf, snprintf(users_buf, MAX_NUMBER, "%d", db_table->get_users_count()));
1.28 paf 206:
1.33 paf 207: status_table+=&row;
1.28 paf 208: }
209: Value& DB_Connection::get_status(Pool& pool, const String *source) {
210: Array& columns=*new(pool) Array(pool);
211: columns+=new(pool) String(pool, "name");
212: columns+=new(pool) String(pool, "time");
213: columns+=new(pool) String(pool, "users");
214: Table& table=*new(pool) Table(pool, 0, &columns, table_cache.size());
215:
216: table_cache.for_each(add_table_to_status_table, &table);
217:
218: return *new(pool) VTable(pool, &table);
1.1 parser 219: }
220:
221: #endif
E-mail: