Browse code

htable: Add return code on successful deletion of item, update RPC commands with replies

Olle E. Johansson authored on 07/12/2021 07:52:08
Showing 2 changed files
... ...
@@ -655,6 +655,13 @@ static void ht_cell_unlink(ht_t *ht, int idx, ht_cell_t *it)
655 655
 	ht->entries[idx].esize--;
656 656
 }
657 657
 
658
+/* Delete htable entry
659
+
660
+Return:
661
+	-1 on error in argument
662
+	0 on entry not found
663
+	1 on entry found and deleted
664
+*/
658 665
 int ht_del_cell(ht_t *ht, str *name)
659 666
 {
660 667
 	unsigned int idx;
... ...
@@ -689,7 +696,7 @@ int ht_del_cell(ht_t *ht, str *name)
689 696
 			ht_cell_unlink(ht, idx, it);
690 697
 			ht_slot_unlock(ht, idx);
691 698
 			ht_cell_free(it);
692
-			return 0;
699
+			return 1;
693 700
 		}
694 701
 		it = it->next;
695 702
 	}
... ...
@@ -1438,6 +1438,7 @@ static const char* htable_store_doc[2] = {
1438 1438
 static void htable_rpc_delete(rpc_t* rpc, void* c) {
1439 1439
 	str htname, keyname;
1440 1440
 	ht_t *ht;
1441
+	int res;
1441 1442
 
1442 1443
 	if (rpc->scan(c, "SS", &htname, &keyname) < 2) {
1443 1444
 		rpc->fault(c, 500, "Not enough parameters (htable name & key name");
... ...
@@ -1453,7 +1454,17 @@ static void htable_rpc_delete(rpc_t* rpc, void* c) {
1453 1454
 		LM_ERR("dmq replication failed\n");
1454 1455
 	}
1455 1456
 
1456
-	ht_del_cell(ht, &keyname);
1457
+	res = ht_del_cell(ht, &keyname);
1458
+
1459
+	if (res  == -1) {
1460
+		rpc->fault(c, 500, "Internal error");
1461
+		return;
1462
+	} else if (res == 0) {
1463
+		rpc->fault(c, 404, "Key not found in htable.");
1464
+		return;
1465
+	}
1466
+	rpc->rpl_printf(c, "Ok. Key deleted.");
1467
+	return;
1457 1468
 }
1458 1469
 
1459 1470
 /*! \brief RPC htable.get command to get one item */
... ...
@@ -1561,7 +1572,7 @@ static void htable_rpc_sets(rpc_t* rpc, void* c) {
1561 1572
 		rpc->fault(c, 500, "Failed to set the item");
1562 1573
 		return;
1563 1574
 	}
1564
-
1575
+	rpc->rpl_printf(c, "Ok. Key set to new value.");
1565 1576
 	return;
1566 1577
 }
1567 1578
 
... ...
@@ -1596,7 +1607,7 @@ static void htable_rpc_seti(rpc_t* rpc, void* c) {
1596 1607
 		rpc->fault(c, 500, "Failed to set the item");
1597 1608
 		return;
1598 1609
 	}
1599
-
1610
+	rpc->rpl_printf(c, "Ok. Key set to new value.");
1600 1611
 	return;
1601 1612
 }
1602 1613