sqlite_context.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_functions.h"
  3. #include "sqlite_context.h"
  4. #include "sqlite_db_migration.h"
  5. #define DB_CONTEXT_METADATA_VERSION 1
  6. const char *database_context_config[] = {
  7. "CREATE TABLE IF NOT EXISTS context (host_id BLOB, id TEXT NOT NULL, version INT NOT NULL, title TEXT NOT NULL, " \
  8. "chart_type TEXT NOT NULL, unit TEXT NOT NULL, priority INT NOT NULL, first_time_t INT NOT NULL, "
  9. "last_time_t INT NOT NULL, deleted INT NOT NULL, "
  10. "family TEXT, PRIMARY KEY (host_id, id));",
  11. NULL
  12. };
  13. const char *database_context_cleanup[] = {
  14. "VACUUM;",
  15. NULL
  16. };
  17. sqlite3 *db_context_meta = NULL;
  18. /*
  19. * Initialize the SQLite database
  20. * Return 0 on success
  21. */
  22. int sql_init_context_database(int memory)
  23. {
  24. char sqlite_database[FILENAME_MAX + 1];
  25. int rc;
  26. if (likely(!memory))
  27. snprintfz(sqlite_database, FILENAME_MAX, "%s/context-meta.db", netdata_configured_cache_dir);
  28. else
  29. strcpy(sqlite_database, ":memory:");
  30. rc = sqlite3_open(sqlite_database, &db_context_meta);
  31. if (rc != SQLITE_OK) {
  32. error_report("Failed to initialize database at %s, due to \"%s\"", sqlite_database, sqlite3_errstr(rc));
  33. sqlite3_close(db_context_meta);
  34. db_context_meta = NULL;
  35. return 1;
  36. }
  37. info("SQLite database %s initialization", sqlite_database);
  38. char buf[1024 + 1] = "";
  39. const char *list[2] = { buf, NULL };
  40. int target_version = DB_CONTEXT_METADATA_VERSION;
  41. if (likely(!memory))
  42. target_version = perform_context_database_migration(db_context_meta, DB_CONTEXT_METADATA_VERSION);
  43. // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
  44. // PRAGMA schema.auto_vacuum = 0 | NONE | 1 | FULL | 2 | INCREMENTAL;
  45. snprintfz(buf, 1024, "PRAGMA auto_vacuum=%s;", config_get(CONFIG_SECTION_SQLITE, "auto vacuum", "INCREMENTAL"));
  46. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  47. // https://www.sqlite.org/pragma.html#pragma_synchronous
  48. // PRAGMA schema.synchronous = 0 | OFF | 1 | NORMAL | 2 | FULL | 3 | EXTRA;
  49. snprintfz(buf, 1024, "PRAGMA synchronous=%s;", config_get(CONFIG_SECTION_SQLITE, "synchronous", "NORMAL"));
  50. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  51. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  52. // PRAGMA schema.journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | WAL | OFF
  53. snprintfz(buf, 1024, "PRAGMA journal_mode=%s;", config_get(CONFIG_SECTION_SQLITE, "journal mode", "WAL"));
  54. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  55. // https://www.sqlite.org/pragma.html#pragma_temp_store
  56. // PRAGMA temp_store = 0 | DEFAULT | 1 | FILE | 2 | MEMORY;
  57. snprintfz(buf, 1024, "PRAGMA temp_store=%s;", config_get(CONFIG_SECTION_SQLITE, "temp store", "MEMORY"));
  58. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  59. // https://www.sqlite.org/pragma.html#pragma_journal_size_limit
  60. // PRAGMA schema.journal_size_limit = N ;
  61. snprintfz(buf, 1024, "PRAGMA journal_size_limit=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "journal size limit", 16777216));
  62. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  63. // https://www.sqlite.org/pragma.html#pragma_cache_size
  64. // PRAGMA schema.cache_size = pages;
  65. // PRAGMA schema.cache_size = -kibibytes;
  66. snprintfz(buf, 1024, "PRAGMA cache_size=%lld;", config_get_number(CONFIG_SECTION_SQLITE, "cache size", -2000));
  67. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  68. snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version);
  69. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  70. if (likely(!memory))
  71. snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir);
  72. else
  73. snprintfz(buf, 1024, "ATTACH DATABASE ':memory:' as meta;");
  74. if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
  75. if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_config[0]))
  76. return 1;
  77. if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_cleanup[0]))
  78. return 1;
  79. return 0;
  80. }
  81. /*
  82. * Close the sqlite database
  83. */
  84. void sql_close_context_database(void)
  85. {
  86. int rc;
  87. if (unlikely(!db_context_meta))
  88. return;
  89. info("Closing context SQLite database");
  90. rc = sqlite3_close_v2(db_context_meta);
  91. if (unlikely(rc != SQLITE_OK))
  92. error_report("Error %d while closing the context SQLite database, %s", rc, sqlite3_errstr(rc));
  93. return;
  94. }
  95. //
  96. // Fetching data
  97. //
  98. #define CTX_GET_CHART_LIST "SELECT c.chart_id, c.type||'.'||c.id, c.name, c.context, c.title, c.unit, c.priority, " \
  99. "c.update_every, c.chart_type, c.family FROM meta.chart c WHERE c.host_id = @host_id and c.chart_id is not null; "
  100. void ctx_get_chart_list(uuid_t *host_uuid, void (*dict_cb)(SQL_CHART_DATA *, void *), void *data)
  101. {
  102. int rc;
  103. static __thread sqlite3_stmt *res = NULL;
  104. if (unlikely(!host_uuid)) {
  105. internal_error(true, "Requesting context chart list without host_id");
  106. return;
  107. }
  108. if (unlikely(!res)) {
  109. rc = prepare_statement(db_context_meta, CTX_GET_CHART_LIST, &res);
  110. if (rc != SQLITE_OK) {
  111. error_report("Failed to prepare statement to fetch chart list");
  112. return;
  113. }
  114. }
  115. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  116. if (unlikely(rc != SQLITE_OK)) {
  117. error_report("Failed to bind host_id to fetch the chart list");
  118. goto skip_load;
  119. }
  120. SQL_CHART_DATA chart_data = { 0 };
  121. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  122. uuid_copy(chart_data.chart_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  123. chart_data.id = (char *) sqlite3_column_text(res, 1);
  124. chart_data.name = (char *) sqlite3_column_text(res, 2);
  125. chart_data.context = (char *) sqlite3_column_text(res, 3);
  126. chart_data.title = (char *) sqlite3_column_text(res, 4);
  127. chart_data.units = (char *) sqlite3_column_text(res, 5);
  128. chart_data.priority = sqlite3_column_int(res, 6);
  129. chart_data.update_every = sqlite3_column_int(res, 7);
  130. chart_data.chart_type = sqlite3_column_int(res, 8);
  131. chart_data.family = (char *) sqlite3_column_text(res, 9);
  132. dict_cb(&chart_data, data);
  133. }
  134. skip_load:
  135. rc = sqlite3_reset(res);
  136. if (rc != SQLITE_OK)
  137. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  138. }
  139. // Dimension list
  140. #define CTX_GET_DIMENSION_LIST "SELECT d.dim_id, d.id, d.name, CASE WHEN INSTR(d.options,\"hidden\") > 0 THEN 1 ELSE 0 END " \
  141. "FROM meta.dimension d WHERE d.chart_id = @id and d.dim_id is not null ORDER BY d.rowid ASC;"
  142. void ctx_get_dimension_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_DIMENSION_DATA *, void *), void *data)
  143. {
  144. int rc;
  145. static __thread sqlite3_stmt *res = NULL;
  146. if (unlikely(!res)) {
  147. rc = prepare_statement(db_context_meta, CTX_GET_DIMENSION_LIST, &res);
  148. if (rc != SQLITE_OK) {
  149. error_report("Failed to prepare statement to fetch chart dimension data");
  150. return;
  151. }
  152. }
  153. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  154. if (unlikely(rc != SQLITE_OK)) {
  155. error_report("Failed to bind chart_id to fetch dimension list");
  156. goto failed;
  157. }
  158. SQL_DIMENSION_DATA dimension_data;
  159. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  160. uuid_copy(dimension_data.dim_id, *((uuid_t *)sqlite3_column_blob(res, 0)));
  161. dimension_data.id = (char *) sqlite3_column_text(res, 1);
  162. dimension_data.name = (char *) sqlite3_column_text(res, 2);
  163. dimension_data.hidden = sqlite3_column_int(res, 3);
  164. dict_cb(&dimension_data, data);
  165. }
  166. failed:
  167. rc = sqlite3_reset(res);
  168. if (rc != SQLITE_OK)
  169. error_report("Failed to reset statement that fetches the chart dimension list, rc = %d", rc);
  170. }
  171. // LABEL LIST
  172. #define CTX_GET_LABEL_LIST "SELECT l.label_key, l.label_value, l.source_type FROM meta.chart_label l WHERE l.chart_id = @id;"
  173. void ctx_get_label_list(uuid_t *chart_uuid, void (*dict_cb)(SQL_CLABEL_DATA *, void *), void *data)
  174. {
  175. int rc;
  176. static __thread sqlite3_stmt *res = NULL;
  177. if (unlikely(!res)) {
  178. rc = prepare_statement(db_context_meta, CTX_GET_LABEL_LIST, &res);
  179. if (rc != SQLITE_OK) {
  180. error_report("Failed to prepare statement to fetch chart labels");
  181. return;
  182. }
  183. }
  184. rc = sqlite3_bind_blob(res, 1, chart_uuid, sizeof(*chart_uuid), SQLITE_STATIC);
  185. if (unlikely(rc != SQLITE_OK)) {
  186. error_report("Failed to bind chart_id to fetch chart labels");
  187. goto failed;
  188. }
  189. SQL_CLABEL_DATA label_data;
  190. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  191. label_data.label_key = (char *) sqlite3_column_text(res, 0);
  192. label_data.label_value = (char *) sqlite3_column_text(res, 1);
  193. label_data.label_source = sqlite3_column_int(res, 2);
  194. dict_cb(&label_data, data);
  195. }
  196. failed:
  197. rc = sqlite3_reset(res);
  198. if (rc != SQLITE_OK)
  199. error_report("Failed to reset statement that fetches chart label data, rc = %d", rc);
  200. return;
  201. }
  202. // CONTEXT LIST
  203. #define CTX_GET_CONTEXT_LIST "SELECT id, version, title, chart_type, unit, priority, first_time_t, " \
  204. "last_time_t, deleted, family FROM context c WHERE c.host_id = @host_id;"
  205. void ctx_get_context_list(uuid_t *host_uuid, void (*dict_cb)(VERSIONED_CONTEXT_DATA *, void *), void *data)
  206. {
  207. if (unlikely(!host_uuid))
  208. return;
  209. int rc;
  210. static __thread sqlite3_stmt *res = NULL;
  211. if (unlikely(!res)) {
  212. rc = prepare_statement(db_context_meta, CTX_GET_CONTEXT_LIST, &res);
  213. if (rc != SQLITE_OK) {
  214. error_report("Failed to prepare statement to fetch stored context list");
  215. return;
  216. }
  217. }
  218. VERSIONED_CONTEXT_DATA context_data = {0};
  219. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  220. if (unlikely(rc != SQLITE_OK)) {
  221. error_report("Failed to bind host_id to fetch versioned context data");
  222. goto failed;
  223. }
  224. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  225. context_data.id = (char *) sqlite3_column_text(res, 0);
  226. context_data.version = sqlite3_column_int64(res, 1);
  227. context_data.title = (char *) sqlite3_column_text(res, 2);
  228. context_data.chart_type = (char *) sqlite3_column_text(res, 3);
  229. context_data.units = (char *) sqlite3_column_text(res, 4);
  230. context_data.priority = sqlite3_column_int64(res, 5);
  231. context_data.first_time_s = sqlite3_column_int64(res, 6);
  232. context_data.last_time_s = sqlite3_column_int64(res, 7);
  233. context_data.deleted = sqlite3_column_int(res, 8);
  234. context_data.family = (char *) sqlite3_column_text(res, 9);
  235. dict_cb(&context_data, data);
  236. }
  237. failed:
  238. rc = sqlite3_reset(res);
  239. if (rc != SQLITE_OK)
  240. error_report("Failed to reset statement that fetches stored context versioned data, rc = %d", rc);
  241. }
  242. //
  243. // Storing Data
  244. //
  245. #define CTX_STORE_CONTEXT "INSERT OR REPLACE INTO context " \
  246. "(host_id, id, version, title, chart_type, unit, priority, first_time_t, last_time_t, deleted, family) " \
  247. "VALUES (@host_id, @context, @version, @title, @chart_type, @unit, @priority, @first_time_t, @last_time_t, @deleted, @family);"
  248. int ctx_store_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  249. {
  250. int rc, rc_stored = 1;
  251. sqlite3_stmt *res = NULL;
  252. if (unlikely(!host_uuid || !context_data || !context_data->id))
  253. return 0;
  254. rc = sqlite3_prepare_v2(db_context_meta, CTX_STORE_CONTEXT, -1, &res, 0);
  255. if (unlikely(rc != SQLITE_OK)) {
  256. error_report("Failed to prepare statement to store context");
  257. return 1;
  258. }
  259. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  260. if (unlikely(rc != SQLITE_OK)) {
  261. error_report("Failed to bind host_uuid to store context details");
  262. goto skip_store;
  263. }
  264. rc = bind_text_null(res, 2, context_data->id, 0);
  265. if (unlikely(rc != SQLITE_OK)) {
  266. error_report("Failed to bind context to store details");
  267. goto skip_store;
  268. }
  269. rc = sqlite3_bind_int64(res, 3, (time_t) context_data->version);
  270. if (unlikely(rc != SQLITE_OK)) {
  271. error_report("Failed to bind first_time_t to store context details");
  272. goto skip_store;
  273. }
  274. rc = bind_text_null(res, 4, context_data->title, 0);
  275. if (unlikely(rc != SQLITE_OK)) {
  276. error_report("Failed to bind context to store details");
  277. goto skip_store;
  278. }
  279. rc = bind_text_null(res, 5, context_data->chart_type, 0);
  280. if (unlikely(rc != SQLITE_OK)) {
  281. error_report("Failed to bind context to store details");
  282. goto skip_store;
  283. }
  284. rc = bind_text_null(res, 6, context_data->units, 0);
  285. if (unlikely(rc != SQLITE_OK)) {
  286. error_report("Failed to bind context to store details");
  287. goto skip_store;
  288. }
  289. rc = sqlite3_bind_int64(res, 7, (time_t) context_data->priority);
  290. if (unlikely(rc != SQLITE_OK)) {
  291. error_report("Failed to bind first_time_t to store context details");
  292. goto skip_store;
  293. }
  294. rc = sqlite3_bind_int64(res, 8, (time_t) context_data->first_time_s);
  295. if (unlikely(rc != SQLITE_OK)) {
  296. error_report("Failed to bind first_time_t to store context details");
  297. goto skip_store;
  298. }
  299. rc = sqlite3_bind_int64(res, 9, (time_t) context_data->last_time_s);
  300. if (unlikely(rc != SQLITE_OK)) {
  301. error_report("Failed to bind last_time_t to store context details");
  302. goto skip_store;
  303. }
  304. rc = sqlite3_bind_int(res, 10, (time_t) context_data->deleted);
  305. if (unlikely(rc != SQLITE_OK)) {
  306. error_report("Failed to bind last_time_t to store context details");
  307. goto skip_store;
  308. }
  309. rc = bind_text_null(res, 11, context_data->family, 1);
  310. if (unlikely(rc != SQLITE_OK)) {
  311. error_report("Failed to bind context to store details");
  312. goto skip_store;
  313. }
  314. rc_stored = execute_insert(res);
  315. if (rc_stored != SQLITE_DONE)
  316. error_report("Failed store context details for context %s, rc = %d", context_data->id, rc_stored);
  317. skip_store:
  318. rc = sqlite3_finalize(res);
  319. if (rc != SQLITE_OK)
  320. error_report("Failed to finalize statement that stores context details, rc = %d", rc);
  321. return (rc_stored != SQLITE_DONE);
  322. }
  323. // Delete a context
  324. #define CTX_DELETE_CONTEXT "DELETE FROM context WHERE host_id = @host_id AND id = @context;"
  325. int ctx_delete_context(uuid_t *host_uuid, VERSIONED_CONTEXT_DATA *context_data)
  326. {
  327. int rc, rc_stored = 1;
  328. sqlite3_stmt *res = NULL;
  329. if (unlikely(!context_data || !context_data->id))
  330. return 0;
  331. rc = sqlite3_prepare_v2(db_context_meta, CTX_DELETE_CONTEXT, -1, &res, 0);
  332. if (unlikely(rc != SQLITE_OK)) {
  333. error_report("Failed to prepare statement to delete context");
  334. return 1;
  335. }
  336. rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
  337. if (unlikely(rc != SQLITE_OK)) {
  338. error_report("Failed to bind host_id to delete context data");
  339. goto skip_delete;
  340. }
  341. rc = sqlite3_bind_text(res, 2, context_data->id, -1, SQLITE_STATIC);
  342. if (unlikely(rc != SQLITE_OK)) {
  343. error_report("Failed to bind context id for data deletion");
  344. goto skip_delete;
  345. }
  346. rc_stored = execute_insert(res);
  347. if (rc_stored != SQLITE_DONE)
  348. error_report("Failed to delete context %s, rc = %d", context_data->id, rc_stored);
  349. #ifdef NETDATA_INTERNAL_CHECKS
  350. else {
  351. char host_uuid_str[UUID_STR_LEN];
  352. uuid_unparse_lower(*host_uuid, host_uuid_str);
  353. info("%s: Deleted context %s under host %s", __FUNCTION__ , context_data->id, host_uuid_str);
  354. }
  355. #endif
  356. skip_delete:
  357. rc = sqlite3_finalize(res);
  358. if (rc != SQLITE_OK)
  359. error_report("Failed to finalize statement where deleting a context, rc = %d", rc);
  360. return (rc_stored != SQLITE_DONE);
  361. }
  362. int sql_context_cache_stats(int op)
  363. {
  364. int count, dummy;
  365. netdata_thread_disable_cancelability();
  366. sqlite3_db_status(db_context_meta, op, &count, &dummy, 0);
  367. netdata_thread_enable_cancelability();
  368. return count;
  369. }
  370. //
  371. // TESTING FUNCTIONS
  372. //
  373. static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, void *data)
  374. {
  375. (void)data;
  376. info(" Context id = %s "
  377. "version = %"PRIu64" "
  378. "title = %s "
  379. "chart_type = %s "
  380. "units = %s "
  381. "priority = %"PRIu64" "
  382. "first time = %"PRIu64" "
  383. "last time = %"PRIu64" "
  384. "deleted = %d "
  385. "family = %s",
  386. context_data->id,
  387. context_data->version,
  388. context_data->title,
  389. context_data->chart_type,
  390. context_data->units,
  391. context_data->priority,
  392. context_data->first_time_s,
  393. context_data->last_time_s,
  394. context_data->deleted,
  395. context_data->family);
  396. }
  397. int ctx_unittest(void)
  398. {
  399. uuid_t host_uuid;
  400. uuid_generate(host_uuid);
  401. int rc = sql_init_context_database(1);
  402. if (rc != SQLITE_OK)
  403. return 1;
  404. // Store a context
  405. VERSIONED_CONTEXT_DATA context_data;
  406. context_data.id = strdupz("cpu.cpu");
  407. context_data.title = strdupz("TestContextTitle");
  408. context_data.units= strdupz("TestContextUnits");
  409. context_data.chart_type = strdupz("TestContextChartType");
  410. context_data.family = strdupz("TestContextFamily");
  411. context_data.priority = 50000;
  412. context_data.deleted = 0;
  413. context_data.first_time_s = 1657781000;
  414. context_data.last_time_s = 1657781100;
  415. context_data.version = now_realtime_usec();
  416. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  417. info("Entry %s inserted", context_data.id);
  418. else
  419. info("Entry %s not inserted", context_data.id);
  420. if (likely(!ctx_store_context(&host_uuid, &context_data)))
  421. info("Entry %s inserted", context_data.id);
  422. else
  423. info("Entry %s not inserted", context_data.id);
  424. // This will change end time
  425. context_data.first_time_s = 1657781000;
  426. context_data.last_time_s = 1657782001;
  427. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  428. info("Entry %s updated", context_data.id);
  429. else
  430. info("Entry %s not updated", context_data.id);
  431. info("List context start after insert");
  432. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  433. info("List context end after insert");
  434. // This will change start time
  435. context_data.first_time_s = 1657782000;
  436. context_data.last_time_s = 1657782001;
  437. if (likely(!ctx_update_context(&host_uuid, &context_data)))
  438. info("Entry %s updated", context_data.id);
  439. else
  440. info("Entry %s not updated", context_data.id);
  441. // This will list one entry
  442. info("List context start after insert");
  443. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  444. info("List context end after insert");
  445. info("List context start after insert");
  446. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  447. info("List context end after insert");
  448. // This will delete the entry
  449. if (likely(!ctx_delete_context(&host_uuid, &context_data)))
  450. info("Entry %s deleted", context_data.id);
  451. else
  452. info("Entry %s not deleted", context_data.id);
  453. freez((void *)context_data.id);
  454. freez((void *)context_data.title);
  455. freez((void *)context_data.chart_type);
  456. freez((void *)context_data.family);
  457. // The list should be empty
  458. info("List context start after delete");
  459. ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
  460. info("List context end after delete");
  461. sql_close_context_database();
  462. return 0;
  463. }