master
h 80 lines 2.61 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_LOG2JOURNAL_HASHED_KEY_H
4 #define NETDATA_LOG2JOURNAL_HASHED_KEY_H
5
6 #include "log2journal.h"
7
8 typedef enum __attribute__((__packed__)) {
9 HK_NONE = 0,
10
11 // permanent flags - they are set once to optimize various decisions and lookups
12
13 HK_HASHTABLE_ALLOCATED = (1 << 0), // this is the key object allocated in the hashtable
14 // objects that do not have this, have a pointer to a key in the hashtable
15 // objects that have this, value is allocated
16
17 HK_FILTERED = (1 << 1), // we checked once if this key in filtered
18 HK_FILTERED_INCLUDED = (1 << 2), // the result of the filtering was to include it in the output
19
20 HK_COLLISION_CHECKED = (1 << 3), // we checked once for collision check of this key
21
22 HK_RENAMES_CHECKED = (1 << 4), // we checked once if there are renames on this key
23 HK_HAS_RENAMES = (1 << 5), // and we found there is a rename rule related to it
24
25 // ephemeral flags - they are unset at the end of each log line
26
27 HK_VALUE_FROM_LOG = (1 << 14), // the value of this key has been read from the log (or from injection, duplication)
28 HK_VALUE_REWRITTEN = (1 << 15), // the value of this key has been rewritten due to one of our rewrite rules
29
30 } HASHED_KEY_FLAGS;
31
32 typedef struct hashed_key {
33 const char *key;
34 uint32_t len;
35 HASHED_KEY_FLAGS flags;
36 XXH64_hash_t hash;
37 union {
38 struct hashed_key *hashtable_ptr; // HK_HASHTABLE_ALLOCATED is not set
39 TXT_L2J value; // HK_HASHTABLE_ALLOCATED is set
40 };
41 } HASHED_KEY;
42
43 static inline void hashed_key_cleanup(HASHED_KEY *k) {
44 if(k->flags & HK_HASHTABLE_ALLOCATED)
45 txt_l2j_cleanup(&k->value);
46 else
47 k->hashtable_ptr = NULL;
48
49 freez((void *)k->key);
50 k->key = NULL;
51 k->len = 0;
52 k->hash = 0;
53 k->flags = HK_NONE;
54 }
55
56 static inline void hashed_key_set(HASHED_KEY *k, const char *name, int32_t len) {
57 hashed_key_cleanup(k);
58
59 if(len == -1) {
60 k->key = strdupz(name);
61 k->len = strlen(k->key);
62 }
63 else {
64 k->key = strndupz(name, len);
65 k->len = len;
66 }
67
68 k->hash = XXH3_64bits(k->key, k->len);
69 k->flags = HK_NONE;
70 }
71
72 static inline bool hashed_keys_match(HASHED_KEY *k1, HASHED_KEY *k2) {
73 return ((k1 == k2) || (k1->hash == k2->hash && strcmp(k1->key, k2->key) == 0));
74 }
75
76 static inline int compare_keys(struct hashed_key *k1, struct hashed_key *k2) {
77 return strcmp(k1->key, k2->key);
78 }
79
80 #endif //NETDATA_LOG2JOURNAL_HASHED_KEY_H