Annotation of parser3/src/types/pa_vhashfile.C, revision 1.26
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.26 ! paf 8: static const char* IDENT="$Date: 2003/11/06 10:25:40 $";
1.1 parser 9:
10: #include "pa_vtable.h"
11: #include "pa_vstring.h"
12: #include "pa_vhashfile.h"
13: #include "pa_threads.h"
1.8 parser 14: #include "pa_globals.h"
1.1 parser 15:
16: // methods
17:
1.22 paf 18: void check(const char *step, apr_status_t status) {
19: if(status==APR_SUCCESS)
20: return;
21:
22: throw Exception("todo.todo",
23: 0,
24: "%s error: %d", step, status);
25: }
26:
27: void VHashfile::open(const String& afile_name) {
28: check("apr_sdbm_open", apr_sdbm_open(&db, afile_name.cstr(String::L_FILE_SPEC),
1.25 paf 29: APR_WRITE|APR_CREATE|APR_SHARELOCK,
1.22 paf 30: 0664, 0));
31: }
32:
33: VHashfile::~VHashfile() {
34: if(db)
35: check("apr_sdbm_close", apr_sdbm_close(db));
36: }
37:
38: void VHashfile::clear() {
39: }
40:
1.8 parser 41: void VHashfile::put_field(const String& aname, Value *avalue) {
42: time_t time_to_die=0;
43: const String *value_string;
44:
1.23 paf 45: if(HashStringValue *hash=avalue->get_hash()) {
46: if(Value *value_value=hash->get(value_name)) {
1.9 parser 47: if(value_value->get_junction())
1.23 paf 48: throw Exception(0,
49: 0,
50: VALUE_NAME" must not be code");
1.8 parser 51:
52: value_string=&value_value->as_string();
53:
1.23 paf 54: if(Value *expires_value=hash->get(expires_name))
1.8 parser 55: time_to_die=time(0)+(time_t)expires_value->as_double();
56: } else
1.23 paf 57: throw Exception(0,
1.8 parser 58: &aname,
1.23 paf 59: "put hash value must contain ."VALUE_NAME);
1.8 parser 60: } else
61: value_string=&avalue->as_string();
1.5 parser 62:
1.23 paf 63: // get_table_ptr(&aname)->put(aname, *value_string, time_to_die);
64:
65: apr_sdbm_datum_t key;
66: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 67: key.dsize=aname.length();
1.23 paf 68:
69: apr_sdbm_datum_t value;
70: value.dptr=const_cast<char*>(value_string->cstr());
1.24 paf 71: value.dsize=value_string->length();
1.23 paf 72:
73: // * APR_SDBM_INSERT return an error if the record exists
74: check("apr_sdbm_store", apr_sdbm_store(db, key, value, APR_SDBM_REPLACE));
1.1 parser 75: }
76:
1.11 paf 77: Value *VHashfile::get_field(const String& aname) {
1.23 paf 78: apr_sdbm_datum_t key;
79: key.dptr=const_cast<char*>(aname.cstr());
1.24 paf 80: key.dsize=aname.length();
1.23 paf 81:
82: apr_sdbm_datum_t value;
83:
84: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &value, key));
85:
86: if(value.dptr)
1.26 ! paf 87: return new VString(*new String(pa_strdup(value.dptr, value.dsize), true));
1.3 parser 88: else
89: return 0;
1.24 paf 90: }
91:
92: void VHashfile::remove(const String& aname) {
93: apr_sdbm_datum_t key;
94: key.dptr=const_cast<char*>(aname.cstr());
95: key.dsize=aname.length();
96:
97: check("apr_sdbm_delete", apr_sdbm_delete(db, key));
1.1 parser 98: }
99:
1.26 ! paf 100: void VHashfile::for_each(void callback(const String::Body, const String&, void*), void* info) const {
1.25 paf 101: check("apr_sdbm_lock", apr_sdbm_lock(db, APR_FLOCK_SHARED));
102: try {
103: apr_sdbm_datum_t apkey;
104: apr_status_t status;
105: if(apr_sdbm_firstkey(db, &apkey)==APR_SUCCESS)
106: do {
107: apr_sdbm_datum_t apvalue;
108: check("apr_sdbm_fetch", apr_sdbm_fetch(db, &apvalue, apkey));
109:
110: const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
111: const char *clvalue=pa_strdup(apvalue.dptr, apvalue.dsize);
1.26 ! paf 112: const String& svalue=*new String(clvalue, true);
! 113: callback(clkey, svalue, info);
1.25 paf 114: } while(apr_sdbm_nextkey(db, &apkey)==APR_SUCCESS);
115: } catch(...) {
116: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
117: rethrow;
1.4 parser 118: }
119:
1.25 paf 120: check("apr_sdbm_unlock", apr_sdbm_unlock(db));
1.26 ! paf 121: }
! 122:
! 123: static void get_hash__put(const String::Body key, const String& value, void* aresult) {
! 124: static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
! 125: }
! 126:
! 127: HashStringValue *VHashfile::get_hash() {
! 128: HashStringValue& result=*new HashStringValue();
! 129:
! 130: for_each(get_hash__put, &result);
! 131: return &result;
1.1 parser 132: }
E-mail: