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

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.6! paf        10: static const char* IDENT_STYLESHEET_MANAGER_C="$Date: 2003/03/06 12:58:33 $";
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.6! paf        47: Stylesheet_manager::Stylesheet_manager(): prev_expiration_pass_time(0) {}
1.1       parser     48: Stylesheet_manager::~Stylesheet_manager() {
1.16.2.6! paf        49:        connection_cache.for_each(expire_connections, time(0)+SECOND/*=in future=expire all*/);
1.1       parser     50: }
                     51: 
1.16.2.6! paf        52: Stylesheet_connectionPtr Stylesheet_manager::get_connection(StringPtr file_spec) {
        !            53:        Stylesheet_connectionPtr result=get_connection_from_cache(file_spec);
        !            54:        if(!result)
        !            55:                result=Stylesheet_connectionPtr(new Stylesheet_connection(file_spec));
        !            56:        return result;
1.1       parser     57: }
                     58: 
1.16.2.5  paf        59: void Stylesheet_manager::close_connection(StringPtr file_spec, 
1.1       parser     60:                                                                                  Stylesheet_connection& connection) {
1.16.2.6! paf        61:        put_connection_to_cache(file_spec, Stylesheet_connectionPtr(&connection));
1.1       parser     62: }
                     63: 
                     64: // stylesheet cache
                     65: /// @todo get rid of memory spending Stack [zeros deep inside got accumulated]
1.16.2.6! paf        66: Stylesheet_connectionPtr Stylesheet_manager::get_connection_from_cache(StringPtr file_spec) { 
1.1       parser     67:        SYNCHRONIZED;
                     68: 
1.16.2.6! paf        69:        if(connection_cache_type::value_type connections=connection_cache.get(file_spec))
1.1       parser     70:                while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec'
1.16.2.6! paf        71:                        Stylesheet_connectionPtr result=connections->pop();
1.1       parser     72:                        if(result->connected()) // not expired?
                     73:                                return result;
                     74:                }
                     75: 
1.16.2.6! paf        76:        return Stylesheet_connectionPtr(0);
1.1       parser     77: }
                     78: 
1.16.2.5  paf        79: void Stylesheet_manager::put_connection_to_cache(StringPtr file_spec, 
1.16.2.6! paf        80:                                                                                                 Stylesheet_connectionPtr connection) { 
1.1       parser     81:        SYNCHRONIZED;
                     82: 
1.16.2.6! 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.16.2.6! paf        85:                connections=connection_cache_type::value_type(new connection_cache_type::value_type::element_type);
1.1       parser     86:                connection_cache.put(file_spec, connections);
                     87:        }       
1.16.2.6! paf        88:        connections->push(connection);
1.1       parser     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.16.2.6! paf        95:                connection_cache.for_each(expire_connections, now-EXPIRE_UNUSED_CONNECTION_SECONDS);
1.1       parser     96: 
                     97:                prev_expiration_pass_time=now;
                     98:        }
                     99: }
1.16.2.6! paf       100: /*
1.3       paf       101: static void add_connection_to_status_cache_table(Array::Item *value, void *info) {
                    102:        Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value);
                    103:        Table& table=*static_cast<Table *>(info);
                    104: 
                    105:        if(connection.connected()) {
                    106:                Pool& pool=table.pool();
1.4       paf       107:                Array& row=*new(pool) Array(pool);
1.3       paf       108: 
1.4       paf       109:                // file
1.3       paf       110:                row+=&connection.file_spec();
1.4       paf       111:                // time
1.3       paf       112:                time_t time_stamp=connection.get_time_used();
1.16.2.2  paf       113:                row+=new(pool) String(pool, pool.copy(ctime(&time_stamp)));
1.3       paf       114: 
                    115:                table+=&row;
                    116:        }
                    117: }
                    118: static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) {
                    119:        Stack& stack=*static_cast<Stack *>(value);
                    120:        Array_iter iter(stack);
                    121:        for(int countdown=stack.top_index(); countdown-->=0; )
                    122:                add_connection_to_status_cache_table(iter.next(), info);
                    123: }
1.16.2.6! paf       124: */
        !           125: ValuePtr Stylesheet_manager::get_status(Pool& pool, StringPtr source) {
        !           126:        VHashPtr result(new VHash);
        !           127: /*
1.2       paf       128:        // cache
                    129:        {
1.4       paf       130:                Array& columns=*new(pool) Array(pool);
1.3       paf       131:                columns+=new(pool) String(pool, "file");
1.2       paf       132:                columns+=new(pool) String(pool, "time");
                    133:                Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size());
                    134: 
                    135:                connection_cache.for_each(add_connections_to_status_cache_table, &table);
                    136: 
                    137:                result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table));
                    138:        }
1.16.2.6! paf       139: */
1.2       paf       140:        return result;
                    141: }
                    142: 
1.1       parser    143: #endif

E-mail: