sqlite_functions.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_SQLITE_FUNCTIONS_H
  3. #define NETDATA_SQLITE_FUNCTIONS_H
  4. #include "daemon/common.h"
  5. #include "sqlite3.h"
  6. // return a node list
  7. struct node_instance_list {
  8. uuid_t node_id;
  9. uuid_t host_id;
  10. char *hostname;
  11. int live;
  12. int queryable;
  13. int hops;
  14. };
  15. typedef enum db_check_action_type {
  16. DB_CHECK_NONE = 0x0000,
  17. DB_CHECK_INTEGRITY = 0x0001,
  18. DB_CHECK_FIX_DB = 0x0002,
  19. DB_CHECK_RECLAIM_SPACE = 0x0004,
  20. DB_CHECK_CONT = 0x00008
  21. } db_check_action_type_t;
  22. #define SQL_MAX_RETRY (100)
  23. #define SQLITE_INSERT_DELAY (10) // Insert delay in case of lock
  24. #define CHECK_SQLITE_CONNECTION(db_meta) \
  25. if (unlikely(!db_meta)) { \
  26. if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE) { \
  27. return 1; \
  28. } \
  29. error_report("Database has not been initialized"); \
  30. return 1; \
  31. }
  32. SQLITE_API int sqlite3_step_monitored(sqlite3_stmt *stmt);
  33. SQLITE_API int sqlite3_exec_monitored(
  34. sqlite3 *db, /* An open database */
  35. const char *sql, /* SQL to be evaluated */
  36. int (*callback)(void*,int,char**,char**), /* Callback function */
  37. void *data, /* 1st argument to callback */
  38. char **errmsg /* Error msg written here */
  39. );
  40. // Initialization and shutdown
  41. int init_database_batch(sqlite3 *database, int rebuild, int init_type, const char *batch[]);
  42. int sql_init_database(db_check_action_type_t rebuild, int memory);
  43. void sql_close_database(void);
  44. // Helpers
  45. int bind_text_null(sqlite3_stmt *res, int position, const char *text, bool can_be_null);
  46. int prepare_statement(sqlite3 *database, const char *query, sqlite3_stmt **statement);
  47. int execute_insert(sqlite3_stmt *res);
  48. int exec_statement_with_uuid(const char *sql, uuid_t *uuid);
  49. int db_execute(sqlite3 *database, const char *cmd);
  50. void initialize_thread_key_pool(void);
  51. // Look up functions
  52. int get_node_id(uuid_t *host_id, uuid_t *node_id);
  53. int get_host_id(uuid_t *node_id, uuid_t *host_id);
  54. struct node_instance_list *get_node_list(void);
  55. void sql_load_node_id(RRDHOST *host);
  56. char *get_hostname_by_node_id(char *node_id);
  57. // Help build archived hosts in memory when agent starts
  58. void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *system_info);
  59. DICTIONARY *sql_load_host_labels(uuid_t *host_id);
  60. // TODO: move to metadata
  61. int update_node_id(uuid_t *host_id, uuid_t *node_id);
  62. void invalidate_node_instances(uuid_t *host_id, uuid_t *claim_id);
  63. // Provide statistics
  64. int sql_metadata_cache_stats(int op);
  65. #endif //NETDATA_SQLITE_FUNCTIONS_H