]> arthur.barton.de Git - netdata.git/blob - src/dictionary.c
ab-debian 0.20170311.01-0ab1, upstream v1.5.0-573-g0fba967b
[netdata.git] / src / dictionary.c
1 #include "common.h"
2
3 // ----------------------------------------------------------------------------
4 // dictionary statistics
5
6 static inline void NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(DICTIONARY *dict) {
7     if(likely(dict->stats))
8         dict->stats->inserts++;
9 }
10 static inline void NETDATA_DICTIONARY_STATS_DELETES_PLUS1(DICTIONARY *dict) {
11     if(likely(dict->stats))
12         dict->stats->deletes++;
13 }
14 static inline void NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
15     if(likely(dict->stats))
16         dict->stats->searches++;
17 }
18 static inline void NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict) {
19     if(likely(dict->stats))
20         dict->stats->entries++;
21 }
22 static inline void NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict) {
23     if(likely(dict->stats))
24         dict->stats->entries--;
25 }
26
27
28 // ----------------------------------------------------------------------------
29 // dictionary locks
30
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);
35     }
36 }
37
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);
42     }
43 }
44
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);
49     }
50 }
51
52
53 // ----------------------------------------------------------------------------
54 // avl index
55
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);
60 }
61
62 static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
63     NAME_VALUE tmp;
64     tmp.hash = (hash)?hash:simple_hash(name);
65     tmp.name = (char *)name;
66
67     NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
68     return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
69 }
70
71 // ----------------------------------------------------------------------------
72 // internal methods
73
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);
76
77     NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
78
79     if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
80         nv->name = (char *)name;
81     else {
82         nv->name = strdupz(name);
83     }
84
85     nv->hash = (hash)?hash:simple_hash(nv->name);
86
87     if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
88         nv->value = value;
89     else {
90         nv->value = mallocz(value_len);
91         memcpy(nv->value, value, value_len);
92     }
93
94     // index it
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.");
98
99     NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict);
100
101     return nv;
102 }
103
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);
106
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.");
110
111     NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
112
113     if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
114         debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
115         freez(nv->value);
116     }
117
118     if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
119         debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
120         freez(nv->name);
121     }
122
123     freez(nv);
124 }
125
126 // ----------------------------------------------------------------------------
127 // API - basic methods
128
129 DICTIONARY *dictionary_create(uint8_t flags) {
130     debug(D_DICTIONARY, "Creating dictionary.");
131
132     DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
133
134     if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
135         dict->stats = callocz(1, sizeof(struct dictionary_stats));
136
137     if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
138         dict->rwlock = callocz(1, sizeof(pthread_rwlock_t));
139         pthread_rwlock_init(dict->rwlock, NULL);
140     }
141
142     avl_init(&dict->values_index, name_value_compare);
143     dict->flags = flags;
144
145     return dict;
146 }
147
148 void dictionary_destroy(DICTIONARY *dict) {
149     debug(D_DICTIONARY, "Destroying dictionary.");
150
151     dictionary_write_lock(dict);
152
153     while(dict->values_index.root)
154         dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
155
156     dictionary_unlock(dict);
157
158     if(dict->stats)
159         freez(dict->stats);
160
161     if(dict->rwlock)
162         freez(dict->rwlock);
163
164     freez(dict);
165 }
166
167 // ----------------------------------------------------------------------------
168
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);
171
172     uint32_t hash = simple_hash(name);
173
174     dictionary_write_lock(dict);
175
176     NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
177     if(unlikely(!nv)) {
178         debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
179
180         nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
181         if(unlikely(!nv))
182             fatal("Cannot create name_value.");
183     }
184     else {
185         debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
186
187         if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
188             debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
189             nv->value = value;
190         }
191         else {
192             debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
193
194             // copy the new value without breaking
195             // any other thread accessing the same entry
196             void *new = mallocz(value_len),
197                     *old = nv->value;
198
199             memcpy(new, value, value_len);
200             nv->value = new;
201
202             debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
203             freez(old);
204         }
205     }
206
207     dictionary_unlock(dict);
208
209     return nv->value;
210 }
211
212 void *dictionary_get(DICTIONARY *dict, const char *name) {
213     debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
214
215     dictionary_read_lock(dict);
216     NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
217     dictionary_unlock(dict);
218
219     if(unlikely(!nv)) {
220         debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
221         return NULL;
222     }
223
224     debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
225     return nv->value;
226 }
227
228 int dictionary_del(DICTIONARY *dict, const char *name) {
229     int ret;
230
231     debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
232
233     dictionary_write_lock(dict);
234
235     NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
236     if(unlikely(!nv)) {
237         debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
238         ret = -1;
239     }
240     else {
241         debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
242         dictionary_name_value_destroy_nolock(dict, nv);
243         ret = 0;
244     }
245
246     dictionary_unlock(dict);
247
248     return ret;
249 }
250
251
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!
256
257 static int dictionary_walker(avl *a, int (*callback)(void *entry, void *data), void *data) {
258     int total = 0, ret = 0;
259
260     if(a->avl_link[0]) {
261         ret = dictionary_walker(a->avl_link[0], callback, data);
262         if(ret < 0) return ret;
263         total += ret;
264     }
265
266     ret = callback(((NAME_VALUE *)a)->value, data);
267     if(ret < 0) return ret;
268     total += ret;
269
270     if(a->avl_link[1]) {
271         ret = dictionary_walker(a->avl_link[1], callback, data);
272         if (ret < 0) return ret;
273         total += ret;
274     }
275
276     return total;
277 }
278
279 int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
280     int ret = 0;
281
282     dictionary_read_lock(dict);
283
284     if(likely(dict->values_index.root))
285         ret = dictionary_walker(dict->values_index.root, callback, data);
286
287     dictionary_unlock(dict);
288
289     return ret;
290 }