rrdfamily.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_RRD_INTERNALS
  3. #include "rrd.h"
  4. typedef struct rrdfamily {
  5. STRING *family;
  6. DICTIONARY *rrdvars;
  7. } RRDFAMILY;
  8. // ----------------------------------------------------------------------------
  9. // RRDFAMILY index
  10. struct rrdfamily_constructor {
  11. const char *family;
  12. };
  13. static void rrdfamily_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdfamily, void *constructor_data) {
  14. RRDFAMILY *rf = rrdfamily;
  15. struct rrdfamily_constructor *ctr = constructor_data;
  16. rf->family = string_strdupz(ctr->family);
  17. rf->rrdvars = rrdvariables_create();
  18. }
  19. static void rrdfamily_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdfamily, void *rrdhost __maybe_unused) {
  20. RRDFAMILY *rf = rrdfamily;
  21. string_freez(rf->family);
  22. rrdvariables_destroy(rf->rrdvars);
  23. rf->family = NULL;
  24. rf->rrdvars = NULL;
  25. }
  26. void rrdfamily_index_init(RRDHOST *host) {
  27. if(!host->rrdfamily_root_index) {
  28. host->rrdfamily_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  29. &dictionary_stats_category_rrdhealth, sizeof(RRDFAMILY));
  30. dictionary_register_insert_callback(host->rrdfamily_root_index, rrdfamily_insert_callback, NULL);
  31. dictionary_register_delete_callback(host->rrdfamily_root_index, rrdfamily_delete_callback, host);
  32. }
  33. }
  34. void rrdfamily_index_destroy(RRDHOST *host) {
  35. dictionary_destroy(host->rrdfamily_root_index);
  36. host->rrdfamily_root_index = NULL;
  37. }
  38. // ----------------------------------------------------------------------------
  39. // RRDFAMILY management
  40. const RRDFAMILY_ACQUIRED *rrdfamily_add_and_acquire(RRDHOST *host, const char *id) {
  41. struct rrdfamily_constructor tmp = {
  42. .family = id,
  43. };
  44. return (const RRDFAMILY_ACQUIRED *)dictionary_set_and_acquire_item_advanced(host->rrdfamily_root_index, id, -1, NULL, sizeof(RRDFAMILY), &tmp);
  45. }
  46. void rrdfamily_release(RRDHOST *host, const RRDFAMILY_ACQUIRED *rfa) {
  47. if(unlikely(!rfa)) return;
  48. dictionary_acquired_item_release(host->rrdfamily_root_index, (const DICTIONARY_ITEM *)rfa);
  49. }
  50. DICTIONARY *rrdfamily_rrdvars_dict(const RRDFAMILY_ACQUIRED *rfa) {
  51. if(unlikely(!rfa)) return NULL;
  52. RRDFAMILY *rf = dictionary_acquired_item_value((const DICTIONARY_ITEM *)rfa);
  53. return(rf->rrdvars);
  54. }