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