浏览代码

htable: new config function sht_reset("tname")

- remove all items in the hash table
Elena-Ramona Modroiu 11 年之前
父节点
当前提交
fcd13777ba
共有 3 个文件被更改,包括 60 次插入3 次删除
  1. 32 0
      modules/htable/ht_api.c
  2. 1 0
      modules/htable/ht_api.h
  3. 27 3
      modules/htable/htable.c

+ 32 - 0
modules/htable/ht_api.c

@@ -1148,6 +1148,38 @@ int ht_rm_cell_re(str *sre, ht_t *ht, int mode)
 	return 0;
 }
 
+int ht_reset_content(ht_t *ht)
+{
+	ht_cell_t *it;
+	ht_cell_t *it0;
+	int i;
+
+	if(ht==NULL)
+		return -1;
+
+	for(i=0; i<ht->htsize; i++)
+	{
+		/* free entries */
+		lock_get(&ht->entries[i].lock);
+		it = ht->entries[i].first;
+		while(it)
+		{
+			it0 = it->next;
+			if(it->prev==NULL)
+				ht->entries[i].first = it->next;
+			else
+				it->prev->next = it->next;
+			if(it->next)
+				it->next->prev = it->prev;
+			ht->entries[i].esize--;
+			ht_cell_free(it);
+			it = it0;
+		}
+		lock_release(&ht->entries[i].lock);
+	}
+	return 0;
+}
+
 int ht_count_cells_re(str *sre, ht_t *ht, int mode)
 {
 	ht_cell_t *it;

+ 1 - 0
modules/htable/ht_api.h

@@ -102,5 +102,6 @@ int ht_get_cell_expire(ht_t *ht, str *name, unsigned int *val);
 int ht_rm_cell_re(str *sre, ht_t *ht, int mode);
 int ht_count_cells_re(str *sre, ht_t *ht, int mode);
 ht_t *ht_get_root(void);
+int ht_reset_content(ht_t *ht);
 
 #endif

+ 27 - 3
modules/htable/htable.c

@@ -1,7 +1,5 @@
 /**
- * $Id$
- *
- * Copyright (C) 2008 Elena-Ramona Modroiu (asipto.com)
+ * Copyright (C) 2008-2014 Elena-Ramona Modroiu (asipto.com)
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -33,6 +31,7 @@
 #include "../../route.h"
 #include "../../dprint.h"
 #include "../../hashes.h"
+#include "../../mod_fix.h"
 #include "../../ut.h"
 #include "../../rpc.h"
 #include "../../rpc_lookup.h"
@@ -66,6 +65,7 @@ static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo);
 static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo);
 static int ht_slot_lock(struct sip_msg* msg, char* key, char* foo);
 static int ht_slot_unlock(struct sip_msg* msg, char* key, char* foo);
+static int ht_reset(struct sip_msg* msg, char* htname, char* foo);
 
 int ht_param(modparam_t type, void* val);
 
@@ -111,6 +111,8 @@ static cmd_export_t cmds[]={
 		ANY_ROUTE},
 	{"sht_unlock",      (cmd_function)ht_slot_unlock,  1, fixup_ht_key, 0,
 		ANY_ROUTE},
+	{"sht_reset",		(cmd_function)ht_reset,		   1, fixup_spve_null, 0,
+		ANY_ROUTE},
 	{"bind_htable",     (cmd_function)bind_htable,     0, 0, 0,
 		ANY_ROUTE},
 	{0,0,0,0,0,0}
@@ -356,6 +358,28 @@ static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo)
 	return 1;
 }
 
+static int ht_reset(struct sip_msg* msg, char* htname, char* foo)
+{
+	ht_t *ht;
+	str sname;
+
+	if(fixup_get_svalue(msg, (gparam_t*)htname, &sname)<0 || sname.len<=0)
+	{
+		LM_ERR("cannot get hash table name\n");
+		return -1;
+	}
+	ht = ht_get_table(&sname);
+	if(ht==NULL)
+	{
+		LM_ERR("cannot get hash table [%.*s]\n", sname.len, sname.s);
+		return -1;
+	}
+	if(ht_reset_content(ht)<0)
+		return -1;
+	return 1;
+}
+
+
 /**
  * lock the slot for a given key in a hash table
  */