]> arthur.barton.de Git - netdata.git/blob - src/dictionary.h
Merge remote-tracking branch 'upstream/master'
[netdata.git] / src / dictionary.h
1 #include <pthread.h>
2
3 #include "web_buffer.h"
4 #include "avl.h"
5
6 #ifndef NETDATA_DICTIONARY_H
7 #define NETDATA_DICTIONARY_H 1
8
9 typedef struct name_value {
10         avl avl;                                // the index - this has to be first!
11
12         uint32_t hash;                  // a simple hash to speed up searching
13                                                         // we first compare hashes, and only if the hashes are equal we do string comparisons
14
15         char *name;
16         void *value;
17 } NAME_VALUE;
18
19 typedef struct dictionary {
20         avl_tree values_index;
21
22         uint8_t flags;
23
24 #ifdef NETDATA_DICTIONARY_WITH_STATISTICS
25         unsigned long long inserts;
26         unsigned long long deletes;
27         unsigned long long searches;
28         unsigned long long entries;
29 #endif /* NETDATA_DICTIONARY_WITH_STATISTICS */
30
31         pthread_rwlock_t rwlock;
32 } DICTIONARY;
33
34 #ifdef NETDATA_DICTIONARY_WITH_STATISTICS
35 #define NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict) (dict)->inserts++
36 #define NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict) (dict)->deletes++
37 #define NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict) (dict)->searches++
38 #define NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict) (dict)->entries++
39 #define NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict) (dict)->entries--
40 #else /* NETDATA_DICTIONARY_WITH_STATISTICS */
41 #define NETDATA_DICTIONARY_STATS_INSERTS_PLUS1(dict)
42 #define NETDATA_DICTIONARY_STATS_DELETES_PLUS1(dict)
43 #define NETDATA_DICTIONARY_STATS_SEARCHES_PLUS1(dict)
44 #define NETDATA_DICTIONARY_STATS_ENTRIES_PLUS1(dict)
45 #define NETDATA_DICTIONARY_STATS_ENTRIES_MINUS1(dict)
46 #endif /* NETDATA_DICTIONARY_WITH_STATISTICS */
47
48 #define DICTIONARY_FLAG_DEFAULT                                 0x00000000
49 #define DICTIONARY_FLAG_SINGLE_THREADED                 0x00000001
50 #define DICTIONARY_FLAG_VALUE_LINK_DONT_CLONE   0x00000002
51 #define DICTIONARY_FLAG_NAME_LINK_DONT_CLONE    0x00000004
52
53 extern DICTIONARY *dictionary_create(uint32_t flags);
54 extern void dictionary_destroy(DICTIONARY *dict);
55 extern void *dictionary_set(DICTIONARY *dict, const char *name, void *value, size_t value_len);
56 extern void *dictionary_get(DICTIONARY *dict, const char *name);
57 extern int dictionary_del(DICTIONARY *dict, const char *name);
58
59 extern int dictionary_get_all(DICTIONARY *dict, int (*callback)(void *entry, void *data), void *data);
60
61 #endif /* NETDATA_DICTIONARY_H */