switch_core_hash.c File Reference

#include <switch.h>
#include "private/switch_core_pvt.h"
#include <sqlite3.h>
#include "../../../libs/sqlite/src/hash.h"

Include dependency graph for switch_core_hash.c:

Go to the source code of this file.

Data Structures

struct  switch_hash

Functions

switch_status_t switch_core_hash_init_case (switch_hash_t **hash, switch_memory_pool_t *pool, switch_bool_t case_sensitive)
switch_status_t switch_core_hash_destroy (switch_hash_t **hash)
switch_status_t switch_core_hash_insert (switch_hash_t *hash, const char *key, const void *data)
switch_status_t switch_core_hash_insert_locked (switch_hash_t *hash, const char *key, const void *data, switch_mutex_t *mutex)
switch_status_t switch_core_hash_insert_wrlock (switch_hash_t *hash, const char *key, const void *data, switch_thread_rwlock_t *rwlock)
 Retrieve data from a given hash.
switch_status_t switch_core_hash_delete (switch_hash_t *hash, const char *key)
switch_status_t switch_core_hash_delete_locked (switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
switch_status_t switch_core_hash_delete_wrlock (switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
switch_status_t switch_core_hash_delete_multi (switch_hash_t *hash, switch_hash_delete_callback_t callback, void *pData)
void * switch_core_hash_find (switch_hash_t *hash, const char *key)
void * switch_core_hash_find_locked (switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
void * switch_core_hash_find_rdlock (switch_hash_t *hash, const char *key, switch_thread_rwlock_t *rwlock)
switch_hash_index_tswitch_hash_first (char *deprecate_me, switch_hash_t *hash)
switch_hash_index_tswitch_hash_next (switch_hash_index_t *hi)
void switch_hash_this (switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)


Function Documentation

switch_status_t switch_core_hash_delete ( switch_hash_t hash,
const char *  key 
)

Definition at line 114 of file switch_core_hash.c.

References SWITCH_STATUS_SUCCESS.

00115 {
00116         sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, NULL);
00117         return SWITCH_STATUS_SUCCESS;
00118 }

switch_status_t switch_core_hash_delete_locked ( switch_hash_t hash,
const char *  key,
switch_mutex_t mutex 
)

Definition at line 120 of file switch_core_hash.c.

References switch_mutex_lock(), switch_mutex_unlock(), and SWITCH_STATUS_SUCCESS.

00121 {
00122         if (mutex) {
00123                 switch_mutex_lock(mutex);
00124         }
00125 
00126         sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, NULL);
00127 
00128         if (mutex) {
00129                 switch_mutex_unlock(mutex);
00130         }
00131 
00132         return SWITCH_STATUS_SUCCESS;
00133 }

switch_status_t switch_core_hash_delete_multi ( switch_hash_t hash,
switch_hash_delete_callback_t  callback,
void *  pData 
)

Definition at line 150 of file switch_core_hash.c.

References switch_event::headers, switch_event_header::next, switch_assert, switch_core_hash_delete(), switch_event_add_header_string(), SWITCH_EVENT_CLONE, switch_event_create_subclass, switch_event_destroy(), switch_hash_first(), switch_hash_next(), switch_hash_this(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, and switch_event_header::value.

00150                                                                                                                                         {
00151 
00152         switch_hash_index_t *hi = NULL;
00153         switch_event_t *event = NULL;
00154         switch_event_header_t *header = NULL;
00155         switch_status_t status = SWITCH_STATUS_GENERR;
00156         
00157         switch_event_create_subclass(&event, SWITCH_EVENT_CLONE, NULL);
00158         switch_assert(event);
00159         
00160         /* iterate through the hash, call callback, if callback returns true, put the key on the list (event)
00161            When done, iterate through the list deleting hash entries
00162          */
00163         
00164         for (hi = switch_hash_first(NULL, hash); hi; hi = switch_hash_next(hi)) {
00165                 const void *key;
00166                 void *val;
00167                 switch_hash_this(hi, &key, NULL, &val);
00168                 if (callback(key, val, pData)) {
00169                         switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
00170                 }
00171         }
00172         
00173         /* now delete them */
00174         for (header = event->headers; header; header = header->next) {
00175                 if (switch_core_hash_delete(hash, header->value) == SWITCH_STATUS_SUCCESS) {
00176                         status = SWITCH_STATUS_SUCCESS;
00177                 }
00178         }
00179 
00180         switch_event_destroy(&event);
00181         
00182         return status;
00183 }

switch_status_t switch_core_hash_delete_wrlock ( switch_hash_t hash,
const char *  key,
switch_thread_rwlock_t rwlock 
)

Definition at line 135 of file switch_core_hash.c.

References SWITCH_STATUS_SUCCESS, switch_thread_rwlock_unlock(), and switch_thread_rwlock_wrlock().

00136 {
00137         if (rwlock) {
00138                 switch_thread_rwlock_wrlock(rwlock);
00139         }
00140 
00141         sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, NULL);
00142 
00143         if (rwlock) {
00144                 switch_thread_rwlock_unlock(rwlock);
00145         }
00146 
00147         return SWITCH_STATUS_SUCCESS;
00148 }

switch_status_t switch_core_hash_destroy ( switch_hash_t **  hash  ) 

Definition at line 64 of file switch_core_hash.c.

References switch_assert, and SWITCH_STATUS_SUCCESS.

00065 {
00066         switch_assert(hash != NULL && *hash != NULL);
00067         sqlite3HashClear(&(*hash)->table);
00068 
00069         if (!(*hash)->pool) {
00070                 free(*hash);
00071         }
00072 
00073         *hash = NULL;
00074 
00075         return SWITCH_STATUS_SUCCESS;
00076 }

void* switch_core_hash_find ( switch_hash_t hash,
const char *  key 
)

Definition at line 186 of file switch_core_hash.c.

00187 {
00188         return sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);
00189 }

void* switch_core_hash_find_locked ( switch_hash_t hash,
const char *  key,
switch_mutex_t mutex 
)

Definition at line 191 of file switch_core_hash.c.

References switch_mutex_lock(), and switch_mutex_unlock().

00192 {
00193         void *val;
00194 
00195         if (mutex) {
00196                 switch_mutex_lock(mutex);
00197         }
00198 
00199         val = sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);
00200 
00201         if (mutex) {
00202                 switch_mutex_unlock(mutex);
00203         }
00204 
00205         return val;
00206 }

void* switch_core_hash_find_rdlock ( switch_hash_t hash,
const char *  key,
switch_thread_rwlock_t rwlock 
)

Definition at line 208 of file switch_core_hash.c.

References switch_thread_rwlock_rdlock(), and switch_thread_rwlock_unlock().

00209 {
00210         void *val;
00211 
00212         if (rwlock) {
00213                 switch_thread_rwlock_rdlock(rwlock);
00214         }
00215 
00216         val = sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);
00217 
00218         if (rwlock) {
00219                 switch_thread_rwlock_unlock(rwlock);
00220         }
00221 
00222         return val;
00223 }

switch_status_t switch_core_hash_init_case ( switch_hash_t **  hash,
switch_memory_pool_t pool,
switch_bool_t  case_sensitive 
)

Definition at line 45 of file switch_core_hash.c.

References switch_assert, switch_core_alloc, SWITCH_STATUS_SUCCESS, switch_zmalloc, and switch_hash::table.

00046 {
00047         switch_hash_t *newhash;
00048 
00049         if (pool) {
00050                 newhash = switch_core_alloc(pool, sizeof(*newhash));
00051                 newhash->pool = pool;
00052         } else {
00053                 switch_zmalloc(newhash, sizeof(*newhash));
00054         }
00055 
00056         switch_assert(newhash);
00057 
00058         sqlite3HashInit(&newhash->table, case_sensitive ? SQLITE_HASH_BINARY : SQLITE_HASH_STRING, 1);
00059         *hash = newhash;
00060 
00061         return SWITCH_STATUS_SUCCESS;
00062 }

switch_status_t switch_core_hash_insert ( switch_hash_t hash,
const char *  key,
const void *  data 
)

Definition at line 78 of file switch_core_hash.c.

References SWITCH_STATUS_SUCCESS.

00079 {
00080         sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, (void *) data);
00081         return SWITCH_STATUS_SUCCESS;
00082 }

switch_status_t switch_core_hash_insert_locked ( switch_hash_t hash,
const char *  key,
const void *  data,
switch_mutex_t mutex 
)

Definition at line 84 of file switch_core_hash.c.

References switch_mutex_lock(), switch_mutex_unlock(), and SWITCH_STATUS_SUCCESS.

00085 {
00086         if (mutex) {
00087                 switch_mutex_lock(mutex);
00088         }
00089 
00090         sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, (void *) data);
00091 
00092         if (mutex) {
00093                 switch_mutex_unlock(mutex);
00094         }
00095 
00096         return SWITCH_STATUS_SUCCESS;
00097 }

switch_hash_index_t* switch_hash_first ( char *  deprecate_me,
switch_hash_t hash 
)

Definition at line 225 of file switch_core_hash.c.

00226 {
00227         return (switch_hash_index_t *) sqliteHashFirst(&hash->table);
00228 }

switch_hash_index_t* switch_hash_next ( switch_hash_index_t hi  ) 

Definition at line 230 of file switch_core_hash.c.

00231 {
00232         return (switch_hash_index_t *) sqliteHashNext((HashElem *) hi);
00233 }

void switch_hash_this ( switch_hash_index_t hi,
const void **  key,
switch_ssize_t klen,
void **  val 
)

Definition at line 235 of file switch_core_hash.c.

00236 {
00237         if (key) {
00238                 *key = sqliteHashKey((HashElem *) hi);
00239                 if (klen) {
00240                         *klen = strlen((char *) *key) + 1;
00241                 }
00242         }
00243         if (val) {
00244                 *val = sqliteHashData((HashElem *) hi);
00245         }
00246 }


Generated on Sun May 20 04:00:09 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7