Annotation of parser3/src/main/pa_stylesheet_manager.C, revision 1.22
1.1 parser 1: /** @file
2: Parser: sql driver manager implementation.
3:
1.17 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.13 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1 parser 6: */
7: #include "pa_config_includes.h"
8: #ifdef XML
1.14 paf 9:
1.22 ! paf 10: static const char * const IDENT_STYLESHEET_MANAGER_C="$Date: 2003/11/28 10:27:00 $";
1.1 parser 11:
12: #include "pa_stylesheet_manager.h"
13: #include "pa_exception.h"
14: #include "pa_common.h"
15: #include "pa_threads.h"
16: #include "pa_stack.h"
1.2 paf 17: #include "pa_vhash.h"
1.3 paf 18: #include "pa_vtable.h"
1.1 parser 19:
20: // globals
21:
1.17 paf 22: Stylesheet_manager stylesheet_manager;
1.1 parser 23:
24: // consts
25:
1.17 paf 26: const time_t SECOND=1;
27: const time_t EXPIRE_UNUSED_CONNECTION_SECONDS=5*60;
28: const time_t CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
1.1 parser 29:
30: // helpers
31:
1.17 paf 32: static void expire_connection(Stylesheet_connection& connection, time_t older_dies) {
1.1 parser 33: if(connection.connected() && connection.expired(older_dies))
34: connection.disconnect();
35: }
1.17 paf 36: static void expire_connections(
37: Stylesheet_manager::connection_cache_type::key_type /*key*/,
38: Stylesheet_manager::connection_cache_type::value_type stack,
39: time_t older_dies) {
1.19 paf 40: for(size_t i=0; i<stack->top_index(); i++)
1.17 paf 41: expire_connection(*stack->get(i), older_dies);
1.1 parser 42: }
43:
1.17 paf 44: // Stylesheet_connection methods
1.1 parser 45:
1.17 paf 46: void Stylesheet_connection::close() {
47: stylesheet_manager.close_connection(ffile_spec, *this);
1.1 parser 48: }
49:
1.17 paf 50: // Stylesheet_manager methods
1.1 parser 51:
1.17 paf 52: Stylesheet_manager::Stylesheet_manager(): prev_expiration_pass_time(0) {
1.1 parser 53:
1.18 paf 54: cache_managers.put(String::Body("stylesheet"), this);
1.17 paf 55: }
56:
57: Stylesheet_manager::~Stylesheet_manager() {
58: connection_cache.for_each(expire_connections, time(0)+SECOND/*=in future=expire all*/);
59: }
60:
1.21 paf 61: Stylesheet_connection_ptr Stylesheet_manager::get_connection(String::Body file_spec) {
1.17 paf 62: Stylesheet_connection* result=get_connection_from_cache(file_spec);
63: if(!result)
64: result=new Stylesheet_connection(file_spec);
65: return Stylesheet_connection_ptr(result);
1.1 parser 66: }
67:
1.21 paf 68: void Stylesheet_manager::close_connection(String::Body file_spec, Stylesheet_connection& connection) {
1.1 parser 69: put_connection_to_cache(file_spec, connection);
70: }
71:
72: // stylesheet cache
73: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.21 paf 74: Stylesheet_connection* Stylesheet_manager::get_connection_from_cache(String::Body file_spec) {
1.1 parser 75: SYNCHRONIZED;
76:
1.17 paf 77: if(connection_cache_type::value_type connections=connection_cache.get(file_spec))
78: while(!connections->is_empty()) { // there are cached stylesheets to that 'file_spec'
79: Stylesheet_connection* result=connections->pop();
1.1 parser 80: if(result->connected()) // not expired?
81: return result;
82: }
83:
84: return 0;
85: }
86:
1.21 paf 87: void Stylesheet_manager::put_connection_to_cache(String::Body file_spec,
1.17 paf 88: Stylesheet_connection& connection) {
1.1 parser 89: SYNCHRONIZED;
90:
1.17 paf 91: connection_cache_type::value_type connections=connection_cache.get(file_spec);
1.1 parser 92: if(!connections) { // there are no cached stylesheets to that 'file_spec' yet?
1.17 paf 93: connections=new connection_cache_value_type;
1.1 parser 94: connection_cache.put(file_spec, connections);
95: }
96: connections->push(&connection);
97: }
98:
1.5 paf 99: void Stylesheet_manager::maybe_expire_cache() {
1.1 parser 100: time_t now=time(0);
101:
102: if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) {
1.17 paf 103: connection_cache.for_each(expire_connections, now-EXPIRE_UNUSED_CONNECTION_SECONDS);
1.1 parser 104:
105: prev_expiration_pass_time=now;
106: }
107: }
1.3 paf 108:
1.22 ! paf 109: static void add_connection_to_status_cache_table(Stylesheet_connection& connection, Table* table) {
1.3 paf 110: if(connection.connected()) {
1.22 ! paf 111: ArrayString& row=*new ArrayString;
1.3 paf 112:
1.4 paf 113: // file
1.22 ! paf 114: row+=new String(connection.file_spec(), String::L_AS_IS);
1.4 paf 115: // time
1.22 ! paf 116: time_t time_used=connection.get_time_used();
! 117: row+=new String(pa_strdup(ctime(&time_used)));
1.3 paf 118:
1.22 ! paf 119: *table+=&row;
1.3 paf 120: }
121: }
1.22 ! paf 122: static void add_connections_to_status_cache_table(
! 123: Stylesheet_manager::connection_cache_type::key_type /*key*/,
! 124: Stylesheet_manager::connection_cache_type::value_type stack, Table* table)
! 125: {
! 126: for(Array_iterator<Stylesheet_connection*> i(*stack); i.has_next(); )
! 127: add_connection_to_status_cache_table(*i.next(), table);
1.3 paf 128: }
1.22 ! paf 129:
1.17 paf 130: Value* Stylesheet_manager::get_status() {
131: VHash* result=new VHash;
1.2 paf 132: // cache
133: {
1.22 ! paf 134: ArrayString& columns=*new ArrayString;
1.17 paf 135: columns+=new String("file");
136: columns+=new String("time");
1.22 ! paf 137: Table& table=*new Table(&columns, connection_cache.count());
1.2 paf 138:
139: connection_cache.for_each(add_connections_to_status_cache_table, &table);
140:
1.22 ! paf 141: result->get_hash()->put(*new String("cache"), new VTable(&table));
1.2 paf 142: }
143: return result;
144: }
145:
1.1 parser 146: #endif
E-mail: