--- parser3/src/main/Attic/pa_db_table.C 2001/11/05 12:08:11 1.12 +++ parser3/src/main/Attic/pa_db_table.C 2002/02/08 08:30:15 1.20 @@ -1,10 +1,10 @@ /** @file Parser: Charset table implementation. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://paf.design.ru) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) - $Id: pa_db_table.C,v 1.12 2001/11/05 12:08:11 paf Exp $ + $Id: pa_db_table.C,v 1.20 2002/02/08 08:30:15 paf Exp $ */ #include "pa_config_includes.h" @@ -25,14 +25,15 @@ // consts -const int DATA_STRING_SERIALIZED_VERSION=0x0100; +const int DATA_STRING_SERIALIZED_VERSION=0x0002; // helper types #ifndef DOXYGEN struct Data_string_serialized_prolog { int version; - time_t time_to_die; + time_t time_of_creating; + time_t lifespan; }; struct DBT_auto : public DBT { @@ -53,7 +54,7 @@ struct DBT_auto : public DBT { // DB_Table DB_Table::DB_Table(Pool& pool, const String& afile_name, DB_Connection& aconnection) : Pooled(pool), - time_used(0), fservices_pool(0), + time_used(0), fconnection(aconnection), dbenv(aconnection.dbenv), ffile_name(afile_name), file_name_cstr(afile_name.cstr(String::UL_FILE_SPEC)), @@ -67,7 +68,7 @@ DB_Table::DB_Table(Pool& pool, const Str PA_DB_ACCESS_METHOD, (parser_multithreaded?DB_THREAD:0) | DB_CREATE, - 0666, + 0664, &dbenv, &dbinfo, &db)); } /// @todo this one of reasons of not having ^try for now @@ -130,7 +131,7 @@ void DB_Table::key_string_to_dbt(const S } String& DB_Table::key_dbt_to_string(Pool& pool, const DBT& key_dbt) { - String& result=*new(*fservices_pool) String(*fservices_pool); + String& result=*new(pool) String(pool); if(key_dbt.size) { char *request_data=(char *)pool.malloc(key_dbt.size); memcpy(request_data, key_dbt.data, key_dbt.size); @@ -139,7 +140,7 @@ String& DB_Table::key_dbt_to_string(Pool return result; } -void DB_Table::data_string_to_dbt(const String& data_string, time_t time_to_die, +void DB_Table::data_string_to_dbt(const String& data_string, time_t lifespan, DBT& data_result) { memset(&data_result, 0, sizeof(data_result)); @@ -151,10 +152,11 @@ void DB_Table::data_string_to_dbt(const *static_cast(data_result.data); prolog.version=DATA_STRING_SERIALIZED_VERSION; - prolog.time_to_die=time_to_die; + prolog.time_of_creating=time(0)/*now*/; + prolog.lifespan=lifespan; } -String *DB_Table::data_dbt_to_string(Pool& pool, const DBT& data_dbt) { +String *DB_Table::data_dbt_to_string(Pool& pool, const DBT& data_dbt, time_t lifespan) { Data_string_serialized_prolog& prolog= *static_cast(data_dbt.data); @@ -164,11 +166,16 @@ String *DB_Table::data_dbt_to_string(Poo "data string version 0x%04X not equal to 0x%04X, recreate file", prolog.version, DATA_STRING_SERIALIZED_VERSION); - if(prolog.time_to_die/*specified*/ && prolog.time_to_die <= time(0)/*expired*/) + if(lifespan/*specified*/ + && prolog.lifespan!=lifespan) // lifespan changed from storing to getting + return 0; // considering this 'expired' + + if(prolog.lifespan/*specified*/ + && ((prolog.time_of_creating+prolog.lifespan) <= time(0)/*expired*/)) return 0; String& result=*new(pool) String(pool); - if(data_dbt.size) { + if(data_dbt.size>sizeof(Data_string_serialized_prolog)) { char *data=(char *)pool.malloc(data_dbt.size); memcpy(data, data_dbt.data, data_dbt.size); result.deserialize( @@ -178,76 +185,46 @@ String *DB_Table::data_dbt_to_string(Poo return &result; } -void DB_Table::put(DB_Transaction *t, const String& key, const String& data, time_t time_to_die) { +void DB_Table::put(const String& key, const String& data, time_t time_to_die) { DBT dbt_key; key_string_to_dbt(key, dbt_key); DBT dbt_data; data_string_to_dbt(data, time_to_die, dbt_data); - check("put", &key, db->put(db, t?t->id():0, &dbt_key, &dbt_data, 0/*flags*/)); + check("put", &key, db->put(db, 0, &dbt_key, &dbt_data, 0/*flags*/)); } -String *DB_Table::get(DB_Transaction *t, Pool& pool, const String& key) { +String *DB_Table::get(Pool& pool, const String& key, time_t lifespan) { DBT dbt_key; key_string_to_dbt(key, dbt_key); DBT_auto dbt_data; - int error=db->get(db, t?t->id():0, &dbt_key, &dbt_data, + int error=db->get(db, 0, &dbt_key, &dbt_data, DEADLOCK_POSSIBILITY_REDUCTION_FLAGS); if(error==DB_NOTFOUND) return 0; else { check("get", &key, error); - String *result=data_dbt_to_string(pool, dbt_data); + String *result=data_dbt_to_string(pool, dbt_data, lifespan); if(!result) // save efforts by deleting expired keys - check("del expired", &key, db->del(db, t?t->id():0, &dbt_key, 0/*flags*/)); + check("del expired", &key, db->del(db, 0, &dbt_key, 0/*flags*/)); return result; } } -void DB_Table::remove(DB_Transaction *t, const String& key) { +void DB_Table::remove(const String& key) { DBT dbt_key; key_string_to_dbt(key, dbt_key); - int error=db->del(db, t?t->id():0, &dbt_key, 0/*flags*/); + int error=db->del(db, 0, &dbt_key, 0/*flags*/); if(error!=DB_NOTFOUND) check("del", &key, error); } -// DB_Transaction - -DB_Transaction::DB_Transaction(Pool& apool, DB_Table& atable, DB_Transaction *& aparent_ref) : - fpool(apool), ftable(atable), fparent_ref(aparent_ref), - parent(aparent_ref), - fid(0), marked_to_rollback(false) { - - check("txn_begin", - &ftable.file_name(), txn_begin(ftable.dbenv.tx_info, parent?parent->fid:0, &fid)); - - aparent_ref=this; -} -DB_Transaction::~DB_Transaction() { - fparent_ref=parent; - - if(parent) // all in hands of our parent - return; - - if(marked_to_rollback) - check("txn_abort", &ftable.file_name(), txn_abort(fid)); - else - check("txn_commit", &ftable.file_name(), txn_commit(fid)); -} -void DB_Transaction::mark_to_rollback() { - if(parent) - parent->mark_to_rollback(); - else - marked_to_rollback=true; -} - // DB_Cursor DB_Cursor::DB_Cursor( - DB_Table& atable, DB_Transaction *transaction, + DB_Table& atable, const String *asource) : ftable(atable), fsource(asource), cursor(0) { check("cursor", fsource, ftable.db->cursor(ftable.db, - transaction?transaction->id():0, &cursor + 0, &cursor #if DB_VERSION_MINOR >= 7 , 0/*flags*/ #endif @@ -272,7 +249,7 @@ bool DB_Cursor::get(Pool& pool, String * check("c_get", fsource, error); - if(data=data_dbt_to_string(pool, dbt_data)) // not expired + if(data=data_dbt_to_string(pool, dbt_data, 0)) // not expired key=&key_dbt_to_string(pool, dbt_key); else { // save efforts by deleting expired keys