Annotation of parser3/src/types/pa_vhashfile.C, revision 1.43
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.43 ! paf 8: static const char * const IDENT="$Date: 2004/06/25 12:04:00 $";
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:
1.41 paf 19: const uint 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 {
1.41 paf 79: uint version;
1.32 paf 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:
1.41 paf 105: // [WARNING: not cast, addresses must be %4=0 on sparc]
1.42 paf 106: Hashfile_value_serialized_prolog prolog;
107: memcpy(&prolog, value.dptr, sizeof(prolog));
1.41 paf 108:
1.42 paf 109: if(prolog.version!=HASHFILE_VALUE_SERIALIZED_VERSION
110: || (prolog.time_to_die/*specified*/
111: && (prolog.time_to_die <= time(0)/*expired*/))) {
1.35 paf 112: // old format || exipred value
113: remove(key);
1.32 paf 114: return 0;
1.35 paf 115: }
1.32 paf 116:
1.35 paf 117: char *input_cstr=value.dptr+sizeof(Hashfile_value_serialized_prolog);
118: size_t input_length=value.dsize-sizeof(Hashfile_value_serialized_prolog);
1.32 paf 119:
1.39 paf 120: return new String(pa_strdup(input_length?input_cstr:"", input_length), true);
1.32 paf 121: }
122:
1.27 paf 123: void VHashfile::put_field(const String& aname, Value *avalue) {
1.34 paf 124: apr_sdbm_t *db=get_db_for_writing();
1.22 paf 125:
1.8 parser 126: time_t time_to_die=0;
127: const String *value_string;
128:
1.23 paf 129: if(HashStringValue *hash=avalue->get_hash()) {
130: if(Value *value_value=hash->get(value_name)) {
1.9 parser 131: if(value_value->get_junction())
1.23 paf 132: throw Exception(0,
133: 0,
134: VALUE_NAME" must not be code");
1.8 parser 135:
136: value_string=&value_value->as_string();
137:
1.32 paf 138: if(Value *expires=hash->get(expires_name)) {
139: if(Value* vdate=expires->as(VDATE_TYPE, false))
140: time_to_die=static_cast<VDate*>(vdate)->get_time(); // $expires[DATE]
141: else if(double days_till_expire=expires->as_double())
142: time_to_die=time(NULL)+(time_t)(60*60*24*days_till_expire); // $expires(days)
143: }
1.8 parser 144: } else
1.23 paf 145: throw Exception(0,
1.8 parser 146: &aname,
1.23 paf 147: "put hash value must contain ."VALUE_NAME);
1.8 parser 148: } else
149: value_string=&avalue->as_string();
1.43 ! paf 150:
! 151: if(aname.is_empty())
! 152: throw Exception("parser.runtime",
! 153: 0,
! 154: "hashfile key must not be empty");
1.5 parser 155:
1.23 paf 156: apr_sdbm_datum_t key;
157: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 158: key.dsize=aname.length();
1.23 paf 159:
1.32 paf 160: apr_sdbm_datum_t value=serialize_value(*value_string, time_to_die);
1.23 paf 161:
162: check("apr_sdbm_store", apr_sdbm_store(db, key, value, APR_SDBM_REPLACE));
1.1 parser 163: }
164:
1.11 paf 165: Value *VHashfile::get_field(const String& aname) {
1.34 paf 166: apr_sdbm_t *db=get_db_for_reading();
167:
1.23 paf 168: apr_sdbm_datum_t key;
169: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 170: key.dsize=aname.length();
1.23 paf 171:
172: apr_sdbm_datum_t value;
173:
174: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &value, key));
175:
1.35 paf 176: const String *sresult=deserialize_value(key, value);
1.32 paf 177: return sresult? new VString(*sresult): 0;
1.24 paf 178: }
179:
1.35 paf 180: void VHashfile::remove(const apr_sdbm_datum_t key) {
1.34 paf 181: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 182:
1.35 paf 183: check("apr_sdbm_delete", apr_sdbm_delete(db, key));
184: }
185:
186: void VHashfile::remove(const String& aname) {
1.24 paf 187: apr_sdbm_datum_t key;
188: key.dptr=const_cast<char*>(aname.cstr());
189: key.dsize=aname.length();
190:
1.35 paf 191: remove(key);
1.1 parser 192: }
193:
1.27 paf 194: void VHashfile::for_each(void callback(apr_sdbm_datum_t, void*), void* info) const {
1.34 paf 195: apr_sdbm_t *db=get_db_for_reading();
196:
1.30 paf 197: Array<apr_sdbm_datum_t> keys;
198:
199: // collect keys
1.25 paf 200: check("apr_sdbm_lock", apr_sdbm_lock(db, APR_FLOCK_SHARED));
201: try {
1.30 paf 202: apr_sdbm_datum_t key;
203: if(apr_sdbm_firstkey(db, &key)==APR_SUCCESS)
1.25 paf 204: do {
1.30 paf 205: keys+=key;
206: } while(apr_sdbm_nextkey(db, &key)==APR_SUCCESS);
1.25 paf 207: } catch(...) {
208: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
209: rethrow;
1.4 parser 210: }
1.30 paf 211: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
1.4 parser 212:
1.30 paf 213: // iterate them
214: keys.for_each(callback, info);
1.26 paf 215: }
1.27 paf 216:
217: #ifndef DOXYGEN
218: struct For_each_string_callback_info {
1.35 paf 219: VHashfile* self;
1.27 paf 220: void* nested_info;
221: void (*nested_callback)(const String::Body, const String&, void*);
222: };
223: #endif
224: static void for_each_string_callback(apr_sdbm_datum_t apkey, void* ainfo) {
225: For_each_string_callback_info& info=*static_cast<For_each_string_callback_info *>(ainfo);
1.35 paf 226: apr_sdbm_t *db=info.self->get_db_for_reading();
1.27 paf 227:
228: apr_sdbm_datum_t apvalue;
1.35 paf 229: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &apvalue, apkey));
230:
231: if(const String* svalue=info.self->deserialize_value(apkey, apvalue)) {
232: const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
1.27 paf 233:
1.32 paf 234: info.nested_callback(clkey, *svalue, info.nested_info);
1.35 paf 235: }
1.27 paf 236: }
1.35 paf 237: void VHashfile::for_each(void callback(const String::Body, const String&, void*), void* ainfo) {
1.27 paf 238: For_each_string_callback_info info;
239:
1.35 paf 240: info.self=this;
1.27 paf 241: info.nested_info=ainfo;
242: info.nested_callback=callback;
243:
244: for_each(for_each_string_callback, &info);
245: }
246:
1.30 paf 247: static void clear_delete_key(apr_sdbm_datum_t key, void* adb) {
248: check("apr_sdbm_delete", apr_sdbm_delete(static_cast<apr_sdbm_t*>(adb), key));
1.27 paf 249: }
250: void VHashfile::clear() {
1.34 paf 251: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 252:
1.30 paf 253: for_each(clear_delete_key, db);
1.27 paf 254: }
255:
1.26 paf 256:
257: static void get_hash__put(const String::Body key, const String& value, void* aresult) {
258: static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
259: }
260: HashStringValue *VHashfile::get_hash() {
261: HashStringValue& result=*new HashStringValue();
262:
263: for_each(get_hash__put, &result);
264: return &result;
1.34 paf 265: }
266:
267: static void delete_file(const char* base_name, const char* ext) {
268: String sfile_name(base_name, false/*already removed tainting at ::open*/);
269: sfile_name<<ext;
270: file_delete(sfile_name);
271: }
272:
273: void VHashfile::delete_files() {
274: close();
275: delete_file(file_name, APR_SDBM_DIRFEXT);
276: delete_file(file_name, APR_SDBM_PAGFEXT);
1.1 parser 277: }
E-mail: