3 // ----------------------------------------------------------------------------
4 // dictionary statistics
6 static inline void NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(DICTIONARY *dict) {
7 if(likely(dict->stats))
8 dict->stats->inserts++;
10 static inline void NETDATA_DICTIONARY_STATS_DELETES_PLUS1(DICTIONARY *dict) {
11 if(likely(dict->stats))
12 dict->stats->deletes++;
14 static inline void NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
15 if(likely(dict->stats))
16 dict->stats->searches++;
18 static inline void NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict) {
19 if(likely(dict->stats))
20 dict->stats->entries++;
22 static inline void NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict) {
23 if(likely(dict->stats))
24 dict->stats->entries--;
28 // ----------------------------------------------------------------------------
31 static inline void dictionary_read_lock(DICTIONARY *dict) {
32 if(likely(dict->rwlock)) {
33 // debug(D_DICTIONARY, "Dictionary READ lock");
34 netdata_rwlock_rdlock(dict->rwlock);
38 static inline void dictionary_write_lock(DICTIONARY *dict) {
39 if(likely(dict->rwlock)) {
40 // debug(D_DICTIONARY, "Dictionary WRITE lock");
41 netdata_rwlock_wrlock(dict->rwlock);
45 static inline void dictionary_unlock(DICTIONARY *dict) {
46 if(likely(dict->rwlock)) {
47 // debug(D_DICTIONARY, "Dictionary UNLOCK lock");
48 netdata_rwlock_unlock(dict->rwlock);
53 // ----------------------------------------------------------------------------
56 static int name_value_compare(void* a, void* b) {
57 if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
58 else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
59 else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
62 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
64 tmp.hash = (hash)?hash:simple_hash(name);
65 tmp.name = (char *)name;
67 NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
68 return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
71 // ----------------------------------------------------------------------------
74 static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
75 debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
77 NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
79 if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
80 nv->name = (char *)name;
82 nv->name = strdupz(name);
85 nv->hash = (hash)?hash:simple_hash(nv->name);
87 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
90 nv->value = mallocz(value_len);
91 memcpy(nv->value, value, value_len);
95 NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict);
96 if(unlikely(avl_insert(&((dict)->values_index), (avl *)(nv)) != (avl *)nv))
97 error("dictionary: INTERNAL ERROR: duplicate insertion to dictionary.");
99 NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict);
104 static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
105 debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
107 NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict);
108 if(unlikely(avl_remove(&(dict->values_index), (avl *)(nv)) != (avl *)nv))
109 error("dictionary: INTERNAL ERROR: dictionary invalid removal of node.");
111 NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
113 if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
114 debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
118 if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
119 debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
126 // ----------------------------------------------------------------------------
127 // API - basic methods
129 DICTIONARY *dictionary_create(uint8_t flags) {
130 debug(D_DICTIONARY, "Creating dictionary.");
132 DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
134 if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
135 dict->stats = callocz(1, sizeof(struct dictionary_stats));
137 if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
138 dict->rwlock = callocz(1, sizeof(netdata_rwlock_t));
139 netdata_rwlock_init(dict->rwlock);
142 avl_init(&dict->values_index, name_value_compare);
148 void dictionary_destroy(DICTIONARY *dict) {
149 debug(D_DICTIONARY, "Destroying dictionary.");
151 dictionary_write_lock(dict);
153 while(dict->values_index.root)
154 dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
156 dictionary_unlock(dict);
162 netdata_rwlock_destroy(dict->rwlock);
169 // ----------------------------------------------------------------------------
171 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
172 debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
174 uint32_t hash = simple_hash(name);
176 dictionary_write_lock(dict);
178 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
180 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
182 nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
184 fatal("Cannot create name_value.");
187 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
189 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
190 debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
194 debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
196 // copy the new value without breaking
197 // any other thread accessing the same entry
198 void *new = mallocz(value_len),
201 memcpy(new, value, value_len);
204 debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
209 dictionary_unlock(dict);
214 void *dictionary_get(DICTIONARY *dict, const char *name) {
215 debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
217 dictionary_read_lock(dict);
218 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
219 dictionary_unlock(dict);
222 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
226 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
230 int dictionary_del(DICTIONARY *dict, const char *name) {
233 debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
235 dictionary_write_lock(dict);
237 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
239 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
243 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
244 dictionary_name_value_destroy_nolock(dict, nv);
248 dictionary_unlock(dict);
254 // ----------------------------------------------------------------------------
255 // API - walk through the dictionary
256 // the dictionary is locked for reading while this happens
257 // do not user other dictionary calls while walking the dictionary - deadlock!
259 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
260 int total = 0, ret = 0;
263 ret = dictionary_walker(a->avl_link[0], callback, data);
264 if(ret < 0) return ret;
268 ret = callback(((NAME_VALUE *)a)->value, data);
269 if(ret < 0) return ret;
273 ret = dictionary_walker(a->avl_link[1], callback, data);
274 if (ret < 0) return ret;
281 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
284 dictionary_read_lock(dict);
286 if(likely(dict->values_index.root))
287 ret = dictionary_walker(dict->values_index.root, callback, data);
289 dictionary_unlock(dict);