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

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.55    ! misha       8: static const char * const IDENT="$Date: 2007/04/26 10:33:21 $";
1.1       parser      9: 
1.32      paf        10: #include "pa_globals.h"
1.48      paf        11: #include "pa_common.h"
1.32      paf        12: #include "pa_threads.h"
1.1       parser     13: #include "pa_vtable.h"
                     14: #include "pa_vstring.h"
                     15: #include "pa_vhashfile.h"
1.32      paf        16: #include "pa_vdate.h"
                     17: 
                     18: // consts
                     19: 
1.41      paf        20: const uint HASHFILE_VALUE_SERIALIZED_VERSION=0x0001;
1.1       parser     21: 
                     22: // methods
                     23: 
1.22      paf        24: void check(const char *step, apr_status_t status) {
                     25:        if(status==APR_SUCCESS)
                     26:                return;
                     27: 
1.40      paf        28:        const char* str=strerror(status);
1.33      paf        29:        throw Exception("file.access",
1.22      paf        30:                0,
1.33      paf        31:                "%s error: %s (%d)", 
1.40      paf        32:                        step, str?str:"<unknown>", status);
1.22      paf        33: }
                     34: 
1.54      misha      35: void check_dir(const char* file_name){
1.51      misha      36:        String& sfile_name = *new String(file_name);
                     37:        if(!entry_exists(sfile_name))
                     38:                create_dir_for_file(sfile_name);
1.54      misha      39: }
1.51      misha      40: 
                     41: 
1.22      paf        42: void VHashfile::open(const String& afile_name) {
1.48      paf        43:        file_name=afile_name.cstr(String::L_FILE_SPEC);
1.22      paf        44: }
                     45: 
1.34      paf        46: void VHashfile::close() {
1.51      misha      47:        if(!is_open())
                     48:                return;
1.34      paf        49: 
1.51      misha      50:        check("apr_sdbm_close", apr_sdbm_close(m_db));
                     51:        m_db=0;
1.34      paf        52: }
                     53: 
1.54      misha      54: bool VHashfile::is_open() {
                     55:        return m_db != 0;
                     56: }
                     57: 
                     58: apr_sdbm_t *VHashfile::get_db_for_reading() {
                     59:        if(is_open()){
                     60:                return m_db;
                     61:        }
1.51      misha      62: 
1.54      misha      63:        if(file_name){
                     64:                check_dir(file_name);
                     65:                check("apr_sdbm_open(shared)", apr_sdbm_open(&m_db, file_name, 
                     66:                                         APR_CREATE|APR_READ|APR_SHARELOCK, 
                     67:                                         0664, 0));
                     68:        }
1.51      misha      69: 
                     70:        if(!is_open())
1.34      paf        71:                throw Exception(0,
                     72:                        0,
1.54      misha      73:                        "can't open %s for reading", type());
1.34      paf        74: 
                     75:        return m_db;
                     76: }
                     77: 
                     78: apr_sdbm_t *VHashfile::get_db_for_writing() {
1.54      misha      79:        if(is_open()){
                     80:        if(apr_sdbm_rdonly(m_db)) {
                     81:                        close(); // close if was opened for reading
                     82:                } else {
                     83:                        return m_db;
                     84:                }
                     85:        }
1.34      paf        86: 
1.54      misha      87:        if(file_name) {
                     88:                check_dir(file_name);
1.34      paf        89:                // reopen in write mode & exclusive lock
                     90:                check("apr_sdbm_open(exclusive)", apr_sdbm_open(&m_db, file_name, 
1.54      misha      91:                                                                                        APR_CREATE|APR_WRITE, 
1.27      paf        92:                                                                                        0664, 0));
                     93:        }
1.34      paf        94: 
1.54      misha      95:        if(!is_open())
                     96:                throw Exception(0,
                     97:                        0,
                     98:                        "can't open %s for writing", type());
                     99: 
1.34      paf       100:        return m_db;
1.27      paf       101: }
                    102: 
1.22      paf       103: VHashfile::~VHashfile() {
1.51      misha     104:        if(is_open())
1.34      paf       105:                close();
1.22      paf       106: }
                    107: 
1.32      paf       108: struct Hashfile_value_serialized_prolog {
1.41      paf       109:        uint version;
1.32      paf       110:        time_t time_to_die;
                    111: };
                    112: 
1.35      paf       113: apr_sdbm_datum_t VHashfile::serialize_value(const String& string, time_t time_to_die) const {
1.32      paf       114:        apr_sdbm_datum_t result;
                    115: 
                    116:        size_t length=string.length();
                    117:        result.dsize=sizeof(Hashfile_value_serialized_prolog)+length;
                    118:        result.dptr=new(PointerFreeGC) char[result.dsize];
                    119: 
                    120:        Hashfile_value_serialized_prolog& prolog=*reinterpret_cast<Hashfile_value_serialized_prolog*>(result.dptr);
                    121:        char *output_cstr=result.dptr+sizeof(Hashfile_value_serialized_prolog);
                    122: 
                    123:        prolog.version=HASHFILE_VALUE_SERIALIZED_VERSION;
                    124:        prolog.time_to_die=time_to_die;
1.45      paf       125:        if(length) // reported errors on storing empty values to hashfiles, but without details. maybe here [win32, intel:solaris, freebsd were OK...]
                    126:                memcpy(output_cstr, string.cstr(), length);
1.32      paf       127: 
                    128:        return result;
                    129: }
                    130: 
1.35      paf       131: const String* VHashfile::deserialize_value(apr_sdbm_datum_t key, const apr_sdbm_datum_t value) {
                    132:        // key not found || it's surely not in our format
1.38      paf       133:        if(!value.dptr || (size_t)value.dsize<sizeof(Hashfile_value_serialized_prolog))
1.35      paf       134:                return 0; 
                    135: 
1.41      paf       136:        // [WARNING: not cast, addresses must be %4=0 on sparc]
1.42      paf       137:        Hashfile_value_serialized_prolog prolog;
                    138:        memcpy(&prolog, value.dptr, sizeof(prolog));
1.41      paf       139:        
1.42      paf       140:        if(prolog.version!=HASHFILE_VALUE_SERIALIZED_VERSION
                    141:                || (prolog.time_to_die/*specified*/ 
                    142:                        && (prolog.time_to_die <= time(0)/*expired*/))) {
1.35      paf       143:                // old format || exipred value
                    144:                remove(key);
1.32      paf       145:                return 0;
1.35      paf       146:        }
1.32      paf       147:        
1.35      paf       148:        char *input_cstr=value.dptr+sizeof(Hashfile_value_serialized_prolog);
                    149:        size_t input_length=value.dsize-sizeof(Hashfile_value_serialized_prolog);
1.32      paf       150: 
1.47      paf       151:        return new String(input_length? pa_strdup(input_cstr, input_length): 0, input_length, true);
1.32      paf       152: }
                    153: 
