|
|
| version 1.11, 2001/11/05 11:46:27 | version 1.12.6.2, 2001/11/23 11:53:03 |
|---|---|
| Line 25 | Line 25 |
| // consts | // consts |
| const int DATA_STRING_SERIALIZED_VERSION=0x0100; | const int DATA_STRING_SERIALIZED_VERSION=0x0002; |
| // helper types | // helper types |
| #ifndef DOXYGEN | #ifndef DOXYGEN |
| struct Data_string_serialized_prolog { | struct Data_string_serialized_prolog { |
| int version; | int version; |
| time_t time_to_die; | time_t time_of_creating; |
| time_t lifespan; | |
| }; | }; |
| struct DBT_auto : public DBT { | struct DBT_auto : public DBT { |
| Line 65 DB_Table::DB_Table(Pool& pool, const Str | Line 66 DB_Table::DB_Table(Pool& pool, const Str |
| check("open/create", &ffile_name, db_open( | check("open/create", &ffile_name, db_open( |
| file_name_cstr, | file_name_cstr, |
| PA_DB_ACCESS_METHOD, | PA_DB_ACCESS_METHOD, |
| parser_multithreaded?DB_THREAD:0 | (parser_multithreaded?DB_THREAD:0) |
| | DB_CREATE, | | DB_CREATE, |
| 0666, | 0664, |
| &dbenv, &dbinfo, &db)); | &dbenv, &dbinfo, &db)); |
| } | } |
| /// @todo this one of reasons of not having ^try for now | /// @todo this one of reasons of not having ^try for now |
| Line 139 String& DB_Table::key_dbt_to_string(Pool | Line 140 String& DB_Table::key_dbt_to_string(Pool |
| return result; | 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) { | DBT& data_result) { |
| memset(&data_result, 0, sizeof(data_result)); | memset(&data_result, 0, sizeof(data_result)); |
| Line 151 void DB_Table::data_string_to_dbt(const | Line 152 void DB_Table::data_string_to_dbt(const |
| *static_cast<Data_string_serialized_prolog *>(data_result.data); | *static_cast<Data_string_serialized_prolog *>(data_result.data); |
| prolog.version=DATA_STRING_SERIALIZED_VERSION; | 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= | Data_string_serialized_prolog& prolog= |
| *static_cast<Data_string_serialized_prolog *>(data_dbt.data); | *static_cast<Data_string_serialized_prolog *>(data_dbt.data); |
| Line 164 String *DB_Table::data_dbt_to_string(Poo | Line 166 String *DB_Table::data_dbt_to_string(Poo |
| "data string version 0x%04X not equal to 0x%04X, recreate file", | "data string version 0x%04X not equal to 0x%04X, recreate file", |
| prolog.version, DATA_STRING_SERIALIZED_VERSION); | 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; | return 0; |
| String& result=*new(pool) String(pool); | String& result=*new(pool) String(pool); |
| Line 184 void DB_Table::put(DB_Transaction *t, co | Line 191 void DB_Table::put(DB_Transaction *t, co |
| check("put", &key, db->put(db, t?t->id():0, &dbt_key, &dbt_data, 0/*flags*/)); | check("put", &key, db->put(db, t?t->id():0, &dbt_key, &dbt_data, 0/*flags*/)); |
| } | } |
| String *DB_Table::get(DB_Transaction *t, Pool& pool, const String& key) { | String *DB_Table::get(DB_Transaction *t, Pool& pool, const String& key, time_t lifespan) { |
| DBT dbt_key; key_string_to_dbt(key, dbt_key); | DBT dbt_key; key_string_to_dbt(key, dbt_key); |
| DBT_auto dbt_data; | DBT_auto dbt_data; |
| int error=db->get(db, t?t->id():0, &dbt_key, &dbt_data, | int error=db->get(db, t?t->id():0, &dbt_key, &dbt_data, |
| Line 193 String *DB_Table::get(DB_Transaction *t, | Line 200 String *DB_Table::get(DB_Transaction *t, |
| return 0; | return 0; |
| else { | else { |
| check("get", &key, error); | 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 | 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, t?t->id():0, &dbt_key, 0/*flags*/)); |
| return result; | return result; |
| Line 272 bool DB_Cursor::get(Pool& pool, String * | Line 279 bool DB_Cursor::get(Pool& pool, String * |
| check("c_get", fsource, error); | 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); | key=&key_dbt_to_string(pool, dbt_key); |
| else { | else { |
| // save efforts by deleting expired keys | // save efforts by deleting expired keys |