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

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

E-mail: