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

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

E-mail: