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

1.1       parser      1: /** @file
                      2:        Parser: @b table class.
                      3: 
1.64      moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.19      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: */
                      7: 
1.32      paf         8: #include "pa_globals.h"
1.48      paf         9: #include "pa_common.h"
1.32      paf        10: #include "pa_threads.h"
1.1       parser     11: #include "pa_vtable.h"
                     12: #include "pa_vstring.h"
                     13: #include "pa_vhashfile.h"
1.32      paf        14: #include "pa_vdate.h"
                     15: 
1.66    ! moko       16: volatile const char * IDENT_PA_VHASHFILE_C="$Id: pa_vhashfile.C,v 1.65 2014/12/11 05:50:24 misha Exp $" IDENT_PA_VHASHFILE_H;
1.64      moko       17: 
1.32      paf        18: // consts
                     19: 
1.41      paf        20: const uint HASHFILE_VALUE_SERIALIZED_VERSION=0x0001;
1.1       parser     21: 
                     22: // methods
                     23: 
1.63      moko       24: void check(const char *step, pa_status_t status) {
                     25:        if(status==PA_SUCCESS)
1.22      paf        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.61      misha      43:        file_name=afile_name.taint_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.63      moko       50:        check("pa_sdbm_close", pa_sdbm_close(m_db));
1.51      misha      51:        m_db=0;
1.34      paf        52: }
                     53: 
1.54      misha      54: bool VHashfile::is_open() {
                     55:        return m_db != 0;
                     56: }
                     57: 
1.63      moko       58: pa_sdbm_t *VHashfile::get_db_for_reading() {
1.54      misha      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);
1.63      moko       65:                check("pa_sdbm_open(shared)", pa_sdbm_open(&m_db, file_name, 
                     66:                                         PA_CREATE|PA_READ|PA_SHARELOCK, 
1.54      misha      67:                                         0664, 0));
                     68:        }
1.51      misha      69: 
                     70:        if(!is_open())
1.56      misha      71:                throw Exception("file.read",
1.34      paf        72:                        0,
1.54      misha      73:                        "can't open %s for reading", type());
1.34      paf        74: 
                     75:        return m_db;
                     76: }
                     77: 
1.63      moko       78: pa_sdbm_t *VHashfile::get_db_for_writing() {
1.54      misha      79:        if(is_open()){
1.63      moko       80:                if(pa_sdbm_rdonly(m_db)) {
1.54      misha      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
1.63      moko       90:                check("pa_sdbm_open(exclusive)", pa_sdbm_open(&m_db, file_name, 
                     91:                                                                                        PA_CREATE|PA_WRITE, 
1.27      paf        92:                                                                                        0664, 0));
                     93:        }
1.34      paf        94: 
1.54      misha      95:        if(!is_open())
1.56      misha      96:                throw Exception("file.access",
1.54      misha      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.63      moko      113: pa_sdbm_datum_t VHashfile::serialize_value(const String& string, time_t time_to_die) const {
                    114:        pa_sdbm_datum_t result;
1.32      paf       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.63      moko      131: const String* VHashfile::deserialize_value(pa_sdbm_datum_t key, const pa_sdbm_datum_t value) {
1.35      paf       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.59      misha     151:        return new String(input_length? pa_strdup(input_cstr, input_length): 0, String::L_TAINTED);
1.32      paf       152: }
                    153: 
1.65      misha     154: #define CHECK(aname) if(aname.is_empty()) throw Exception(PARSER_RUNTIME, 0, "hashfile key must not be empty");
                    155: 
1.27      paf       156: void VHashfile::put_field(const String& aname, Value *avalue) {
1.65      misha     157:        CHECK(aname)
1.57      misha     158: 
1.63      moko      159:        pa_sdbm_t *db=get_db_for_writing();
1.22      paf       160: 
1.8       parser    161:        time_t time_to_die=0;
                    162:        const String *value_string;
                    163: 
1.23      paf       164:        if(HashStringValue *hash=avalue->get_hash()) {
                    165:                if(Value *value_value=hash->get(value_name)) {
1.9       parser    166:                        if(value_value->get_junction())
1.56      misha     167:                                throw Exception(PARSER_RUNTIME,
1.23      paf       168:                                        0,
                    169:                                        VALUE_NAME" must not be code");
1.8       parser    170: 
                    171:                        value_string=&value_value->as_string();
                    172: 
1.32      paf       173:                        if(Value *expires=hash->get(expires_name)) {
1.62      misha     174:                                if(Value* vdate=expires->as(VDATE_TYPE))
1.32      paf       175:                                        time_to_die=static_cast<VDate*>(vdate)->get_time(); // $expires[DATE]
                    176:                                else if(double days_till_expire=expires->as_double())
                    177:                                        time_to_die=time(NULL)+(time_t)(60*60*24*days_till_expire); // $expires(days)
                    178:                        }
1.8       parser    179:                } else
1.56      misha     180:                        throw Exception(PARSER_RUNTIME,
1.8       parser    181:                                &aname,
1.66    ! moko      182:                                "put hash value must contain ." VALUE_NAME);
1.8       parser    183:        } else
                    184:                value_string=&avalue->as_string();
1.43      paf       185: 
1.63      moko      186:        pa_sdbm_datum_t key;
1.23      paf       187:        key.dptr=const_cast<char*>(aname.cstr());
1.24      paf       188:        key.dsize=aname.length();
1.23      paf       189: 
1.63      moko      190:        pa_sdbm_datum_t value=serialize_value(*value_string, time_to_die);
1.23      paf       191: 
1.57      misha     192: #ifndef PAIRMAX
                    193: // !see PAIRMAX definition in sdbm_private.h. values should be the same
                    194: #define PAIRMAX 8008
                    195: #endif
                    196: 
                    197:        if(key.dsize+value.dsize > PAIRMAX)
                    198:                throw Exception(PARSER_RUNTIME,
                    199:                        0,
                    200:                        "hashfile record length (key+value) exceeds limit (%d bytes)", PAIRMAX);
                    201: 
1.63      moko      202:        check("pa_sdbm_store", pa_sdbm_store(db, key, value, PA_SDBM_REPLACE));
1.1       parser    203: }
                    204: 
1.11      paf       205: Value *VHashfile::get_field(const String& aname) {
1.65      misha     206:        CHECK(aname)
                    207: 
1.63      moko      208:        pa_sdbm_t *db=get_db_for_reading();
1.34      paf       209: 
1.63      moko      210:        pa_sdbm_datum_t key;
1.23      paf       211:        key.dptr=const_cast<char*>(aname.cstr());
1.24      paf       212:        key.dsize=aname.length();
1.23      paf       213: 
1.63      moko      214:        pa_sdbm_datum_t value;
1.23      paf       215: 
1.63      moko      216:        check("pa_sdbm_fetch", pa_sdbm_fetch(db, &value, key));
1.23      paf       217: 
1.35      paf       218:        const String *sresult=deserialize_value(key, value);
1.32      paf       219:        return sresult? new VString(*sresult): 0;
1.24      paf       220: }
                    221: 
1.63      moko      222: void VHashfile::remove(const pa_sdbm_datum_t key) {
                    223:        pa_sdbm_t *db=get_db_for_writing();
1.27      paf       224: 
1.63      moko      225:        check("pa_sdbm_delete", pa_sdbm_delete(db, key));
1.35      paf       226: }
                    227: 
                    228: void VHashfile::remove(const String& aname) {
1.65      misha     229:        CHECK(aname)
                    230: 
1.63      moko      231:        pa_sdbm_datum_t key;
1.24      paf       232:        key.dptr=const_cast<char*>(aname.cstr());
                    233:        key.dsize=aname.length();
                    234: 
1.35      paf       235:        remove(key);
1.1       parser    236: }
                    237: 
1.63      moko      238: void VHashfile::for_each(bool callback(pa_sdbm_datum_t, void*), void* info) {
                    239:        pa_sdbm_t *db=get_db_for_reading();
1.34      paf       240: 
1.30      paf       241:        // collect keys
1.63      moko      242:        Array<pa_sdbm_datum_t>* keys=0;
                    243:        check("pa_sdbm_lock", pa_sdbm_lock(db, PA_FLOCK_SHARED));
1.25      paf       244:        try {
1.63      moko      245:                pa_sdbm_datum_t key;
                    246:                if(pa_sdbm_firstkey(db, &key)==PA_SUCCESS)
1.49      paf       247:                {
                    248:                        size_t count=0;
1.25      paf       249:                        do {
1.49      paf       250:                                // must cound beforehead, becase doing reallocs later would be VERY slow and cause HUGE fragmentation
                    251:                                count++;
1.63      moko      252:                        } while(pa_sdbm_nextkey(db, &key)==PA_SUCCESS);
1.49      paf       253: 
1.63      moko      254:                        keys=new Array<pa_sdbm_datum_t>(count);
1.49      paf       255: 
1.63      moko      256:                        if(pa_sdbm_firstkey(db, &key)==PA_SUCCESS)
1.49      paf       257:                                do {
                    258:                                        // must clone because it points to page which may go away 
                    259:                                        // [if they modify hashfile inside foreach]
                    260:                                        key.dptr = pa_strdup(key.dptr, key.dsize);
                    261:                                        *keys+=key;
1.63      moko      262:                                } while(pa_sdbm_nextkey(db, &key)==PA_SUCCESS);
1.49      paf       263:                }
1.25      paf       264:        } catch(...) {
1.63      moko      265:                        check("pa_sdbm_unlock", pa_sdbm_unlock(db));
1.25      paf       266:                        rethrow;
1.4       parser    267:        }
1.63      moko      268:        check("pa_sdbm_unlock", pa_sdbm_unlock(db));
1.4       parser    269: 
1.30      paf       270:        // iterate them
1.49      paf       271:        if(keys)
                    272:                keys->for_each(callback, info);
1.26      paf       273: }
1.27      paf       274: 
                    275: #ifndef DOXYGEN
                    276: struct For_each_string_callback_info {
1.35      paf       277:        VHashfile* self;
1.27      paf       278:        void* nested_info;
1.50      paf       279:        bool (*nested_callback)(const String::Body, const String&, void*);
1.27      paf       280: };
                    281: #endif
1.63      moko      282: static bool for_each_string_callback(pa_sdbm_datum_t apkey, void* ainfo) {
1.27      paf       283:        For_each_string_callback_info& info=*static_cast<For_each_string_callback_info *>(ainfo);
1.63      moko      284:        pa_sdbm_t *db=info.self->get_db_for_reading();
1.27      paf       285: 
1.63      moko      286:        pa_sdbm_datum_t apvalue;
                    287:        check("pa_sdbm_fetch", pa_sdbm_fetch(db, &apvalue, apkey));
1.35      paf       288: 
                    289:        if(const String* svalue=info.self->deserialize_value(apkey, apvalue)) {
                    290:                const char *clkey=pa_strdup(apkey.dptr, apkey.dsize);
1.27      paf       291: 
1.50      paf       292:                return info.nested_callback(clkey, *svalue, info.nested_info);
1.35      paf       293:        }
1.50      paf       294:        return false;
1.27      paf       295: }
1.50      paf       296: void VHashfile::for_each(bool callback(const String::Body, const String&, void*), void* ainfo) {
1.27      paf       297:        For_each_string_callback_info info;
                    298:        
1.35      paf       299:        info.self=this;
1.27      paf       300:        info.nested_info=ainfo;
                    301:        info.nested_callback=callback;
                    302: 
                    303:        for_each(for_each_string_callback, &info);
                    304: }
                    305: 
1.50      paf       306: static bool get_hash__put(const String::Body key, const String& value, void* aresult) {
1.26      paf       307:        static_cast<HashStringValue*>(aresult)->put(key, new VString(value));
1.50      paf       308:        return false;
1.26      paf       309: }
                    310: HashStringValue *VHashfile::get_hash() {
                    311:        HashStringValue& result=*new HashStringValue();
                    312: 
                    313:        for_each(get_hash__put, &result);
                    314:        return &result;
1.34      paf       315: }
                    316: 
                    317: static void delete_file(const char* base_name, const char* ext) {
1.59      misha     318:        String sfile_name(base_name);
1.34      paf       319:        sfile_name<<ext;
1.54      misha     320:        file_delete(sfile_name, false);
1.34      paf       321: }
                    322: 
                    323: void VHashfile::delete_files() {
1.54      misha     324:        if(is_open())
1.51      misha     325:                close();
1.54      misha     326: 
                    327:        if(file_name){
1.63      moko      328:                delete_file(file_name, PA_SDBM_DIRFEXT);
                    329:                delete_file(file_name, PA_SDBM_PAGFEXT);
1.51      misha     330:        }
1.1       parser    331: }

E-mail: