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 #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)
63 #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)
65 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
67 tmp.hash = (hash)?hash:simple_hash(name);
68 tmp.name = (char *)name;
70 NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
71 return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
74 // ----------------------------------------------------------------------------
77 static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
78 debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
80 NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
82 if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
83 nv->name = (char *)name;
85 nv->name = strdupz(name);
88 nv->hash = (hash)?hash:simple_hash(nv->name);
90 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
93 nv->value = mallocz(value_len);
94 memcpy(nv->value, value, value_len);
98 dictionary_name_value_index_add_nolock(dict, nv);
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 dictionary_name_value_index_del_nolock(dict, nv);
109 NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
111 if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
112 debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
116 if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
117 debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
124 // ----------------------------------------------------------------------------
125 // API - basic methods
127 DICTIONARY *dictionary_create(uint8_t flags) {
128 debug(D_DICTIONARY, "Creating dictionary.");
130 DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
132 if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
133 dict->stats = callocz(1, sizeof(struct dictionary_stats));
135 if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
136 dict->rwlock = callocz(1, sizeof(pthread_rwlock_t));
137 pthread_rwlock_init(dict->rwlock, NULL);
140 avl_init(&dict->values_index, name_value_compare);
146 void dictionary_destroy(DICTIONARY *dict) {
147 debug(D_DICTIONARY, "Destroying dictionary.");
149 dictionary_write_lock(dict);
151 while(dict->values_index.root)
152 dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
154 dictionary_unlock(dict);
165 // ----------------------------------------------------------------------------
167 void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
168 debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
170 uint32_t hash = simple_hash(name);
172 dictionary_write_lock(dict);
174 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
176 debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
178 nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
180 fatal("Cannot create name_value.");
183 debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
185 if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
186 debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
190 debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
192 // copy the new value without breaking
193 // any other thread accessing the same entry
194 void *new = mallocz(value_len),
197 memcpy(new, value, value_len);
200 debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
205 dictionary_unlock(dict);
210 void *dictionary_get(DICTIONARY *dict, const char *name) {
211 debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
213 dictionary_read_lock(dict);
214 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
215 dictionary_unlock(dict);
218 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
222 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
226 int dictionary_del(DICTIONARY *dict, const char *name) {
229 debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
231 dictionary_write_lock(dict);
233 NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
235 debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
239 debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
240 dictionary_name_value_destroy_nolock(dict, nv);
244 dictionary_unlock(dict);
250 // ----------------------------------------------------------------------------
251 // API - walk through the dictionary
252 // the dictionary is locked for reading while this happens
253 // do not user other dictionary calls while walking the dictionary - deadlock!
255 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
256 int total = 0, ret = 0;
259 ret = dictionary_walker(a->avl_link[0], callback, data);
260 if(ret < 0) return ret;
264 ret = callback(((NAME_VALUE *)a)->value, data);
265 if(ret < 0) return ret;
269 ret = dictionary_walker(a->avl_link[1], callback, data);
270 if (ret < 0) return ret;
277 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
280 dictionary_read_lock(dict);
282 if(likely(dict->values_index.root))
283 ret = dictionary_walker(dict->values_index.root, callback, data);
285 dictionary_unlock(dict);