-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
flb_hash.c
424 lines (360 loc) · 10.2 KB
/
flb_hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_hash.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_str.h>
/*
* This hash generation function is taken originally from Redis source code:
*
* https://github.com/antirez/redis/blob/unstable/src/dict.c#L109
*
* ----
* MurmurHash2, by Austin Appleby
* Note - This code makes a few assumptions about how your machine behaves -
* 1. We can read a 4-byte value from any address without crashing
* 2. sizeof(int) == 4
*
* And it has a few limitations -
*
* 1. It will not work incrementally.
* 2. It will not produce the same results on little-endian and big-endian
* machines.
*/
static unsigned int gen_hash(const void *key, int len)
{
/* 'm' and 'r' are mixing constants generated offline.
They're not really 'magic', they just happen to work well. */
uint32_t seed = 5381;
const uint32_t m = 0x5bd1e995;
const int r = 24;
/* Initialize the hash to a 'random' value */
uint32_t h = seed ^ len;
/* Mix 4 bytes at a time into the hash */
const unsigned char *data = (const unsigned char *) key;
while (len >= 4) {
uint32_t k = *(uint32_t *) data;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
data += 4;
len -= 4;
}
/* Handle the last few bytes of the input array */
switch (len) {
case 3:
h ^= data[2] << 16;
case 2:
h ^= data[1] << 8;
case 1:
h ^= data[0];
h *= m;
};
/* Do a few final mixes of the hash to ensure the last few
* bytes are well-incorporated. */
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return (unsigned int) h;
}
static inline void flb_hash_entry_free(struct flb_hash *ht,
struct flb_hash_entry *entry)
{
mk_list_del(&entry->_head);
mk_list_del(&entry->_head_parent);
entry->table->count--;
ht->total_count--;
flb_free(entry->key);
flb_free(entry->val);
flb_free(entry);
}
struct flb_hash *flb_hash_create(int evict_mode, size_t size, int max_entries)
{
int i;
struct flb_hash_table *tmp;
struct flb_hash *ht;
if (size <= 0) {
return NULL;
}
ht = flb_malloc(sizeof(struct flb_hash));
if (!ht) {
flb_errno();
return NULL;
}
mk_list_init(&ht->entries);
ht->evict_mode = evict_mode;
ht->max_entries = max_entries;
ht->size = size;
ht->total_count = 0;
ht->table = flb_calloc(1, sizeof(struct flb_hash_table) * size);
if (!ht->table) {
flb_errno();
flb_free(ht);
return NULL;
}
/* Initialize chains list head */
for (i = 0; i < size; i++) {
tmp = &ht->table[i];
tmp->count = 0;
mk_list_init(&tmp->chains);
}
return ht;
}
void flb_hash_destroy(struct flb_hash *ht)
{
int i;
struct mk_list *tmp;
struct mk_list *head;
struct flb_hash_entry *entry;
struct flb_hash_table *table;
for (i = 0; i < ht->size; i++) {
table = &ht->table[i];
mk_list_foreach_safe(head, tmp, &table->chains) {
entry = mk_list_entry(head, struct flb_hash_entry, _head);
flb_hash_entry_free(ht, entry);
}
}
flb_free(ht->table);
flb_free(ht);
}
static void flb_hash_evict_random(struct flb_hash *ht)
{
int id;
int count = 0;
struct mk_list *tmp;
struct mk_list *head;
struct flb_hash_entry *entry;
id = random() % ht->total_count;
mk_list_foreach_safe(head, tmp, &ht->entries) {
if (id == count) {
entry = mk_list_entry(head, struct flb_hash_entry, _head_parent);
flb_hash_entry_free(ht, entry);
break;
}
count++;
}
}
int flb_hash_add(struct flb_hash *ht,
const char *key, int key_len,
const char *val, size_t val_size)
{
int id;
unsigned int hash;
struct mk_list *tmp;
struct mk_list *head;
struct flb_hash_entry *entry;
struct flb_hash_entry *old;
struct flb_hash_table *table;
if (!key || key_len <= 0 || !val || val_size <= 0) {
return -1;
}
/* Check capacity */
if (ht->max_entries > 0 && ht->total_count >= ht->max_entries) {
/* FIXME: handle eviction mode */
if (ht->evict_mode == FLB_HASH_EVICT_NONE) {
}
else if (ht->evict_mode == FLB_HASH_EVICT_OLDER) {
}
else if (ht->evict_mode == FLB_HASH_EVICT_LESS_USED) {
}
else if (ht->evict_mode == FLB_HASH_EVICT_RANDOM) {
flb_hash_evict_random(ht);
}
}
/* Generate hash number */
hash = gen_hash(key, key_len);
id = (hash % ht->size);
/* Allocate the entry */
entry = flb_malloc(sizeof(struct flb_hash_entry));
if (!entry) {
flb_errno();
return -1;
}
entry->created = time(NULL);
entry->hits = 0;
/* Store the key and value as a new memory region */
entry->key = flb_strndup(key, key_len);
entry->key_len = key_len;
entry->val = flb_malloc(val_size + 1);
if (!entry->val) {
flb_errno();
flb_free(entry->key);
flb_free(entry);
return -1;
}
/*
* Copy the buffer and append a NULL byte in case the caller set and
* expects a string.
*/
memcpy(entry->val, val, val_size);
entry->val[val_size] = '\0';
entry->val_size = val_size;
/* Link the new entry in our table at the end of the list */
table = &ht->table[id];
entry->table = table;
/* Check if the new key already exists */
if (table->count == 0) {
mk_list_add(&entry->_head, &table->chains);
mk_list_add(&entry->_head_parent, &ht->entries);
}
else {
mk_list_foreach_safe(head, tmp, &table->chains) {
old = mk_list_entry(head, struct flb_hash_entry, _head);
if (strcmp(old->key, entry->key) == 0) {
flb_hash_entry_free(ht, old);
break;
}
}
mk_list_add(&entry->_head, &table->chains);
mk_list_add(&entry->_head_parent, &ht->entries);
}
table->count++;
ht->total_count++;
return id;
}
int flb_hash_get(struct flb_hash *ht,
const char *key, int key_len,
const char **out_buf, size_t * out_size)
{
int id;
unsigned int hash;
struct mk_list *head;
struct flb_hash_table *table;
struct flb_hash_entry *entry;
if (!key || key_len <= 0) {
return -1;
}
hash = gen_hash(key, key_len);
id = (hash % ht->size);
table = &ht->table[id];
if (table->count == 0) {
return -1;
}
if (table->count == 1) {
entry = mk_list_entry_first(&table->chains,
struct flb_hash_entry, _head);
if (entry->key_len != key_len
|| strncmp(entry->key, key, key_len) != 0) {
entry = NULL;
}
}
else {
/* Iterate entries */
mk_list_foreach(head, &table->chains) {
entry = mk_list_entry(head, struct flb_hash_entry, _head);
if (entry->key_len != key_len) {
entry = NULL;
continue;
}
if (strncmp(entry->key, key, key_len) == 0) {
break;
}
entry = NULL;
}
}
if (!entry) {
return -1;
}
if (!entry->val) {
return -1;
}
entry->hits++;
*out_buf = entry->val;
*out_size = entry->val_size;
return id;
}
/*
* Get an entry based in the table id. Note that a table id might have multiple
* entries so the 'key' parameter is required to get an exact match.
*/
int flb_hash_get_by_id(struct flb_hash *ht, int id,
const char *key,
const char **out_buf, size_t * out_size)
{
struct mk_list *head;
struct flb_hash_entry *entry = NULL;
struct flb_hash_table *table;
table = &ht->table[id];
if (table->count == 0) {
return -1;
}
if (table->count == 1) {
entry = mk_list_entry_first(&table->chains,
struct flb_hash_entry, _head);
}
else {
mk_list_foreach(head, &table->chains) {
entry = mk_list_entry(head, struct flb_hash_entry, _head);
if (strcmp(entry->key, key) == 0) {
break;
}
entry = NULL;
}
}
if (!entry) {
return -1;
}
*out_buf = entry->val;
*out_size = entry->val_size;
return 0;
}
int flb_hash_del(struct flb_hash *ht, const char *key)
{
int id;
int len;
unsigned int hash;
struct mk_list *head;
struct flb_hash_entry *entry = NULL;
struct flb_hash_table *table;
if (!key) {
return -1;
}
len = strlen(key);
if (len == 0) {
return -1;
}
hash = gen_hash(key, len);
id = (hash % ht->size);
table = &ht->table[id];
if (table->count == 1) {
entry = mk_list_entry_first(&table->chains,
struct flb_hash_entry,
_head);
if (strcmp(entry->key, key) != 0) {
entry = NULL;
}
}
else {
mk_list_foreach(head, &table->chains) {
entry = mk_list_entry(head, struct flb_hash_entry, _head);
if (strcmp(entry->key, key) == 0) {
break;
}
entry = NULL;
}
}
if (!entry) {
return -1;
}
flb_hash_entry_free(ht, entry);
return 0;
}