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 pthread_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 pthread_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 pthread_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(pthread_rwlock_t));
139 pthread_rwlock_init(dict->rwlock, NULL);
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);
167 // ----------------------------------------------------------------------------
169 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
170 debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
172 uint32_t hash = simple_hash(name);
174 dictionary_write_lock(dict);
176 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
178 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
180 nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
182 fatal("Cannot create name_value.");
185 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
187 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
188 debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
192 debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
194 // copy the new value without breaking
195 // any other thread accessing the same entry
196 void *new = mallocz(value_len),
199 memcpy(new, value, value_len);
202 debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
207 dictionary_unlock(dict);
212 void *dictionary_get(DICTIONARY *dict, const char *name) {
213 debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
215 dictionary_read_lock(dict);
216 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
217 dictionary_unlock(dict);
220 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
224 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
228 int dictionary_del(DICTIONARY *dict, const char *name) {
231 debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
233 dictionary_write_lock(dict);
235 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
237 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
241 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
242 dictionary_name_value_destroy_nolock(dict, nv);
246 dictionary_unlock(dict);
252 // ----------------------------------------------------------------------------
253 // API - walk through the dictionary
254 // the dictionary is locked for reading while this happens
255 // do not user other dictionary calls while walking the dictionary - deadlock!
257 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
258 int total = 0, ret = 0;
261 ret = dictionary_walker(a->avl_link[0], callback, data);
262 if(ret < 0) return ret;
266 ret = callback(((NAME_VALUE *)a)->value, data);
267 if(ret < 0) return ret;
271 ret = dictionary_walker(a->avl_link[1], callback, data);
272 if (ret < 0) return ret;
279 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
282 dictionary_read_lock(dict);
284 if(likely(dict->values_index.root))
285 ret = dictionary_walker(dict->values_index.root, callback, data);
287 dictionary_unlock(dict);