Annotation of parser3/src/main/pa_db_table.C, revision 1.6

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

E-mail: