summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/flb_hash_table.c
blob: d31c4259179198a583dbe3e8e9dd1d9deb972f09 (plain)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  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://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_table.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_str.h>

#include <cfl/cfl.h>

static inline void flb_hash_table_entry_free(struct flb_hash_table *ht,
                                             struct flb_hash_table_entry *entry)
{
    mk_list_del(&entry->_head);
    mk_list_del(&entry->_head_parent);
    entry->table->count--;
    ht->total_count--;
    flb_free(entry->key);
    if (entry->val && entry->val_size > 0) {
        flb_free(entry->val);
    }
    flb_free(entry);
}

struct flb_hash_table *flb_hash_table_create(int evict_mode, size_t size, int max_entries)
{
    int i;
    struct flb_hash_table_chain *tmp;
    struct flb_hash_table *ht;

    if (size <= 0) {
        return NULL;
    }

    ht = flb_malloc(sizeof(struct flb_hash_table));
    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->cache_ttl = 0;
    ht->table = flb_calloc(1, sizeof(struct flb_hash_table_chain) * 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;
}

struct flb_hash_table *flb_hash_table_create_with_ttl(int cache_ttl, int evict_mode,
                                                      size_t size, int max_entries)
{
    struct flb_hash_table *ht;

    ht = flb_hash_table_create(evict_mode, size, max_entries);
    if (!ht) {
        flb_errno();
        return NULL;
    }

    ht->cache_ttl = cache_ttl;
    return ht;
}

int flb_hash_table_del_ptr(struct flb_hash_table *ht, const char *key, int key_len,
                           void *ptr)
{
    int id;
    uint64_t hash;
    struct mk_list *head;
    struct flb_hash_table_entry *entry = NULL;
    struct flb_hash_table_chain *table;

    /* Generate hash number */
    hash = cfl_hash_64bits(key, key_len);
    id = (hash % ht->size);

    /* Link the new entry in our table at the end of the list */
    table = &ht->table[id];

    mk_list_foreach(head, &table->chains) {
        entry = mk_list_entry(head, struct flb_hash_table_entry, _head);
        if (strncmp(entry->key, key, key_len) == 0 && entry->val == ptr) {
            break;
        }
        entry = NULL;
    }

    if (!entry) {
        return -1;
    }

    /* delete the entry */
    flb_hash_table_entry_free(ht, entry);
    return 0;
}


void flb_hash_table_destroy(struct flb_hash_table *ht)
{
    int i;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_hash_table_entry *entry;
    struct flb_hash_table_chain *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_table_entry, _head);
            flb_hash_table_entry_free(ht, entry);
        }
    }

    flb_free(ht->table);
    flb_free(ht);
}

static void flb_hash_table_evict_random(struct flb_hash_table *ht)
{
    int id;
    int count = 0;
    struct mk_list *tmp;
    struct mk_list *head;
    struct flb_hash_table_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_table_entry, _head_parent);
            flb_hash_table_entry_free(ht, entry);
            break;
        }
        count++;
    }
}

static void flb_hash_table_evict_less_used(struct flb_hash_table *ht)
{
    struct mk_list *head;
    struct flb_hash_table_entry *entry;
    struct flb_hash_table_entry *entry_less_used = NULL;

    mk_list_foreach(head, &ht->entries) {
        entry = mk_list_entry(head, struct flb_hash_table_entry, _head_parent);
        if (!entry_less_used) {
            entry_less_used = entry;
        }
        else if (entry->hits < entry_less_used->hits) {
            entry_less_used = entry;
        }
    }

    flb_hash_table_entry_free(ht, entry_less_used);
}

static void flb_hash_table_evict_older(struct flb_hash_table *ht)
{
    struct flb_hash_table_entry *entry;

    entry = mk_list_entry_first(&ht->entries, struct flb_hash_table_entry, _head_parent);
    flb_hash_table_entry_free(ht, entry);
}

static struct flb_hash_table_entry *hash_get_entry(struct flb_hash_table *ht,
                                                   const char *key, int key_len, int *out_id)
{
    int id;
    uint64_t hash;
    struct mk_list *head;
    struct flb_hash_table_chain *table;
    struct flb_hash_table_entry *entry;

    if (!key || key_len <= 0) {
        return NULL;
    }

    hash = cfl_hash_64bits(key, key_len);
    id = (hash % ht->size);

    table = &ht->table[id];
    if (table->count == 0) {
        return NULL;
    }

    if (table->count == 1) {
        entry = mk_list_entry_first(&table->chains,
                                    struct flb_hash_table_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_table_entry, _head);
            if (entry->key_len != key_len) {
                entry = NULL;
                continue;
            }

            if (strncmp(entry->key, key, key_len) == 0) {
                break;
            }

            entry = NULL;
        }
    }

    if (entry) {
        *out_id = id;
    }

    return entry;
}

static int entry_set_value(struct flb_hash_table_entry *entry, void *val, size_t val_size)
{
    char *ptr;

    /*
     * If the entry already contains a previous value in the heap, just remove
     * the previously assigned memory.
     */
    if (entry->val_size > 0) {
        flb_free(entry->val);
    }

    /*
     * Now set the new value. If val_size > 0, we create a new memory area, otherwise
     * it means the caller just wants to store a pointer address, no allocation
     * is required.
     */
    if (val_size > 0) {
        entry->val = flb_malloc(val_size + 1);
        if (!entry->val) {
            flb_errno();
            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);
        ptr = (char *) entry->val;
        ptr[val_size] = '\0';
        entry->val_size = val_size;
    }
    else {
        /* just do a reference */
        entry->val = val;
        entry->val_size = -1;
    }

    entry->created = time(NULL);

    return 0;
}

int flb_hash_table_add(struct flb_hash_table *ht, const char *key, int key_len,
                       void *val, ssize_t val_size)
{
    int id;
    int ret;
    uint64_t hash;
    struct flb_hash_table_entry *entry;
    struct flb_hash_table_chain *table;

    if (!key || key_len <= 0) {
        return -1;
    }

    /* Check capacity */
    if (ht->max_entries > 0 && ht->total_count >= ht->max_entries) {
        if (ht->evict_mode == FLB_HASH_TABLE_EVICT_NONE) {
            /* Do nothing */
        }
        else if (ht->evict_mode == FLB_HASH_TABLE_EVICT_OLDER) {
            flb_hash_table_evict_older(ht);
        }
        else if (ht->evict_mode == FLB_HASH_TABLE_EVICT_LESS_USED) {
            flb_hash_table_evict_less_used(ht);
        }
        else if (ht->evict_mode == FLB_HASH_TABLE_EVICT_RANDOM) {
            flb_hash_table_evict_random(ht);
        }
    }

    /* Check if this is a replacement */
    entry = hash_get_entry(ht, key, key_len, &id);
    if (entry) {
        /*
         * The key already exists, just perform a value replacement, check if the
         * value refers to our own previous allocation.
         */
        ret = entry_set_value(entry, val, val_size);
        if (ret == -1) {
            return -1;
        }

        return id;
    }

    /*
     * Below is just code to handle the creation of a new entry in the table
     */

    /* Generate hash number */
    hash = cfl_hash_64bits(key, key_len);
    id = (hash % ht->size);

    /* Allocate the entry */
    entry = flb_calloc(1, sizeof(struct flb_hash_table_entry));
    if (!entry) {
        flb_errno();
        return -1;
    }
    entry->created = time(NULL);
    entry->hash = hash;
    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_size = 0;

    /* store or reference the value */
    ret = entry_set_value(entry, val, val_size);
    if (ret == -1) {
        flb_free(entry);
        return -1;
    }

    /* Link the new entry in our table at the end of the list */
    table = &ht->table[id];
    entry->table = table;

    /* Add the new entry */
    mk_list_add(&entry->_head, &table->chains);
    mk_list_add(&entry->_head_parent, &ht->entries);

    /* Update counters */
    table->count++;
    ht->total_count++;

    return id;
}

int flb_hash_table_get(struct flb_hash_table *ht,
                       const char *key, int key_len,
                       void **out_buf, size_t *out_size)
{
    int id;
    struct flb_hash_table_entry *entry;
    time_t expiration;

    entry = hash_get_entry(ht, key, key_len, &id);
    if (!entry) {
        return -1;
    }

    if (ht->cache_ttl > 0) {
        expiration = entry->created + ht->cache_ttl;
        if (time(NULL) > expiration) {
            flb_hash_table_entry_free(ht, entry);
            return -1;
        }
    }

    entry->hits++;
    *out_buf = entry->val;
    *out_size = entry->val_size;

    return id;
}

/* check if a hash exists */
int flb_hash_table_exists(struct flb_hash_table *ht, uint64_t hash)
{
    int id;
    struct mk_list *head;
    struct flb_hash_table_chain *table;
    struct flb_hash_table_entry *entry;

    id = (hash % ht->size);
    table = &ht->table[id];

    /* Iterate entries */
    mk_list_foreach(head, &table->chains) {
        entry = mk_list_entry(head, struct flb_hash_table_entry, _head);
        if (entry->hash == hash) {
            return FLB_TRUE;
        }
    }

    return FLB_FALSE;
}

/*
 * 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_table_get_by_id(struct flb_hash_table *ht, int id,
                             const char *key,
                             const char **out_buf, size_t * out_size)
{
    struct mk_list *head;
    struct flb_hash_table_entry *entry = NULL;
    struct flb_hash_table_chain *table;

    if (ht->size <= id) {
        return -1;
    }

    table = &ht->table[id];
    if (table->count == 0) {
        return -1;
    }

    if (table->count == 1) {
        entry = mk_list_entry_first(&table->chains,
                                    struct flb_hash_table_entry, _head);
    }
    else {
        mk_list_foreach(head, &table->chains) {
            entry = mk_list_entry(head, struct flb_hash_table_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;
}

void *flb_hash_table_get_ptr(struct flb_hash_table *ht, const char *key, int key_len)
{
    int id;
    struct flb_hash_table_entry *entry;

    entry = hash_get_entry(ht, key, key_len, &id);
    if (!entry) {
        return NULL;
    }

    entry->hits++;
    return entry->val;
}

int flb_hash_table_del(struct flb_hash_table *ht, const char *key)
{
    int id;
    int len;
    uint64_t hash;
    struct mk_list *head;
    struct flb_hash_table_entry *entry = NULL;
    struct flb_hash_table_chain *table;

    if (!key) {
        return -1;
    }

    len = strlen(key);
    if (len == 0) {
        return -1;
    }

    hash = cfl_hash_64bits(key, len);
    id = (hash % ht->size);

    table = &ht->table[id];
    if (table->count == 1) {
        entry = mk_list_entry_first(&table->chains,
                                    struct flb_hash_table_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_table_entry, _head);
            if (strcmp(entry->key, key) == 0) {
                break;
            }
            entry = NULL;
        }
    }

    if (!entry) {
        return -1;
    }

    flb_hash_table_entry_free(ht, entry);

    return 0;
}