Browse code

htable: added functions to remove items using variables

Daniel-Constantin Mierla authored on 22/05/2017 11:04:15
Showing 2 changed files
... ...
@@ -767,6 +767,74 @@ sht_rm_name_re("ha=>.*");
767 767
 ...
768 768
 sht_rm_value_re("ha=>.*");
769 769
 ...
770
+</programlisting>
771
+		</example>
772
+	</section>
773
+	<section id="htable.f.sht_rm_name">
774
+		<title>
775
+		<function moreinfo="none">sht_rm_name(htable, op, val)</function>
776
+		</title>
777
+		<para>
778
+			Delete all entries in the htable that match the name against
779
+			the val parameter.
780
+		</para>
781
+		<para>
782
+			The op parameter can be:
783
+		</para>
784
+		<itemizedlist>
785
+		<listitem>
786
+		<para>
787
+			<emphasis>re</emphasis> - match the val parameter as regular
788
+			expression.
789
+		</para>
790
+		</listitem>
791
+		</itemizedlist>
792
+		<para>
793
+			All parameters can be static strings or contain variables.
794
+		</para>
795
+		<para>
796
+			This function can be used from ANY_ROUTE.
797
+		</para>
798
+		<example>
799
+		<title><function>sht_rm_name</function> usage</title>
800
+		<programlisting format="linespecific">
801
+...
802
+sht_rm_name("ha", "re", ".*");
803
+...
804
+</programlisting>
805
+		</example>
806
+	</section>
807
+	<section id="htable.f.sht_rm_value">
808
+		<title>
809
+		<function moreinfo="none">sht_rm_value(htable, op, val)</function>
810
+		</title>
811
+		<para>
812
+			Delete all entries in the htable that match the value against
813
+			the val parameter.
814
+		</para>
815
+		<para>
816
+			The op parameter can be:
817
+		</para>
818
+		<itemizedlist>
819
+		<listitem>
820
+		<para>
821
+			<emphasis>re</emphasis> - match the val parameter as regular
822
+			expression.
823
+		</para>
824
+		</listitem>
825
+		</itemizedlist>
826
+		<para>
827
+			All parameters can be static strings or contain variables.
828
+		</para>
829
+		<para>
830
+			This function can be used from ANY_ROUTE.
831
+		</para>
832
+		<example>
833
+		<title><function>sht_rm_value_re</function> usage</title>
834
+		<programlisting format="linespecific">
835
+...
836
+sht_rm_value("ha", "re", ".*");
837
+...
770 838
 </programlisting>
771 839
 		</example>
772 840
 	</section>
... ...
@@ -67,6 +67,8 @@ static void destroy(void);
67 67
 static int fixup_ht_key(void** param, int param_no);
68 68
 static int ht_rm_name_re(struct sip_msg* msg, char* key, char* foo);
69 69
 static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo);
70
+static int w_ht_rm_name(struct sip_msg* msg, char* hname, char* op, char *val);
71
+static int w_ht_rm_value(struct sip_msg* msg, char* hname, char* op, char *val);
70 72
 static int w_ht_slot_lock(struct sip_msg* msg, char* key, char* foo);
71 73
 static int w_ht_slot_unlock(struct sip_msg* msg, char* key, char* foo);
72 74
 static int ht_reset(struct sip_msg* msg, char* htname, char* foo);
... ...
@@ -107,6 +109,10 @@ static cmd_export_t cmds[]={
107 109
 		ANY_ROUTE},
108 110
 	{"sht_rm_value_re", (cmd_function)ht_rm_value_re,  1, fixup_ht_key, 0,
109 111
 		ANY_ROUTE},
112
+	{"sht_rm_name",  (cmd_function)w_ht_rm_name,   3, fixup_spve_all, 0,
113
+		ANY_ROUTE},
114
+	{"sht_rm_value", (cmd_function)w_ht_rm_value,  3, fixup_spve_all, 0,
115
+		ANY_ROUTE},
110 116
 	{"sht_lock",        (cmd_function)w_ht_slot_lock,    1, fixup_ht_key, 0,
111 117
 		ANY_ROUTE},
112 118
 	{"sht_unlock",      (cmd_function)w_ht_slot_unlock,  1, fixup_ht_key, 0,
... ...
@@ -407,6 +413,72 @@ static int ht_rm_value_re(struct sip_msg* msg, char* key, char* foo)
407 413
 	return 1;
408 414
 }
409 415
 
416
+static int ht_rm_items(sip_msg_t* msg, str* hname, str* op, str *val,
417
+		int mkey)
418
+{
419
+	ht_t *ht;
420
+	int_str isval;
421
+
422
+	ht = ht_get_table(hname);
423
+	if(ht==NULL) {
424
+		LM_ERR("cannot get hash table [%.*s]\n", hname->len, hname->s);
425
+		return -1;
426
+	}
427
+
428
+	switch(op->len) {
429
+		case 2:
430
+			if(strncmp(op->s, "re", 2)==0) {
431
+				isval.s = *val;
432
+				if (ht_dmq_replicate_action(HT_DMQ_RM_CELL_RE, &ht->name, NULL,
433
+							AVP_VAL_STR, &isval, mkey)!=0) {
434
+					LM_ERR("dmq relication failed (op %d)\n", mkey);
435
+				}
436
+				if(ht_rm_cell_re(val, ht, mkey)<0) {
437
+					return -1;
438
+				}
439
+				return 1;
440
+			}
441
+			LM_WARN("unsupported match operator: %.*s\n", op->len, op->s);
442
+			break;
443
+		default:
444
+			LM_WARN("unsupported match operator: %.*s\n", op->len, op->s);
445
+	}
446
+	return -1;
447
+}
448
+
449
+static int w_ht_rm_items(sip_msg_t* msg, char* hname, char* op, char *val,
450
+		int mkey)
451
+{
452
+	str sname;
453
+	str sop;
454
+	str sval;
455
+
456
+	if(fixup_get_svalue(msg, (gparam_t*)hname, &sname)<0 || sname.len<=0) {
457
+		LM_ERR("cannot get the hash table name\n");
458
+		return -1;
459
+	}
460
+	if(fixup_get_svalue(msg, (gparam_t*)op, &sop)<0 || sop.len<=0) {
461
+		LM_ERR("cannot get the match operation\n");
462
+		return -1;
463
+	}
464
+	if(fixup_get_svalue(msg, (gparam_t*)val, &sval)<0 || sval.len<=0) {
465
+		LM_ERR("cannot the get match value\n");
466
+		return -1;
467
+	}
468
+
469
+	return ht_rm_items(msg, &sname, &sop, &sval, mkey);
470
+}
471
+
472
+static int w_ht_rm_name(sip_msg_t* msg, char* hname, char* op, char *val)
473
+{
474
+	return w_ht_rm_items(msg, hname, op, val, 0);
475
+}
476
+
477
+static int w_ht_rm_value(sip_msg_t* msg, char* hname, char* op, char *val)
478
+{
479
+	return w_ht_rm_items(msg, hname, op, val, 1);
480
+}
481
+
410 482
 static int ht_reset_by_name(str *hname)
411 483
 {
412 484
 	ht_t *ht;