Annotation of parser3/src/types/pa_vhashfile.C, revision 1.41

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

E-mail: