forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.c
1232 lines (1056 loc) · 30.4 KB
/
persistence.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
/**
* persistence.c -- on-disk persistence functionality
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* 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 <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "persistence.h"
#include "error.h"
#include "gkhash.h"
#include "sort.h"
#include "tpl.h"
#include "util.h"
#include "xmalloc.h"
static uint32_t *persisted_dates = NULL;
static uint32_t persisted_dates_len = 0;
/* Determine the path for the given database file.
*
* On error, a fatal error is thrown.
* On success, the databases path string is returned. */
static char *
set_db_path (const char *fn) {
struct stat info;
char *rpath = NULL, *path = NULL;
const char *dbpath = NULL;
if (!conf.db_path)
dbpath = DB_PATH;
else
dbpath = conf.db_path;
rpath = realpath (dbpath, NULL);
if (rpath == NULL)
FATAL ("Unable to open the specified db path/file '%s'. %s", dbpath, strerror (errno));
/* sanity check: Is db_path accessible and a directory? */
if (stat (rpath, &info) != 0)
FATAL ("Unable to access database path: %s", strerror (errno));
else if (!(info.st_mode & S_IFDIR))
FATAL ("Database path is not a directory.");
path = xmalloc (snprintf (NULL, 0, "%s/%s", rpath, fn) + 1);
sprintf (path, "%s/%s", rpath, fn);
free (rpath);
return path;
}
/* Given a database filename, restore a string key, uint32_t value back to the
* storage */
static void
restore_global_si08 (khash_t (si08) *hash, const char *fn) {
tpl_node *tn;
char *key = NULL;
char fmt[] = "A(sv)";
uint16_t val;
tn = tpl_map (fmt, &key, &val);
tpl_load (tn, TPL_FILE, fn);
while (tpl_unpack (tn, 1) > 0) {
ins_si08 (hash, key, val);
free (key);
}
tpl_free (tn);
}
/* Given a hash and a filename, persist to disk a string key, uint32_t value */
static void
persist_global_si08 (khash_t (si08) *hash, const char *fn) {
tpl_node *tn;
khint_t k;
const char *key = NULL;
char fmt[] = "A(sv)";
uint16_t val;
if (!hash || kh_size (hash) == 0)
return;
tn = tpl_map (fmt, &key, &val);
for (k = 0; k < kh_end (hash); ++k) {
if (!kh_exist (hash, k) || (!(key = kh_key (hash, k))))
continue;
val = kh_value (hash, k);
tpl_pack (tn, 1);
}
tpl_dump (tn, TPL_FILE, fn);
tpl_free (tn);
}
/* Given a database filename, restore a string key, uint32_t value back to the
* storage */
static void
restore_global_si32 (khash_t (si32) *hash, const char *fn) {
tpl_node *tn;
char *key = NULL;
char fmt[] = "A(su)";
uint32_t val;
tn = tpl_map (fmt, &key, &val);
tpl_load (tn, TPL_FILE, fn);
while (tpl_unpack (tn, 1) > 0) {
ins_si32 (hash, key, val);
free (key);
}
tpl_free (tn);
}
/* Given a hash and a filename, persist to disk a string key, uint32_t value */
static void
persist_global_si32 (khash_t (si32) *hash, const char *fn) {
tpl_node *tn;
khint_t k;
const char *key = NULL;
char fmt[] = "A(su)";
uint32_t val;
if (!hash || kh_size (hash) == 0)
return;
tn = tpl_map (fmt, &key, &val);
for (k = 0; k < kh_end (hash); ++k) {
if (!kh_exist (hash, k) || (!(key = kh_key (hash, k))))
continue;
val = kh_value (hash, k);
tpl_pack (tn, 1);
}
tpl_dump (tn, TPL_FILE, fn);
tpl_free (tn);
}
/* Given a database filename, restore a uint64_t key, GLastParse value back to
* the storage */
static void
restore_global_iglp (khash_t (iglp) *hash, const char *fn) {
tpl_node *tn;
uint64_t key;
GLastParse val = { 0 };
char fmt[] = "A(US(uIUvc#))";
tn = tpl_map (fmt, &key, &val, READ_BYTES);
tpl_load (tn, TPL_FILE, fn);
while (tpl_unpack (tn, 1) > 0) {
ins_iglp (hash, key, val);
}
tpl_free (tn);
}
/* Given a hash and a filename, persist to disk a uint64_t key, uint32_t value */
static void
persist_global_iglp (khash_t (iglp) *hash, const char *fn) {
tpl_node *tn;
khint_t k;
uint64_t key;
GLastParse val = { 0 };
char fmt[] = "A(US(uIUvc#))";
if (!hash || kh_size (hash) == 0)
return;
tn = tpl_map (fmt, &key, &val, READ_BYTES);
for (k = 0; k < kh_end (hash); ++k) {
if (!kh_exist (hash, k))
continue;
key = kh_key (hash, k);
val = kh_value (hash, k);
tpl_pack (tn, 1);
}
tpl_dump (tn, TPL_FILE, fn);
tpl_free (tn);
}
/* Given a filename, ensure we have a valid return path
*
* On error, NULL is returned.
* On success, the valid path is returned */
static char *
check_restore_path (const char *fn) {
char *path = set_db_path (fn);
if (access (path, F_OK) != -1)
return path;
LOG_DEBUG (("DB file %s doesn't exist. %s\n", path, strerror (errno)));
free (path);
return NULL;
}
static char *
build_filename (const char *type, const char *modstr, const char *mtrstr) {
char *fn = xmalloc (snprintf (NULL, 0, "%s_%s_%s.db", type, modstr, mtrstr) + 1);
sprintf (fn, "%s_%s_%s.db", type, modstr, mtrstr);
return fn;
}
/* Get the database filename given a module and a metric.
*
* On error, a fatal error is triggered.
* On success, the filename is returned */
static char *
get_filename (GModule module, GKHashMetric mtrc) {
char *mtrstr = NULL, *modstr = NULL, *type = NULL, *fn = NULL;
if (!(mtrstr = get_mtr_str (mtrc.metric.storem)))
FATAL ("Unable to allocate metric name.");
if (!(modstr = get_module_str (module)))
FATAL ("Unable to allocate module name.");
if (!(type = get_mtr_type_str (mtrc.type)))
FATAL ("Unable to allocate module name.");
fn = build_filename (type, modstr, mtrstr);
free (mtrstr);
free (type);
free (modstr);
return fn;
}
/* Dump to disk the database file and frees its memory */
static void
close_tpl (tpl_node *tn, const char *fn) {
tpl_dump (tn, TPL_FILE, fn);
tpl_free (tn);
}
/* Check if the given date can be inserted based on how many dates we need to
* keep conf.keep_last.
*
* Returns -1 if it fails to insert the date.
* Returns 1 if the date exists.
* Returns 2 if the date shouldn't be inserted.
* On success or if the date is inserted 0 is returned */
static int
insert_restored_date (uint32_t date) {
uint32_t i, len = 0;
/* no keep last, simply insert the restored date to our storage */
if (!conf.keep_last || persisted_dates_len < conf.keep_last)
return ht_insert_date (date);
len = MIN (persisted_dates_len, conf.keep_last);
for (i = 0; i < len; ++i)
if (persisted_dates[i] == date)
return ht_insert_date (date);
return 2;
}
/* Given a database filename, restore a string key, uint32_t value back to
* the storage */
static int
restore_si32 (GSMetric metric, const char *path, int module) {
khash_t (si32) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(su))";
int date = 0, ret = 0;
char *key = NULL;
uint32_t val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_si32 (hash, key, val);
free (key);
}
}
tpl_free (tn);
return 0;
}
/* Given a database filename, restore a string key, uint32_t value back to
* the storage */
static int
migrate_si32_to_ii32 (GSMetric metric, const char *path, int module) {
khash_t (ii32) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(su))";
int date = 0, ret = 0;
char *key = NULL;
uint32_t val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_ii32 (hash, djb2 ((unsigned char *) key), val);
free (key);
}
}
tpl_free (tn);
return 0;
}
static char *
migrate_unique_key (char *key) {
char *nkey = NULL, *token = NULL, *ptr = NULL;
char agent_hex[64] = { 0 };
uint32_t delims = 0;
if (!key || count_matches (key, '|') < 2)
return NULL;
nkey = xstrdup ("");
while ((ptr = strchr (key, '|'))) {
if (!(token = extract_by_delim (&key, "|"))) {
free (nkey);
return NULL;
}
append_str (&nkey, token);
append_str (&nkey, "|");
free (token);
key++;
delims++;
}
if (delims == 2) {
sprintf (agent_hex, "%" PRIx32, djb2 ((unsigned char *) key));
append_str (&nkey, agent_hex);
}
return nkey;
}
/* Given a database filename, restore a string key, uint32_t value back to
* the storage */
static int
migrate_si32_to_ii32_unique_keys (GSMetric metric, const char *path, int module) {
khash_t (si32) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(su))";
int date = 0, ret = 0;
char *key = NULL, *nkey = NULL;
uint32_t val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
if ((nkey = migrate_unique_key (key)))
ins_si32 (hash, nkey, val);
free (key);
free (nkey);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a string key, uint32_t value */
static int
persist_si32 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (si32) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(su))";
uint32_t val = 0;
const char *key = NULL;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a uint32_t key, string value back to
* the storage */
static int
migrate_is32_to_ii08 (GSMetric metric, const char *path, int module) {
khash_t (ii08) * hash = NULL;
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si08) * mtpr = get_hdb (db, MTRC_METH_PROTO);
tpl_node *tn;
char fmt[] = "A(iA(us))";
int date = 0, ret = 0;
uint32_t key = 0;
char *val = NULL;
khint_t k;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
k = kh_get (si08, mtpr, val);
/* key found, return current value */
if (k == kh_end (mtpr)) {
free (val);
continue;
}
ins_ii08 (hash, key, kh_val (mtpr, k));
free (val);
}
}
tpl_free (tn);
return 0;
}
/* Given a database filename, restore a uint32_t key, string value back to
* the storage */
static int
restore_is32 (GSMetric metric, const char *path, int module) {
khash_t (is32) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(us))";
int date = 0, ret = 0;
uint32_t key = 0;
char *val = NULL, *dupval = NULL;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
dupval = xstrdup (val);
if (ins_is32 (hash, key, dupval) != 0)
free (dupval);
free (val);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a uint32_t key, string value */
static int
persist_is32 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (is32) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(us))";
char *val = NULL;
uint32_t key = 0;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a uint32_t key, uint32_t value back to
* the storage */
static int
restore_ii08 (GSMetric metric, const char *path, int module) {
khash_t (ii08) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(uv))";
int date = 0, ret = 0;
uint32_t key = 0;
uint16_t val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_ii08 (hash, key, val);
}
}
tpl_free (tn);
return 0;
}
/* Given a database filename, restore a uint32_t key, uint32_t value back to
* the storage */
static int
restore_ii32 (GSMetric metric, const char *path, int module) {
khash_t (ii32) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(uu))";
int date = 0, ret = 0;
uint32_t key = 0, val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_ii32 (hash, key, val);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a uint32_t key, uint32_t value */
static int
persist_ii32 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (ii32) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(uu))";
uint32_t key = 0, val = 0;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a hash and a filename, persist to disk a uint32_t key, uint32_t value */
static int
persist_ii08 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (ii08) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(uv))";
uint32_t key = 0;
uint16_t val = 0;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a uint64_t key, uint8_t value back to
* the storage */
static int
restore_u648 (GSMetric metric, const char *path, int module) {
khash_t (u648) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(Uv))";
int date = 0, ret = 0;
uint64_t key;
uint16_t val = 0;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_u648 (hash, key, val);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a uint64_t key, uint8_t value */
static int
persist_u648 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (u648) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(Uv))";
uint64_t key;
uint16_t val = 0;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a uint32_t key, uint64_t value back to
* the storage */
static int
restore_iu64 (GSMetric metric, const char *path, int module) {
khash_t (iu64) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(uU))";
int date = 0, ret = 0;
uint32_t key;
uint64_t val;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_iu64 (hash, key, val);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a uint32_t key, uint64_t value */
static int
persist_iu64 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (iu64) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(uU))";
uint32_t key;
uint64_t val;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a string key, uint64_t value back to
* the storage */
static int
restore_su64 (GSMetric metric, const char *path, int module) {
khash_t (su64) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(sU))";
int date = 0, ret = 0;
char *key = NULL;
uint64_t val;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_su64 (hash, key, val);
free (key);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a string key, uint64_t value */
static int
persist_su64 (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (su64) * hash = NULL;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(sU))";
const char *key = NULL;
uint64_t val;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, val, { tpl_pack (tn, 2); });
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Given a database filename, restore a uint32_t key, GSLList value back to the
* storage */
static int
restore_igsl (GSMetric metric, const char *path, int module) {
khash_t (igsl) * hash = NULL;
tpl_node *tn;
char fmt[] = "A(iA(uu))";
int date = 0, ret = 0;
uint32_t key, val;
if (!(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
tpl_load (tn, TPL_FILE, path);
while (tpl_unpack (tn, 1) > 0) {
if ((ret = insert_restored_date (date)) == 2)
continue;
if (ret == -1 || !(hash = get_hash (module, date, metric)))
break;
while (tpl_unpack (tn, 2) > 0) {
ins_igsl (hash, key, val);
}
}
tpl_free (tn);
return 0;
}
/* Given a hash and a filename, persist to disk a uint32_t key, GSLList value */
static int
persist_igsl (GSMetric metric, const char *path, int module) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (igkh) * dates = get_hdb (db, MTRC_DATES);
khash_t (igsl) * hash = NULL;
GSLList *node;
tpl_node *tn = NULL;
int date = 0;
char fmt[] = "A(iA(uu))";
uint32_t key, val;
if (!dates || !(tn = tpl_map (fmt, &date, &key, &val)))
return 1;
/* *INDENT-OFF* */
HT_FOREACH_KEY (dates, date, {
if (!(hash = get_hash (module, date, metric)))
return -1;
kh_foreach (hash, key, node, {
while (node) {
val = (*(uint32_t *) node->data);
node = node->next;
}
tpl_pack (tn, 2);
});
tpl_pack (tn, 1);
});
/* *INDENT-ON* */
close_tpl (tn, path);
return 0;
}
/* Entry function to restore hash data by type */
static void
restore_by_type (GKHashMetric mtrc, const char *fn, int module) {
char *path = NULL;
if (!(path = check_restore_path (fn)))
goto clean;
switch (mtrc.type) {
case MTRC_TYPE_SI32:
restore_si32 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_IS32:
restore_is32 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_II08:
restore_ii08 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_II32:
restore_ii32 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_U648:
restore_u648 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_IU64:
restore_iu64 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_SU64:
restore_su64 (mtrc.metric.storem, path, module);
break;
case MTRC_TYPE_IGSL:
restore_igsl (mtrc.metric.storem, path, module);
break;
default:
break;
}
clean:
free (path);
}
/* Entry function to restore hash data by metric type */
static void
restore_metric_type (GModule module, GKHashMetric mtrc) {
char *fn = NULL;
fn = get_filename (module, mtrc);
restore_by_type (mtrc, fn, module);
free (fn);
}
static int
migrate_metric (GModule module, GKHashMetric mtrc) {
GKDB *db = get_db_instance (DB_INSTANCE);
khash_t (si32) * db_props = get_hdb (db, MTRC_DB_PROPS);
int ret = 0;
char *fn = NULL, *path = NULL;
char *modstr = NULL, *mtrstr = NULL;
khint_t k;
k = kh_get (si32, db_props, "version");
/* db is up-to-date, thus no need to migrate anything */
if (k != kh_end (db_props) && kh_val (db_props, k) == DB_VERSION)
return 0;
switch (mtrc.metric.storem) {
case MTRC_UNIQUE_KEYS:
if (!(path = check_restore_path ("SI32_UNIQUE_KEYS.db")))
break;
if (migrate_si32_to_ii32_unique_keys (mtrc.metric.storem, path, -1) != 0)
break;
unlink (path);
ret++;
break;
case MTRC_KEYMAP:
if (!(modstr = get_module_str (module)))
FATAL ("Unable to allocate module name.");
fn = build_filename ("SI32", modstr, "MTRC_KEYMAP");
if (!(path = check_restore_path (fn)))
break;
if (migrate_si32_to_ii32 (mtrc.metric.storem, path, module) != 0)
break;
unlink (path);
ret++;
break;
case MTRC_METHODS:
case MTRC_PROTOCOLS:
if (!(mtrstr = get_mtr_str (mtrc.metric.storem)))
FATAL ("Unable to allocate metric name.");
if (!(modstr = get_module_str (module)))
FATAL ("Unable to allocate module name.");
fn = build_filename ("IS32", modstr, mtrstr);
if (!(path = check_restore_path (fn)))
break;
if (migrate_is32_to_ii08 (mtrc.metric.storem, path, module) != 0)
break;
unlink (path);
ret++;
break;
case MTRC_AGENT_KEYS:
if (!(path = check_restore_path ("SI32_AGENT_KEYS.db")))
break;
if (migrate_si32_to_ii32 (mtrc.metric.storem, path, -1) != 0)
break;
unlink (path);
ret++;
break;
default: