|
|
| version 1.6, 2001/11/08 14:47:32 | version 1.28, 2006/04/09 13:38:47 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: sql driver manager implementation. | Parser: sql driver manager implementation. |
| Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| $Id$ | |
| */ | */ |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #ifdef XML | #ifdef XML |
| static const char * const IDENT_STYLESHEET_MANAGER_C="$Date$"; | |
| #include "pa_stylesheet_manager.h" | #include "pa_stylesheet_manager.h" |
| #include "pa_stylesheet_connection.h" | |
| #include "ltdl.h" | |
| #include "pa_exception.h" | #include "pa_exception.h" |
| #include "pa_common.h" | #include "pa_common.h" |
| #include "pa_threads.h" | #include "pa_threads.h" |
| Line 21 | Line 19 |
| // globals | // globals |
| Stylesheet_manager *stylesheet_manager; | Stylesheet_manager* stylesheet_manager=0; |
| // consts | // consts |
| const int EXPIRE_UNUSED_CONNECTION_SECONDS=5*60; | const time_t EXPIRE_UNUSED_CONNECTION_SECONDS=5*60; |
| const int CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2; | const time_t CHECK_EXPIRED_CONNECTION_SECONDS=EXPIRE_UNUSED_CONNECTION_SECONDS*2; |
| // helpers | // helpers |
| static void expire_connection(Array::Item *value, void *info) { | static void expire_connection(Stylesheet_connection& connection, time_t older_dies) { |
| Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value); | |
| time_t older_dies=reinterpret_cast<time_t>(info); | |
| if(connection.connected() && connection.expired(older_dies)) | if(connection.connected() && connection.expired(older_dies)) |
| connection.disconnect(); | connection.disconnect(); |
| } | } |
| static void expire_connections(const Hash::Key& key, Hash::Val *value, void *info) { | static void expire_connections( |
| Stack& stack=*static_cast<Stack *>(value); | Stylesheet_manager::connection_cache_type::key_type /*key*/, |
| for(int i=0; i<=stack.top_index(); i++) | Stylesheet_manager::connection_cache_type::value_type stack, |
| expire_connection(stack.get(i), info); | time_t older_dies) { |
| for(size_t i=0; i<stack->top_index(); i++) | |
| expire_connection(*stack->get(i), older_dies); | |
| } | } |
| // Stylesheet_manager | // Stylesheet_connection methods |
| Stylesheet_manager::Stylesheet_manager(Pool& apool) : Cache_manager(apool), | void Stylesheet_connection::close() { |
| connection_cache(apool), | stylesheet_manager->close_connection(ffile_spec, *this); |
| prev_expiration_pass_time(0) { | |
| } | |
| Stylesheet_manager::~Stylesheet_manager() { | |
| connection_cache.for_each(expire_connections, | |
| reinterpret_cast<void *>((time_t)0/*=in past=expire all*/)); | |
| } | } |
| Stylesheet_connection& Stylesheet_manager::get_connection(const String& request_file_spec) { | // Stylesheet_manager methods |
| Pool& pool=request_file_spec.pool(); // request pool | |
| // first trying to get cached stylesheet | Stylesheet_manager::Stylesheet_manager(): prev_expiration_pass_time(0) { |
| Stylesheet_connection *result=get_connection_from_cache(request_file_spec); | } |
| if(!result) { | |
| // then just construct it | |
| // make global_file_spec C-string on global pool | |
| const char *request_file_spec_cstr=request_file_spec.cstr(String::UL_FILE_SPEC); | |
| char *global_file_spec_cstr=(char *)malloc(strlen(request_file_spec_cstr)+1); | |
| strcpy(global_file_spec_cstr, request_file_spec_cstr); | |
| // make global_file_spec string on global pool | |
| String& global_file_spec=*new(this->pool()) String(this->pool(), global_file_spec_cstr); | |
| result=new(this->pool()) Stylesheet_connection(this->pool(), global_file_spec); | Stylesheet_manager::~Stylesheet_manager() { |
| } | connection_cache.for_each<time_t>(expire_connections, time(0)+(time_t)10/*=in future=expire all*/); |
| // associate with services[request] (deassociates at close) | |
| result->set_services(&pool); | |
| // return it | |
| return *result; | |
| } | } |
| void Stylesheet_manager::close_connection(const String& file_spec, | Stylesheet_connection_ptr Stylesheet_manager::get_connection(String::Body file_spec) { |
| Stylesheet_connection& connection) { | Stylesheet_connection* result=get_connection_from_cache(file_spec); |
| // deassociate from services[request] | if(!result) |
| connection.set_services(0); | result=new Stylesheet_connection(file_spec); |
| put_connection_to_cache(file_spec, connection); | return Stylesheet_connection_ptr(result); |
| } | } |
| void Stylesheet_manager::close_connection(String::Body file_spec, Stylesheet_connection& connection) { | |
| put_connection_to_cache(file_spec, connection); | |
| } | |
| // stylesheet cache | // stylesheet cache |
| /// @todo get rid of memory spending Stack [zeros deep inside got accumulated] | /// @todo get rid of memory spending Stack [zeros deep inside got accumulated] |
| Stylesheet_connection *Stylesheet_manager::get_connection_from_cache(const String& file_spec) { | Stylesheet_connection* Stylesheet_manager::get_connection_from_cache(String::Body file_spec) { |
| SYNCHRONIZED; | SYNCHRONIZED; |
| if(Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec))) | if(connection_cache_type::value_type connections=connection_cache.get(file_spec)) |
| while(connections->top_index()>=0) { // there are cached stylesheets to that 'file_spec' | while(!connections->is_empty()) { // there are cached stylesheets to that 'file_spec' |
| Stylesheet_connection *result=static_cast<Stylesheet_connection *>(connections->pop()); | Stylesheet_connection* result=connections->pop(); |
| if(result->connected()) // not expired? | if(result->connected()) // not expired? |
| return result; | return result; |
| } | } |
| Line 100 Stylesheet_connection *Stylesheet_manage | Line 81 Stylesheet_connection *Stylesheet_manage |
| return 0; | return 0; |
| } | } |
| void Stylesheet_manager::put_connection_to_cache(const String& file_spec, | void Stylesheet_manager::put_connection_to_cache(String::Body file_spec, |
| Stylesheet_connection& connection) { | Stylesheet_connection& connection) { |
| SYNCHRONIZED; | SYNCHRONIZED; |
| Stack *connections=static_cast<Stack *>(connection_cache.get(file_spec)); | connection_cache_type::value_type connections=connection_cache.get(file_spec); |
| if(!connections) { // there are no cached stylesheets to that 'file_spec' yet? | if(!connections) { // there are no cached stylesheets to that 'file_spec' yet? |
| connections=NEW Stack(pool()); // NOTE: never freed up! | connections=new connection_cache_value_type; |
| connection_cache.put(file_spec, connections); | connection_cache.put(file_spec, connections); |
| } | } |
| connections->push(&connection); | connections->push(&connection); |
| Line 116 void Stylesheet_manager::maybe_expire_ca | Line 97 void Stylesheet_manager::maybe_expire_ca |
| time_t now=time(0); | time_t now=time(0); |
| if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) { | if(prev_expiration_pass_time<now-CHECK_EXPIRED_CONNECTION_SECONDS) { |
| connection_cache.for_each(expire_connections, | connection_cache.for_each<time_t>(expire_connections, now-EXPIRE_UNUSED_CONNECTION_SECONDS); |
| reinterpret_cast<void *>(now-EXPIRE_UNUSED_CONNECTION_SECONDS)); | |
| prev_expiration_pass_time=now; | prev_expiration_pass_time=now; |
| } | } |
| } | } |
| static void add_connection_to_status_cache_table(Array::Item *value, void *info) { | static void add_connection_to_status_cache_table(Stylesheet_connection& connection, Table* table) { |
| Stylesheet_connection& connection=*static_cast<Stylesheet_connection *>(value); | |
| Table& table=*static_cast<Table *>(info); | |
| if(connection.connected()) { | if(connection.connected()) { |
| Pool& pool=table.pool(); | ArrayString& row=*new ArrayString; |
| Array& row=*new(pool) Array(pool); | |
| // file | // file |
| row+=&connection.file_spec(); | row+=new String(connection.file_spec(), String::L_AS_IS); |
| // time | // time |
| time_t time_stamp=connection.get_time_used(); | time_t time_used=connection.get_time_used(); |
| const char *unsafe_time_cstr=ctime(&time_stamp); | row+=new String(pa_strdup(ctime(&time_used))); |
| int time_buf_size=strlen(unsafe_time_cstr); | |
| char *safe_time_buf=(char *)pool.malloc(time_buf_size); | |
| memcpy(safe_time_buf, unsafe_time_cstr, time_buf_size); | |
| row+=new(pool) String(pool, safe_time_buf, time_buf_size); | |
| table+=&row; | *table+=&row; |
| } | } |
| } | } |
| static void add_connections_to_status_cache_table(const Hash::Key& key, Hash::Val *value, void *info) { | static void add_connections_to_status_cache_table( |
| Stack& stack=*static_cast<Stack *>(value); | Stylesheet_manager::connection_cache_type::key_type /*key*/, |
| Array_iter iter(stack); | Stylesheet_manager::connection_cache_type::value_type stack, Table* table) |
| for(int countdown=stack.top_index(); countdown-->=0; ) | { |
| add_connection_to_status_cache_table(iter.next(), info); | for(Array_iterator<Stylesheet_connection*> i(*stack); i.has_next(); ) |
| add_connection_to_status_cache_table(*i.next(), table); | |
| } | } |
| Value& Stylesheet_manager::get_status(Pool& pool, const String *source) { | Value* Stylesheet_manager::get_status() { |
| VHash& result=*new(pool) VHash(pool); | VHash* result=new VHash; |
| // cache | // cache |
| { | { |
| Array& columns=*new(pool) Array(pool); | ArrayString& columns=*new ArrayString; |
| columns+=new(pool) String(pool, "file"); | columns+=new String("file"); |
| columns+=new(pool) String(pool, "time"); | columns+=new String("time"); |
| Table& table=*new(pool) Table(pool, 0, &columns, connection_cache.size()); | Table& table=*new Table(&columns, connection_cache.count()); |
| connection_cache.for_each(add_connections_to_status_cache_table, &table); | connection_cache.for_each<Table*>(add_connections_to_status_cache_table, &table); |
| result.hash(source).put(*new(pool) String(pool, "cache"), new(pool) VTable(pool, &table)); | result->get_hash()->put(*new String("cache"), new VTable(&table)); |
| } | } |
| return result; | return result; |
| } | } |