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

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.17    ! paf         7:        $Id: hashfile.C,v 1.15.6.2 2001/12/06 19:12:33 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(
                     41:                params->as_string(0, "DB_HOME must be string"), 
                     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: 
                     88:        // transaction
1.17    ! paf        89: ////   DB_Transaction transaction(pool, *self.get_table_ptr(&method_name), self.current_transaction);
        !            90:        DB_Table_ptr table_ptr=self.get_table_ptr(&method_name);
1.7       parser     91: 
                     92:        // execute body
1.17    ! paf        93: ////   try {
1.16      paf        94:                if(lifespan) { // 'lifespan' specified? try cached copy...
1.17    ! paf        95:                        if(String *cached_body=/*transaction.*/table_ptr->get(self.current_transaction, pool, key, lifespan)) { // have cached copy?
1.7       parser     96:                                r.write_assign_lang(*cached_body);
                     97:                                // happy with it
                     98:                                return;
                     99:                        }
1.16      paf       100:                } else // 'lifespan'=0, forget cached copy
1.17    ! paf       101:                        /*transaction.*/table_ptr->remove(self.current_transaction, key);
1.7       parser    102: 
1.8       parser    103:                // save
                    104:                Autosave_marked_to_cancel_cache saved(self);
                    105: 
1.7       parser    106:                // process
                    107:                Value& processed_body=r.process(body_code);
                    108:                r.write_assign_lang(processed_body);
                    109:                
1.16      paf       110:                // put it to cache if 'lifespan' specified & never called ^delete[]
                    111:                if(lifespan && !self.marked_to_cancel_cache())
1.17    ! paf       112:                        /*transaction.*/table_ptr->put(self.current_transaction, key, processed_body.as_string(), lifespan);
        !           113: ////   } catch(...) { // process/commit problem
        !           114: ////           transaction.mark_to_rollback();
1.7       parser    115:                
1.17    ! paf       116: ////           /*re*/throw; 
        !           117: ////   }
1.7       parser    118: }
                    119: 
1.8       parser    120: static void _delete(Request& r, const String& method_name, MethodParams *params) {
                    121:        Pool& pool=r.pool();
                    122:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    123:        
                    124:        if(params->size()==0)
                    125:                self.mark_to_cancel_cache();
                    126:        else {
                    127:                // key
                    128:                const String &key=params->as_string(0, "key must be string");
                    129:                // remove
1.11      paf       130:                self.get_table_ptr(&method_name)->remove(self.current_transaction, key);
1.8       parser    131:        }
                    132: }
                    133: 
1.12      paf       134: static void _clear(Request& r, const String& method_name, MethodParams *) {
                    135:        Pool& pool=r.pool();
                    136:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    137:        DB_Cursor cursor(
                    138:                *self.get_table_ptr(&method_name), 
                    139:                self.current_transaction, &method_name);
                    140: 
                    141:        while(true) {
                    142:                if(!cursor.move(DB_NEXT))
                    143:                        break;
                    144: 
                    145:                cursor.remove(0/*flags*/);
                    146:        }
                    147: }
                    148: 
1.14      paf       149: static void _foreach(Request& r, const String& method_name, MethodParams *params) {
                    150:        Pool& pool=r.pool();
                    151:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    152: 
                    153:        const String& key_var_name=params->as_string(0, "key-var name must be string");
                    154:        const String& value_var_name=params->as_string(1, "value-var name must be string");
                    155:        Value& body_code=params->as_junction(2, "body must be code");
                    156:        Value *delim_maybe_code=params->size()>3?&params->get(3):0;
                    157: 
                    158:        bool need_delim=false;
                    159:        Value& var_context=*body_code.get_junction()->wcontext;
                    160:        VString& vkey=*new(pool) VString(pool);
                    161:        VString& vvalue=*new(pool) VString(pool);
                    162: 
                    163:        DB_Cursor cursor(
                    164:                *self.get_table_ptr(&method_name), 
                    165:                self.current_transaction, &method_name);
                    166:        while(true) {
                    167:                String *key;
                    168:                String *data;
                    169:                if(!cursor.get(pool, key, data, DB_NEXT))
                    170:                        break;
                    171: 
                    172:                if(!key) 
                    173:                        continue; // expired
                    174: 
                    175:                vkey.set_string(*key);
                    176:                vvalue.set_string(*data);
                    177:                var_context.put_element(key_var_name, &vkey);
                    178:                var_context.put_element(value_var_name, &vvalue);
                    179: 
                    180:                Value& processed_body=r.process(body_code);
                    181:                if(delim_maybe_code) { // delimiter set?
                    182:                        const String *string=processed_body.get_string();
                    183:                        if(need_delim && string && string->size()) // need delim & iteration produced string?
                    184:                                r.write_pass_lang(r.process(*delim_maybe_code));
                    185:                        need_delim=true;
                    186:                }
                    187:                r.write_pass_lang(processed_body);
                    188:        }
                    189: }
                    190: 
1.1       parser    191: // constructor
                    192: 
                    193: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
                    194:        set_name(*NEW String(pool(), HASH_CLASS_NAME));
                    195: 
1.9       paf       196:        // ^hashfile::open[db_home;filename]
                    197:        add_native_method("open", Method::CT_DYNAMIC, _open, 2, 2);
1.3       parser    198:        // ^transaction{code}
                    199:        add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.5       parser    200:        // ^hash[]
                    201:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1       parser    202:        // ^cache[key](seconds){code}
                    203:        add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
1.8       parser    204:        // ^hashfile.delete[key]
                    205:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.12      paf       206:        // ^hashfile.clear[]
                    207:        add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14      paf       208:        // ^hashfile.foreach[key;value]{code}[delim]
                    209:        add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1       parser    210: }
                    211: 
                    212: // global variable
                    213: 
                    214: Methoded *hashfile_base_class;
                    215: 
1.4       parser    216: #endif
                    217: 
1.1       parser    218: // creator
                    219: 
                    220: Methoded *MHashfile_create(Pool& pool) {
1.4       parser    221:        return 
1.13      paf       222: #ifdef DB2
1.4       parser    223:                hashfile_base_class=new(pool) MHashfile(pool)
                    224: #else
                    225:                0
                    226: #endif
                    227:        ;
1.1       parser    228: }

E-mail: