Annotation of parser3/src/classes/hashfile.C, revision 1.22
1.1 parser 1: /** @file
2: Parser: @b hashfile parser class.
3:
1.21 paf 4: Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.22 ! paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 6:
1.22 ! paf 7: $Id: hashfile.C,v 1.21 2002/02/08 07:27:40 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(
1.18 paf 41: r.absolute(params->as_string(0, "DB_HOME must be string")),
1.11 paf 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: // execute body
1.20 paf 57: r.write_assign_lang(r.process(body_code));
1.3 parser 58: }
59:
1.5 parser 60: static void _hash(Request& r, const String& method_name, MethodParams *params) {
61: Pool& pool=r.pool();
62: VHashfile& self=*static_cast<VHashfile *>(r.self);
63:
64: // write out result
65: VHash& result=*new(pool) VHash(pool, *self.get_hash(&method_name));
66: result.set_name(method_name);
67: r.write_no_lang(result);
68: }
69:
1.7 parser 70: static void _cache(Request& r, const String& method_name, MethodParams *params) {
71: Pool& pool=r.pool();
72: VHashfile& self=*static_cast<VHashfile *>(r.self);
73:
74: // key, expires, body code
75: const String &key=params->as_string(0, "key must be string");
1.16 paf 76: time_t lifespan=(time_t)params->as_double(1, "lifespan must be number", r);
1.7 parser 77: Value& body_code=params->as_junction(2, "body must be code");
78:
1.17 paf 79: DB_Table_ptr table_ptr=self.get_table_ptr(&method_name);
1.7 parser 80:
1.20 paf 81: if(lifespan) { // 'lifespan' specified? try cached copy...
82: if(String *cached_body=table_ptr->get(pool, key, lifespan)) { // have cached copy?
1.19 paf 83: r.write_assign_lang(*cached_body);
84: // happy with it
85: return;
86: }
87: } else // 'lifespan'=0, forget cached copy
1.20 paf 88: table_ptr->remove(key);
1.19 paf 89:
90: // save
91: Autosave_marked_to_cancel_cache saved(self);
92:
93: // process
94: Value& processed_body=r.process(body_code);
95: r.write_assign_lang(processed_body);
96:
97: // put it to cache if 'lifespan' specified & never called ^delete[]
98: if(lifespan && !self.marked_to_cancel_cache())
1.20 paf 99: table_ptr->put(key, processed_body.as_string(), lifespan);
1.7 parser 100: }
101:
1.8 parser 102: static void _delete(Request& r, const String& method_name, MethodParams *params) {
103: Pool& pool=r.pool();
104: VHashfile& self=*static_cast<VHashfile *>(r.self);
105:
106: if(params->size()==0)
107: self.mark_to_cancel_cache();
108: else {
109: // key
110: const String &key=params->as_string(0, "key must be string");
111: // remove
1.20 paf 112: self.get_table_ptr(&method_name)->remove(key);
1.8 parser 113: }
114: }
115:
1.12 paf 116: static void _clear(Request& r, const String& method_name, MethodParams *) {
117: Pool& pool=r.pool();
118: VHashfile& self=*static_cast<VHashfile *>(r.self);
1.20 paf 119: DB_Cursor cursor(*self.get_table_ptr(&method_name), &method_name);
1.12 paf 120:
121: while(true) {
122: if(!cursor.move(DB_NEXT))
123: break;
124:
125: cursor.remove(0/*flags*/);
126: }
127: }
128:
1.14 paf 129: static void _foreach(Request& r, const String& method_name, MethodParams *params) {
130: Pool& pool=r.pool();
131: VHashfile& self=*static_cast<VHashfile *>(r.self);
132:
133: const String& key_var_name=params->as_string(0, "key-var name must be string");
134: const String& value_var_name=params->as_string(1, "value-var name must be string");
135: Value& body_code=params->as_junction(2, "body must be code");
136: Value *delim_maybe_code=params->size()>3?¶ms->get(3):0;
137:
138: bool need_delim=false;
139: Value& var_context=*body_code.get_junction()->wcontext;
140: VString& vkey=*new(pool) VString(pool);
141: VString& vvalue=*new(pool) VString(pool);
142:
1.20 paf 143: DB_Cursor cursor(*self.get_table_ptr(&method_name), &method_name);
1.14 paf 144: while(true) {
145: String *key;
146: String *data;
147: if(!cursor.get(pool, key, data, DB_NEXT))
148: break;
149:
150: if(!key)
151: continue; // expired
152:
153: vkey.set_string(*key);
154: vvalue.set_string(*data);
155: var_context.put_element(key_var_name, &vkey);
156: var_context.put_element(value_var_name, &vvalue);
157:
158: Value& processed_body=r.process(body_code);
159: if(delim_maybe_code) { // delimiter set?
160: const String *string=processed_body.get_string();
161: if(need_delim && string && string->size()) // need delim & iteration produced string?
162: r.write_pass_lang(r.process(*delim_maybe_code));
163: need_delim=true;
164: }
165: r.write_pass_lang(processed_body);
166: }
167: }
168:
1.1 parser 169: // constructor
170:
171: MHashfile::MHashfile(Pool& apool) : Methoded(apool) {
172: set_name(*NEW String(pool(), HASH_CLASS_NAME));
173:
1.9 paf 174: // ^hashfile::open[db_home;filename]
175: add_native_method("open", Method::CT_DYNAMIC, _open, 2, 2);
1.3 parser 176: // ^transaction{code}
177: add_native_method("transaction", Method::CT_DYNAMIC, _transaction, 1, 1);
1.5 parser 178: // ^hash[]
179: add_native_method("hash", Method::CT_DYNAMIC, _hash, 0, 0);
1.1 parser 180: // ^cache[key](seconds){code}
181: add_native_method("cache", Method::CT_DYNAMIC, _cache, 3, 3);
1.8 parser 182: // ^hashfile.delete[key]
183: add_native_method("delete", Method::CT_DYNAMIC, _delete, 0, 1);
1.12 paf 184: // ^hashfile.clear[]
185: add_native_method("clear", Method::CT_DYNAMIC, _clear, 0, 0);
1.14 paf 186: // ^hashfile.foreach[key;value]{code}[delim]
187: add_native_method("foreach", Method::CT_DYNAMIC, _foreach, 2+1, 2+1+1);
1.1 parser 188: }
189:
190: // global variable
191:
192: Methoded *hashfile_base_class;
193:
1.4 parser 194: #endif
195:
1.1 parser 196: // creator
197:
198: Methoded *MHashfile_create(Pool& pool) {
1.4 parser 199: return
1.13 paf 200: #ifdef DB2
1.4 parser 201: hashfile_base_class=new(pool) MHashfile(pool)
202: #else
203: 0
204: #endif
205: ;
1.1 parser 206: }
E-mail: