Annotation of parser3/src/classes/hashfile.C, revision 1.12
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.12 ! paf 7: $Id: hashfile.C,v 1.11 2001/10/26 13:48:18 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.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.1 parser 148: // constructor
149:
150: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
151: set_name(*NEW String(pool(), HASH_CLASS_NAME));
152:
1.9 paf 153: // ^hashfile::open[db_home;filename]
154: add_native_method("open", Method::CT_DYNAMIC, _open, 2, 2);
1.3 parser 155: // ^transaction{code}
156: add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.5 parser 157: // ^hash[]
158: add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1 parser 159: // ^cache[key](seconds){code}
160: add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
1.8 parser 161: // ^hashfile.delete[key]
162: add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.12 ! paf 163: // ^hashfile.clear[]
! 164: add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.1 parser 165: }
166:
167: // global variable
168:
169: Methoded *hashfile_base_class;
170:
1.4 parser 171: #endif
172:
1.1 parser 173: // creator
174:
175: Methoded *MHashfile_create(Pool& pool) {
1.4 parser 176: return
177: #ifdef HAVE_LIBDB
178: hashfile_base_class=new(pool) MHashfile(pool)
179: #else
180: 0
181: #endif
182: ;
1.1 parser 183: }
E-mail: