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