Annotation of parser3/src/main/pa_db_connection.C, revision 1.18
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.18 ! paf 7: $Id: pa_db_connection.C,v 1.17 2001/10/27 09:35:12 paf Exp $
1.1 parser 8: */
9:
10: #include "pa_config_includes.h"
11: #ifdef HAVE_LIBDB
12:
13: #include "pa_db_connection.h"
1.11 paf 14: #include "pa_db_table.h"
1.1 parser 15: #include "pa_exception.h"
1.11 paf 16: #include "pa_threads.h"
17: #include "pa_stack.h"
18: #include "pa_common.h"
1.1 parser 19:
1.14 paf 20: // defines
21:
22: #define DB_ERROR_PREFIX "db_err: "
23: #define DB_EXCEPTION_NO_LOG_MESSAGE1 DB_ERROR_PREFIX"Ignoring log file"
24: #define DB_EXCEPTION_NO_LOG_MESSAGE2 DB_ERROR_PREFIX"log_get: unable to find checkpoint record"
25:
1.11 paf 26: // consts
27:
1.15 paf 28: /// @test increase
29: const int DB_CHECKPOINT_MINUTES=1;
30:
1.11 paf 31: const int EXPIRE_UNUSED_TABLE_SECONDS=60;
32: const int CHECK_EXPIRED_TABLES_SECONDS=EXPIRE_UNUSED_TABLE_SECONDS*2;
33:
34: // callbacks
1.10 parser 35:
1.11 paf 36: static void db_paniccall(DB_ENV *dbenv, int error) {
37: throw Exception(0, 0,
38: 0,
39: "db_panic: %s (%d)",
40: strerror(error), error);
41: }
42:
43: static void db_errcall(const char *, char *buffer) {
44: throw Exception(0, 0,
45: 0,
1.14 paf 46: DB_ERROR_PREFIX "%s",
1.11 paf 47: buffer);
48: }
1.5 parser 49:
1.13 paf 50: static void expire_table(const Hash::Key& key, Hash::Val *& value, void *info) {
51: DB_Connection& table=*static_cast<DB_Connection *>(value);
1.11 paf 52: time_t older_dies=reinterpret_cast<time_t>(info);
1.5 parser 53:
1.13 paf 54: if(table.expired(older_dies)) {
55: table.~DB_Connection(); value=0;
56: }
1.11 paf 57: }
1.5 parser 58:
1.3 parser 59: // DB_Connection
60:
1.13 paf 61: DB_Connection::DB_Connection(Pool& apool, const String& adb_home) : Pooled(apool),
62: time_used(0),
63: fdb_home(adb_home),
64: table_cache(apool),
1.11 paf 65: prev_expiration_pass_time(0) {
66: //_asm int 3;
67: char DB_DATA_DIR__VALUE[MAX_STRING];
68: char DB_LOG_DIR__VALUE[MAX_STRING];
69: char DB_TMP_DIR__VALUE[MAX_STRING];
70:
71: const char *db_home_cstr=fdb_home.cstr(String::UL_FILE_SPEC);
72:
73: snprintf(DB_DATA_DIR__VALUE, MAX_STRING, "DB_DATA_DIR %s", db_home_cstr);
74: snprintf(DB_LOG_DIR__VALUE, MAX_STRING, "DB_LOG_DIR %s", db_home_cstr);
75: snprintf(DB_TMP_DIR__VALUE, MAX_STRING, "DB_TMP_DIR %s", db_home_cstr);
76:
77: char *db_config[] = {
78: DB_DATA_DIR__VALUE,
79: DB_LOG_DIR__VALUE,
80: DB_TMP_DIR__VALUE,
81: 0
82: };
83:
84: u_int32_t flags=
1.13 paf 85: DB_THREAD
86: | DB_CREATE
1.11 paf 87: | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
88:
1.16 paf 89: // trying to open with SOFT RECOVER option set
90: memset(&dbenv, 0, sizeof(dbenv));
91:
1.14 paf 92: // error handlers
93: dbenv.db_paniccall=db_paniccall;
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:
122: throw Exception(0, 0,
1.3 parser 123: source,
1.11 paf 124: "db %s error: %s (%d)",
125: operation, strerror(error), error);
1.1 parser 126: }
127: }
128:
1.15 paf 129: DB_Table_ptr DB_Connection::get_table_ptr(const String& request_file_name, const String *source) {
130: // checkpoint
131: check("checkpoint", source,
132: txn_checkpoint(dbenv.tx_info, 0/*kbyte*/, DB_CHECKPOINT_MINUTES/*min*/));
1.11 paf 133:
134: // first trying to get cached table
135: DB_Table *result=get_table_from_cache(request_file_name);
1.13 paf 136: if(!result) { // no cached table
1.11 paf 137: // make global_file_name C-string on global pool
1.13 paf 138: const char *request_file_name_cstr=request_file_name.cstr(String::UL_AS_IS);
1.11 paf 139: char *global_file_name_cstr=(char *)malloc(strlen(request_file_name_cstr)+1);
140: strcpy(global_file_name_cstr, request_file_name_cstr);
141: // make global_file_name string on global pool
142: String& global_file_name=*new(this->pool()) String(this->pool(), global_file_name_cstr);
143:
144: // allocate in global pool
145: // associate with services[request]
146: // NOTE: never freed up!
147: result=new(this->pool()) DB_Table(this->pool(), global_file_name, *this);
1.13 paf 148: // cache it
149: put_table_to_cache(global_file_name, *result);
1.4 parser 150: }
1.1 parser 151:
1.13 paf 152: // return auto-it
153: return DB_Table_ptr(result);
1.1 parser 154: }
1.11 paf 155:
156: // table cache
157: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
158: DB_Table *DB_Connection::get_table_from_cache(const String& file_name) {
159: SYNCHRONIZED;
1.17 paf 160:
161: maybe_expire_table_cache();
1.11 paf 162:
1.13 paf 163: return static_cast<DB_Table *>(table_cache.get(file_name));
1.3 parser 164: }
165:
1.13 paf 166: void DB_Connection::put_table_to_cache(const String& file_name, DB_Table& table) {
1.11 paf 167: SYNCHRONIZED;
1.3 parser 168:
1.13 paf 169: table_cache.put(file_name, &table);
1.3 parser 170: }
171:
1.11 paf 172: void DB_Connection::maybe_expire_table_cache() {
173: time_t now=time(0);
174:
175: if(prev_expiration_pass_time<now-CHECK_EXPIRED_TABLES_SECONDS) {
1.13 paf 176: table_cache.for_each(expire_table,
1.11 paf 177: reinterpret_cast<void *>(now-EXPIRE_UNUSED_TABLE_SECONDS));
1.3 parser 178:
1.11 paf 179: prev_expiration_pass_time=now;
1.9 parser 180: }
1.1 parser 181: }
182:
183: #endif
E-mail: