Annotation of parser3/src/main/pa_stylesheet_manager.C, revision 1.39

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

E-mail: