forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gkmhash.c
1479 lines (1216 loc) · 39.7 KB
/
gkmhash.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* gkhash.c -- default hash table functions
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2023 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "gkmhash.h"
#include "error.h"
#include "gkhash.h"
#include "persistence.h"
#include "sort.h"
#include "util.h"
#include "xmalloc.h"
/* *INDENT-OFF* */
/* Per module - These metrics are not dated */
const GKHashMetric global_metrics[] = {
{ .metric.storem=MTRC_UNIQUE_KEYS , MTRC_TYPE_SI32 , new_si32_ht , des_si32_free , del_si32_free , 1 , NULL , "SI32_UNIQUE_KEYS.db" } ,
{ .metric.storem=MTRC_AGENT_KEYS , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 0 , NULL , "II32_AGENT_KEYS.db" } ,
{ .metric.storem=MTRC_AGENT_VALS , MTRC_TYPE_IS32 , new_is32_ht , des_is32_free , del_is32_free , 1 , NULL , "IS32_AGENT_VALS.db" } ,
{ .metric.storem=MTRC_CNT_VALID , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 1 , NULL , "II32_CNT_VALID.db" } ,
{ .metric.storem=MTRC_CNT_BW , MTRC_TYPE_IU64 , new_iu64_ht , des_iu64 , del_iu64 , 1 , NULL , "IU64_CNT_BW.db" } ,
};
/* Per module & per date */
GKHashMetric module_metrics[] = {
{ .metric.storem=MTRC_KEYMAP , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_ROOTMAP , MTRC_TYPE_IS32 , new_is32_ht , des_is32_free , del_is32_free , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_DATAMAP , MTRC_TYPE_IS32 , new_is32_ht , des_is32_free , del_is32_free , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_UNIQMAP , MTRC_TYPE_U648 , new_u648_ht , des_u648 , del_u648 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_ROOT , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_HITS , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_VISITORS , MTRC_TYPE_II32 , new_ii32_ht , des_ii32 , del_ii32 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_BW , MTRC_TYPE_IU64 , new_iu64_ht , des_iu64 , del_iu64 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_CUMTS , MTRC_TYPE_IU64 , new_iu64_ht , des_iu64 , del_iu64 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_MAXTS , MTRC_TYPE_IU64 , new_iu64_ht , des_iu64 , del_iu64 , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_METHODS , MTRC_TYPE_II08 , new_ii08_ht , des_ii08 , del_ii08 , 0 , NULL , NULL } ,
{ .metric.storem=MTRC_PROTOCOLS , MTRC_TYPE_II08 , new_ii08_ht , des_ii08 , del_ii08 , 0 , NULL , NULL } ,
{ .metric.storem=MTRC_AGENTS , MTRC_TYPE_IGSL , new_igsl_ht , des_igsl_free , del_igsl_free , 1 , NULL , NULL } ,
{ .metric.storem=MTRC_METADATA , MTRC_TYPE_SU64 , new_su64_ht , des_su64_free , del_su64_free , 1 , NULL , NULL } ,
};
size_t module_metrics_len = ARRAY_SIZE (module_metrics);
size_t global_metrics_len = ARRAY_SIZE (global_metrics);
/* *INDENT-ON* */
/* Allocate memory for a new store container GKHashStorage instance.
*
* On success, the newly allocated GKHashStorage is returned . */
static GKHashStorage *
new_gkhstorage (void) {
GKHashStorage *storage = xcalloc (1, sizeof (GKHashStorage));
return storage;
}
/* Allocate memory for a new module GKHashModule instance.
*
* On success, the newly allocated GKHashStorage is returned . */
static GKHashModule *
new_gkhmodule (uint32_t size) {
GKHashModule *storage = xcalloc (size, sizeof (GKHashModule));
return storage;
}
/* Allocate memory for a new global GKHashGlobal instance.
*
* On success, the newly allocated GKHashGlobal is returned . */
static GKHashGlobal *
new_gkhglobal (void) {
GKHashGlobal *storage = xcalloc (1, sizeof (GKHashGlobal));
return storage;
}
/* Initialize a global hash structure.
*
* On success, a pointer to that hash structure is returned. */
static GKHashGlobal *
init_gkhashglobal (void) {
GKHashGlobal *storage = NULL;
int n = 0, i;
storage = new_gkhglobal ();
n = global_metrics_len;
for (i = 0; i < n; i++) {
storage->metrics[i] = global_metrics[i];
storage->metrics[i].hash = global_metrics[i].alloc ();
}
return storage;
}
/* Initialize module metrics and mallocs its hash structure */
static void
init_tables (GModule module, GKHashModule *storage) {
int n = 0, i;
n = module_metrics_len;
for (i = 0; i < n; i++) {
storage[module].metrics[i] = module_metrics[i];
storage[module].metrics[i].hash = module_metrics[i].alloc ();
}
}
/* Initialize a module hash structure.
*
* On success, a pointer to that hash structure is returned. */
static GKHashModule *
init_gkhashmodule (void) {
GKHashModule *storage = NULL;
GModule module;
size_t idx = 0;
storage = new_gkhmodule (TOTAL_MODULES);
FOREACH_MODULE (idx, module_list) {
module = module_list[idx];
storage[module].module = module;
init_tables (module, storage);
}
return storage;
}
/* Destroys malloc'd global metrics */
static void
free_global_metrics (GKHashGlobal *ghash) {
int i, n = 0;
GKHashMetric mtrc;
if (!ghash)
return;
n = global_metrics_len;
for (i = 0; i < n; i++) {
mtrc = ghash->metrics[i];
mtrc.des (mtrc.hash, mtrc.free_data);
}
}
/* Destroys malloc'd mdule metrics */
static void
free_module_metrics (GKHashModule *mhash, GModule module, uint8_t free_data) {
int i, n = 0;
GKHashMetric mtrc;
if (!mhash)
return;
n = module_metrics_len;
for (i = 0; i < n; i++) {
mtrc = mhash[module].metrics[i];
mtrc.des (mtrc.hash, free_data ? mtrc.free_data : 0);
}
}
/* For each module metric, deletes all entries from the hash table */
static void
del_module_metrics (GKHashModule *mhash, GModule module, uint8_t free_data) {
int i, n = 0;
GKHashMetric mtrc;
n = module_metrics_len;
for (i = 0; i < n; i++) {
mtrc = mhash[module].metrics[i];
mtrc.del (mtrc.hash, free_data);
}
}
/* Destroys all hash tables and possibly all the malloc'd data within */
static void
free_stores (GKHashStorage *store) {
GModule module;
size_t idx = 0;
free_global_metrics (store->ghash);
FOREACH_MODULE (idx, module_list) {
module = module_list[idx];
free_module_metrics (store->mhash, module, 1);
}
free (store->ghash);
free (store->mhash);
free (store);
}
/* Insert an uint32_t key (date) and a GKHashStorage payload
*
* On error, -1 is returned.
* On key found, 1 is returned.
* On success 0 is returned */
static int
ins_igkh (khash_t (igkh) *hash, uint32_t key) {
GKHashStorage *store = NULL;
khint_t k;
int ret;
if (!hash)
return -1;
k = kh_put (igkh, hash, key, &ret);
/* operation failed */
if (ret == -1)
return -1;
/* the key is present in the hash table */
if (ret == 0)
return 1;
store = new_gkhstorage ();
store->mhash = init_gkhashmodule ();
store->ghash = init_gkhashglobal ();
kh_val (hash, k) = store;
return 0;
}
/* Given a hash and a key (date), get the relevant store
*
* On error or not found, NULL is returned.
* On success, a pointer to that store is returned. */
static void *
get_store (khash_t (igkh) *hash, uint32_t key) {
GKHashStorage *store = NULL;
khint_t k;
k = kh_get (igkh, hash, key);
/* key not found, return NULL */
if (k == kh_end (hash))
return NULL;
store = kh_val (hash, k);
return store;
}
/* Given a store, a module and the metric, get the hash table
*
* On error or not found, NULL is returned.
* On success, a pointer to that hash table is returned. */
static void *
get_hash_from_store (GKHashStorage *store, int module, GSMetric metric) {
int mtrc = 0, cnt = 0;
if (!store)
return NULL;
if (module == -1) {
mtrc = metric - MTRC_METADATA - 1;
cnt = MTRC_CNT_BW - MTRC_UNIQUE_KEYS + 1;
if (mtrc >= cnt) {
LOG_DEBUG (("Out of bounds when attempting to get hash %d\n", metric));
return NULL;
}
}
/* ###NOTE: BE CAREFUL here, to avoid the almost unnecessary loop, we simply
* use the index from the enum to make it O(1). The metrics array has to be
* created in the same order as the GSMetric enum */
if (module < 0)
return store->ghash->metrics[mtrc].hash;
return store->mhash[module].metrics[metric].hash;
}
/* Given a module a key (date) and the metric, get the hash table
*
* On error or not found, NULL is returned.
* On success, a pointer to that hash table is returned. */
void *
get_hash (int module, uint64_t key, GSMetric metric) {
GKHashStorage *store = NULL;
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * hash = get_hdb (db, MTRC_DATES);
if ((store = get_store (hash, key)) == NULL)
return NULL;
return get_hash_from_store (store, module, metric);
}
/* Given a module and a metric, get the cache hash table
*
* On success, a pointer to that hash table is returned. */
static void *
get_hash_from_cache (GModule module, GSMetric metric) {
GKDB *db = get_db_instance (DB_INSTANCE);
return db->cache[module].metrics[metric].hash;
}
GSLList *
ht_get_keymap_list_from_key (GModule module, uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
GSLList *list = NULL;
khiter_t kv;
khint_t k;
khash_t (ii32) * hash = NULL;
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
if (!dates)
return NULL;
for (k = kh_begin (dates); k != kh_end (dates); ++k) {
if (!kh_exist (dates, k))
continue;
if (!(hash = get_hash (module, kh_key (dates, k), MTRC_KEYMAP)))
continue;
if ((kv = kh_get (ii32, hash, key)) == kh_end (hash))
continue;
list = list_insert_prepend (list, i322ptr (kh_val (hash, kv)));
}
return list;
}
/* Insert a unique visitor key string (IP/DATE/UA), mapped to an auto
* incremented value.
*
* If the given key exists, its value is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
uint32_t
ht_insert_unique_key (uint32_t date, const char *key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si32) * seqs = get_hdb (db, MTRC_SEQS);
khash_t (si32) * hash = get_hash (-1, date, MTRC_UNIQUE_KEYS);
uint32_t val = 0;
char *dupkey = NULL;
if (!hash)
return 0;
if ((val = get_si32 (hash, key)) != 0)
return val;
dupkey = xstrdup (key);
if ((val = ins_si32_inc (hash, dupkey, ht_ins_seq, seqs, "ht_unique_keys")) == 0)
free (dupkey);
return val;
}
/* Insert a user agent key string, mapped to an auto incremented value.
*
* If the given key exists, its value is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
uint32_t
ht_insert_agent_key (uint32_t date, uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si32) * seqs = get_hdb (db, MTRC_SEQS);
khash_t (ii32) * hash = get_hash (-1, date, MTRC_AGENT_KEYS);
uint32_t val = 0;
if (!hash)
return 0;
if ((val = get_ii32 (hash, key)) != 0)
return val;
return ins_ii32_inc (hash, key, ht_ins_seq, seqs, "ht_agent_keys");
}
/* Insert a user agent uint32_t key, mapped to a user agent string value.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_agent_value (uint32_t date, uint32_t key, char *value) {
khash_t (is32) * hash = get_hash (-1, date, MTRC_AGENT_VALS);
char *dupval = NULL;
if (!hash)
return -1;
if ((kh_get (is32, hash, key)) != kh_end (hash))
return 0;
dupval = xstrdup (value);
if (ins_is32 (hash, key, dupval) != 0)
free (dupval);
return 0;
}
/* Insert a keymap string key.
*
* If the given key exists, its value is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
uint32_t
ht_insert_keymap (GModule module, uint32_t date, uint32_t key, uint32_t *ckey) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si32) * seqs = get_hdb (db, MTRC_SEQS);
khash_t (ii32) * hash = get_hash (module, date, MTRC_KEYMAP);
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_KEYMAP);
uint32_t val = 0;
char *modstr = NULL;
if (!hash)
return 0;
if ((val = get_ii32 (hash, key)) != 0) {
*ckey = get_ii32 (cache, key);
return val;
}
modstr = get_module_str (module);
if ((val = ins_ii32_inc (hash, key, ht_ins_seq, seqs, modstr)) == 0) {
free (modstr);
return val;
}
*ckey = ins_ii32_ai (cache, key);
free (modstr);
return val;
}
/* Insert a rootmap uint32_t key from the keymap store mapped to its string
* value.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_rootmap (GModule module, uint32_t date, uint32_t key, const char *value,
uint32_t ckey) {
khash_t (is32) * hash = get_hash (module, date, MTRC_ROOTMAP);
khash_t (is32) * cache = get_hash_from_cache (module, MTRC_ROOTMAP);
char *dupval = NULL;
int ret = 0;
if (!hash)
return -1;
dupval = xstrdup (value);
if ((ret = ins_is32 (hash, key, dupval)) == 0)
ins_is32 (cache, ckey, dupval);
else
free (dupval);
return ret;
}
/* Insert a datamap uint32_t key and string value.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_datamap (GModule module, uint32_t date, uint32_t key, const char *value,
uint32_t ckey) {
khash_t (is32) * hash = get_hash (module, date, MTRC_DATAMAP);
khash_t (is32) * cache = get_hash_from_cache (module, MTRC_DATAMAP);
char *dupval = NULL;
int ret = 0;
if (!hash)
return -1;
dupval = xstrdup (value);
if ((ret = ins_is32 (hash, key, dupval)) == 0)
ins_is32 (cache, ckey, dupval);
else
free (dupval);
return ret;
}
/* Insert a uniqmap string key.
*
* If the given key exists, 0 is returned.
* On error, 0 is returned.
* On success the value of the key inserted is returned */
int
ht_insert_uniqmap (GModule module, uint32_t date, uint32_t key, uint32_t value) {
khash_t (u648) * hash = get_hash (module, date, MTRC_UNIQMAP);
uint64_t k = 0;
if (!hash)
return 0;
k = u64encode (key, value);
return ins_u648 (hash, k, 1) == 0 ? 1 : 0;
}
/* Insert a data uint32_t key mapped to the corresponding uint32_t root key.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_root (GModule module, uint32_t date, uint32_t key, uint32_t value, uint32_t dkey,
uint32_t rkey) {
khash_t (ii32) * hash = get_hash (module, date, MTRC_ROOT);
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_ROOT);
if (!hash)
return -1;
ins_ii32 (cache, dkey, rkey);
return ins_ii32 (hash, key, value);
}
/* Increases hits counter from a uint32_t key.
*
* On error, 0 is returned.
* On success the inserted value is returned */
uint32_t
ht_insert_hits (GModule module, uint32_t date, uint32_t key, uint32_t inc, uint32_t ckey) {
khash_t (ii32) * hash = get_hash (module, date, MTRC_HITS);
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_HITS);
if (!hash)
return 0;
inc_ii32 (cache, ckey, inc);
return inc_ii32 (hash, key, inc);
}
/* Increases visitors counter from a uint32_t key.
*
* On error, 0 is returned.
* On success the inserted value is returned */
uint32_t
ht_insert_visitor (GModule module, uint32_t date, uint32_t key, uint32_t inc, uint32_t ckey) {
khash_t (ii32) * hash = get_hash (module, date, MTRC_VISITORS);
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_VISITORS);
if (!hash)
return 0;
inc_ii32 (cache, ckey, inc);
return inc_ii32 (hash, key, inc);
}
/* Increases bandwidth counter from a uint32_t key.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_bw (GModule module, uint32_t date, uint32_t key, uint64_t inc, uint32_t ckey) {
khash_t (iu64) * hash = get_hash (module, date, MTRC_BW);
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_BW);
if (!hash)
return -1;
inc_iu64 (cache, ckey, inc);
return inc_iu64 (hash, key, inc);
}
/* Increases cumulative time served counter from a uint32_t key.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_cumts (GModule module, uint32_t date, uint32_t key, uint64_t inc, uint32_t ckey) {
khash_t (iu64) * hash = get_hash (module, date, MTRC_CUMTS);
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_CUMTS);
if (!hash)
return -1;
inc_iu64 (cache, ckey, inc);
return inc_iu64 (hash, key, inc);
}
/* Insert the maximum time served counter from a uint32_t key.
* Note: it compares the current value with the given value.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_maxts (GModule module, uint32_t date, uint32_t key, uint64_t value, uint32_t ckey) {
khash_t (iu64) * hash = get_hash (module, date, MTRC_MAXTS);
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_MAXTS);
if (!hash)
return -1;
if (get_iu64 (cache, ckey) < value)
ins_iu64 (cache, ckey, value);
if (get_iu64 (hash, key) < value)
ins_iu64 (hash, key, value);
return 0;
}
/* Insert a method given an uint32_t key and string value.
*
* On error, or if key exists, -1 is returned.
* On success 0 is returned */
int
ht_insert_method (GModule module, uint32_t date, uint32_t key, const char *value,
uint32_t ckey) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (ii08) * hash = get_hash (module, date, MTRC_METHODS);
khash_t (ii08) * cache = get_hash_from_cache (module, MTRC_METHODS);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
int ret = 0;
uint8_t val = 0;
if (!hash)
return -1;
if (!(val = get_si08 (mtpr, value)))
return -1;
if ((ret = ins_ii08 (hash, key, val)) == 0)
ins_ii08 (cache, ckey, val);
return ret;
}
/* Insert a protocol given an uint32_t key and string value.
*
* On error, or if key exists, -1 is returned.
* On success 0 is returned */
int
ht_insert_protocol (GModule module, uint32_t date, uint32_t key, const char *value,
uint32_t ckey) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (ii08) * hash = get_hash (module, date, MTRC_PROTOCOLS);
khash_t (ii08) * cache = get_hash_from_cache (module, MTRC_PROTOCOLS);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
int ret = 0;
uint8_t val = 0;
if (!hash)
return -1;
if (!(val = get_si08 (mtpr, value)))
return -1;
if ((ret = ins_ii08 (hash, key, val)) == 0)
ins_ii08 (cache, ckey, val);
return ret;
}
/* Insert an agent for a hostname given an uint32_t key and uint32_t value.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_agent (GModule module, uint32_t date, uint32_t key, uint32_t value) {
khash_t (igsl) * hash = get_hash (module, date, MTRC_AGENTS);
if (!hash)
return -1;
return ins_igsl (hash, key, value);
}
/* Insert meta data counters from a string key.
*
* On error, -1 is returned.
* On success 0 is returned */
int
ht_insert_meta_data (GModule module, uint32_t date, const char *key, uint64_t value) {
khash_t (su64) * hash = get_hash (module, date, MTRC_METADATA);
if (!hash)
return -1;
return inc_su64 (hash, key, value);
}
int
ht_insert_date (uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * hash = get_hdb (db, MTRC_DATES);
if (!hash)
return -1;
return ins_igkh (hash, key);
}
uint32_t
ht_inc_cnt_valid (uint32_t date, uint32_t inc) {
khash_t (ii32) * hash = get_hash (-1, date, MTRC_CNT_VALID);
if (!hash)
return 0;
return inc_ii32 (hash, 1, inc);
}
int
ht_inc_cnt_bw (uint32_t date, uint64_t inc) {
khash_t (iu64) * hash = get_hash (-1, date, MTRC_CNT_BW);
if (!hash)
return 0;
return inc_iu64 (hash, 1, inc);
}
uint32_t
ht_sum_valid (void) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (ii32) * hash = NULL;
uint32_t k = 0;
uint32_t sum = 0;
if (!dates)
return 0;
/* *INDENT-OFF* */
HT_SUM_VAL (dates, k, {
if ((hash = get_hash (-1, k, MTRC_CNT_VALID)))
sum += get_ii32 (hash, 1);
});
/* *INDENT-ON* */
return sum;
}
uint64_t
ht_sum_bw (void) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (iu64) * hash = NULL;
uint32_t k = 0;
uint64_t sum = 0;
if (!dates)
return 0;
/* *INDENT-OFF* */
HT_SUM_VAL (dates, k, {
if ((hash = get_hash (-1, k, MTRC_CNT_BW)))
sum += get_iu64 (hash, 1);
});
/* *INDENT-ON* */
return sum;
}
/* Get the number of elements in a dates hash.
*
* Return 0 if the operation fails, else number of elements. */
uint32_t
ht_get_size_dates (void) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * hash = get_hdb (db, MTRC_DATES);
if (!hash)
return 0;
return kh_size (hash);
}
/* Get the number of elements in a datamap.
*
* Return -1 if the operation fails, else number of elements. */
uint32_t
ht_get_size_datamap (GModule module) {
khash_t (is32) * cache = get_hash_from_cache (module, MTRC_DATAMAP);
if (!cache)
return 0;
return kh_size (cache);
}
/* Get the number of elements in a uniqmap.
*
* On error, 0 is returned.
* On success the number of elements in MTRC_UNIQMAP is returned */
uint32_t
ht_get_size_uniqmap (GModule module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (u648) * hash = NULL;
uint32_t k = 0;
uint32_t sum = 0;
if (!dates)
return 0;
/* *INDENT-OFF* */
HT_SUM_VAL (dates, k, {
if ((hash = get_hash (module, k, MTRC_UNIQMAP)))
sum += kh_size (hash);
});
/* *INDENT-ON* */
return sum;
}
/* Get the string data value of a given uint32_t key.
*
* On error, NULL is returned.
* On success the string value for the given key is returned */
char *
ht_get_datamap (GModule module, uint32_t key) {
khash_t (is32) * cache = get_hash_from_cache (module, MTRC_DATAMAP);
if (!cache)
return NULL;
return get_is32 (cache, key);
}
/* Get the string root from MTRC_ROOTMAP given an uint32_t data key.
*
* On error, NULL is returned.
* On success the string value for the given key is returned */
char *
ht_get_root (GModule module, uint32_t key) {
int root_key = 0;
khash_t (ii32) * hashroot = get_hash_from_cache (module, MTRC_ROOT);
khash_t (is32) * hashrootmap = get_hash_from_cache (module, MTRC_ROOTMAP);
if (!hashroot || !hashrootmap)
return NULL;
/* not found */
if ((root_key = get_ii32 (hashroot, key)) == 0)
return NULL;
return get_is32 (hashrootmap, root_key);
}
/* Get the int visitors value from MTRC_VISITORS given an int key.
*
* If key is not found, 0 is returned.
* On error, -1 is returned.
* On success the int value for the given key is returned */
uint32_t
ht_get_hits (GModule module, int key) {
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_HITS);
if (!cache)
return 0;
return get_ii32 (cache, key);
}
/* Get the uint32_t visitors value from MTRC_VISITORS given an uint32_t key.
*
* If key is not found, 0 is returned.
* On error, -1 is returned.
* On success the uint32_t value for the given key is returned */
uint32_t
ht_get_visitors (GModule module, uint32_t key) {
khash_t (ii32) * cache = get_hash_from_cache (module, MTRC_VISITORS);
if (!cache)
return 0;
return get_ii32 (cache, key);
}
/* Get the uint64_t value from MTRC_BW given an uint32_t key.
*
* On error, or if key is not found, 0 is returned.
* On success the uint64_t value for the given key is returned */
uint64_t
ht_get_bw (GModule module, uint32_t key) {
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_BW);
if (!cache)
return 0;
return get_iu64 (cache, key);
}
/* Get the uint64_t value from MTRC_CUMTS given an uint32_t key.
*
* On error, or if key is not found, 0 is returned.
* On success the uint64_t value for the given key is returned */
uint64_t
ht_get_cumts (GModule module, uint32_t key) {
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_CUMTS);
if (!cache)
return 0;
return get_iu64 (cache, key);
}
/* Get the uint64_t value from MTRC_MAXTS given an uint32_t key.
*
* On error, or if key is not found, 0 is returned.
* On success the uint64_t value for the given key is returned */
uint64_t
ht_get_maxts (GModule module, uint32_t key) {
khash_t (iu64) * cache = get_hash_from_cache (module, MTRC_MAXTS);
if (!cache)
return 0;
return get_iu64 (cache, key);
}
uint8_t
get_method_proto (const char *value) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
uint8_t val = 0;
if (!mtpr)
return 0;
if ((val = get_si08 (mtpr, value)) != 0)
return val;
return 0;
}
/* Get the string value from MTRC_METHODS given an uint32_t key.
*
* On error, NULL is returned.
* On success the string value for the given key is returned */
char *
ht_get_method (GModule module, uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (ii08) * cache = get_hash_from_cache (module, MTRC_METHODS);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
uint8_t val = 0;
khint_t k;
if (!(val = get_ii08 (cache, key)))
return NULL;
for (k = kh_begin (mtpr); k != kh_end (mtpr); ++k) {
if (kh_exist (mtpr, k) && kh_val (mtpr, k) == val)
return xstrdup (kh_key (mtpr, k));
}
return NULL;
}
/* Get the string value from MTRC_PROTOCOLS given an uint32_t key.
*
* On error, NULL is returned.
* On success the string value for the given key is returned */
char *
ht_get_protocol (GModule module, uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (ii08) * cache = get_hash_from_cache (module, MTRC_PROTOCOLS);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
uint8_t val = 0;
khint_t k;
if (!(val = get_ii08 (cache, key)))
return NULL;
for (k = kh_begin (mtpr); k != kh_end (mtpr); ++k) {
if (kh_exist (mtpr, k) && kh_val (mtpr, k) == val)
return xstrdup (kh_key (mtpr, k));
}
return NULL;
}
/* Get the string value from ht_agent_vals (user agent) given an uint32_t key.
*
* On error, NULL is returned.
* On success the string value for the given key is returned */
char *
ht_get_host_agent_val (uint32_t key) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (is32) * hash = NULL;
char *data = NULL;
uint32_t k = 0;
if (!dates)
return NULL;
/* *INDENT-OFF* */
HT_FIRST_VAL (dates, k, {