Annotation of parser3/src/main/pa_db_connection.C, revision 1.25
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)
5: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
6:
1.25 ! paf 7: $Id: pa_db_connection.C,v 1.24 2001/10/30 15:08:19 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.1 parser 21:
1.14 paf 22: // defines
23:
1.11 paf 24: // consts
25:
1.15 paf 26: /// @test increase
27: const int DB_CHECKPOINT_MINUTES=1;
28:
1.11 paf 29: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
30: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
31:
32: // callbacks
1.10 parser 33:
1.11 paf 34: static void db_paniccall(DB_ENV *dbenv, int error) {
35: throw Exception(0, 0,
36: 0,
37: "db_panic: %s (%d)",
38: strerror(error), error);
39: }
40:
41: static void db_errcall(const char *, char *buffer) {
42: throw Exception(0, 0,
43: 0,
1.23 paf 44: "db_err: %s",
1.11 paf 45: buffer);
46: }
1.5 parser 47:
1.13 paf 48: static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
1.22 paf 49: DB_Table& table=*static_cast<DB_Table *>(value);
1.11 paf 50: time_t older_dies=reinterpret_cast<time_t>(info);
1.5 parser 51:
1.13 paf 52: if(table.expired(older_dies)) {
1.22 paf 53: table.~DB_Table(); value=0;
1.13 paf 54: }
1.11 paf 55: }
1.5 parser 56:
1.3 parser 57: // DB_Connection
58:
1.13 paf 59: DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
60: time_used(0),
61: fdb_home(adb_home),
62: table_cache(apool),
1.11 paf 63: prev_expiration_pass_time(0) {
64: //_asm int 3;
65: char DB_DATA_DIR__VALUE[MAX_STRING];
66: char DB_LOG_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_LOG_DIR__VALUE, MAX_STRING, "DB_LOG_DIR %s", db_home_cstr);
73: snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
74:
75: char *db_config[] = {
76: DB_DATA_DIR__VALUE,
77: DB_LOG_DIR__VALUE,
78: DB_TMP_DIR__VALUE,
79: 0
80: };
81:
82: u_int32_t flags=
1.25 ! paf 83: parser_multithreaded?DB_THREAD:0
1.13 paf 84: | DB_CREATE
1.11 paf 85: | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
86:
1.16 paf 87: // trying to open with SOFT RECOVER option set
88: memset(&dbenv, 0, sizeof(dbenv));
89:
1.14 paf 90: // error handlers
1.20 paf 91: #if DB_VERSION_MINOR >= 7
1.14 paf 92: dbenv.db_paniccall=db_paniccall;
1.20 paf 93: #endif
1.14 paf 94: dbenv.db_errcall=db_errcall;
1.16 paf 95:
96: // init
97: check("db_appinit", &fdb_home, db_appinit(
98: db_home_cstr,
99: db_config,
100: &dbenv,
101: flags
102: ));
1.11 paf 103: }
104:
1.13 paf 105: DB_Connection::~DB_Connection() {
1.11 paf 106: // close tables
1.13 paf 107: table_cache.for_each(expire_table,
1.11 paf 108: reinterpret_cast<void *>(0/* =in the past = expire[close] all*/));
109:
110: // destroy connection data
1.13 paf 111: check("db_appexit", &fdb_home, db_appexit(&dbenv));
1.11 paf 112: }
113:
114:
1.1 parser 115: void DB_Connection::check(const char *operation, const String *source, int error) {
116: switch(error) {
117: case 0:
118: // no error
119: break;
120:
121: default:
1.21 paf 122: if(error<0)
123: throw Exception(0, 0,
124: source,
125: "db %s error: %d",
126: operation, error);
127: else
128: throw Exception(0, 0,
129: source,
130: "db %s error: %s (%d)",
131: operation, strerror(error), error);
1.1 parser 132: }
133: }
134:
1.15 paf 135: DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {
136: // checkpoint
1.21 paf 137: {
138: int error=txn_checkpoint(dbenv.tx_info, 0/*kbyte*/, DB_CHECKPOINT_MINUTES/*min*/);
139: /*
140: DB_INCOMPLETE if there were pages that needed to be written
141: but that memp_sync was unable to write immediately.
142: In this case, the txn_checkpoint call should be retried.
143:
144: we'll just ignore that
145: */
146: if(error!=DB_INCOMPLETE)
147: check("checkpoint", source, error);
148: }
1.11 paf 149:
150: // first trying to get cached table
151: DB_Table *result=get_table_from_cache(request_file_name);
1.13 paf 152: if(!result) { // no cached table
1.11 paf 153: // make global_file_name C-string on global pool
1.24 paf 154: const char *request_file_name_cstr=request_file_name.cstr();
1.11 paf 155: char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
156: strcpy(global_file_name_cstr, request_file_name_cstr);
157: // make global_file_name string on global pool
158: String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
159:
160: // allocate in global pool
161: // associate with services[request]
162: // NOTE: never freed up!
163: result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.13 paf 164: // cache it
165: put_table_to_cache(global_file_name, *result);
1.4 parser 166: }
1.1 parser 167:
1.13 paf 168: // return auto-it
169: return DB_Table_ptr(result);
1.1 parser 170: }
1.11 paf 171:
172: // table cache
173: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
174: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) {
175: SYNCHRONIZED;
1.17 paf 176:
177: maybe_expire_table_cache();
1.11 paf 178:
1.13 paf 179: return static_cast<DB_Table *>(table_cache.get(file_name));
1.3 parser 180: }
181:
1.13 paf 182: void DB_Connection::put_table_to_cache(const String& file_name, DB_Table& table) {
1.11 paf 183: SYNCHRONIZED;
1.3 parser 184:
1.13 paf 185: table_cache.put(file_name, &table);
1.3 parser 186: }
187:
1.11 paf 188: void DB_Connection::maybe_expire_table_cache() {
189: time_t now=time(0);
190:
191: if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
1.13 paf 192: table_cache.for_each(expire_table,
1.11 paf 193: reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3 parser 194:
1.11 paf 195: prev_expiration_pass_time=now;
1.9 parser 196: }
1.1 parser 197: }
198:
199: #endif
E-mail: