1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
+// NOT TO BE USED BY USERS YET
4
+#define DICTIONARY_FLAG_REFERENCE_COUNTERS (1 << 6) // maintain reference counter in walkthrough and foreach
5
+
6
+typedef struct dictionary DICTIONARY;
7
+#define DICTIONARY_INTERNALS
8
+
9
#include "../libnetdata.h"
10
11
+#ifndef ENABLE_DBENGINE
12
+#define DICTIONARY_WITH_AVL
13
+#warning Compiling DICTIONARY with an AVL index
14
+#else
15
+#define DICTIONARY_WITH_JUDYHS
16
+#endif
17
+
18
+#ifdef DICTIONARY_WITH_JUDYHS
19
+#include <Judy.h>
20
+#endif
21
+
22
+/*
23
+ * This version uses JudyHS arrays to index the dictionary
24
+ *
25
+ * The following output is from the unit test, at the end of this file:
26
+ *
27
+ * This is the JudyHS version:
28
+ *
29
+ * 1000000 x dictionary_set() (dictionary size 0 entries, 0 KB)...
30
+ * 1000000 x dictionary_get(existing) (dictionary size 1000000 entries, 74001 KB)...
31
+ * 1000000 x dictionary_get(non-existing) (dictionary size 1000000 entries, 74001 KB)...
32
+ * Walking through the dictionary (dictionary size 1000000 entries, 74001 KB)...
33
+ * 1000000 x dictionary_del(existing) (dictionary size 1000000 entries, 74001 KB)...
34
+ * 1000000 x dictionary_set() (dictionary size 0 entries, 0 KB)...
35
+ * Destroying dictionary (dictionary size 1000000 entries, 74001 KB)...
36
+ *
37
+ * TIMINGS:
38
+ * adding 316027 usec, positive search 156740 usec, negative search 84524, walk through 15036 usec, deleting 361444, destroy 107394 usec
39
+ *
40
+ * This is from the JudySL version:
41
+ *
42
+ * Creating dictionary of 1000000 entries...
43
+ * Checking index of 1000000 entries...
44
+ * Walking 1000000 entries and checking name-value pairs...
45
+ * Created and checked 1000000 entries, found 0 errors - used 58376 KB of memory
46
+ * Destroying dictionary of 1000000 entries...
47
+ * Deleted 1000000 entries
48
+ * create 338975 usec, check 156080 usec, walk 80764 usec, destroy 444569 usec
49
+ *
50
+ * This is the AVL version:
51
+ *
52
+ * Creating dictionary of 1000000 entries...
53
+ * Checking index of 1000000 entries...
54
+ * Walking 1000000 entries and checking name-value pairs...
55
+ * Created and checked 1000000 entries, found 0 errors - used 89626 KB of memory
56
+ * Destroying dictionary of 1000000 entries...
57
+ * create 413892 usec, check 220006 usec, walk 34247 usec, destroy 98062 usec
58
+ *
59
+ * So, the JudySL is a lot slower to WALK and DESTROY (DESTROY does a WALK)
60
+ * It is slower, because for every item, JudySL copies the KEY/NAME to a
61
+ * caller supplied buffer (Index). So, by just walking over 1 million items,
62
+ * JudySL does 1 million strcpy() !!!
63
+ *
64
+ * It also seems that somehow JudySLDel() is unbelievably slow too!
65
+ *
66
+ */
67
+
68
+
69
+/*
70
+ * Every item in the dictionary has the following structure.
71
+ */
72
+typedef struct name_value {
73
+#ifdef DICTIONARY_WITH_AVL
74
+ avl_t avl_node;
75
+#endif
76
+
77
+ struct name_value *next; // a double linked list to allow fast insertions and deletions
78
+ struct name_value *prev;
79
+
80
+ char *name; // the name of the dictionary item
81
+ void *value; // the value of the dictionary item
82
+} NAME_VALUE;
83
+
84
+/*
85
+ * When DICTIONARY_FLAG_WITH_STATISTICS is set, we need to keep track of all the memory
86
+ * we allocate and free. So, we need to keep track of the sizes of all names and values.
87
+ * We do this by overloading NAME_VALUE with the following additional fields.
88
+ */
89
+
90
+typedef enum name_value_flags {
91
+ NAME_VALUE_FLAG_NONE = 0,
92
+ NAME_VALUE_FLAG_DELETED = (1 << 0), // this item is deleted
93
+} NAME_VALUE_FLAGS;
94
+
95
+typedef struct name_value_with_stats {
96
+ NAME_VALUE name_value_data_here; // never used - just to put the lengths at the right position
97
+
98
+ size_t name_len; // the size of the name, including the terminating zero
99
+ size_t value_len; // the size of the value (assumed binary)
100
+
101
+ size_t refcount; // the reference counter
102
+ NAME_VALUE_FLAGS flags; // the flags for this item
103
+} NAME_VALUE_WITH_STATS;
104
+
105
+struct dictionary_stats {
106
+ size_t inserts;
107
+ size_t deletes;
108
+ size_t searches;
109
+ size_t resets;
110
+ size_t entries;
111
+ size_t memory;
112
+};
113
+
114
+struct dictionary {
115
+ DICTIONARY_FLAGS flags; // the flags of the dictionary
116
+
117
+ NAME_VALUE *first_item; // the double linked list base pointers
118
+ NAME_VALUE *last_item;
119
+
120
+#ifdef DICTIONARY_WITH_AVL
121
+ avl_tree_type values_index;
122
+ NAME_VALUE *hash_base;
123
+#endif
124
+
125
+#ifdef DICTIONARY_WITH_JUDYHS
126
+ Pvoid_t JudyHSArray; // the hash table
127
+#endif
128
+
129
+ netdata_rwlock_t *rwlock; // the r/w lock when DICTIONARY_FLAG_SINGLE_THREADED is not set
130
+
131
+ void (*ins_callback)(const char *name, void *value, void *data);
132
+ void *ins_callback_data;
133
+
134
+ void (*del_callback)(const char *name, void *value, void *data);
135
+ void *del_callback_data;
136
+
137
+ struct dictionary_stats *stats; // the statistics when DICTIONARY_FLAG_WITH_STATISTICS is set
138
+};
139
+
140
+void dictionary_register_insert_callback(DICTIONARY *dict, void (*ins_callback)(const char *name, void *value, void *data), void *data) {
141
+ dict->ins_callback = ins_callback;
142
+ dict->ins_callback_data = data;
143
+}
144
+
145
+void dictionary_register_delete_callback(DICTIONARY *dict, void (*del_callback)(const char *name, void *value, void *data), void *data) {
146
+ dict->del_callback = del_callback;
147
+ dict->del_callback_data = data;
148
+}
149
+
150
// ----------------------------------------------------------------------------
6
-// dictionary statistics
151
+// dictionary statistics maintenance
152
8
-static inline void NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(DICTIONARY *dict) {
9
- if(likely(dict->stats))
10
- dict->stats->inserts++;
153
+size_t dictionary_stats_allocated_memory(DICTIONARY *dict) {
154
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
155
+ return dict->stats->memory;
156
+ return 0;
157
}
12
-static inline void NETDATA_DICTIONARY_STATS_DELETES_PLUS1(DICTIONARY *dict) {
13
- if(likely(dict->stats))
14
- dict->stats->deletes++;
158
+size_t dictionary_stats_entries(DICTIONARY *dict) {
159
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
160
+ return dict->stats->entries;
161
+ return 0;
162
+}
163
+size_t dictionary_stats_searches(DICTIONARY *dict) {
164
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
165
+ return dict->stats->searches;
166
+ return 0;
167
+}
168
+size_t dictionary_stats_inserts(DICTIONARY *dict) {
169
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
170
+ return dict->stats->inserts;
171
+ return 0;
172
}
16
-static inline void NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
17
- if(likely(dict->stats))
173
+size_t dictionary_stats_deletes(DICTIONARY *dict) {
174
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
175
+ return dict->stats->deletes;
176
+ return 0;
177
+}
178
+size_t dictionary_stats_resets(DICTIONARY *dict) {
179
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
180
+ return dict->stats->resets;
181
+ return 0;
182
+}
183
+
184
+static inline void DICTIONARY_STATS_SEARCHES_PLUS1(DICTIONARY *dict) {
185
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS))
186
dict->stats->searches++;
187
}
20
-static inline void NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict) {
21
- if(likely(dict->stats))
188
+static inline void DICTIONARY_STATS_ENTRIES_PLUS1(DICTIONARY *dict, size_t size) {
189
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
190
+ dict->stats->inserts++;
191
dict->stats->entries++;
192
+ dict->stats->memory += size;
193
+ }
194
}
24
-static inline void NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict) {
25
- if(likely(dict->stats))
195
+static inline void DICTIONARY_STATS_ENTRIES_MINUS1(DICTIONARY *dict, size_t size) {
196
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
197
+ dict->stats->deletes++;
198
dict->stats->entries--;
199
+ dict->stats->memory -= size;
200
+ }
201
+}
202
+static inline void DICTIONARY_STATS_VALUE_RESETS_PLUS1(DICTIONARY *dict, size_t oldsize, size_t newsize) {
203
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
204
+ dict->stats->resets++;
205
+ dict->stats->memory += newsize;
206
+ dict->stats->memory -= oldsize;
207
+ }
208
}
28
-
209
210
// ----------------------------------------------------------------------------
211
// dictionary locks
212
33
-static inline void dictionary_read_lock(DICTIONARY *dict) {
34
- if(likely(dict->rwlock)) {
213
+static inline size_t dictionary_lock_init(DICTIONARY *dict) {
214
+ if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
215
+ dict->rwlock = mallocz(sizeof(netdata_rwlock_t));
216
+ netdata_rwlock_init(dict->rwlock);
217
+ return sizeof(netdata_rwlock_t);
218
+ }
219
+ dict->rwlock = NULL;
220
+ return 0;
221
+}
222
+
223
+static inline size_t dictionary_lock_free(DICTIONARY *dict) {
224
+ if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
225
+ netdata_rwlock_destroy(dict->rwlock);
226
+ freez(dict->rwlock);
227
+ return sizeof(netdata_rwlock_t);
228
+ }
229
+ return 0;
230
+}
231
+
232
+static inline void dictionary_lock_rlock(DICTIONARY *dict) {
233
+ if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
234
// debug(D_DICTIONARY, "Dictionary READ lock");
235
netdata_rwlock_rdlock(dict->rwlock);
236
}
237
}
238
40
-static inline void dictionary_write_lock(DICTIONARY *dict) {
41
- if(likely(dict->rwlock)) {
239
+static inline void dictionary_lock_wrlock(DICTIONARY *dict) {
240
+ if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
241
// debug(D_DICTIONARY, "Dictionary WRITE lock");
242
netdata_rwlock_wrlock(dict->rwlock);
243
}
244
}
245
246
static inline void dictionary_unlock(DICTIONARY *dict) {
48
- if(likely(dict->rwlock)) {
247
+ if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
248
// debug(D_DICTIONARY, "Dictionary UNLOCK lock");
249
netdata_rwlock_unlock(dict->rwlock);
250
}
251
}
252
253
+// ----------------------------------------------------------------------------
254
+// reference counters
255
+
256
+static inline size_t reference_counter_init(DICTIONARY *dict) {
257
+ (void)dict;
258
+
259
+ // allocate memory required for reference counters
260
+ // return number of bytes
261
+ return 0;
262
+}
263
+
264
+static inline size_t reference_counter_free(DICTIONARY *dict) {
265
+ (void)dict;
266
+
267
+ // free memory required for reference counters
268
+ // return number of bytes
269
+ return 0;
270
+}
271
+
272
+static void reference_counter_acquire(DICTIONARY *dict, NAME_VALUE *nv) {
273
+ if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
274
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
275
+ __atomic_fetch_add(&nvs->refcount, 1, __ATOMIC_SEQ_CST);
276
+ }
277
+}
278
+
279
+static void reference_counter_release(DICTIONARY *dict, NAME_VALUE *nv) {
280
+ if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
281
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
282
+ __atomic_fetch_sub(&nvs->refcount, 1, __ATOMIC_SEQ_CST);
283
+ }
284
+}
285
+
286
+static int reference_counter_mark_deleted(DICTIONARY *dict, NAME_VALUE *nv) {
287
+ if(unlikely(dict->flags & DICTIONARY_FLAG_REFERENCE_COUNTERS)) {
288
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
289
+ nvs->flags |= NAME_VALUE_FLAG_DELETED;
290
+ return 1;
291
+ }
292
+ return 0;
293
+}
294
295
// ----------------------------------------------------------------------------
56
-// avl index
296
+// hash table
297
298
+#ifdef DICTIONARY_WITH_AVL
299
static int name_value_compare(void* a, void* b) {
59
- if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
60
- else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
61
- else return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
300
+ return strcmp(((NAME_VALUE *)a)->name, ((NAME_VALUE *)b)->name);
301
}
302
64
-static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
303
+static void hashtable_init_unsafe(DICTIONARY *dict) {
304
+ avl_init(&dict->values_index, name_value_compare);
305
+}
306
+
307
+static size_t hashtable_destroy_unsafe(DICTIONARY *dict) {
308
+ (void)dict;
309
+ return 0;
310
+}
311
+
312
+static inline int hashtable_delete_unsafe(DICTIONARY *dict, const char *name, size_t name_len, NAME_VALUE *nv) {
313
+ (void)name;
314
+ (void)name_len;
315
+
316
+ if(unlikely(avl_remove(&(dict->values_index), (avl_t *)(nv)) != (avl_t *)nv))
317
+ return 0;
318
+
319
+ return 1;
320
+}
321
+
322
+static inline NAME_VALUE *hashtable_get_unsafe(DICTIONARY *dict, const char *name, size_t name_len) {
323
+ (void)name_len;
324
+
325
NAME_VALUE tmp;
66
- tmp.hash = (hash)?hash:simple_hash(name);
326
tmp.name = (char *)name;
68
-
69
- NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict);
327
return (NAME_VALUE *)avl_search(&(dict->values_index), (avl_t *) &tmp);
328
}
329
330
+static inline NAME_VALUE **hashtable_insert_unsafe(DICTIONARY *dict, const char *name, size_t name_len) {
331
+ // AVL needs a NAME_VALUE to insert into the dictionary but we don't have it yet.
332
+ // So, the only thing we can do, is return an existing one if it is already there.
333
+ // Returning NULL will make the caller thing we added it, will allocate one
334
+ // and will call hashtable_inserted_name_value_unsafe(), at which we will do
335
+ // the actual indexing.
336
+
337
+ dict->hash_base = hashtable_get_unsafe(dict, name, name_len);
338
+ return &dict->hash_base;
339
+}
340
+
341
+static inline void hashtable_inserted_name_value_unsafe(DICTIONARY *dict, const char *name, size_t name_len, NAME_VALUE *nv) {
342
+ // we have our new NAME_VALUE object.
343
+ // Let's index it.
344
+
345
+ (void)name;
346
+ (void)name_len;
347
+
348
+ if(unlikely(avl_insert(&((dict)->values_index), (avl_t *)(nv)) != (avl_t *)nv))
349
+ error("dictionary: INTERNAL ERROR: duplicate insertion to dictionary.");
350
+}
351
+#endif
352
+
353
+#ifdef DICTIONARY_WITH_JUDYHS
354
+static void hashtable_init_unsafe(DICTIONARY *dict) {
355
+ dict->JudyHSArray = NULL;
356
+}
357
+
358
+static size_t hashtable_destroy_unsafe(DICTIONARY *dict) {
359
+ if(unlikely(!dict->JudyHSArray)) return 0;
360
+
361
+ JError_t J_Error;
362
+ Word_t ret = JudyHSFreeArray(&dict->JudyHSArray, &J_Error);
363
+ if(unlikely(ret == (Word_t) JERR)) {
364
+ error("DICTIONARY: Cannot destroy JudyHS, JU_ERRNO_* == %u, ID == %d",
365
+ JU_ERRNO(&J_Error), JU_ERRID(&J_Error));
366
+ }
367
+
368
+ debug(D_DICTIONARY, "Dictionary: hash table freed %lu bytes", ret);
369
+
370
+ dict->JudyHSArray = NULL;
371
+ return (size_t)ret;
372
+}
373
+
374
+static inline NAME_VALUE **hashtable_insert_unsafe(DICTIONARY *dict, const char *name, size_t name_len) {
375
+ JError_t J_Error;
376
+ Pvoid_t *Rc = JudyHSIns(&dict->JudyHSArray, (void *)name, name_len, &J_Error);
377
+ if (unlikely(Rc == PJERR)) {
378
+ fatal("DICTIONARY: Cannot insert entry with name '%s' to JudyHS, JU_ERRNO_* == %u, ID == %d",
379
+ name, JU_ERRNO(&J_Error), JU_ERRID(&J_Error));
380
+ }
381
+
382
+ // if *Rc == 0, new item added to the array
383
+ // otherwise the existing item value is returned in *Rc
384
+
385
+ // we return a pointer to a pointer, so that the caller can
386
+ // put anything needed at the value of the index.
387
+ // The pointer to pointer we return has to be used before
388
+ // any other operation that may change the index (insert/delete).
389
+ return (NAME_VALUE **)Rc;
390
+}
391
+
392
+static inline int hashtable_delete_unsafe(DICTIONARY *dict, const char *name, size_t name_len, NAME_VALUE *nv) {
393
+ (void)nv;
394
+
395
+ if(unlikely(!dict->JudyHSArray)) return 0;
396
+
397
+ JError_t J_Error;
398
+ int ret = JudyHSDel(&dict->JudyHSArray, (void *)name, name_len, &J_Error);
399
+ if(unlikely(ret == JERR)) {
400
+ error("DICTIONARY: Cannot delete entry with name '%s' from JudyHS, JU_ERRNO_* == %u, ID == %d", name,
401
+ JU_ERRNO(&J_Error), JU_ERRID(&J_Error));
402
+ return 0;
403
+ }
404
+
405
+ // Hey, this is problematic! We need the value back, not just an int with a status!
406
+ // https://sourceforge.net/p/judy/feature-requests/23/
407
+
408
+ if(unlikely(ret == 0)) {
409
+ // not found in the dictionary
410
+ return 0;
411
+ }
412
+ else {
413
+ // found and deleted from the dictionary
414
+ return 1;
415
+ }
416
+}
417
+
418
+static inline NAME_VALUE *hashtable_get_unsafe(DICTIONARY *dict, const char *name, size_t name_len) {
419
+ if(unlikely(!dict->JudyHSArray)) return NULL;
420
+
421
+ DICTIONARY_STATS_SEARCHES_PLUS1(dict);
422
+
423
+ Pvoid_t *Rc;
424
+ Rc = JudyHSGet(dict->JudyHSArray, (void *)name, name_len);
425
+ if(likely(Rc)) {
426
+ // found in the hash table
427
+ return (NAME_VALUE *)*Rc;
428
+ }
429
+ else {
430
+ // not found in the hash table
431
+ return NULL;
432
+ }
433
+}
434
+
435
+static inline void hashtable_inserted_name_value_unsafe(DICTIONARY *dict, const char *name, size_t name_len, NAME_VALUE *nv) {
436
+ (void)dict;
437
+ (void)name;
438
+ (void)name_len;
439
+ (void)nv;
440
+ ;
441
+}
442
+
443
+#endif // DICTIONARY_WITH_JUDYHS
444
+
445
// ----------------------------------------------------------------------------
74
-// internal methods
446
+// linked list management
447
+
448
+static inline void linkedlist_namevalue_link_unsafe(DICTIONARY *dict, NAME_VALUE *nv) {
449
+ if (unlikely(!dict->first_item)) {
450
+ // we are the only ones here
451
+ nv->next = NULL;
452
+ nv->prev = NULL;
453
+ dict->first_item = dict->last_item = nv;
454
+ return;
455
+ }
456
76
-static NAME_VALUE *dictionary_name_value_create_nolock(DICTIONARY *dict, const char *name, void *value, size_t value_len, uint32_t hash) {
457
+ if(dict->flags & DICTIONARY_FLAG_ADD_IN_FRONT) {
458
+ // add it at the beginning
459
+ nv->prev = NULL;
460
+ nv->next = dict->first_item;
461
+
462
+ if (likely(nv->next)) nv->next->prev = nv;
463
+ dict->first_item = nv;
464
+ }
465
+ else {
466
+ // add it at the end
467
+ nv->next = NULL;
468
+ nv->prev = dict->last_item;
469
+
470
+ if (likely(nv->prev)) nv->prev->next = nv;
471
+ dict->last_item = nv;
472
+ }
473
+}
474
+
475
+static inline void linkedlist_namevalue_unlink_unsafe(DICTIONARY *dict, NAME_VALUE *nv) {
476
+ if(nv->next) nv->next->prev = nv->prev;
477
+ if(nv->prev) nv->prev->next = nv->next;
478
+ if(dict->first_item == nv) dict->first_item = nv->next;
479
+ if(dict->last_item == nv) dict->last_item = nv->prev;
480
+}
481
+
482
+// ----------------------------------------------------------------------------
483
+// NAME_VALUE methods
484
+
485
+static inline size_t namevalue_alloc_size(DICTIONARY *dict) {
486
+ return (dict->flags & DICTIONARY_FLAG_WITH_STATISTICS) ? sizeof(NAME_VALUE_WITH_STATS) : sizeof(NAME_VALUE);
487
+}
488
+
489
+static inline size_t namevalue_get_namelen(DICTIONARY *dict, NAME_VALUE *nv) {
490
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
491
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
492
+ return nvs->name_len;
493
+ }
494
+ return 0;
495
+}
496
+static inline size_t namevalue_get_valuelen(DICTIONARY *dict, NAME_VALUE *nv) {
497
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
498
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
499
+ return nvs->value_len;
500
+ }
501
+ return 0;
502
+}
503
+static inline void namevalue_set_valuelen(DICTIONARY *dict, NAME_VALUE *nv, size_t value_len) {
504
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
505
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
506
+ nvs->value_len = value_len;
507
+ }
508
+}
509
+static inline void namevalue_set_namevaluelen(DICTIONARY *dict, NAME_VALUE *nv, size_t name_len, size_t value_len) {
510
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
511
+ NAME_VALUE_WITH_STATS *nvs = (NAME_VALUE_WITH_STATS *)nv;
512
+ nvs->name_len = name_len;
513
+ nvs->value_len = value_len;
514
+ }
515
+}
516
+
517
+static NAME_VALUE *namevalue_create_unsafe(DICTIONARY *dict, const char *name, size_t name_len, void *value, size_t value_len) {
518
debug(D_DICTIONARY, "Creating name value entry for name '%s'.", name);
519
79
- NAME_VALUE *nv = callocz(1, sizeof(NAME_VALUE));
520
+ size_t size = namevalue_alloc_size(dict);
521
+ NAME_VALUE *nv = mallocz(size);
522
+ size_t allocated = size;
523
81
- if(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)
524
+ namevalue_set_namevaluelen(dict, nv, name_len, value_len);
525
+
526
+ if(likely(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE))
527
nv->name = (char *)name;
528
else {
84
- nv->name = strdupz(name);
529
+ nv->name = mallocz(name_len);
530
+ memcpy(nv->name, name, name_len);
531
+ allocated += name_len;
532
}
533
87
- nv->hash = (hash)?hash:simple_hash(nv->name);
88
-
89
- if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)
534
+ if(likely(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE))
535
nv->value = value;
536
else {
92
- nv->value = mallocz(value_len);
93
- memcpy(nv->value, value, value_len);
94
- }
537
+ if(likely(value_len)) {
538
+ if (value) {
539
+ // a value has been supplied
540
+ // copy it
541
+ nv->value = mallocz(value_len);
542
+ memcpy(nv->value, value, value_len);
543
+ }
544
+ else {
545
+ // no value has been supplied
546
+ // allocate a clear memory block
547
+ nv->value = callocz(1, value_len);
548
+ }
549
+ }
550
+ else {
551
+ // the caller want an item without any value
552
+ nv->value = NULL;
553
+ }
554
96
- // index it
97
- NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict);
98
- if(unlikely(avl_insert(&((dict)->values_index), (avl_t *)(nv)) != (avl_t *)nv))
99
- error("dictionary: INTERNAL ERROR: duplicate insertion to dictionary.");
555
+ allocated += value_len;
556
+ }
557
101
- NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict);
558
+ DICTIONARY_STATS_ENTRIES_PLUS1(dict, allocated);
559
560
return nv;
561
}
562
106
-static void dictionary_name_value_destroy_nolock(DICTIONARY *dict, NAME_VALUE *nv) {
107
- debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
563
+static void namevalue_reset_unsafe(DICTIONARY *dict, NAME_VALUE *nv, void *value, size_t value_len) {
564
+ debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", nv->name);
565
109
- NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict);
110
- if(unlikely(avl_remove(&(dict->values_index), (avl_t *)(nv)) != (avl_t *)nv))
111
- error("dictionary: INTERNAL ERROR: dictionary invalid removal of node.");
566
+ if(likely(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
567
+ debug(D_DICTIONARY, "Dictionary: linking value to '%s'", nv->name);
568
+ nv->value = value;
569
+ namevalue_set_valuelen(dict, nv, value_len);
570
+ }
571
+ else {
572
+ debug(D_DICTIONARY, "Dictionary: cloning value to '%s'", nv->name);
573
+ DICTIONARY_STATS_VALUE_RESETS_PLUS1(dict, namevalue_get_valuelen(dict, nv), value_len);
574
+
575
+ void *old = nv->value;
576
+ void *new = mallocz(value_len);
577
+ memcpy(new, value, value_len);
578
+ nv->value = new;
579
+ namevalue_set_valuelen(dict, nv, value_len);
580
+
581
+ debug(D_DICTIONARY, "Dictionary: freeing old value of '%s'", nv->name);
582
+ freez(old);
583
+ }
584
+}
585
+
586
+static size_t namevalue_destroy_unsafe(DICTIONARY *dict, NAME_VALUE *nv) {
587
+ debug(D_DICTIONARY, "Destroying name value entry for name '%s'.", nv->name);
588
113
- NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict);
589
+ size_t freed = 0;
590
115
- if(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE)) {
116
- debug(D_REGISTRY, "Dictionary freeing value of '%s'", nv->name);
591
+ if(unlikely(!(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE))) {
592
+ debug(D_DICTIONARY, "Dictionary freeing value of '%s'", nv->name);
593
freez(nv->value);
594
+ freed += namevalue_get_valuelen(dict, nv);
595
}
596
120
- if(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE)) {
121
- debug(D_REGISTRY, "Dictionary freeing name '%s'", nv->name);
597
+ if(unlikely(!(dict->flags & DICTIONARY_FLAG_NAME_LINK_DONT_CLONE))) {
598
+ debug(D_DICTIONARY, "Dictionary freeing name '%s'", nv->name);
599
freez(nv->name);
600
+ freed += namevalue_get_namelen(dict, nv);
601
}
602
603
freez(nv);
604
+ freed += namevalue_alloc_size(dict);
605
+
606
+ DICTIONARY_STATS_ENTRIES_MINUS1(dict, freed);
607
+
608
+ return freed;
609
}
610
611
// ----------------------------------------------------------------------------
129
-// API - basic methods
612
+// API - dictionary management
613
131
-DICTIONARY *dictionary_create(uint8_t flags) {
614
+DICTIONARY *dictionary_create(DICTIONARY_FLAGS flags) {
615
debug(D_DICTIONARY, "Creating dictionary.");
616
134
- DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
135
-
136
- if(flags & DICTIONARY_FLAG_WITH_STATISTICS)
137
- dict->stats = callocz(1, sizeof(struct dictionary_stats));
617
+ if((flags & DICTIONARY_FLAG_REFERENCE_COUNTERS) && (flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
618
+ error("DICTIONARY: requested reference counters on single threaded dictionary. Not adding reference counters.");
619
+ flags &= ~DICTIONARY_FLAG_REFERENCE_COUNTERS;
620
+ }
621
139
- if(!(flags & DICTIONARY_FLAG_SINGLE_THREADED)) {
140
- dict->rwlock = callocz(1, sizeof(netdata_rwlock_t));
141
- netdata_rwlock_init(dict->rwlock);
622
+ if(flags & DICTIONARY_FLAG_REFERENCE_COUNTERS) {
623
+ // we need statistics to allocate the extra NAME_VALUE attributes
624
+ flags |= DICTIONARY_FLAG_WITH_STATISTICS;
625
}
626
144
- avl_init(&dict->values_index, name_value_compare);
627
+ DICTIONARY *dict = callocz(1, sizeof(DICTIONARY));
628
+ size_t allocated = sizeof(DICTIONARY);
629
+
630
dict->flags = flags;
631
+ dict->first_item = dict->last_item = NULL;
632
147
- return dict;
633
+ allocated += dictionary_lock_init(dict);
634
+ allocated += reference_counter_init(dict);
635
+
636
+ if(flags & DICTIONARY_FLAG_WITH_STATISTICS) {
637
+ dict->stats = callocz(1, sizeof(struct dictionary_stats));
638
+ allocated += sizeof(struct dictionary_stats);
639
+ dict->stats->memory = allocated;
640
+ }
641
+ else
642
+ dict->stats = NULL;
643
+
644
+ hashtable_init_unsafe(dict);
645
+ return (DICTIONARY *)dict;
646
}
647
150
-void dictionary_destroy(DICTIONARY *dict) {
648
+size_t dictionary_destroy(DICTIONARY *dict) {
649
debug(D_DICTIONARY, "Destroying dictionary.");
650
153
- dictionary_write_lock(dict);
651
+ dictionary_lock_wrlock(dict);
652
+
653
+ size_t freed = 0;
654
+ NAME_VALUE *nv = dict->first_item;
655
+ while (nv) {
656
+ // cache nv->next
657
+ // because we are going to free nv
658
+ NAME_VALUE *nvnext = nv->next;
659
+ freed += namevalue_destroy_unsafe(dict, nv);
660
+ nv = nvnext;
661
+ // to speed up destruction, we don't
662
+ // unlink nv from the linked-list here
663
+ }
664
+
665
+ dict->first_item = NULL;
666
+ dict->last_item = NULL;
667
155
- while(dict->values_index.root)
156
- dictionary_name_value_destroy_nolock(dict, (NAME_VALUE *)dict->values_index.root);
668
+ // destroy the dictionary
669
+ freed += hashtable_destroy_unsafe(dict);
670
671
dictionary_unlock(dict);
672
+ freed += dictionary_lock_free(dict);
673
+ freed += reference_counter_free(dict);
674
160
- if(dict->stats)
675
+ if(unlikely(dict->flags & DICTIONARY_FLAG_WITH_STATISTICS)) {
676
freez(dict->stats);
162
-
163
- if(dict->rwlock) {
164
- netdata_rwlock_destroy(dict->rwlock);
165
- freez(dict->rwlock);
677
+ dict->stats = NULL;
678
+ freed += sizeof(struct dictionary_stats);
679
}
680
681
freez(dict);
682
+ freed += sizeof(DICTIONARY);
683
+
684
+ return freed;
685
}
686
687
// ----------------------------------------------------------------------------
688
+// API - items management
689
173
-void *dictionary_set_with_name_ptr(DICTIONARY *dict, const char *name, void *value, size_t value_len, char **name_ptr) {
174
- debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
175
-
176
- uint32_t hash = simple_hash(name);
690
+void *dictionary_set_unsafe(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
691
+ if(unlikely(!name || !*name)) {
692
+ error("Attempted to dictionary_set() a dictionary item without a name");
693
+ return NULL;
694
+ }
695
178
- dictionary_write_lock(dict);
696
+ size_t name_len = strlen(name) + 1; // we need the terminating null too
697
180
- NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
181
- if(unlikely(!nv)) {
182
- debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
698
+ debug(D_DICTIONARY, "SET dictionary entry with name '%s'.", name);
699
184
- nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
185
- if(unlikely(!nv))
186
- fatal("Cannot create name_value.");
700
+ // DISCUSSION:
701
+ // Is it better to gain a read-lock and do a hashtable_get_unsafe()
702
+ // before we write lock to do hashtable_insert_unsafe()?
703
+ //
704
+ // Probably this depends on the use case.
705
+ // For statsd for example that does dictionary_set() to update received values,
706
+ // it could be beneficial to do a get() before we insert().
707
+ //
708
+ // But the caller has the option to do this on his/her own.
709
+ // So, let's do the fastest here and let the caller decide the flow of calls.
710
+
711
+ NAME_VALUE *nv, **pnv = hashtable_insert_unsafe(dict, name, name_len);
712
+ if(likely(*pnv == 0)) {
713
+ // a new item added to the index
714
+ nv = *pnv = namevalue_create_unsafe(dict, name, name_len, value, value_len);
715
+ hashtable_inserted_name_value_unsafe(dict, name, name_len, nv);
716
+ linkedlist_namevalue_link_unsafe(dict, nv);
717
+
718
+ if(dict->ins_callback)
719
+ dict->ins_callback(nv->name, nv->value, dict->ins_callback_data);
720
}
188
- else if(!(dict->flags & DICTIONARY_FLAG_DONT_OVERWRITE_VALUE)) {
189
- debug(D_DICTIONARY, "Dictionary entry with name '%s' found. Changing its value.", name);
721
+ else {
722
+ // the item is already in the index
723
+ // so, either we will return the old one
724
+ // or overwrite the value, depending on dictionary flags
725
191
- if(dict->flags & DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE) {
192
- debug(D_REGISTRY, "Dictionary: linking value to '%s'", name);
193
- nv->value = value;
194
- }
195
- else {
196
- debug(D_REGISTRY, "Dictionary: cloning value to '%s'", name);
726
+ nv = *pnv;
727
+ if(!(dict->flags & DICTIONARY_FLAG_DONT_OVERWRITE_VALUE))
728
+ namevalue_reset_unsafe(dict, nv, value, value_len);
729
+ }
730
198
- // copy the new value without breaking
199
- // any other thread accessing the same entry
200
- void *new = mallocz(value_len),
201
- *old = nv->value;
731
+ return nv->value;
732
+}
733
203
- memcpy(new, value, value_len);
204
- nv->value = new;
734
+void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len) {
735
+ dictionary_lock_wrlock(dict);
736
+ void *ret = dictionary_set_unsafe(dict, name, value, value_len);
737
+ dictionary_unlock(dict);
738
+ return ret;
739
+}
740
206
- debug(D_REGISTRY, "Dictionary: freeing old value of '%s'", name);
207
- freez(old);
208
- }
741
+void *dictionary_get_unsafe(DICTIONARY *dict, const char *name) {
742
+ if(unlikely(!name || !*name)) {
743
+ error("Attempted to dictionary_get() without a name");
744
+ return NULL;
745
}
746
211
- dictionary_unlock(dict);
747
+ size_t name_len = strlen(name) + 1; // we need the terminating null too
748
213
- if(name_ptr) *name_ptr = nv->name;
214
- return nv->value;
215
-}
216
-
217
-void *dictionary_get(DICTIONARY *dict, const char *name) {
749
debug(D_DICTIONARY, "GET dictionary entry with name '%s'.", name);
750
220
- dictionary_read_lock(dict);
221
- NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
222
- dictionary_unlock(dict);
223
-
751
+ NAME_VALUE *nv = hashtable_get_unsafe(dict, name, name_len);
752
if(unlikely(!nv)) {
753
debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
754
return NULL;
758
return nv->value;
759
}
760
233
-int dictionary_del(DICTIONARY *dict, const char *name) {
234
- int ret;
761
+void *dictionary_get(DICTIONARY *dict, const char *name) {
762
+ dictionary_lock_rlock(dict);
763
+ void *ret = dictionary_get_unsafe(dict, name);
764
+ dictionary_unlock(dict);
765
+ return ret;
766
+}
767
+
768
+int dictionary_del_unsafe(DICTIONARY *dict, const char *name) {
769
+ if(unlikely(!name || !*name)) {
770
+ error("Attempted to dictionary_det() without a name");
771
+ return -1;
772
+ }
773
+
774
+ size_t name_len = strlen(name) + 1; // we need the terminating null too
775
776
debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
777
238
- dictionary_write_lock(dict);
778
+ // Unfortunately, the JudyHSDel() does not return the value of the
779
+ // item that was deleted, so we have to find it before we delete it,
780
+ // since we need to release our structures too.
781
240
- NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
782
+ int ret;
783
+ NAME_VALUE *nv = hashtable_get_unsafe(dict, name, name_len);
784
if(unlikely(!nv)) {
785
debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
786
ret = -1;
787
}
788
else {
789
debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
247
- dictionary_name_value_destroy_nolock(dict, nv);
790
+
791
+ if(hashtable_delete_unsafe(dict, name, name_len, nv) == 0)
792
+ error("DICTIONARY: INTERNAL ERROR: tried to delete item with name '%s' that is not in the index", name);
793
+
794
+ if(!reference_counter_mark_deleted(dict, nv)) {
795
+ linkedlist_namevalue_unlink_unsafe(dict, nv);
796
+
797
+ if(dict->del_callback)
798
+ dict->del_callback(nv->name, nv->value, dict->del_callback_data);
799
+
800
+ namevalue_destroy_unsafe(dict, nv);
801
+ }
802
ret = 0;
803
}
804
+ return ret;
805
+}
806
807
+int dictionary_del(DICTIONARY *dict, const char *name) {
808
+ dictionary_lock_wrlock(dict);
809
+ int ret = dictionary_del_unsafe(dict, name);
810
dictionary_unlock(dict);
252
-
811
return ret;
812
}
813
256
-
814
// ----------------------------------------------------------------------------
258
-// API - walk through the dictionary
259
-// the dictionary is locked for reading while this happens
260
-// do not user other dictionary calls while walking the dictionary - deadlock!
815
+// traversal with loop
816
+
817
+void *dictionary_foreach_start_rw(DICTFE *dfe, DICTIONARY *dict, char rw) {
818
+ if(unlikely(!dfe || !dict)) return NULL;
819
262
-static int dictionary_walker(avl_t *a, int (*callback)(void *entry, void *data), void *data) {
263
- int total = 0, ret = 0;
820
+ dfe->dict = dict;
821
+ dfe->started_ut = now_realtime_usec();
822
265
- if(a->avl_link[0]) {
266
- ret = dictionary_walker(a->avl_link[0], callback, data);
267
- if(ret < 0) return ret;
268
- total += ret;
823
+ if(rw == 'r' || rw == 'R')
824
+ dictionary_lock_rlock(dict);
825
+ else
826
+ dictionary_lock_wrlock(dict);
827
+
828
+ NAME_VALUE *nv = dict->first_item;
829
+ dfe->last_position_index = (void *)nv;
830
+
831
+ if(likely(nv)) {
832
+ dfe->next_position_index = (void *)nv->next;
833
+ dfe->name = nv->name;
834
+ dfe->value = (void *)nv->value;
835
+ reference_counter_acquire(dict, nv);
836
}
837
+ else {
838
+ dfe->next_position_index = NULL;
839
+ dfe->name = NULL;
840
+ dfe->value = NULL;
841
+ }
842
+
843
+ return dfe->value;
844
+}
845
+
846
+void *dictionary_foreach_next(DICTFE *dfe) {
847
+ if(unlikely(!dfe || !dfe->dict)) return NULL;
848
+
849
+ NAME_VALUE *nv = (NAME_VALUE *)dfe->last_position_index;
850
+ if(likely(nv))
851
+ reference_counter_release(dfe->dict, nv);
852
+
853
+ nv = dfe->last_position_index = dfe->next_position_index;
854
271
- ret = callback(((NAME_VALUE *)a)->value, data);
272
- if(ret < 0) return ret;
273
- total += ret;
855
+ if(likely(nv)) {
856
+ dfe->next_position_index = (void *)nv->next;
857
+ dfe->name = nv->name;
858
+ dfe->value = (void *)nv->value;
859
275
- if(a->avl_link[1]) {
276
- ret = dictionary_walker(a->avl_link[1], callback, data);
277
- if (ret < 0) return ret;
278
- total += ret;
860
+ reference_counter_acquire(dfe->dict, nv);
861
+ }
862
+ else {
863
+ dfe->next_position_index = NULL;
864
+ dfe->name = NULL;
865
+ dfe->value = NULL;
866
}
867
281
- return total;
868
+ return dfe->value;
869
}
870
284
-int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data) {
871
+usec_t dictionary_foreach_done(DICTFE *dfe) {
872
+ if(unlikely(!dfe || !dfe->dict)) return 0;
873
+
874
+ NAME_VALUE *nv = (NAME_VALUE *)dfe->last_position_index;
875
+ if(nv)
876
+ reference_counter_release(dfe->dict, nv);
877
+
878
+ dictionary_unlock((DICTIONARY *)dfe->dict);
879
+ dfe->dict = NULL;
880
+ dfe->last_position_index = NULL;
881
+ dfe->next_position_index = NULL;
882
+ dfe->name = NULL;
883
+ dfe->value = NULL;
884
+
885
+ usec_t usec = now_realtime_usec() - dfe->started_ut;
886
+ dfe->started_ut = 0;
887
+
888
+ return usec;
889
+}
890
+
891
+// ----------------------------------------------------------------------------
892
+// API - walk through the dictionary
893
+// the dictionary is locked for reading while this happens
894
+// do not use other dictionary calls while walking the dictionary - deadlock!
895
+
896
+int dictionary_walkthrough_rw(DICTIONARY *dict, char rw, int (*callback)(const char *name, void *entry, void *data), void *data) {
897
+ if(rw == 'r' || rw == 'R')
898
+ dictionary_lock_rlock(dict);
899
+ else
900
+ dictionary_lock_wrlock(dict);
901
+
902
+ // written in such a way, that the callback can delete the active element
903
+
904
int ret = 0;
905
+ NAME_VALUE *nv = dict->first_item, *nv_next = nv->next;
906
+ while(nv) {
907
+ nv_next = nv->next;
908
+
909
+ reference_counter_acquire(dict, nv);
910
+ int r = callback(nv->name, nv->value, data);
911
+ reference_counter_release(dict, nv);
912
+ if(unlikely(r < 0)) {
913
+ ret = r;
914
+ break;
915
+ }
916
287
- dictionary_read_lock(dict);
917
+ ret += r;
918
289
- if(likely(dict->values_index.root))
290
- ret = dictionary_walker(dict->values_index.root, callback, data);
919
+ nv = nv_next;
920
+ }
921
922
dictionary_unlock(dict);
923
924
return ret;
925
}
926
297
-static int dictionary_walker_name_value(avl_t *a, int (*callback)(char *name, void *entry, void *data), void *data) {
298
- int total = 0, ret = 0;
927
+// ----------------------------------------------------------------------------
928
+// unit test
929
300
- if(a->avl_link[0]) {
301
- ret = dictionary_walker_name_value(a->avl_link[0], callback, data);
302
- if(ret < 0) return ret;
303
- total += ret;
930
+static void dictionary_unittest_free_char_pp(char **pp, size_t entries) {
931
+ for(size_t i = 0; i < entries ;i++)
932
+ freez(pp[i]);
933
+
934
+ freez(pp);
935
+}
936
+
937
+static char **dictionary_unittest_generate_names(size_t entries) {
938
+ char **names = mallocz(sizeof(char *) * entries);
939
+ for(size_t i = 0; i < entries ;i++) {
940
+ char buf[25 + 1] = "";
941
+ snprintfz(buf, 25, "name.%zu.0123456789.%zu \t !@#$%%^&*(),./[]{}\\|~`", i, entries / 2 + i);
942
+ names[i] = strdupz(buf);
943
}
944
+ return names;
945
+}
946
306
- ret = callback(((NAME_VALUE *)a)->name, ((NAME_VALUE *)a)->value, data);
307
- if(ret < 0) return ret;
308
- total += ret;
947
+static char **dictionary_unittest_generate_values(size_t entries) {
948
+ char **values = mallocz(sizeof(char *) * entries);
949
+ for(size_t i = 0; i < entries ;i++) {
950
+ char buf[25 + 1] = "";
951
+ snprintfz(buf, 25, "value-%zu-0987654321.%zu%%^&*(),. \t !@#$/[]{}\\|~`", i, entries / 2 + i);
952
+ values[i] = strdupz(buf);
953
+ }
954
+ return values;
955
+}
956
310
- if(a->avl_link[1]) {
311
- ret = dictionary_walker_name_value(a->avl_link[1], callback, data);
312
- if (ret < 0) return ret;
313
- total += ret;
957
+static size_t dictionary_unittest_set_clone(DICTIONARY *dict, char **names, char **values, size_t entries) {
958
+ size_t errors = 0;
959
+ for(size_t i = 0; i < entries ;i++) {
960
+ size_t vallen = strlen(values[i]) + 1;
961
+ char *val = (char *)dictionary_set(dict, names[i], values[i], vallen);
962
+ if(val == values[i]) { fprintf(stderr, ">>> %s() returns reference to value\n", __FUNCTION__); errors++; }
963
+ if(!val || memcmp(val, values[i], vallen) != 0) { fprintf(stderr, ">>> %s() returns invalid value\n", __FUNCTION__); errors++; }
964
}
965
+ return errors;
966
+}
967
316
- return total;
968
+static size_t dictionary_unittest_set_nonclone(DICTIONARY *dict, char **names, char **values, size_t entries) {
969
+ size_t errors = 0;
970
+ for(size_t i = 0; i < entries ;i++) {
971
+ size_t vallen = strlen(values[i]) + 1;
972
+ char *val = (char *)dictionary_set(dict, names[i], values[i], vallen);
973
+ if(val != values[i]) { fprintf(stderr, ">>> %s() returns invalid pointer to value\n", __FUNCTION__); errors++; }
974
+ }
975
+ return errors;
976
}
977
319
-int dictionary_get_all_name_value(DICTIONARY *dict, int (*callback)(char *name, void *entry, void *data), void *data) {
320
- int ret = 0;
978
+static size_t dictionary_unittest_get_clone(DICTIONARY *dict, char **names, char **values, size_t entries) {
979
+ size_t errors = 0;
980
+ for(size_t i = 0; i < entries ;i++) {
981
+ size_t vallen = strlen(values[i]) + 1;
982
+ char *val = (char *)dictionary_get(dict, names[i]);
983
+ if(val == values[i]) { fprintf(stderr, ">>> %s() returns reference to value\n", __FUNCTION__); errors++; }
984
+ if(!val || memcmp(val, values[i], vallen) != 0) { fprintf(stderr, ">>> %s() returns invalid value\n", __FUNCTION__); errors++; }
985
+ }
986
+ return errors;
987
+}
988
322
- dictionary_read_lock(dict);
989
+static size_t dictionary_unittest_get_nonclone(DICTIONARY *dict, char **names, char **values, size_t entries) {
990
+ size_t errors = 0;
991
+ for(size_t i = 0; i < entries ;i++) {
992
+ char *val = (char *)dictionary_get(dict, names[i]);
993
+ if(val != values[i]) { fprintf(stderr, ">>> %s() returns invalid pointer to value\n", __FUNCTION__); errors++; }
994
+ }
995
+ return errors;
996
+}
997
324
- if(likely(dict->values_index.root))
325
- ret = dictionary_walker_name_value(dict->values_index.root, callback, data);
998
+static size_t dictionary_unittest_get_nonexisting(DICTIONARY *dict, char **names, char **values, size_t entries) {
999
+ (void)names;
1000
+ size_t errors = 0;
1001
+ for(size_t i = 0; i < entries ;i++) {
1002
+ char *val = (char *)dictionary_get(dict, values[i]);
1003
+ if(val) { fprintf(stderr, ">>> %s() returns non-existing item\n", __FUNCTION__); errors++; }
1004
+ }
1005
+ return errors;
1006
+}
1007
327
- dictionary_unlock(dict);
1008
+static size_t dictionary_unittest_del_nonexisting(DICTIONARY *dict, char **names, char **values, size_t entries) {
1009
+ (void)names;
1010
+ size_t errors = 0;
1011
+ for(size_t i = 0; i < entries ;i++) {
1012
+ int ret = dictionary_del(dict, values[i]);
1013
+ if(ret != -1) { fprintf(stderr, ">>> %s() deleted non-existing item\n", __FUNCTION__); errors++; }
1014
+ }
1015
+ return errors;
1016
+}
1017
329
- return ret;
1018
+static size_t dictionary_unittest_del_existing(DICTIONARY *dict, char **names, char **values, size_t entries) {
1019
+ (void)values;
1020
+ size_t errors = 0;
1021
+
1022
+ size_t forward_from = 0, forward_to = entries / 3;
1023
+ size_t middle_from = forward_to, middle_to = entries * 2 / 3;
1024
+ size_t backward_from = middle_to, backward_to = entries;
1025
+
1026
+ for(size_t i = forward_from; i < forward_to ;i++) {
1027
+ int ret = dictionary_del(dict, names[i]);
1028
+ if(ret == -1) { fprintf(stderr, ">>> %s() didn't delete (forward) existing item\n", __FUNCTION__); errors++; }
1029
+ }
1030
+
1031
+ for(size_t i = middle_to - 1; i >= middle_from ;i--) {
1032
+ int ret = dictionary_del(dict, names[i]);
1033
+ if(ret == -1) { fprintf(stderr, ">>> %s() didn't delete (middle) existing item\n", __FUNCTION__); errors++; }
1034
+ }
1035
+
1036
+ for(size_t i = backward_to - 1; i >= backward_from ;i--) {
1037
+ int ret = dictionary_del(dict, names[i]);
1038
+ if(ret == -1) { fprintf(stderr, ">>> %s() didn't delete (backward) existing item\n", __FUNCTION__); errors++; }
1039
+ }
1040
+
1041
+ return errors;
1042
+}
1043
+
1044
+static size_t dictionary_unittest_reset_clone(DICTIONARY *dict, char **names, char **values, size_t entries) {
1045
+ (void)values;
1046
+ // set the name as value too
1047
+ size_t errors = 0;
1048
+ for(size_t i = 0; i < entries ;i++) {
1049
+ size_t vallen = strlen(names[i]) + 1;
1050
+ char *val = (char *)dictionary_set(dict, names[i], names[i], vallen);
1051
+ if(val == names[i]) { fprintf(stderr, ">>> %s() returns reference to value\n", __FUNCTION__); errors++; }
1052
+ if(!val || memcmp(val, names[i], vallen) != 0) { fprintf(stderr, ">>> %s() returns invalid value\n", __FUNCTION__); errors++; }
1053
+ }
1054
+ return errors;
1055
+}
1056
+
1057
+static size_t dictionary_unittest_reset_nonclone(DICTIONARY *dict, char **names, char **values, size_t entries) {
1058
+ (void)values;
1059
+ // set the name as value too
1060
+ size_t errors = 0;
1061
+ for(size_t i = 0; i < entries ;i++) {
1062
+ size_t vallen = strlen(names[i]) + 1;
1063
+ char *val = (char *)dictionary_set(dict, names[i], names[i], vallen);
1064
+ if(val != names[i]) { fprintf(stderr, ">>> %s() returns invalid pointer to value\n", __FUNCTION__); errors++; }
1065
+ if(!val) { fprintf(stderr, ">>> %s() returns invalid value\n", __FUNCTION__); errors++; }
1066
+ }
1067
+ return errors;
1068
+}
1069
+
1070
+static size_t dictionary_unittest_reset_dont_overwrite_nonclone(DICTIONARY *dict, char **names, char **values, size_t entries) {
1071
+ // set the name as value too
1072
+ size_t errors = 0;
1073
+ for(size_t i = 0; i < entries ;i++) {
1074
+ size_t vallen = strlen(names[i]) + 1;
1075
+ char *val = (char *)dictionary_set(dict, names[i], names[i], vallen);
1076
+ if(val != values[i]) { fprintf(stderr, ">>> %s() returns invalid pointer to value\n", __FUNCTION__); errors++; }
1077
+ }
1078
+ return errors;
1079
+}
1080
+
1081
+static int dictionary_unittest_walkthrough_callback(const char *name, void *value, void *data) {
1082
+ (void)name;
1083
+ (void)value;
1084
+ (void)data;
1085
+ return 1;
1086
+}
1087
+
1088
+static size_t dictionary_unittest_walkthrough(DICTIONARY *dict, char **names, char **values, size_t entries) {
1089
+ (void)names;
1090
+ (void)values;
1091
+ int sum = dictionary_walkthrough_read(dict, dictionary_unittest_walkthrough_callback, NULL);
1092
+ if(sum < (int)entries) return entries - sum;
1093
+ else return sum - entries;
1094
+}
1095
+
1096
+static int dictionary_unittest_walkthrough_delete_this_callback(const char *name, void *value, void *data) {
1097
+ (void)value;
1098
+
1099
+ if(dictionary_del_having_write_lock((DICTIONARY *)data, name) == -1)
1100
+ return 0;
1101
+
1102
+ return 1;
1103
+}
1104
+
1105
+static size_t dictionary_unittest_walkthrough_delete_this(DICTIONARY *dict, char **names, char **values, size_t entries) {
1106
+ (void)names;
1107
+ (void)values;
1108
+ int sum = dictionary_walkthrough_write(dict, dictionary_unittest_walkthrough_delete_this_callback, dict);
1109
+ if(sum < (int)entries) return entries - sum;
1110
+ else return sum - entries;
1111
+}
1112
+
1113
+static int dictionary_unittest_walkthrough_stop_callback(const char *name, void *value, void *data) {
1114
+ (void)name;
1115
+ (void)value;
1116
+ (void)data;
1117
+ return -1;
1118
+}
1119
+
1120
+static size_t dictionary_unittest_walkthrough_stop(DICTIONARY *dict, char **names, char **values, size_t entries) {
1121
+ (void)names;
1122
+ (void)values;
1123
+ (void)entries;
1124
+ int sum = dictionary_walkthrough_read(dict, dictionary_unittest_walkthrough_stop_callback, NULL);
1125
+ if(sum != -1) return 1;
1126
+ return 0;
1127
+}
1128
+
1129
+static size_t dictionary_unittest_foreach(DICTIONARY *dict, char **names, char **values, size_t entries) {
1130
+ (void)names;
1131
+ (void)values;
1132
+ (void)entries;
1133
+ size_t count = 0;
1134
+ char *item;
1135
+ dfe_start_read(dict, item)
1136
+ count++;
1137
+ dfe_done(item);
1138
+
1139
+ if(count > entries) return count - entries;
1140
+ return entries - count;
1141
+}
1142
+
1143
+static size_t dictionary_unittest_foreach_delete_this(DICTIONARY *dict, char **names, char **values, size_t entries) {
1144
+ (void)names;
1145
+ (void)values;
1146
+ (void)entries;
1147
+ size_t count = 0;
1148
+ char *item;
1149
+ dfe_start_write(dict, item)
1150
+ if(dictionary_del_having_write_lock(dict, item_name) != -1) count++;
1151
+ dfe_done(item);
1152
+
1153
+ if(count > entries) return count - entries;
1154
+ return entries - count;
1155
+}
1156
+
1157
+static size_t dictionary_unittest_destroy(DICTIONARY *dict, char **names, char **values, size_t entries) {
1158
+ (void)names;
1159
+ (void)values;
1160
+ (void)entries;
1161
+ size_t bytes = dictionary_destroy(dict);
1162
+ fprintf(stderr, " %s() freed %zu bytes,", __FUNCTION__, bytes);
1163
+ return 0;
1164
+}
1165
+
1166
+static usec_t dictionary_unittest_run_and_measure_time(DICTIONARY *dict, char *message, char **names, char **values, size_t entries, size_t *errors, size_t (*callback)(DICTIONARY *dict, char **names, char **values, size_t entries)) {
1167
+ fprintf(stderr, "%-40s... ", message);
1168
+
1169
+ usec_t started = now_realtime_usec();
1170
+ size_t errs = callback(dict, names, values, entries);
1171
+ usec_t ended = now_realtime_usec();
1172
+ usec_t dt = ended - started;
1173
+
1174
+ if(callback == dictionary_unittest_destroy) dict = NULL;
1175
+
1176
+ fprintf(stderr, " %zu errors, %zu items in dictionary, %llu usec \n", errs, dict? dictionary_stats_entries(dict):0, dt);
1177
+ *errors += errs;
1178
+ return dt;
1179
+}
1180
+
1181
+void dictionary_unittest_clone(DICTIONARY *dict, char **names, char **values, size_t entries, size_t *errors) {
1182
+ dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, errors, dictionary_unittest_set_clone);
1183
+ dictionary_unittest_run_and_measure_time(dict, "getting entries", names, values, entries, errors, dictionary_unittest_get_clone);
1184
+ dictionary_unittest_run_and_measure_time(dict, "getting non-existing entries", names, values, entries, errors, dictionary_unittest_get_nonexisting);
1185
+ dictionary_unittest_run_and_measure_time(dict, "resetting entries", names, values, entries, errors, dictionary_unittest_reset_clone);
1186
+ dictionary_unittest_run_and_measure_time(dict, "deleting non-existing entries", names, values, entries, errors, dictionary_unittest_del_nonexisting);
1187
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach read loop", names, values, entries, errors, dictionary_unittest_foreach);
1188
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback", names, values, entries, errors, dictionary_unittest_walkthrough);
1189
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback stop", names, values, entries, errors, dictionary_unittest_walkthrough_stop);
1190
+ dictionary_unittest_run_and_measure_time(dict, "deleting existing entries", names, values, entries, errors, dictionary_unittest_del_existing);
1191
+ dictionary_unittest_run_and_measure_time(dict, "walking through empty", names, values, 0, errors, dictionary_unittest_walkthrough);
1192
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach empty", names, values, 0, errors, dictionary_unittest_foreach);
1193
+ dictionary_unittest_run_and_measure_time(dict, "destroying empty dictionary", names, values, entries, errors, dictionary_unittest_destroy);
1194
+}
1195
+
1196
+void dictionary_unittest_nonclone(DICTIONARY *dict, char **names, char **values, size_t entries, size_t *errors) {
1197
+ dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, errors, dictionary_unittest_set_nonclone);
1198
+ dictionary_unittest_run_and_measure_time(dict, "getting entries", names, values, entries, errors, dictionary_unittest_get_nonclone);
1199
+ dictionary_unittest_run_and_measure_time(dict, "getting non-existing entries", names, values, entries, errors, dictionary_unittest_get_nonexisting);
1200
+ dictionary_unittest_run_and_measure_time(dict, "resetting entries", names, values, entries, errors, dictionary_unittest_reset_nonclone);
1201
+ dictionary_unittest_run_and_measure_time(dict, "deleting non-existing entries", names, values, entries, errors, dictionary_unittest_del_nonexisting);
1202
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach read loop", names, values, entries, errors, dictionary_unittest_foreach);
1203
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback", names, values, entries, errors, dictionary_unittest_walkthrough);
1204
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback stop", names, values, entries, errors, dictionary_unittest_walkthrough_stop);
1205
+ dictionary_unittest_run_and_measure_time(dict, "deleting existing entries", names, values, entries, errors, dictionary_unittest_del_existing);
1206
+ dictionary_unittest_run_and_measure_time(dict, "walking through empty", names, values, 0, errors, dictionary_unittest_walkthrough);
1207
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach empty", names, values, 0, errors, dictionary_unittest_foreach);
1208
+ dictionary_unittest_run_and_measure_time(dict, "destroying empty dictionary", names, values, entries, errors, dictionary_unittest_destroy);
1209
+}
1210
+
1211
+int dictionary_unittest(size_t entries) {
1212
+ if(entries < 10) entries = 10;
1213
+
1214
+ DICTIONARY *dict;
1215
+ size_t errors = 0;
1216
+
1217
+ fprintf(stderr, "Generating %zu names and values...\n", entries);
1218
+ char **names = dictionary_unittest_generate_names(entries);
1219
+ char **values = dictionary_unittest_generate_values(entries);
1220
+
1221
+ fprintf(stderr, "\nCreating dictionary single threaded, clone, %zu items\n", entries);
1222
+ dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS);
1223
+ dictionary_unittest_clone(dict, names, values, entries, &errors);
1224
+
1225
+ fprintf(stderr, "\nCreating dictionary multi threaded, clone, %zu items\n", entries);
1226
+ dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS);
1227
+ dictionary_unittest_clone(dict, names, values, entries, &errors);
1228
+
1229
+ fprintf(stderr, "\nCreating dictionary single threaded, non-clone, add-in-front options, %zu items\n", entries);
1230
+ dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_ADD_IN_FRONT);
1231
+ dictionary_unittest_nonclone(dict, names, values, entries, &errors);
1232
+
1233
+ fprintf(stderr, "\nCreating dictionary multi threaded, non-clone, add-in-front options, %zu items\n", entries);
1234
+ dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_ADD_IN_FRONT);
1235
+ dictionary_unittest_nonclone(dict, names, values, entries, &errors);
1236
+
1237
+ fprintf(stderr, "\nCreating dictionary single-threaded, non-clone, don't overwrite options, %zu items\n", entries);
1238
+ dict = dictionary_create(DICTIONARY_FLAG_SINGLE_THREADED|DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
1239
+ dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, &errors, dictionary_unittest_set_nonclone);
1240
+ dictionary_unittest_run_and_measure_time(dict, "resetting non-overwrite entries", names, values, entries, &errors, dictionary_unittest_reset_dont_overwrite_nonclone);
1241
+ dictionary_unittest_run_and_measure_time(dict, "traverse foreach read loop", names, values, entries, &errors, dictionary_unittest_foreach);
1242
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback", names, values, entries, &errors, dictionary_unittest_walkthrough);
1243
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough read callback stop", names, values, entries, &errors, dictionary_unittest_walkthrough_stop);
1244
+ dictionary_unittest_run_and_measure_time(dict, "destroying full dictionary", names, values, entries, &errors, dictionary_unittest_destroy);
1245
+
1246
+ fprintf(stderr, "\nCreating dictionary multi-threaded, non-clone, don't overwrite options, %zu items\n", entries);
1247
+ dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
1248
+ dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, &errors, dictionary_unittest_set_nonclone);
1249
+ dictionary_unittest_run_and_measure_time(dict, "walkthrough write delete this", names, values, entries, &errors, dictionary_unittest_walkthrough_delete_this);
1250
+ dictionary_unittest_run_and_measure_time(dict, "destroying empty dictionary", names, values, entries, &errors, dictionary_unittest_destroy);
1251
+
1252
+ fprintf(stderr, "\nCreating dictionary multi-threaded, non-clone, don't overwrite options, %zu items\n", entries);
1253
+ dict = dictionary_create(DICTIONARY_FLAG_WITH_STATISTICS|DICTIONARY_FLAG_NAME_LINK_DONT_CLONE|DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE|DICTIONARY_FLAG_DONT_OVERWRITE_VALUE);
1254
+ dictionary_unittest_run_and_measure_time(dict, "adding entries", names, values, entries, &errors, dictionary_unittest_set_nonclone);
1255
+ dictionary_unittest_run_and_measure_time(dict, "foreach write delete this", names, values, entries, &errors, dictionary_unittest_foreach_delete_this);
1256
+ dictionary_unittest_run_and_measure_time(dict, "destroying empty dictionary", names, values, entries, &errors, dictionary_unittest_destroy);
1257
+
1258
+ dictionary_unittest_free_char_pp(names, entries);
1259
+ dictionary_unittest_free_char_pp(values, entries);
1260
+
1261
+ fprintf(stderr, "\n%zu errors found\n", errors);
1262
+ return (int)errors;
1263
}