dictionary.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_DICTIONARY_H
  3. #define NETDATA_DICTIONARY_H 1
  4. #include "../libnetdata.h"
  5. /*
  6. * Netdata DICTIONARY features:
  7. *
  8. * CLONE or LINK
  9. * Names and Values in the dictionary can be cloned or linked.
  10. * In clone mode, the dictionary does all the memory management.
  11. * The default is clone for both names and values.
  12. * Set DICTIONARY_FLAG_NAME_LINK_DONT_CLONE to link names.
  13. * Set DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE to link names.
  14. *
  15. * ORDERED
  16. * Items are ordered in the order they are added (new items are appended at the end).
  17. * You may reverse the order by setting the flag DICTIONARY_FLAG_ADD_IN_FRONT.
  18. *
  19. * LOOKUP
  20. * The dictionary uses JudyHS to maintain a very fast randomly accessible hash table.
  21. *
  22. * MULTI-THREADED and SINGLE-THREADED
  23. * Each dictionary may be single threaded (no locks), or multi-threaded (multiple readers or one writer).
  24. * The default is multi-threaded. Add the flag DICTIONARY_FLAG_SINGLE_THREADED for single-threaded.
  25. *
  26. * WALK-THROUGH and FOREACH traversal
  27. * The dictionary can be traversed on read or write mode, either with a callback (walkthrough) or with
  28. * a loop (foreach).
  29. *
  30. * In write mode traversal, the caller may delete only the current item, but may add as many items as needed.
  31. *
  32. */
  33. #ifndef DICTIONARY_INTERNALS
  34. typedef void DICTIONARY;
  35. #endif
  36. typedef enum dictionary_flags {
  37. DICTIONARY_FLAG_NONE = 0, // the default is the opposite of all below
  38. DICTIONARY_FLAG_SINGLE_THREADED = (1 << 0), // don't use any locks (default: use locks)
  39. DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE = (1 << 1), // don't copy the value, just point to the one provided (default: copy)
  40. DICTIONARY_FLAG_NAME_LINK_DONT_CLONE = (1 << 2), // don't copy the name, just point to the one provided (default: copy)
  41. DICTIONARY_FLAG_WITH_STATISTICS = (1 << 3), // maintain statistics about dictionary operations (default: disabled)
  42. DICTIONARY_FLAG_DONT_OVERWRITE_VALUE = (1 << 4), // don't overwrite values of dictionary items (default: overwrite)
  43. DICTIONARY_FLAG_ADD_IN_FRONT = (1 << 5), // add dictionary items at the front of the linked list (default: at the end)
  44. DICTIONARY_FLAG_RESERVED1 = (1 << 6), // this is reserved for DICTIONARY_FLAG_REFERENCE_COUNTERS
  45. } DICTIONARY_FLAGS;
  46. // Create a dictionary
  47. extern DICTIONARY *dictionary_create(DICTIONARY_FLAGS flags);
  48. // an insert callback to be called just after an item is added to the dictionary
  49. // this callback is called while the dictionary is write locked!
  50. extern void dictionary_register_insert_callback(DICTIONARY *dict, void (*ins_callback)(const char *name, void *value, void *data), void *data);
  51. // a delete callback to be called just before an item is deleted forever
  52. // this callback is called while the dictionary is write locked!
  53. extern void dictionary_register_delete_callback(DICTIONARY *dict, void (*del_callback)(const char *name, void *value, void *data), void *data);
  54. // a merge callback to be called when DICTIONARY_FLAG_DONT_OVERWRITE_VALUE
  55. // and an item is already found in the dictionary - the dictionary does nothing else in this case
  56. // the old_value will remain in the dictionary - the new_value is ignored
  57. extern void dictionary_register_conflict_callback(DICTIONARY *dict, void (*conflict_callback)(const char *name, void *old_value, void *new_value, void *data), void *data);
  58. // Destroy a dictionary
  59. // returns the number of bytes freed
  60. // the returned value will not include name and value sizes if DICTIONARY_FLAG_WITH_STATISTICS is not set
  61. extern size_t dictionary_destroy(DICTIONARY *dict);
  62. // Set an item in the dictionary
  63. // - if an item with the same name does not exist, create one
  64. // - if an item with the same name exists, then:
  65. // a) if DICTIONARY_FLAG_DONT_OVERWRITE_VALUE is set, just return the existing value (ignore the new value)
  66. // else b) reset the value to the new value passed at the call
  67. //
  68. // When DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE is set, the value is linked, otherwise it is copied
  69. // When DICTIONARY_FLAG_NAME_LINK_DONT_CLONE is set, the name is linked, otherwise it is copied
  70. //
  71. // When neither DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE nor DICTIONARY_FLAG_NAME_LINK_DONT_CLONE are set, all the
  72. // memory management for names and values is done by the dictionary.
  73. //
  74. // Passing NULL as value, the dictionary will callocz() the newly allocated value, otherwise it will copy it.
  75. // Passing 0 as value_len, the dictionary will set the value to NULL (no allocations for value will be made).
  76. extern void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) NEVERNULL;
  77. // Get an item from the dictionary
  78. // If it returns NULL, the item is not found
  79. extern void *dictionary_get(DICTIONARY *dict, const char *name);
  80. // Delete an item from the dictionary
  81. // returns 0 if the item was found and has been deleted
  82. // returns -1 if the item was not found in the index
  83. extern int dictionary_del(DICTIONARY *dict, const char *name);
  84. // UNSAFE functions, without locks
  85. // to be used when the user is traversing with the right lock type
  86. // Read lock is acquired by dictionary_walktrhough_read() and dfe_start_read()
  87. // Write lock is acquired by dictionary_walktrhough_write() and dfe_start_write()
  88. // For code readability, please use these macros:
  89. #define dictionary_get_having_read_lock(dict, name) dictionary_get_unsafe(dict, name)
  90. #define dictionary_get_having_write_lock(dict, name) dictionary_get_unsafe(dict, name)
  91. #define dictionary_set_having_write_lock(dict, name, value, value_len) dictionary_set_unsafe(dict, name, value, value_len)
  92. #define dictionary_del_having_write_lock(dict, name) dictionary_del_unsafe(dict, name)
  93. extern void *dictionary_get_unsafe(DICTIONARY *dict, const char *name);
  94. extern void *dictionary_set_unsafe(DICTIONARY *dict, const char *name, void *value, size_t value_len);
  95. extern int dictionary_del_unsafe(DICTIONARY *dict, const char *name);
  96. // Traverse (walk through) the items of the dictionary.
  97. // The order of traversal is currently the order of insertion.
  98. //
  99. // The callback function may return a negative number to stop the traversal,
  100. // in which case that negative value is returned to the caller.
  101. //
  102. // If all callback calls return zero or positive numbers, the sum of all of
  103. // them is returned to the caller.
  104. //
  105. // You cannot alter the dictionary from inside a dictionary_walkthrough_read() - deadlock!
  106. // You can only delete the current item from inside a dictionary_walkthrough_write() - you can add as many as you want.
  107. //
  108. #define dictionary_walkthrough_read(dict, callback, data) dictionary_walkthrough_rw(dict, 'r', callback, data)
  109. #define dictionary_walkthrough_write(dict, callback, data) dictionary_walkthrough_rw(dict, 'w', callback, data)
  110. extern int dictionary_walkthrough_rw(DICTIONARY *dict, char rw, int (*callback)(const char *name, void *value, void *data), void *data);
  111. // Traverse with foreach
  112. //
  113. // Use like this:
  114. //
  115. // DICTFE dfe = {};
  116. // for(MY_ITEM *item = dfe_start_read(&dfe, dict); item ; item = dfe_next(&dfe)) {
  117. // // do things with the item and its dfe.name
  118. // }
  119. // dfe_done(&dfe);
  120. //
  121. // You cannot alter the dictionary from within a dfe_read_start() - deadlock!
  122. // You can only delete the current item from inside a dfe_start_write() - you can add as many as you want.
  123. //
  124. #ifdef DICTIONARY_INTERNALS
  125. #define DICTFE_CONST
  126. #else
  127. #define DICTFE_CONST const
  128. #endif
  129. typedef DICTFE_CONST struct dictionary_foreach {
  130. DICTFE_CONST char *name; // the dictionary name of the last item used
  131. void *value; // the dictionary value of the last item used
  132. // same as the return value of dictfe_start() and dictfe_next()
  133. // the following are for internal use only - to keep track of the point we are
  134. usec_t started_ut; // the time the caller started iterating (now_realtime_usec())
  135. DICTIONARY *dict; // the dictionary upon we work
  136. void *last_position_index; // the internal position index, to remember the position we are at
  137. void *next_position_index; // the internal position index, of the next item
  138. } DICTFE;
  139. #define dfe_start_read(dict, value) dfe_start_rw(dict, value, 'r')
  140. #define dfe_start_write(dict, value) dfe_start_rw(dict, value, 'w')
  141. #define dfe_start_rw(dict, value, mode) \
  142. do { \
  143. DICTFE value ## _dfe = {}; \
  144. const char *value ## _name; (void)(value ## _name); \
  145. for((value) = dictionary_foreach_start_rw(&value ## _dfe, (dict), (mode)), ( value ## _name ) = value ## _dfe.name; \
  146. (value) ;\
  147. (value) = dictionary_foreach_next(&value ## _dfe), ( value ## _name ) = value ## _dfe.name)
  148. #define dfe_done(value) \
  149. dictionary_foreach_done(&value ## _dfe); \
  150. } while(0)
  151. extern void * dictionary_foreach_start_rw(DICTFE *dfe, DICTIONARY *dict, char rw);
  152. extern void * dictionary_foreach_next(DICTFE *dfe);
  153. extern usec_t dictionary_foreach_done(DICTFE *dfe);
  154. // Get statistics about the dictionary
  155. // If DICTIONARY_FLAG_WITH_STATISTICS is not set, these return zero
  156. extern size_t dictionary_stats_allocated_memory(DICTIONARY *dict);
  157. extern size_t dictionary_stats_entries(DICTIONARY *dict);
  158. extern size_t dictionary_stats_inserts(DICTIONARY *dict);
  159. extern size_t dictionary_stats_searches(DICTIONARY *dict);
  160. extern size_t dictionary_stats_deletes(DICTIONARY *dict);
  161. extern size_t dictionary_stats_resets(DICTIONARY *dict);
  162. extern int dictionary_unittest(size_t entries);
  163. #endif /* NETDATA_DICTIONARY_H */