| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_DICTIONARY_CALLBACKS_H |
| 4 | #define NETDATA_DICTIONARY_CALLBACKS_H |
| 5 | |
| 6 | #include "dictionary-internals.h" |
| 7 | |
| 8 | // ---------------------------------------------------------------------------- |
| 9 | // callbacks execution |
| 10 | |
| 11 | static inline void dictionary_execute_insert_callback(DICTIONARY *dict, DICTIONARY_ITEM *item, void *constructor_data) { |
| 12 | if(likely(!dict->hooks || !dict->hooks->insert_callback)) |
| 13 | return; |
| 14 | |
| 15 | if(unlikely(is_view_dictionary(dict))) |
| 16 | fatal("DICTIONARY: called %s() on a view.", __FUNCTION__ ); |
| 17 | |
| 18 | dictionary_internal_error(false, dict, |
| 19 | "DICTIONARY: Running insert callback on item '%s' of dictionary", |
| 20 | item_get_name(item)); |
| 21 | |
| 22 | dict->hooks->insert_callback(item, item->shared->value, constructor_data?constructor_data:dict->hooks->insert_callback_data); |
| 23 | DICTIONARY_STATS_CALLBACK_INSERTS_PLUS1(dict); |
| 24 | } |
| 25 | |
| 26 | static inline bool dictionary_execute_conflict_callback(DICTIONARY *dict, DICTIONARY_ITEM *item, void *new_value, void *constructor_data) { |
| 27 | if(likely(!dict->hooks || !dict->hooks->conflict_callback)) |
| 28 | return false; |
| 29 | |
| 30 | if(unlikely(is_view_dictionary(dict))) |
| 31 | fatal("DICTIONARY: called %s() on a view.", __FUNCTION__ ); |
| 32 | |
| 33 | dictionary_internal_error(false, dict, |
| 34 | "DICTIONARY: Running conflict callback on item '%s' of dictionary", |
| 35 | item_get_name(item)); |
| 36 | |
| 37 | bool ret = dict->hooks->conflict_callback( |
| 38 | item, item->shared->value, new_value, |
| 39 | constructor_data ? constructor_data : dict->hooks->conflict_callback_data); |
| 40 | |
| 41 | DICTIONARY_STATS_CALLBACK_CONFLICTS_PLUS1(dict); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static inline void dictionary_execute_react_callback(DICTIONARY *dict, DICTIONARY_ITEM *item, void *constructor_data) { |
| 47 | if(likely(!dict->hooks || !dict->hooks->react_callback)) |
| 48 | return; |
| 49 | |
| 50 | if(unlikely(is_view_dictionary(dict))) |
| 51 | fatal("DICTIONARY: called %s() on a view.", __FUNCTION__ ); |
| 52 | |
| 53 | dictionary_internal_error(false, dict, |
| 54 | "DICTIONARY: Running react callback on item '%s' of dictionary", |
| 55 | item_get_name(item)); |
| 56 | |
| 57 | dict->hooks->react_callback(item, item->shared->value, |
| 58 | constructor_data?constructor_data:dict->hooks->react_callback_data); |
| 59 | |
| 60 | DICTIONARY_STATS_CALLBACK_REACTS_PLUS1(dict); |
| 61 | } |
| 62 | |
| 63 | static inline void dictionary_execute_delete_callback(DICTIONARY *dict, DICTIONARY_ITEM *item) { |
| 64 | if(likely(!dict->hooks || !dict->hooks->delete_callback)) |
| 65 | return; |
| 66 | |
| 67 | // We may execute delete callback on items deleted from a view, |
| 68 | // because we may have references to it, after the master is gone |
| 69 | // so, the shared structure will remain until the last reference is released. |
| 70 | |
| 71 | dictionary_internal_error(false, dict, |
| 72 | "DICTIONARY: Running delete callback on item '%s' of dictionary", |
| 73 | item_get_name(item)); |
| 74 | |
| 75 | dict->hooks->delete_callback(item, item->shared->value, dict->hooks->delelte_callback_data); |
| 76 | |
| 77 | DICTIONARY_STATS_CALLBACK_DELETES_PLUS1(dict); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | #endif //NETDATA_DICTIONARY_CALLBACKS_H |