<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15473] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15473">15473</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-11-14 15:59:01 -0600 (Sat, 14 Nov 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>add cache_db handle api (odbc/sqlite abstraction)</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeswitch_coreh">freeswitch/trunk/src/include/switch_core.h</a></li>
<li><a href="#freeswitchtrunksrcincludeswitch_odbch">freeswitch/trunk/src/include/switch_odbc.h</a></li>
<li><a href="#freeswitchtrunksrcswitch_core_dbc">freeswitch/trunk/src/switch_core_db.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_core_sqldbc">freeswitch/trunk/src/switch_core_sqldb.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_odbcc">freeswitch/trunk/src/switch_odbc.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeswitch_coreh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_core.h (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_core.h        2009-11-14 21:00:51 UTC (rev 15472)
+++ freeswitch/trunk/src/include/switch_core.h        2009-11-14 21:59:01 UTC (rev 15473)
</span><span class="lines">@@ -116,6 +116,7 @@
</span><span class="cx"> struct switch_core_runtime;
</span><span class="cx"> struct switch_core_port_allocator;
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> /*!
</span><span class="cx"> \defgroup core1 Core Library
</span><span class="cx"> \ingroup FREESWITCH
</span><span class="lines">@@ -1403,7 +1404,7 @@
</span><span class="cx"> \param filename the path to the db file to open
</span><span class="cx"> \return the db handle
</span><span class="cx"> */
</span><del>-SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(char *filename);
</del><ins>+SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(const char *filename);
</ins><span class="cx">
</span><span class="cx"> /*!
</span><span class="cx"> \brief Execute a sql stmt until it is accepted
</span><span class="lines">@@ -1920,6 +1921,22 @@
</span><span class="cx"> \}
</span><span class="cx"> */
</span><span class="cx">
</span><ins>+typedef struct {
+        switch_core_db_t *db;
+        switch_odbc_handle_t *odbc_dbh;
+        time_t last_used;
+        switch_mutex_t *mutex;
+        switch_memory_pool_t *pool;
+} switch_cache_db_handle_t;
+
+SWITCH_DECLARE(void) switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh);
+SWITCH_DECLARE(switch_cache_db_handle_t *)switch_cache_db_get_db_handle(const char *db_name, const char *odbc_user, const char *odbc_pass);
+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql);
+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh, char *sql,
+                                                                                                                                         switch_core_db_callback_func_t callback, void *pdata);
+
+
+
</ins><span class="cx"> SWITCH_END_EXTERN_C
</span><span class="cx"> #endif
</span><span class="cx"> /* For Emacs:
</span></span></pre></div>
<a id="freeswitchtrunksrcincludeswitch_odbch"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_odbc.h (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_odbc.h        2009-11-14 21:00:51 UTC (rev 15472)
+++ freeswitch/trunk/src/include/switch_odbc.h        2009-11-14 21:59:01 UTC (rev 15473)
</span><span class="lines">@@ -51,7 +51,7 @@
</span><span class="cx">         SWITCH_ODBC_FAIL = -1
</span><span class="cx"> } switch_odbc_status_t;
</span><span class="cx">
</span><del>-SWITCH_DECLARE(switch_odbc_handle_t *) switch_odbc_handle_new(char *dsn, char *username, char *password);
</del><ins>+SWITCH_DECLARE(switch_odbc_handle_t *) switch_odbc_handle_new(const char *dsn, const char *username, const char *password);
</ins><span class="cx"> SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_disconnect(switch_odbc_handle_t *handle);
</span><span class="cx"> SWITCH_DECLARE(switch_odbc_status_t) switch_odbc_handle_connect(switch_odbc_handle_t *handle);
</span><span class="cx"> SWITCH_DECLARE(void) switch_odbc_handle_destroy(switch_odbc_handle_t **handlep);
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_core_dbc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core_db.c (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core_db.c        2009-11-14 21:00:51 UTC (rev 15472)
+++ freeswitch/trunk/src/switch_core_db.c        2009-11-14 21:59:01 UTC (rev 15473)
</span><span class="lines">@@ -195,12 +195,12 @@
</span><span class="cx">         return sqlite3_vmprintf(zFormat, ap);
</span><span class="cx"> }
</span><span class="cx">
</span><del>-SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(char *filename)
</del><ins>+SWITCH_DECLARE(switch_core_db_t *) switch_core_db_open_file(const char *filename)
</ins><span class="cx"> {
</span><span class="cx">         switch_core_db_t *db;
</span><span class="cx">         char path[1024];
</span><span class="cx">
</span><del>-        db_pick_path(filename, path, sizeof(path));
</del><ins>+        db_pick_path((char *)filename, path, sizeof(path));
</ins><span class="cx">         if (switch_core_db_open(path, &db)) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n", switch_core_db_errmsg(db));
</span><span class="cx">                 switch_core_db_close(db);
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_core_sqldbc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_core_sqldb.c (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_core_sqldb.c        2009-11-14 21:00:51 UTC (rev 15472)
+++ freeswitch/trunk/src/switch_core_sqldb.c        2009-11-14 21:59:01 UTC (rev 15473)
</span><span class="lines">@@ -45,6 +45,180 @@
</span><span class="cx">         int thread_running;
</span><span class="cx"> } sql_manager;
</span><span class="cx">
</span><ins>+static switch_mutex_t *dbh_mutex = NULL;
+static switch_hash_t *dbh_hash = NULL;
+
+#define SQL_CACHE_TIMEOUT 300
+
+static void sql_close(time_t prune)
+{
+        switch_hash_index_t *hi;
+        const void *var;
+        void *val;
+        switch_cache_db_handle_t *dbh = NULL;
+        int locked = 0;
+        char *key;
+
+        switch_mutex_lock(dbh_mutex);
+ top:
+        locked = 0;
+        
+        for (hi = switch_hash_first(NULL, dbh_hash); hi; hi = switch_hash_next(hi)) {
+                switch_hash_this(hi, &var, NULL, &val);
+                key = (char *) var;
+
+                if ((dbh = (switch_cache_db_handle_t *) val)) {
+                        time_t diff = 0;
+
+                        if (prune > 0 && prune > dbh->last_used) {
+                                diff = (time_t) prune - dbh->last_used;
+                        }
+                        
+                        if (diff < SQL_CACHE_TIMEOUT) {
+                                continue;
+                        }
+
+                        if (switch_mutex_trylock(dbh->mutex) == SWITCH_STATUS_SUCCESS) {
+                                if (dbh->db) {
+                                        switch_core_db_close(dbh->db);
+                                        dbh->db = NULL;
+                                } else if (switch_odbc_available() && dbh->odbc_dbh) {
+                                        switch_odbc_handle_destroy(&dbh->odbc_dbh);
+                                }
+
+                                switch_core_hash_delete(dbh_hash, key);
+                                switch_mutex_unlock(dbh->mutex);
+                                switch_core_destroy_memory_pool(&dbh->pool);
+                                goto top;
+        
+                        } else {
+                                if (!prune) locked++;
+                                continue;
+                        }
+                }
+        }
+
+        if (locked) {
+                goto top;
+        }
+
+        switch_mutex_unlock(dbh_mutex);
+}
+
+SWITCH_DECLARE(void) switch_cache_db_release_db_handle(switch_cache_db_handle_t **dbh)
+{
+        if (dbh && *dbh) {
+                switch_mutex_unlock((*dbh)->mutex);
+                *dbh = NULL;
+        }
+}
+
+SWITCH_DECLARE(switch_cache_db_handle_t *)switch_cache_db_get_db_handle(const char *db_name, const char *odbc_user, const char *odbc_pass)
+{
+        switch_thread_id_t self = switch_thread_self();
+        char thread_str[256] = "";
+        switch_cache_db_handle_t *dbh = NULL;
+        
+        switch_assert(db_name);
+
+        snprintf(thread_str, sizeof(thread_str) - 1, "%s_%lu", db_name, (unsigned long)(intptr_t)self);
+
+        switch_mutex_lock(dbh_mutex);
+        if (!(dbh = switch_core_hash_find(dbh_hash, thread_str))) {
+                switch_memory_pool_t *pool = NULL;
+                switch_core_db_t *db = NULL;
+                switch_odbc_handle_t *odbc_dbh = NULL;
+
+                if (switch_odbc_available() && db_name && odbc_user && odbc_pass) {
+                        if ((odbc_dbh = switch_odbc_handle_new(db_name, odbc_user, odbc_pass))) {
+                                if (switch_odbc_handle_connect(odbc_dbh) != SWITCH_STATUS_SUCCESS) {
+                                        switch_odbc_handle_destroy(&odbc_dbh);
+                                }
+                        }
+                } else {
+                        db = switch_core_db_open_file(db_name);
+                }
+
+                if (!db && !odbc_dbh) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Failure!\n");
+                        goto end;
+                }
+
+                switch_core_new_memory_pool(&pool);
+                dbh = switch_core_alloc(pool, sizeof(*dbh));
+                dbh->pool = pool;
+
+                
+                if (db) dbh->db = db; else dbh->odbc_dbh = odbc_dbh;
+                switch_mutex_init(&dbh->mutex, SWITCH_MUTEX_NESTED, dbh->pool);
+                switch_mutex_lock(dbh->mutex);
+
+                switch_core_hash_insert(dbh_hash, thread_str, dbh);
+        }
+
+ end:
+
+        if (dbh) dbh->last_used = switch_epoch_time_now(NULL);
+
+        switch_mutex_unlock(dbh_mutex);
+
+        return dbh;
+}
+
+
+
+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql(switch_cache_db_handle_t *dbh, char *sql)
+{
+        switch_status_t status = SWITCH_STATUS_FALSE;
+
+        if (switch_odbc_available() && dbh->odbc_dbh) {
+                switch_odbc_statement_handle_t stmt;
+                if ((status = switch_odbc_handle_exec(dbh->odbc_dbh, sql, &stmt)) != SWITCH_ODBC_SUCCESS) {
+                        char *err_str;
+                        err_str = switch_odbc_handle_get_error(dbh->odbc_dbh, stmt);
+                        if (!zstr(err_str)) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ERR: [%s]\n[%s]\n", sql, err_str);
+                        }
+                        switch_safe_free(err_str);
+                }
+                switch_odbc_statement_handle_free(&stmt);
+        } else {
+                char *errmsg;
+
+ status = switch_core_db_exec(dbh->db, sql, NULL, NULL, &errmsg);
+
+ if (errmsg) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR [%s]\n%s\n", errmsg, sql);
+ switch_core_db_free(errmsg);
+ }
+                
+        }
+
+        return status;
+
+}
+
+SWITCH_DECLARE(switch_status_t) switch_cache_db_execute_sql_callback(switch_cache_db_handle_t *dbh,
+                                                                                                                                         char *sql, switch_core_db_callback_func_t callback, void *pdata)
+        
+{
+        switch_status_t status = SWITCH_STATUS_FALSE;
+        char *errmsg = NULL;
+        
+        if (switch_odbc_available() && dbh->odbc_dbh) {
+                status = switch_odbc_handle_callback_exec(dbh->odbc_dbh, sql, callback, pdata);
+        } else {
+                status = switch_core_db_exec(dbh->db, sql, callback, pdata, &errmsg);
+                
+                if (errmsg) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "SQL ERR: [%s] %s\n", sql, errmsg);
+                        free(errmsg);
+                }
+        }
+
+        return status;
+}
+
</ins><span class="cx"> static switch_status_t switch_core_db_persistant_execute_trans(switch_core_db_t *db, char *sql, uint32_t retries)
</span><span class="cx"> {
</span><span class="cx">         char *errmsg;
</span><span class="lines">@@ -147,6 +321,12 @@
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+
+
+
+
+
+
</ins><span class="cx"> #define SQLLEN 1024 * 64
</span><span class="cx"> static void *SWITCH_THREAD_FUNC switch_core_sql_thread(switch_thread_t * thread, void *obj)
</span><span class="cx"> {
</span><span class="lines">@@ -159,7 +339,8 @@
</span><span class="cx">         char *sql;
</span><span class="cx">         switch_size_t newlen;
</span><span class="cx">         int lc = 0;
</span><del>-        
</del><ins>+        uint32_t loops = 0, sec = 0;
+
</ins><span class="cx">         switch_assert(sqlbuf);
</span><span class="cx">
</span><span class="cx">         if (!sql_manager.event_db) {
</span><span class="lines">@@ -169,6 +350,14 @@
</span><span class="cx">         sql_manager.thread_running = 1;
</span><span class="cx">
</span><span class="cx">         for (;;) {
</span><ins>+                if (++loops == 1000) {
+                        if (++sec == SQL_CACHE_TIMEOUT) {
+                                sql_close(switch_epoch_time_now(NULL));
+                                sec = 0;
+                        }
+                        loops = 0;
+                }
+
</ins><span class="cx">                 if (switch_queue_trypop(sql_manager.sql_queue[0], &pop) == SWITCH_STATUS_SUCCESS ||
</span><span class="cx">                         switch_queue_trypop(sql_manager.sql_queue[1], &pop) == SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                         sql = (char *) pop;
</span><span class="lines">@@ -446,6 +635,9 @@
</span><span class="cx">
</span><span class="cx">         sql_manager.memory_pool = pool;
</span><span class="cx">
</span><ins>+        switch_mutex_init(&dbh_mutex, SWITCH_MUTEX_NESTED, sql_manager.memory_pool);
+        switch_core_hash_init(&dbh_hash, sql_manager.memory_pool);
+
</ins><span class="cx">         /* Activate SQL database */
</span><span class="cx">         if ((sql_manager.db = switch_core_db_handle()) == 0) {
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB!\n");
</span><span class="lines">@@ -597,10 +789,13 @@
</span><span class="cx">
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Waiting for unfinished SQL transactions\n");
</span><span class="cx">         switch_thread_join(&st, sql_manager.thread);
</span><ins>+        sql_close(0);
</ins><span class="cx">
</span><span class="cx">         switch_core_db_close(sql_manager.db);
</span><span class="cx">         switch_core_db_close(sql_manager.event_db);
</span><span class="cx">
</span><ins>+        switch_core_hash_destroy(&dbh_hash);
+
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /* For Emacs:
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_odbcc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_odbc.c (15472 => 15473)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_odbc.c        2009-11-14 21:00:51 UTC (rev 15472)
+++ freeswitch/trunk/src/switch_odbc.c        2009-11-14 21:59:01 UTC (rev 15473)
</span><span class="lines">@@ -59,7 +59,7 @@
</span><span class="cx"> };
</span><span class="cx"> #endif
</span><span class="cx">
</span><del>-SWITCH_DECLARE(switch_odbc_handle_t *) switch_odbc_handle_new(char *dsn, char *username, char *password)
</del><ins>+SWITCH_DECLARE(switch_odbc_handle_t *) switch_odbc_handle_new(const char *dsn, const char *username, const char *password)
</ins><span class="cx"> {
</span><span class="cx"> #ifdef SWITCH_HAVE_ODBC
</span><span class="cx">         switch_odbc_handle_t *new_handle;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>