Browse code

htable: new rpc command htable.store

- save the content of hash table to database

Daniel-Constantin Mierla authored on 25/03/2020 18:31:49
Showing 1 changed files
... ...
@@ -1285,9 +1285,14 @@ static const char* htable_flush_doc[2] = {
1285 1285
 	0
1286 1286
 };
1287 1287
 static const char* htable_reload_doc[2] = {
1288
-	"Reload hash table.",
1288
+	"Reload hash table from database.",
1289 1289
 	0
1290 1290
 };
1291
+static const char* htable_store_doc[2] = {
1292
+	"Store hash table to database.",
1293
+	0
1294
+};
1295
+
1291 1296
 
1292 1297
 static void htable_rpc_delete(rpc_t* rpc, void* c) {
1293 1298
 	str htname, keyname;
... ...
@@ -1754,6 +1759,56 @@ static void htable_rpc_reload(rpc_t* rpc, void* c)
1754 1754
 	return;
1755 1755
 }
1756 1756
 
1757
+/*! \brief RPC htable.store command to store content of a hash table to db */
1758
+static void htable_rpc_store(rpc_t* rpc, void* c)
1759
+{
1760
+	str htname;
1761
+	ht_t *ht;
1762
+
1763
+	if(ht_db_url.len<=0) {
1764
+		rpc->fault(c, 500, "No htable db_url");
1765
+		return;
1766
+	}
1767
+	if(ht_db_init_con()!=0) {
1768
+		rpc->fault(c, 500, "Failed to init htable db connection");
1769
+		return;
1770
+	}
1771
+	if(ht_db_open_con()!=0) {
1772
+		rpc->fault(c, 500, "Failed to open htable db connection");
1773
+		return;
1774
+	}
1775
+
1776
+	if (rpc->scan(c, "S", &htname) < 1) {
1777
+		ht_db_close_con();
1778
+		rpc->fault(c, 500, "No htable name given");
1779
+		return;
1780
+	}
1781
+	ht = ht_get_table(&htname);
1782
+	if(ht==NULL) {
1783
+		ht_db_close_con();
1784
+		rpc->fault(c, 500, "No such htable");
1785
+		return;
1786
+	}
1787
+	if(ht->dbtable.s==NULL || ht->dbtable.len<=0) {
1788
+		ht_db_close_con();
1789
+		rpc->fault(c, 500, "No database htable");
1790
+		return;
1791
+	}
1792
+	LM_DBG("sync db table [%.*s] from ht [%.*s]\n",
1793
+			ht->dbtable.len, ht->dbtable.s,
1794
+			ht->name.len, ht->name.s);
1795
+	ht_db_delete_records(&ht->dbtable);
1796
+	if(ht_db_save_table(ht, &ht->dbtable)!=0) {
1797
+		LM_ERR("failed syncing hash table [%.*s] to db\n",
1798
+					ht->name.len, ht->name.s);
1799
+		ht_db_close_con();
1800
+		rpc->fault(c, 500, "Storing htable failed");
1801
+		return;
1802
+	}
1803
+	ht_db_close_con();
1804
+	return;
1805
+}
1806
+
1757 1807
 rpc_export_t htable_rpc[] = {
1758 1808
 	{"htable.dump", htable_rpc_dump, htable_dump_doc, RET_ARRAY},
1759 1809
 	{"htable.delete", htable_rpc_delete, htable_delete_doc, 0},
... ...
@@ -1762,6 +1817,7 @@ rpc_export_t htable_rpc[] = {
1762 1762
 	{"htable.seti", htable_rpc_seti, htable_seti_doc, 0},
1763 1763
 	{"htable.listTables", htable_rpc_list, htable_list_doc, RET_ARRAY},
1764 1764
 	{"htable.reload", htable_rpc_reload, htable_reload_doc, 0},
1765
+	{"htable.store", htable_rpc_store, htable_store_doc, 0},
1765 1766
 	{"htable.stats", htable_rpc_stats, htable_stats_doc, RET_ARRAY},
1766 1767
 	{"htable.flush", htable_rpc_flush, htable_flush_doc, 0},
1767 1768
 	{0, 0, 0, 0}