2007-03-29 22:34:40 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Michael Jerris <mike@jerris.com>
|
|
|
|
* Paul D. Tinsley <pdt at jackhammer.org>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_core_hash.c -- Main Core Library (hash functions)
|
|
|
|
*
|
|
|
|
*/
|
2008-01-27 17:36:53 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
#include <switch.h>
|
2007-05-14 17:10:46 +00:00
|
|
|
#include "private/switch_core_pvt.h"
|
2007-09-29 01:06:08 +00:00
|
|
|
#include <sqlite3.h>
|
|
|
|
#include "../../../libs/sqlite/src/hash.h"
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
struct switch_hash {
|
|
|
|
Hash table;
|
|
|
|
};
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2008-11-05 00:20:30 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_init_case(switch_hash_t **hash, switch_memory_pool_t *pool, switch_bool_t case_sensitive)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2007-09-29 01:06:08 +00:00
|
|
|
switch_hash_t *newhash;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
newhash = switch_core_alloc(pool, sizeof(*newhash));
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(newhash);
|
2007-03-29 22:34:40 +00:00
|
|
|
|
2008-11-05 00:20:30 +00:00
|
|
|
sqlite3HashInit(&newhash->table, case_sensitive ? SQLITE_HASH_BINARY : SQLITE_HASH_STRING, 1);
|
2007-09-29 01:06:08 +00:00
|
|
|
*hash = newhash;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_destroy(switch_hash_t **hash)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(hash != NULL && *hash != NULL);
|
2007-09-29 01:06:08 +00:00
|
|
|
sqlite3HashClear(&(*hash)->table);
|
|
|
|
*hash = NULL;
|
2007-05-03 16:28:23 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert(switch_hash_t *hash, const char *key, const void *data)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-05-27 04:30:03 +00:00
|
|
|
sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, (void *) data);
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_insert_locked(switch_hash_t *hash, const char *key, const void *data, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
2007-09-29 01:06:08 +00:00
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, (void *) data);
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete(switch_hash_t *hash, const char *key)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-05-27 04:30:03 +00:00
|
|
|
sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, NULL);
|
2007-03-29 22:34:40 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_core_hash_delete_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3HashInsert(&hash->table, key, (int) strlen(key) + 1, NULL);
|
|
|
|
|
2007-05-03 16:28:23 +00:00
|
|
|
if (mutex) {
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_core_hash_find(switch_hash_t *hash, const char *key)
|
2007-03-29 22:34:40 +00:00
|
|
|
{
|
2008-05-27 04:30:03 +00:00
|
|
|
return sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);
|
2007-03-29 22:34:40 +00:00
|
|
|
}
|
2007-05-03 16:28:23 +00:00
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
SWITCH_DECLARE(void *) switch_core_hash_find_locked(switch_hash_t *hash, const char *key, switch_mutex_t *mutex)
|
2007-05-03 16:28:23 +00:00
|
|
|
{
|
|
|
|
void *val;
|
|
|
|
|
|
|
|
if (mutex) {
|
|
|
|
switch_mutex_lock(mutex);
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
val = sqlite3HashFind(&hash->table, key, (int) strlen(key) + 1);
|
|
|
|
|
2007-05-03 16:28:23 +00:00
|
|
|
if (mutex) {
|
|
|
|
switch_mutex_unlock(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
2007-09-29 01:06:08 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_hash_first(char *depricate_me, switch_hash_t *hash)
|
|
|
|
{
|
|
|
|
return (switch_hash_index_t *) sqliteHashFirst(&hash->table);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_hash_index_t *) switch_hash_next(switch_hash_index_t *hi)
|
|
|
|
{
|
|
|
|
return (switch_hash_index_t *) sqliteHashNext((HashElem *) hi);
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_hash_this(switch_hash_index_t *hi, const void **key, switch_ssize_t *klen, void **val)
|
|
|
|
{
|
|
|
|
if (key) {
|
|
|
|
*key = sqliteHashKey((HashElem *) hi);
|
2007-12-11 21:31:57 +00:00
|
|
|
if (klen) {
|
|
|
|
*klen = strlen((char *) *key) + 1;
|
|
|
|
}
|
2007-09-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
if (val) {
|
|
|
|
*val = sqliteHashData((HashElem *) hi);
|
|
|
|
}
|
|
|
|
}
|
2008-01-27 05:02:52 +00:00
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2008-01-27 05:02:52 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2008-01-27 05:02:52 +00:00
|
|
|
*/
|