Browse code

htable: implemented starts with matching for remove functions

Daniel-Constantin Mierla authored on 23/05/2017 00:33:19
Showing 4 changed files
... ...
@@ -788,6 +788,12 @@ sht_rm_value_re("ha=>.*");
788 788
 			expression.
789 789
 		</para>
790 790
 		</listitem>
791
+		<listitem>
792
+		<para>
793
+			<emphasis>sw</emphasis> - match the val parameter as 'starts
794
+			with'.
795
+		</para>
796
+		</listitem>
791 797
 		</itemizedlist>
792 798
 		<para>
793 799
 			All parameters can be static strings or contain variables.
... ...
@@ -822,6 +828,12 @@ sht_rm_name("ha", "re", ".*");
822 828
 			expression.
823 829
 		</para>
824 830
 		</listitem>
831
+		<listitem>
832
+		<para>
833
+			<emphasis>sw</emphasis> - match the val parameter as 'starts
834
+			with'.
835
+		</para>
836
+		</listitem>
825 837
 		</itemizedlist>
826 838
 		<para>
827 839
 			All parameters can be static strings or contain variables.
... ...
@@ -830,7 +842,7 @@ sht_rm_name("ha", "re", ".*");
830 842
 			This function can be used from ANY_ROUTE.
831 843
 		</para>
832 844
 		<example>
833
-		<title><function>sht_rm_value_re</function> usage</title>
845
+		<title><function>sht_rm_value</function> usage</title>
834 846
 		<programlisting format="linespecific">
835 847
 ...
836 848
 sht_rm_value("ha", "re", ".*");
... ...
@@ -1270,6 +1270,61 @@ int ht_rm_cell_re(str *sre, ht_t *ht, int mode)
1270 1270
 	return 0;
1271 1271
 }
1272 1272
 
1273
+int ht_rm_cell_op(str *sre, ht_t *ht, int mode, int op)
1274
+{
1275
+	ht_cell_t *it;
1276
+	ht_cell_t *it0;
1277
+	int match;
1278
+	int i;
1279
+
1280
+	if(sre==NULL || sre->len<=0 || ht==NULL)
1281
+		return -1;
1282
+
1283
+	for(i=0; i<ht->htsize; i++)
1284
+	{
1285
+		/* free entries */
1286
+		ht_slot_lock(ht, i);
1287
+		it = ht->entries[i].first;
1288
+		while(it)
1289
+		{
1290
+			it0 = it->next;
1291
+			match = 0;
1292
+			if(mode==0)
1293
+			{
1294
+				if(op==HT_RM_OP_SW) {
1295
+					if(sre->len<=it->name.len
1296
+							&& strncmp(it->name.s, sre->s, sre->len)==0) {
1297
+						match = 1;
1298
+					}
1299
+				}
1300
+			} else {
1301
+				if(op==HT_RM_OP_SW) {
1302
+					if(it->flags&AVP_VAL_STR) {
1303
+						if(sre->len<=it->value.s.len
1304
+								&& strncmp(it->value.s.s, sre->s, sre->len)==0) {
1305
+							match = 1;
1306
+						}
1307
+					}
1308
+				}
1309
+			}
1310
+			if(match==1)
1311
+			{
1312
+				if(it->prev==NULL)
1313
+					ht->entries[i].first = it->next;
1314
+				else
1315
+					it->prev->next = it->next;
1316
+				if(it->next)
1317
+					it->next->prev = it->prev;
1318
+				ht->entries[i].esize--;
1319
+				ht_cell_free(it);
1320
+			}
1321
+			it = it0;
1322
+		}
1323
+		ht_slot_unlock(ht, i);
1324
+	}
1325
+	return 0;
1326
+}
1327
+
1273 1328
 int ht_reset_content(ht_t *ht)
1274 1329
 {
1275 1330
 	ht_cell_t *it;
... ...
@@ -115,6 +115,9 @@ int ht_count_cells_re(str *sre, ht_t *ht, int mode);
115 115
 ht_t *ht_get_root(void);
116 116
 int ht_reset_content(ht_t *ht);
117 117
 
118
+#define HT_RM_OP_SW	1
119
+int ht_rm_cell_op(str *sre, ht_t *ht, int mode, int op);
120
+
118 121
 void ht_iterator_init(void);
119 122
 int ht_iterator_start(str *iname, str *hname);
120 123
 int ht_iterator_next(str *iname);
... ...
@@ -437,6 +437,10 @@ static int ht_rm_items(sip_msg_t* msg, str* hname, str* op, str *val,
437 437
 					return -1;
438 438
 				}
439 439
 				return 1;
440
+			} else if(strncmp(op->s, "sw", 2)==0) {
441
+				if(ht_rm_cell_op(val, ht, mkey, HT_RM_OP_SW)<0) {
442
+					return -1;
443
+				}
440 444
 			}
441 445
 			LM_WARN("unsupported match operator: %.*s\n", op->len, op->s);
442 446
 			break;