Annotation of parser3/src/types/pa_vhashfile.C, revision 1.42
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.42 ! paf 8: static const char * const IDENT="$Date: 2004/06/25 11:18:31 $";
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.5 parser 150:
1.23 paf 151: apr_sdbm_datum_t key;
152: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 153: key.dsize=aname.length();
1.23 paf 154:
1.32 paf 155: apr_sdbm_datum_t value=serialize_value(*value_string, time_to_die);
1.23 paf 156:
157: check("apr_sdbm_store", apr_sdbm_store(db, key, value, APR_SDBM_REPLACE));
1.1 parser 158: }
159:
1.11 paf 160: Value *VHashfile::get_field(const String& aname) {
1.34 paf 161: apr_sdbm_t *db=get_db_for_reading();
162:
1.23 paf 163: apr_sdbm_datum_t key;
164: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 165: key.dsize=aname.length();
1.23 paf 166:
167: apr_sdbm_datum_t value;
168:
169: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &value, key));
170:
1.35 paf 171: const String *sresult=deserialize_value(key, value);
1.32 paf 172: return sresult? new VString(*sresult): 0;
1.24 paf 173: }
174:
1.35 paf 175: void VHashfile::remove(const apr_sdbm_datum_t key) {
1.34 paf 176: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 177:
1.35 paf 178: check("apr_sdbm_delete", apr_sdbm_delete(db, key));
179: }
180:
181: void VHashfile::remove(const String& aname) {
1.24 paf 182: apr_sdbm_datum_t key;
183: key.dptr=const_cast<char*>(aname.cstr());
184: key.dsize=aname.length();
185:
1.35 paf 186: remove(key);
1.1 parser 187: }
188:
1.27 paf 189: void VHashfile::for_each(void callback(apr_sdbm_datum_t, void*), void* info) const {
1.34 paf 190: apr_sdbm_t *db=get_db_for_reading();
191:
1.30 paf 192: Array<apr_sdbm_datum_t> keys;
193:
194: // collect keys
1.25 paf 195: check("apr_sdbm_lock", apr_sdbm_lock(db, APR_FLOCK_SHARED));
196: try {
1.30 paf 197: apr_sdbm_datum_t key;
198: if(apr_sdbm_firstkey(db, &key)==APR_SUCCESS)
1.25 paf 199: do {
1.30 paf 200: keys+=key;
201: } while(apr_sdbm_nextkey(db, &key)==APR_SUCCESS);
1.25 paf 202: } catch(...) {
203: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
204: rethrow;
1.4 parser 205: }
1.30 paf 206: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
1.4 parser 207:
1.30 paf 208: // iterate them
209: keys.for_each(callback, info);
1.26 paf 210: }
1.27 paf 211:
212: #ifndef DOXYGEN
213: struct For_each_string_callback_info {
1.35 paf 214: VHashfile* self;
1.27 paf 215: void* nested_info;
216: void (*nested_callback)(const String::Body, const String&, void*);
217: };
218: #endif
219: static void for_each_string_callback(apr_sdbm_datum_t apkey, void* ainfo) {
220: For_each_string_callback_info& info=*static_cast<For_each_string_callback_info *>(ainfo);
1.35 paf 221: apr_sdbm_t *db=info.self->get_db_for_reading();
1.27 paf 222:
223: apr_sdbm_datum_t apvalue;
1.35 paf 224: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &apvalue, apkey));
225:
226: if(const String* svalue=info.self->deserialize_value(apkey, apvalue)) {
227: const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
1.27 paf 228:
1.32 paf 229: info.nested_callback(clkey, *svalue, info.nested_info);
1.35 paf 230: }
1.27 paf 231: }
1.35 paf 232: void VHashfile::for_each(void callback(const String::Body, const String&, void*), void* ainfo) {
1.27 paf 233: For_each_string_callback_info info;
234:
1.35 paf 235: info.self=this;
1.27 paf 236: info.nested_info=ainfo;
237: info.nested_callback=callback;
238:
239: for_each(for_each_string_callback, &info);
240: }
241:
1.30 paf 242: static void clear_delete_key(apr_sdbm_datum_t key, void* adb) {
243: check("apr_sdbm_delete", apr_sdbm_delete(static_cast<apr_sdbm_t*>(adb), key));
1.27 paf 244: }
245: void VHashfile::clear() {
1.34 paf 246: apr_sdbm_t *db=get_db_for_writing();
1.27 paf 247:
1.30 paf 248: for_each(clear_delete_key, db);
1.27 paf 249: }
250:
1.26 paf 251:
252: static void get_hash__put(const String::Body key, const String& value, void* aresult) {
253: static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
254: }
255: HashStringValue *VHashfile::get_hash() {
256: HashStringValue& result=*new HashStringValue();
257:
258: for_each(get_hash__put, &result);
259: return &result;
1.34 paf 260: }
261:
262: static void delete_file(const char* base_name, const char* ext) {
263: String sfile_name(base_name, false/*already removed tainting at ::open*/);
264: sfile_name<<ext;
265: file_delete(sfile_name);
266: }
267:
268: void VHashfile::delete_files() {
269: close();
270: delete_file(file_name, APR_SDBM_DIRFEXT);
271: delete_file(file_name, APR_SDBM_PAGFEXT);
1.1 parser 272: }
E-mail: