Annotation of parser3/src/types/pa_vhashfile.C, revision 1.40
1.1 parser 1: /** @file
2: Parser: @b table class.
3:
1.20 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.19 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 6: */
7:
1.40 ! paf 8: static const char * const IDENT="$Date: 2004/03/29 07:43:19 $";
1.1 parser 9:
1.32 paf 10: #include "pa_globals.h"
11: #include "pa_threads.h"
1.1 parser 12: #include "pa_vtable.h"
13: #include "pa_vstring.h"
14: #include "pa_vhashfile.h"
1.32 paf 15: #include "pa_vdate.h"
16:
17: // consts
18:
19: const int HASHFILE_VALUE_SERIALIZED_VERSION=0x0001;
1.1 parser 20:
21: // methods
22:
1.22 paf 23: void check(const char *step, apr_status_t status) {
24: if(status==APR_SUCCESS)
25: return;
26:
1.40 ! paf 27: const char* str=strerror(status);
1.33 paf 28: throw Exception("file.access",
1.22 paf 29: 0,
1.33 paf 30: "%s error: %s (%d)",
1.40 ! paf 31: step, str?str:"<unknown>", status);
1.22 paf 32: }
33:
34: void VHashfile::open(const String& afile_name) {
1.34 paf 35: check("apr_sdbm_open(shared)", apr_sdbm_open(&m_db, file_name=afile_name.cstr(String::L_FILE_SPEC),
1.27 paf 36: APR_CREATE|APR_READ|APR_SHARELOCK,
1.22 paf 37: 0664, 0));
38: }
39:
1.34 paf 40: void VHashfile::close() {
41: check_db();
42:
43: check("apr_sdbm_close", apr_sdbm_close(m_db)); m_db=0;
44: }
45:
46: void VHashfile::check_db() const {
47: if(!m_db)
48: throw Exception(0,
49: 0,
50: "%s is closed", type());
51: }
52:
53: apr_sdbm_t *VHashfile::get_db_for_reading() const {
54: check_db();
55:
56: return m_db;
57: }
58:
59: apr_sdbm_t *VHashfile::get_db_for_writing() {
60: check_db();
61:
62: if(apr_sdbm_rdonly(m_db)) {
63: // reopen in write mode & exclusive lock
64: close();
65: check("apr_sdbm_open(exclusive)", apr_sdbm_open(&m_db, file_name,
1.27 paf 66: APR_WRITE,
67: 0664, 0));
68: }
1.34 paf 69:
70: return m_db;
1.27 paf 71: }
72:
1.22 paf 73: VHashfile::~VHashfile() {
1.34 paf 74: if(m_db)
75: close();
1.22 paf 76: }
77:
1.32 paf 78: struct Hashfile_value_serialized_prolog {
79: int version;
80: time_t time_to_die;
81: };
82:
1.35 paf 83: apr_sdbm_datum_t VHashfile::serialize_value(const String& string, time_t time_to_die) const {
1.32 paf 84: apr_sdbm_datum_t result;
85:
86: size_t length=string.length();
87: result.dsize=sizeof(Hashfile_value_serialized_prolog)+length;
88: result.dptr=new(PointerFreeGC) char[result.dsize];
89:
90: Hashfile_value_serialized_prolog& prolog=*reinterpret_cast<Hashfile_value_serialized_prolog*>(result.dptr);
91: char *output_cstr=result.dptr+sizeof(Hashfile_value_serialized_prolog);
92:
93: prolog.version=HASHFILE_VALUE_SERIALIZED_VERSION;
94: prolog.time_to_die=time_to_die;
95: memcpy(output_cstr, string.cstr(), length);
96:
97: return result;
98: }
99:
1.35 paf 100: const String* VHashfile::deserialize_value(apr_sdbm_datum_t key, const apr_sdbm_datum_t value) {
101: // key not found || it's surely not in our format
1.38 paf 102: if(!value.dptr || (size_t)value.dsize<sizeof(Hashfile_value_serialized_prolog))
1.35 paf 103: return 0;
104:
105: Hashfile_value_serialized_prolog& prolog=*reinterpret_cast<Hashfile_value_serialized_prolog*>(value.dptr);
106: if(prolog.version!=HASHFILE_VALUE_SERIALIZED_VERSION
107: || (prolog.time_to_die/*specified*/
108: && (prolog.time_to_die <= time(0)/*expired*/))) {
109: // old format || exipred value
110: remove(key);
1.32 paf 111: return 0;
1.35 paf 112: }
1.32 paf 113:
1.35 paf 114: char *input_cstr=value.dptr+sizeof(Hashfile_value_serialized_prolog);
115: size_t input_length=value.dsize-sizeof(Hashfile_value_serialized_prolog);
1.32 paf 116:
1.39 paf 117: return new String(pa_strdup(input_length?input_cstr:"", input_length), true);
1.32 paf 118: }
119:
1.27 paf 120: void VHashfile::put_field(const String& aname, Value *avalue) {
1.34 paf 121: apr_sdbm_t *db=get_db_for_writing();
1.22 paf 122:
1.8 parser 123: time_t time_to_die=0;
124: const String *value_string;
125:
1.23 paf 126: if(HashStringValue *hash=avalue->get_hash()) {
127: if(Value *value_value=hash->get(value_name)) {
1.9 parser 128: if(value_value->get_junction())
1.23 paf 129: throw Exception(0,
130: 0,
131: VALUE_NAME" must not be code");
1.8 parser 132:
133: value_string=&value_value->as_string();
134:
1.32 paf 135: if(Value *expires=hash->get(expires_name)) {
136: if(Value* vdate=expires->as(VDATE_TYPE, false))
137: time_to_die=static_cast<VDate*>(vdate)->get_time(); // $expires[DATE]
138: else if(double days_till_expire=expires->as_double())
139: time_to_die=time(NULL)+(time_t)(60*60*24*days_till_expire); // $expires(days)
140: }
1.8 parser 141: } else
1.23 paf 142: throw Exception(0,
1.8 parser 143: &aname,
1.23 paf 144: "put hash value must contain ."VALUE_NAME);
1.8 parser 145: } else
146: value_string=&avalue->as_string();
1.5 parser 147:
1.23 paf 148: apr_sdbm_datum_t key;
149: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 150: key.dsize=aname.length();
1.23 paf 151:
1.32 paf 152: apr_sdbm_datum_t value=serialize_value(*value_string, time_to_die);
1.23 paf 153:
154: check("apr_sdbm_store", apr_sdbm_store(db, key, value, APR_SDBM_REPLACE));
1.1 parser 155: }
156:
1.11 paf 157: Value *VHashfile::get_field(const String& aname) {
1.34 paf 158: apr_sdbm_t *db=get_db_for_reading();
159:
1.23 paf 160: apr_sdbm_datum_t key;
161: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 162: key.dsize=aname.length();
1.23 paf 163:
164: apr_sdbm_datum_t value;
165:
166: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &value, key));
167:
1.35 paf 168: const String *sresult=deserialize_value(key, value);
1.32 paf 169: return sresult? new VString(*sresult): 0;
1.24 paf 170: }
171:
1.35 paf 172: void VHashfile::remove(const apr_sdbm_datum_t key) {
1.34 paf 173: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 174:
1.35 paf 175: check("apr_sdbm_delete", apr_sdbm_delete(db, key));
176: }
177:
178: void VHashfile::remove(const String& aname) {
1.24 paf 179: apr_sdbm_datum_t key;
180: key.dptr=const_cast<char*>(aname.cstr());
181: key.dsize=aname.length();
182:
1.35 paf 183: remove(key);
1.1 parser 184: }
185:
1.27 paf 186: void VHashfile::for_each(void callback(apr_sdbm_datum_t, void*), void* info) const {
1.34 paf 187: apr_sdbm_t *db=get_db_for_reading();
188:
1.30 paf 189: Array<apr_sdbm_datum_t> keys;
190:
191: // collect keys
1.25 paf 192: check("apr_sdbm_lock", apr_sdbm_lock(db, APR_FLOCK_SHARED));
193: try {
1.30 paf 194: apr_sdbm_datum_t key;
195: if(apr_sdbm_firstkey(db, &key)==APR_SUCCESS)
1.25 paf 196: do {
1.30 paf 197: keys+=key;
198: } while(apr_sdbm_nextkey(db, &key)==APR_SUCCESS);
1.25 paf 199: } catch(...) {
200: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
201: rethrow;
1.4 parser 202: }
1.30 paf 203: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
1.4 parser 204:
1.30 paf 205: // iterate them
206: keys.for_each(callback, info);
1.26 paf 207: }
1.27 paf 208:
209: #ifndef DOXYGEN
210: struct For_each_string_callback_info {
1.35 paf 211: VHashfile* self;
1.27 paf 212: void* nested_info;
213: void (*nested_callback)(const String::Body, const String&, void*);
214: };
215: #endif
216: static void for_each_string_callback(apr_sdbm_datum_t apkey, void* ainfo) {
217: For_each_string_callback_info& info=*static_cast<For_each_string_callback_info *>(ainfo);
1.35 paf 218: apr_sdbm_t *db=info.self->get_db_for_reading();
1.27 paf 219:
220: apr_sdbm_datum_t apvalue;
1.35 paf 221: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &apvalue, apkey));
222:
223: if(const String* svalue=info.self->deserialize_value(apkey, apvalue)) {
224: const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
1.27 paf 225:
1.32 paf 226: info.nested_callback(clkey, *svalue, info.nested_info);
1.35 paf 227: }
1.27 paf 228: }
1.35 paf 229: void VHashfile::for_each(void callback(const String::Body, const String&, void*), void* ainfo) {
1.27 paf 230: For_each_string_callback_info info;
231:
1.35 paf 232: info.self=this;
1.27 paf 233: info.nested_info=ainfo;
234: info.nested_callback=callback;
235:
236: for_each(for_each_string_callback, &info);
237: }
238:
1.30 paf 239: static void clear_delete_key(apr_sdbm_datum_t key, void* adb) {
240: check("apr_sdbm_delete", apr_sdbm_delete(static_cast<apr_sdbm_t*>(adb), key));
1.27 paf 241: }
242: void VHashfile::clear() {
1.34 paf 243: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 244:
1.30 paf 245: for_each(clear_delete_key, db);
1.27 paf 246: }
247:
1.26 paf 248:
249: static void get_hash__put(const String::Body key, const String& value, void* aresult) {
250: static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
251: }
252: HashStringValue *VHashfile::get_hash() {
253: HashStringValue& result=*new HashStringValue();
254:
255: for_each(get_hash__put, &result);
256: return &result;
1.34 paf 257: }
258:
259: static void delete_file(const char* base_name, const char* ext) {
260: String sfile_name(base_name, false/*already removed tainting at ::open*/);
261: sfile_name<<ext;
262: file_delete(sfile_name);
263: }
264:
265: void VHashfile::delete_files() {
266: close();
267: delete_file(file_name, APR_SDBM_DIRFEXT);
268: delete_file(file_name, APR_SDBM_PAGFEXT);
1.1 parser 269: }
E-mail: