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

1.1       parser      1: /** @file
                      2:        Parser: @b hashfile parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      6: 
1.11    ! paf         7:        $Id: hashfile.C,v 1.10 2001/10/26 07:06:34 paf Exp $
1.1       parser      8: */
                      9: 
                     10: #include "pa_config_includes.h"
1.4       parser     11: #include "classes.h"
1.1       parser     12: #ifdef HAVE_LIBDB
                     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.9       paf        46: static void _clear(Request& r, const String& method_name, MethodParams *params) {
                     47:        Pool& pool=r.pool();
                     48:        
                     49:        // db_home & file_name
                     50:        const String &db_home=params->as_string(0, "DB_HOME must be string");
                     51:        const String &file_name=params->as_string(1, "filename must be string");
                     52: 
1.11    ! paf        53:        DB_manager->get_connection_ptr(db_home, &method_name)->
        !            54:                clear_dbfile(file_name);
1.3       parser     55: }
                     56: 
                     57: 
                     58: static void _transaction(Request& r, const String& method_name, MethodParams *params) {
                     59:        Pool& pool=r.pool();
                     60:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     61:        
                     62:        // body code
                     63:        Value& body_code=params->as_junction(0, "body must be code");
                     64: 
1.9       paf        65:        // table
1.11    ! paf        66:        DB_Table_ptr table_ptr=self.get_table_ptr(&method_name);
1.3       parser     67: 
                     68:        // transaction
1.11    ! paf        69:        DB_Transaction transaction(pool, *table_ptr, self.current_transaction);
1.3       parser     70: 
                     71:        // execute body
                     72:        try {
                     73:                r.write_assign_lang(r.process(body_code));
                     74:        } catch(...) { // process/commit problem
                     75:                transaction.mark_to_rollback();
                     76:                
                     77:                /*re*/throw; 
                     78:        }
                     79: }
                     80: 
1.5       parser     81: static void _hash(Request& r, const String& method_name, MethodParams *params) {
                     82:        Pool& pool=r.pool();
                     83:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     84:        
                     85:        // write out result
                     86:        VHash& result=*new(pool) VHash(pool, *self.get_hash(&method_name));
                     87:        result.set_name(method_name);
                     88:        r.write_no_lang(result);
                     89: }
                     90: 
1.7       parser     91: static void _cache(Request& r, const String& method_name, MethodParams *params) {
                     92:        Pool& pool=r.pool();
                     93:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                     94:        
                     95:        // key, expires, body code
                     96:        const String &key=params->as_string(0, "key must be string");
                     97:        double expires=params->as_double(1, "expires must be number", r);
                     98:        Value& body_code=params->as_junction(2, "body must be code");
                     99: 
                    100:        // transaction
1.11    ! paf       101:        DB_Transaction transaction(pool, *self.get_table_ptr(&method_name), self.current_transaction);
1.7       parser    102: 
                    103:        // execute body
                    104:        try {
                    105:                if(expires) { // 'expires' specified? try cached copy...
1.11    ! paf       106:                        if(String *cached_body=transaction.get(key)) { // have cached copy?
1.7       parser    107:                                r.write_assign_lang(*cached_body);
                    108:                                // happy with it
                    109:                                return;
                    110:                        }
                    111:                } else // 'expires'=0, forget cached copy
1.11    ! paf       112:                        transaction.remove(key);
1.7       parser    113: 
1.8       parser    114:                // save
                    115:                Autosave_marked_to_cancel_cache saved(self);
                    116: 
1.7       parser    117:                // process
                    118:                Value& processed_body=r.process(body_code);
                    119:                r.write_assign_lang(processed_body);
                    120:                
1.8       parser    121:                // put it to cache if 'expires' specified & never called ^delete[]
                    122:                if(expires && !self.marked_to_cancel_cache())
1.11    ! paf       123:                        transaction.put(key, processed_body.as_string(), time(0)+(time_t)expires);
1.7       parser    124:        } catch(...) { // process/commit problem
                    125:                transaction.mark_to_rollback();
                    126:                
                    127:                /*re*/throw; 
                    128:        }
                    129: }
                    130: 
1.8       parser    131: static void _delete(Request& r, const String& method_name, MethodParams *params) {
                    132:        Pool& pool=r.pool();
                    133:        VHashfile& self=*static_cast<VHashfile *>(r.self);
                    134:        
                    135:        if(params->size()==0)
                    136:                self.mark_to_cancel_cache();
                    137:        else {
                    138:                // key
                    139:                const String &key=params->as_string(0, "key must be string");
                    140:                // remove
1.11    ! paf       141:                self.get_table_ptr(&method_name)->remove(self.current_transaction, key);
1.8       parser    142:        }
                    143: }
                    144: 
1.1       parser    145: // constructor
                    146: 
                    147: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
                    148:        set_name(*NEW String(pool(), HASH_CLASS_NAME));
                    149: 
1.9       paf       150:        // ^hashfile::open[db_home;filename]
                    151:        add_native_method("open", Method::CT_DYNAMIC, _open, 2, 2);
                    152:        // ^hashfile:clear[db_home;filename]
                    153:        add_native_method("clear", Method::CT_STATIC, _clear, 2, 2);
1.3       parser    154:        // ^transaction{code}
                    155:        add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.5       parser    156:        // ^hash[]
                    157:        add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1       parser    158:        // ^cache[key](seconds){code}
                    159:        add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
1.8       parser    160:        // ^hashfile.delete[key]
                    161:        add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.1       parser    162: }
                    163: 
                    164: // global variable
                    165: 
                    166: Methoded *hashfile_base_class;
                    167: 
1.4       parser    168: #endif
                    169: 
1.1       parser    170: // creator
                    171: 
                    172: Methoded *MHashfile_create(Pool& pool) {
1.4       parser    173:        return 
                    174: #ifdef HAVE_LIBDB
                    175:                hashfile_base_class=new(pool) MHashfile(pool)
                    176: #else
                    177:                0
                    178: #endif
                    179:        ;
1.1       parser    180: }

E-mail: