13 #include "dictionary.h"
15 // ----------------------------------------------------------------------------
18 static inline void dictionary_read_lock(DICTIONARY *dict) {
19 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
20 // debug(D_DICTIONARY, "Dictionary READ lock");
21 pthread_rwlock_rdlock(&dict->rwlock);
25 static inline void dictionary_write_lock(DICTIONARY *dict) {
26 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
27 // debug(D_DICTIONARY, "Dictionary WRITE lock");
28 pthread_rwlock_wrlock(&dict->rwlock);
32 static inline void dictionary_unlock(DICTIONARY *dict) {
33 if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
34 // debug(D_DICTIONARY, "Dictionary UNLOCK lock");
35 pthread_rwlock_unlock(&dict->rwlock);
40 // ----------------------------------------------------------------------------
43 static int name_value_compare(void* a, void* b) {
44 if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
45 else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
46 else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
49 #define dictionary_name_value_index_add_nolock(dict, nv) do { NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict); avl_insert(&((dict)->values_index), (avl *)(nv)); } while(0)
50 #define dictionary_name_value_index_del_nolock(dict, nv) do { NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict); avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
52 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
54 tmp.hash = (hash)?hash:simple_hash(name);
55 tmp.name = (char *)name;
57 NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
58 return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
61 // ----------------------------------------------------------------------------
64 static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
65 debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
67 NAME_VALUE *nv = calloc(1, sizeof(NAME_VALUE));
68 if(unlikely(!nv)) fatal("Cannot allocate name_value of size %z", sizeof(NAME_VALUE));
70 if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
71 nv->name = (char *)name;
73 nv->name = strdup(name);
74 if (unlikely(!nv->name))
75 fatal("Cannot allocate name_value.name of size %z", strlen(name));
78 nv->hash = (hash)?hash:simple_hash(nv->name);
80 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
83 nv->value = malloc(value_len);
84 if (unlikely(!nv->value))
85 fatal("Cannot allocate name_value.value of size %z", value_len);
87 memcpy(nv->value, value, value_len);
91 dictionary_name_value_index_add_nolock(dict, nv);
92 NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict);
97 static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
98 debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
100 dictionary_name_value_index_del_nolock(dict, nv);
102 NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
104 if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
105 debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
109 if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
110 debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
117 // ----------------------------------------------------------------------------
118 // API - basic methods
120 DICTIONARY *dictionary_create(uint32_t flags) {
121 debug(D_DICTIONARY, "Creating dictionary.");
123 DICTIONARY *dict = calloc(1, sizeof(DICTIONARY));
124 if(unlikely(!dict)) fatal("Cannot allocate DICTIONARY");
126 avl_init(&dict->values_index, name_value_compare);
127 pthread_rwlock_init(&dict->rwlock, NULL);
134 void dictionary_destroy(DICTIONARY *dict) {
135 debug(D_DICTIONARY, "Destroying dictionary.");
137 dictionary_write_lock(dict);
139 while(dict->values_index.root)
140 dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
142 dictionary_unlock(dict);
147 // ----------------------------------------------------------------------------
149 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
150 debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
152 uint32_t hash = simple_hash(name);
154 dictionary_write_lock(dict);
156 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
158 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
160 nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
162 fatal("Cannot create name_value.");
165 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
167 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
168 debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
172 debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
174 void *value = malloc(value_len),
177 if(unlikely(!nv->value))
178 fatal("Cannot allocate value of size %z", value_len);
180 memcpy(value, value, value_len);
183 debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
188 dictionary_unlock(dict);
193 void *dictionary_get(DICTIONARY *dict, const char *name) {
194 debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
196 dictionary_read_lock(dict);
197 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
198 dictionary_unlock(dict);
201 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
205 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
209 int dictionary_del(DICTIONARY *dict, const char *name) {
212 debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
214 dictionary_write_lock(dict);
216 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
218 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
222 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
223 dictionary_name_value_destroy_nolock(dict, nv);
227 dictionary_unlock(dict);
233 // ----------------------------------------------------------------------------
234 // API - walk through the dictionary
235 // the dictionary is locked for reading while this happens
236 // do not user other dictionary calls while walking the dictionary - deadlock!
238 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
239 int total = 0, ret = 0;
242 ret = dictionary_walker(a->right, callback, data);
243 if(ret < 0) return ret;
247 ret = callback(((NAME_VALUE *)a)->value, data);
248 if(ret < 0) return ret;
252 dictionary_walker(a->left, callback, data);
253 if (ret < 0) return ret;
260 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
263 dictionary_read_lock(dict);
265 if(likely(dict->values_index.root))
266 ret = dictionary_walker(dict->values_index.root, callback, data);
268 dictionary_unlock(dict);