1.27      paf       154: void VHashfile::put_field(const String& aname, Value *avalue) {
1.34      paf       155:        apr_sdbm_t *db=get_db_for_writing();
1.22      paf       156: 
1.8       parser    157:        time_t time_to_die=0;
                    158:        const String *value_string;
                    159: 
1.23      paf       160:        if(HashStringValue *hash=avalue->get_hash()) {
                    161:                if(Value *value_value=hash->get(value_name)) {
1.9       parser    162:                        if(value_value->get_junction())
1.23      paf       163:                                throw Exception(0,
                    164:                                        0,
                    165:                                        VALUE_NAME" must not be code");
1.8       parser    166: 
                    167:                        value_string=&value_value->as_string();
                    168: 
1.32      paf       169:                        if(Value *expires=hash->get(expires_name)) {
                    170:                                if(Value* vdate=expires->as(VDATE_TYPE, false))
                    171:                                        time_to_die=static_cast<VDate*>(vdate)->get_time(); // $expires[DATE]
                    172:                                else if(double days_till_expire=expires->as_double())
                    173:                                        time_to_die=time(NULL)+(time_t)(60*60*24*days_till_expire); // $expires(days)
                    174:                        }
1.8       parser    175:                } else
1.23      paf       176:                        throw Exception(0,
1.8       parser    177:                                &aname,
1.23      paf       178:                                "put hash value must contain ."VALUE_NAME);
1.8       parser    179:        } else
                    180:                value_string=&avalue->as_string();
1.43      paf       181: 
                    182:        if(aname.is_empty())
1.52      misha     183:                throw Exception(PARSER_RUNTIME,
1.43      paf       184:                        0,
                    185:                        "hashfile key must not be empty");
1.5       parser    186: 
1.23      paf       187:        apr_sdbm_datum_t key;
                    188:        key.dptr=const_cast<char*>(aname.cstr());
1.24      paf       189:        key.dsize=aname.length();
1.23      paf       190: 
1.32      paf       191:        apr_sdbm_datum_t value=serialize_value(*value_string, time_to_die);
1.23      paf       192: 
                    193:        check("apr_sdbm_store", apr_sdbm_store(db, key, value, APR_SDBM_REPLACE));
1.1       parser    194: }
                    195: 
1.11      paf       196: Value *VHashfile::get_field(const String& aname) {
1.34      paf       197:        apr_sdbm_t *db=get_db_for_reading();
                    198: 
1.23      paf       199:        apr_sdbm_datum_t key;
                    200:        key.dptr=const_cast<char*>(aname.cstr());
1.24      paf       201:        key.dsize=aname.length();
1.23      paf       202: 
                    203:        apr_sdbm_datum_t value;
                    204: 
                    205:        check("apr_sdbm_fetch", apr_sdbm_fetch(db, &value, key));
                    206: 
1.35      paf       207:        const String *sresult=deserialize_value(key, value);
1.32      paf       208:        return sresult? new VString(*sresult): 0;
1.24      paf       209: }
                    210: 
1.35      paf       211: void VHashfile::remove(const apr_sdbm_datum_t key) {
1.34      paf       212:        apr_sdbm_t *db=get_db_for_writing();
1.27      paf       213: 
1.35      paf       214:        check("apr_sdbm_delete", apr_sdbm_delete(db, key));
                    215: }
                    216: 
                    217: void VHashfile::remove(const String& aname) {
1.24      paf       218:        apr_sdbm_datum_t key;
                    219:        key.dptr=const_cast<char*>(aname.cstr());
                    220:        key.dsize=aname.length();
                    221: 
1.35      paf       222:        remove(key);
1.1       parser    223: }
                    224: 
1.51      misha     225: void VHashfile::for_each(bool callback(apr_sdbm_datum_t, void*), void* info) {
1.34      paf       226:        apr_sdbm_t *db=get_db_for_reading();
                    227: 
1.30      paf       228:        // collect keys
1.49      paf       229:        Array<apr_sdbm_datum_t>* keys=0;
1.25      paf       230:        check("apr_sdbm_lock", apr_sdbm_lock(db, APR_FLOCK_SHARED));
                    231:        try {
1.30      paf       232:                apr_sdbm_datum_t key;
                    233:                if(apr_sdbm_firstkey(db, &key)==APR_SUCCESS)
1.49      paf       234:                {
                    235:                        size_t count=0;
1.25      paf       236:                        do {
1.49      paf       237:                                // must cound beforehead, becase doing reallocs later would be VERY slow and cause HUGE fragmentation
                    238:                                count++;
1.30      paf       239:                        } while(apr_sdbm_nextkey(db, &key)==APR_SUCCESS);
1.49      paf       240: 
                    241:                        keys=new Array<apr_sdbm_datum_t>(count);
                    242: 
                    243:                        if(apr_sdbm_firstkey(db, &key)==APR_SUCCESS)
                    244:                                do {
                    245:                                        // must clone because it points to page which may go away 
                    246:                                        // [if they modify hashfile inside foreach]
                    247:                                        key.dptr = pa_strdup(key.dptr, key.dsize);
                    248:                                        *keys+=key;
                    249:                                } while(apr_sdbm_nextkey(db, &key)==APR_SUCCESS);
                    250:                }
1.25      paf       251:        } catch(...) {
                    252:                        check("apr_sdbm_unlock", apr_sdbm_unlock(db));
                    253:                        rethrow;
1.4       parser    254:        }
1.30      paf       255:        check("apr_sdbm_unlock", apr_sdbm_unlock(db));
1.4       parser    256: 
1.30      paf       257:        // iterate them
1.49      paf       258:        if(keys)
                    259:                keys->for_each(callback, info);
1.26      paf       260: }
1.27      paf       261: 
                    262: #ifndef DOXYGEN
                    263: struct For_each_string_callback_info {
1.35      paf       264:        VHashfile* self;
1.27      paf       265:        void* nested_info;
1.50      paf       266:        bool (*nested_callback)(const String::Body, const String&, void*);
1.27      paf       267: };
                    268: #endif
1.50      paf       269: static bool for_each_string_callback(apr_sdbm_datum_t apkey, void* ainfo) {
1.27      paf       270:        For_each_string_callback_info& info=*static_cast<For_each_string_callback_info *>(ainfo);
1.35      paf       271:        apr_sdbm_t *db=info.self->get_db_for_reading();
1.27      paf       272: 
                    273:        apr_sdbm_datum_t apvalue;
1.35      paf       274:        check("apr_sdbm_fetch", apr_sdbm_fetch(db, &apvalue, apkey));
                    275: 
                    276:        if(const String* svalue=info.self->deserialize_value(apkey, apvalue)) {
                    277:                const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
1.27      paf       278: 
1.50      paf       279:                return info.nested_callback(clkey, *svalue, info.nested_info);
1.35      paf       280:        }
1.50      paf       281:        return false;
1.27      paf       282: }
1.50      paf       283: void VHashfile::for_each(bool callback(const String::Body, const String&, void*), void* ainfo) {
1.27      paf       284:        For_each_string_callback_info info;
                    285:        
1.35      paf       286:        info.self=this;
1.27      paf       287:        info.nested_info=ainfo;
                    288:        info.nested_callback=callback;
                    289: 
                    290:        for_each(for_each_string_callback, &info);
                    291: }
                    292: 
1.50      paf       293: static bool get_hash__put(const String::Body key, const String& value, void* aresult) {
1.26      paf       294:        static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
1.50      paf       295:        return false;
1.26      paf       296: }
                    297: HashStringValue *VHashfile::get_hash() {
                    298:        HashStringValue& result=*new HashStringValue();
                    299: 
                    300:        for_each(get_hash__put, &result);
                    301:        return &result;
1.34      paf       302: }
                    303: 
                    304: static void delete_file(const char* base_name, const char* ext) {
                    305:        String sfile_name(base_name, false/*already removed tainting at ::open*/);
                    306:        sfile_name<<ext;
1.54      misha     307:        file_delete(sfile_name, false);
1.34      paf       308: }
                    309: 
                    310: void VHashfile::delete_files() {
1.54      misha     311:        if(is_open())
1.51      misha     312:                close();
1.54      misha     313: 
                    314:        if(file_name){
1.51      misha     315:                delete_file(file_name, APR_SDBM_DIRFEXT);
                    316:                delete_file(file_name, APR_SDBM_PAGFEXT);
                    317:        }
1.1       parser    318: }

E-mail: