Annotation of parser3/src/classes/hashfile.C, revision 1.19

1.1       parser      1: /** @file
                      2:        Parser: @b hashfile parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.15      paf         5:        Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1       parser      6: 
1.19    ! paf         7:        $Id: hashfile.C,v 1.18 2001/12/19 16:19:11 paf Exp $
1.1       parser      8: */
                      9: 
                     10: #include "pa_config_includes.h"
1.4       parser     11: #include "classes.h"
1.13      paf        12: #ifdef DB2
1.1       parser     13: 
                     14: #include "pa_request.h"
                     15: #include "pa_vhashfile.h"
                     16: #include "pa_vhash.h"
                     17: 
                     18: // defines
                     19: 
                     20: #define HASH_CLASS_NAME "hashfile"
                     21: 
                     22: // class
                     23: 
                     24: class MHashfile : public Methoded {
                     25: public: // VStateless_class
                     26:        Value *create_new_value(Pool& pool) { return new(pool) VHashfile(pool); }
                     27: 
                     28: public:
                     29:        MHashfile(Pool& pool);
                     30: public: // Methoded
                     31:        bool used_directly() { return true; }
                     32: };
                     33: 
                     34: // methods
                     35: 
1.3       parser     36: static void _open(Request& r, const String& method_name, MethodParams *params) {
1.1       parser     37:        Pool& pool=r.pool();
                     38:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     39:        
1.11      paf        40:        self.assign(
1.18      paf        41:                r.absolute(params->as_string(0, "DB_HOME must be string")), 
1.11      paf        42:                params->as_string(1, "filename must be string")
1.9       paf        43:        );
                     44: }
1.1       parser     45: 
1.3       parser     46: static void _transaction(Request& r, const String& method_name, MethodParams *params) {
                     47:        Pool& pool=r.pool();
                     48:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     49:        
                     50:        // body code
                     51:        Value& body_code=params->as_junction(0, "body must be code");
                     52: 
1.9       paf        53:        // table
1.11      paf        54:        DB_Table_ptr table_ptr=self.get_table_ptr(&method_name);
1.3       parser     55: 
                     56:        // transaction
1.11      paf        57:        DB_Transaction transaction(pool, *table_ptr, self.current_transaction);
1.3       parser     58: 
                     59:        // execute body
                     60:        try {
                     61:                r.write_assign_lang(r.process(body_code));
                     62:        } catch(...) { // process/commit problem
                     63:                transaction.mark_to_rollback();
                     64:                
                     65:                /*re*/throw; 
                     66:        }
                     67: }
                     68: 
1.5       parser     69: static void _hash(Request& r, const String& method_name, MethodParams *params) {
                     70:        Pool& pool=r.pool();
                     71:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     72:        
                     73:        // write out result
                     74:        VHash& result=*new(pool) VHash(pool, *self.get_hash(&method_name));
                     75:        result.set_name(method_name);
                     76:        r.write_no_lang(result);
                     77: }
                     78: 
1.7       parser     79: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                     80:        Pool& pool=r.pool();
                     81:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     82:        
                     83:        // key, expires, body code
                     84:        const String &key=params->as_string(0, "key must be string");
1.16      paf        85:        time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
1.7       parser     86:        Value& body_code=params->as_junction(2, "body must be code");
                     87: 
1.17      paf        88:        DB_Table_ptr table_ptr=self.get_table_ptr(&method_name);
1.7       parser     89: 
1.16      paf        90:                if(lifespan) { // 'lifespan' specified? try cached copy...
1.19    ! paf        91:                if(String *cached_body=table_ptr->get(self.current_transaction, pool, key, lifespan)) { // have cached copy?
        !            92:                        r.write_assign_lang(*cached_body);
        !            93:                        // happy with it
        !            94:                        return;
        !            95:                }
        !            96:        } else // 'lifespan'=0, forget cached copy
        !            97:                table_ptr->remove(self.current_transaction, key);
        !            98: 
        !            99:        // save
        !           100:        Autosave_marked_to_cancel_cache saved(self);
        !           101: 
        !           102:        // process
        !           103:        Value& processed_body=r.process(body_code);
        !           104:        r.write_assign_lang(processed_body);
        !           105:        
        !           106:        // put it to cache if 'lifespan' specified & never called ^delete[]
        !           107:        if(lifespan && !self.marked_to_cancel_cache())
        !           108:                table_ptr->put(self.current_transaction, key, processed_body.as_string(), lifespan);
1.7       parser    109: }
                    110: 
1.8       parser    111: static void _delete(Request& r, const String& method_name, MethodParams *params) {
                    112:        Pool& pool=r.pool();
                    113:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    114:        
                    115:        if(params->size()==0)
                    116:                self.mark_to_cancel_cache();
                    117:        else {
                    118:                // key
                    119:                const String &key=params->as_string(0, "key must be string");
                    120:                // remove
1.11      paf       121:                self.get_table_ptr(&method_name)->remove(self.current_transaction, key);
1.8       parser    122:        }
                    123: }
                    124: 
1.12      paf       125: static void _clear(Request& r, const String& method_name, MethodParams *) {
                    126:        Pool& pool=r.pool();
                    127:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    128:        DB_Cursor cursor(
                    129:                *self.get_table_ptr(&method_name), 
                    130:                self.current_transaction, &method_name);
                    131: 
                    132:        while(true) {
                    133:                if(!cursor.move(DB_NEXT))
                    134:                        break;
                    135: 
                    136:                cursor.remove(0/*flags*/);
                    137:        }
                    138: }
                    139: 
1.14      paf       140: static void _foreach(Request& r, const String& method_name, MethodParams *params) {
                    141:        Pool& pool=r.pool();
                    142:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    143: 
                    144:        const String& key_var_name=params->as_string(0, "key-var name must be string");
                    145:        const String& value_var_name=params->as_string(1, "value-var name must be string");
                    146:        Value& body_code=params->as_junction(2, "body must be code");
                    147:        Value *delim_maybe_code=params->size()>3?&params->get(3):0;
                    148: 
                    149:        bool need_delim=false;
                    150:        Value& var_context=*body_code.get_junction()->wcontext;
                    151:        VString& vkey=*new(pool) VString(pool);
                    152:        VString& vvalue=*new(pool) VString(pool);
                    153: 
                    154:        DB_Cursor cursor(
                    155:                *self.get_table_ptr(&method_name), 
                    156:                self.current_transaction, &method_name);
                    157:        while(true) {
                    158:                String *key;
                    159:                String *data;
                    160:                if(!cursor.get(pool, key, data, DB_NEXT))
                    161:                        break;
                    162: 
                    163:                if(!key) 
                    164:                        continue; // expired
                    165: 
                    166:                vkey.set_string(*key);
                    167:                vvalue.set_string(*data);
                    168:                var_context.put_element(key_var_name, &vkey);
                    169:                var_context.put_element(value_var_name, &vvalue);
                    170: 
                    171:                Value& processed_body=r.process(body_code);
                    172:                if(delim_maybe_code) { // delimiter set?
                    173:                        const String *string=processed_body.get_string();
                    174:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    175:                                r.write_pass_lang(r.process(*delim_maybe_code));
                    176:                        need_delim=true;
                    177:                }
                    178:                r.write_pass_lang(processed_body);
                    179:        }
                    180: }
                    181: 
1.1       parser    182: // constructor
                    183: 
                    184: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
                    185:        set_name(*NEW String(pool(), HASH_CLASS_NAME));
                    186: 
1.9       paf       187:        // ^hashfile::open[db_home;filename]
                    188:        add_native_method("open", Method::CT_DYNAMIC, _open, 2, 2);
1.3       parser    189:        // ^transaction{code}
                    190:        add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.5       parser    191:        // ^hash[]
                    192:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1       parser    193:        // ^cache[key](seconds){code}
                    194:        add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
1.8       parser    195:        // ^hashfile.delete[key]
                    196:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.12      paf       197:        // ^hashfile.clear[]
                    198:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14      paf       199:        // ^hashfile.foreach[key;value]{code}[delim]
                    200:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    201: }
                    202: 
                    203: // global variable
                    204: 
                    205: Methoded *hashfile_base_class;
                    206: 
1.4       parser    207: #endif
                    208: 
1.1       parser    209: // creator
                    210: 
                    211: Methoded *MHashfile_create(Pool& pool) {
1.4       parser    212:        return 
1.13      paf       213: #ifdef DB2
1.4       parser    214:                hashfile_base_class=new(pool) MHashfile(pool)
                    215: #else
                    216:                0
                    217: #endif
                    218:        ;
1.1       parser    219: }

E-mail: