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