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

1.1       parser      1: /** @file
                      2:        Parser: sql driver manager implementation.
                      3: 
1.16.2.1  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.16.2.7! paf        10: static const char* IDENT_STYLESHEET_MANAGER_C="$Date: 2003/03/06 15:32:37 $";
1.1       parser     11: 
                     12: #include "pa_stylesheet_manager.h"
                     13: #include "pa_stylesheet_connection.h"
                     14: #include "pa_exception.h"
                     15: #include "pa_common.h"
                     16: #include "pa_threads.h"
                     17: #include "pa_stack.h"
1.2       paf        18: #include "pa_vhash.h"
1.3       paf        19: #include "pa_vtable.h"
1.1       parser     20: 
                     21: // globals
                     22: 
1.16.2.5  paf        23: Stylesheet_manager stylesheet_manager;
1.1       parser     24: 
                     25: // consts
                     26: 
1.16.2.6  paf        27: const time_t SECOND=1;
                     28: const time_t EXPIRE_UNUSED_CONNECTION_SECONDS=5*60;
                     29: const time_t CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2;
1.1       parser     30: 
                     31: // helpers
                     32: 
1.16.2.6  paf        33: static void expire_connection(Stylesheet_connection& connection, time_t older_dies) {
1.1       parser     34:        if(connection.connected() && connection.expired(older_dies))
                     35:                connection.disconnect();
                     36: }
1.16.2.6  paf        37: static void expire_connections(
                     38:                                                           Stylesheet_manager::connection_cache_type::key_type /*key*/, 
                     39:                                                           Stylesheet_manager::connection_cache_type::value_type stack, 
                     40:                                                           time_t older_dies) {
                     41:        for(int i=0; i<=stack->top_index(); i++)
                     42:                expire_connection(*stack->get(i), older_dies);
1.1       parser     43: }
                     44: 
                     45: // Stylesheet_manager
                     46: 
1.16.2.7! paf        47: Stylesheet_manager::Stylesheet_manager(): prev_expiration_pass_time(0) {
        !            48: 
        !            49:        cache_managers.put(StringPtr(new String("stylesheet")), this);
        !            50: }
        !            51: 
1.1       parser     52: Stylesheet_manager::~Stylesheet_manager() {
1.16.2.6  paf        53:        connection_cache.for_each(expire_connections, time(0)+SECOND/*=in future=expire all*/);
1.1       parser     54: }
                     55: 
1.16.2.6  paf        56: Stylesheet_connectionPtr Stylesheet_manager::get_connection(StringPtr file_spec) {
                     57:        Stylesheet_connectionPtr result=get_connection_from_cache(file_spec);
                     58:        if(!result)
                     59:                result=Stylesheet_connectionPtr(new Stylesheet_connection(file_spec));
                     60:        return result;
1.1       parser     61: }
                     62: 
1.16.2.5  paf        63: void Stylesheet_manager::close_connection(StringPtr file_spec, 
1.1       parser     64:                                                                                  Stylesheet_connection& connection) {
1.16.2.6  paf        65:        put_connection_to_cache(file_spec, Stylesheet_connectionPtr(&connection));
1.1       parser     66: }
                     67: 
                     68: // stylesheet cache
                     69: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.16.2.6  paf        70: Stylesheet_connectionPtr Stylesheet_manager::get_connection_from_cache(StringPtr file_spec) { 
1.1       parser     71:        SYNCHRONIZED;
                     72: 
1.16.2.6  paf        73:        if(connection_cache_type::value_type connections=connection_cache.get(file_spec))
1.1       parser     74:                while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec'
1.16.2.6  paf        75:                        Stylesheet_connectionPtr result=connections->pop();
1.1       parser     76:                        if(result->connected()) // not expired?
                     77:                                return result;
                     78:                }
                     79: 
1.16.2.6  paf        80:        return Stylesheet_connectionPtr(0);
1.1       parser     81: }
                     82: 
1.16.2.5  paf        83: void Stylesheet_manager::put_connection_to_cache(StringPtr file_spec, 
1.16.2.6  paf        84:                                                                                                 Stylesheet_connectionPtr connection) { 
1.1       parser     85:        SYNCHRONIZED;
                     86: 
1.16.2.6  paf        87:        connection_cache_type::value_type connections=connection_cache.get(file_spec);
1.1       parser     88:        if(!connections) { // there are no cached stylesheets to that 'file_spec' yet?
1.16.2.6  paf        89:                connections=connection_cache_type::value_type(new connection_cache_type::value_type::element_type);
1.1       parser     90:                connection_cache.put(file_spec, connections);
                     91:        }       
1.16.2.6  paf        92:        connections->push(connection);
1.1       parser     93: }
                     94: 
1.5       paf        95: void Stylesheet_manager::maybe_expire_cache() {
1.1       parser     96:        time_t now=time(0);
                     97: 
                     98:        if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) {
1.16.2.6  paf        99:                connection_cache.for_each(expire_connections, now-EXPIRE_UNUSED_CONNECTION_SECONDS);
1.1       parser    100: 
                    101:                prev_expiration_pass_time=now;
                    102:        }
                    103: }
1.16.2.6  paf       104: /*
1.3       paf       105: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
                    106:        Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
                    107:        Table& table=*static_cast<Table *>(info);
                    108: 
                    109:        if(connection.connected()) {
                    110:                Pool& pool=table.pool();
1.4       paf       111:                Array& row=*new(pool) Array(pool);
1.3       paf       112: 
1.4       paf       113:                // file
1.3       paf       114:                row+=&connection.file_spec();
1.4       paf       115:                // time
1.3       paf       116:                time_t time_stamp=connection.get_time_used();
1.16.2.2  paf       117:                row+=new(pool) String(pool, pool.copy(ctime(&time_stamp)));
1.3       paf       118: 
                    119:                table+=&row;
                    120:        }
                    121: }
                    122: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
                    123:        Stack& stack=*static_cast<Stack *>(value);
                    124:        Array_iter iter(stack);
                    125:        for(int countdown=stack.top_index(); countdown-->=0; )
                    126:                add_connection_to_status_cache_table(iter.next(), info);
                    127: }
1.16.2.6  paf       128: */
                    129: ValuePtr Stylesheet_manager::get_status(Pool& pool, StringPtr source) {
                    130:        VHashPtr result(new VHash);
                    131: /*
1.2       paf       132:        // cache
                    133:        {
1.4       paf       134:                Array& columns=*new(pool) Array(pool);
1.3       paf       135:                columns+=new(pool) String(pool, "file");
1.2       paf       136:                columns+=new(pool) String(pool, "time");
                    137:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
                    138: 
                    139:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    140: 
                    141:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
                    142:        }
1.16.2.6  paf       143: */
1.2       paf       144:        return result;
                    145: }
                    146: 
1.1       parser    147: #endif

E-mail: