-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathosbf_aux.c
More file actions
1336 lines (1177 loc) · 34 KB
/
osbf_aux.c
File metadata and controls
1336 lines (1177 loc) · 34 KB
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
/*
* osbf_aux.c
*
* This software is licensed to the public under the Free Software
* Foundation's GNU GPL, version 2. You may obtain a copy of the
* GPL by visiting the Free Software Foundations web site at
* www.fsf.org, and a copy is included in this distribution.
*
* Copyright 2005, 2006, 2007 Fidelis Assis, all rights reserved.
* Copyright 2005, 2006, 2007 Williams Yerazunis, all rights reserved.
*
* Read the HISTORY_AND_AGREEMENT for details.
* Patched by Kevin Hoffman on Sep 29, 2009 to not use file locking
* if OSBF_NO_FILE_LOCKING is defined (useful if you do your own).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include "osbflib.h"
#define BUCKET_BUFFER_SIZE 5000
/* Version names */
const char *db_version_names[] = {
"SBPH-Markovian",
"OSB-Bayes",
"Correlate",
"Neural",
"OSB-Winnow",
"OSBF-Bayes",
"Unknown"
};
uint32_t microgroom_chain_length = OSBF_MICROGROOM_CHAIN_LENGTH;
uint32_t microgroom_stop_after = OSBF_MICROGROOM_STOP_AFTER;
/*****************************************************************/
/*
* Pack a chain moving buckets to a place closer to their
* right positions whenever possible, using the buckets marked as free.
* At the end, all buckets still marked as free are zeroed.
*/
void
osbf_packchain (CLASS_STRUCT * class, uint32_t packstart, uint32_t packlen)
{
uint32_t packend, ifrom, ito, free_start;
uint32_t thash;
packend = packstart + packlen;
if (packend >= NUM_BUCKETS (class))
packend -= NUM_BUCKETS (class);
#ifdef DEBUG_packchain
{
uint32_t i, rp, d, h;
fprintf (stderr, "Before packing\n");
for (i = packstart; i != packend; i = NEXT_BUCKET (class, i))
{
h = BUCKET_HASH (class, i);
rp = HASH_INDEX (class, h);
if (i >= rp)
d = i - rp;
else
d = NUM_BUCKETS (class) + i - rp;
fprintf (stderr, " i: %5" PRIu32 " d: %3" PRIu32 " value: %" PRIu32
" h: %08X flags: %02X\n", i, d, BUCKET_VALUE (class, i),
h, BUCKET_FLAGS (class, i));
}
}
#endif
/* search the first marked-free bucket */
for (free_start = packstart;
free_start != packend; free_start = NEXT_BUCKET (class, free_start))
if (MARKED_FREE (class, free_start))
break;
if (free_start != packend)
{
for (ifrom = NEXT_BUCKET (class, free_start);
ifrom != packend; ifrom = NEXT_BUCKET (class, ifrom))
{
if (!MARKED_FREE (class, ifrom))
{
/* see if there's a free bucket closer to its right place */
thash = BUCKET_HASH (class, ifrom);
ito = HASH_INDEX (class, thash);
while (ito != ifrom && !MARKED_FREE (class, ito))
ito = NEXT_BUCKET (class, ito);
/* if found a marked-free bucket, use it */
if (MARKED_FREE (class, ito))
{
/* copy bucket and flags */
BUCKET_HASH (class, ito) = thash;
BUCKET_KEY (class, ito) = BUCKET_KEY (class, ifrom);
BUCKET_VALUE (class, ito) = BUCKET_VALUE (class, ifrom);
BUCKET_FLAGS (class, ito) = BUCKET_FLAGS (class, ifrom);
/* mark the from bucket as free */
MARK_IT_FREE (class, ifrom);
}
}
}
}
#ifdef DEBUG_packchain
{
uint32_t i, rp, d, h;
fprintf (stderr, "Before zeroing\n");
for (i = packstart; i != packend; i = NEXT_BUCKET (class, i))
{
h = BUCKET_HASH (class, i);
rp = HASH_INDEX (class, h);
if (i >= rp)
d = i - rp;
else
d = NUM_BUCKETS (class) + i - rp;
fprintf (stderr, " i: %5" PRIu32 " d: %3" PRIu32 " value: %" PRIu32
" h: %08X flags: %02X\n", i, d, BUCKET_VALUE (class, i),
h, BUCKET_FLAGS (class, i));
}
}
#endif
for (ito = packstart; ito != packend; ito = NEXT_BUCKET (class, ito))
if (MARKED_FREE (class, ito))
{
BUCKET_VALUE (class, ito) = 0;
UNMARK_IT_FREE (class, ito);
}
#ifdef DEBUG_packchain
{
uint32_t i, rp, d, h;
fprintf (stderr, "After packing\n");
for (i = packstart; i != packend; i = NEXT_BUCKET (class, i))
{
h = BUCKET_HASH (class, i);
rp = HASH_INDEX (class, h);
if (i >= rp)
d = i - rp;
else
d = NUM_BUCKETS (class) + i - rp;
fprintf (stderr, " i: %5" PRIu32 " d: %3" PRIu32 " value: %" PRIu32
" h: %08X flags: %02X\n", i, d, BUCKET_VALUE (class, i),
h, BUCKET_FLAGS (class, i));
}
}
#endif
}
/*****************************************************************/
/*
* Prune and pack a chain in a class database
* Returns the number of freed (zeroed) buckets
*/
uint32_t
osbf_microgroom (CLASS_STRUCT * class, uint32_t bindex)
{
uint32_t i_aux, j_aux, right_position;
static uint32_t microgroom_count = 0;
uint32_t packstart, packlen;
uint32_t zeroed_countdown, min_value, min_value_any;
uint32_t distance, max_distance;
uint32_t groom_locked = OSBF_MICROGROOM_LOCKED;
j_aux = 0;
zeroed_countdown = microgroom_stop_after;
i_aux = j_aux = 0;
microgroom_count++;
/* move to start of chain that overflowed,
* then prune just that chain.
*/
min_value = OSBF_MAX_BUCKET_VALUE;
i_aux = j_aux = HASH_INDEX (class, bindex);
min_value_any = BUCKET_VALUE (class, i_aux);
if (!BUCKET_IN_CHAIN (class, i_aux))
return 0; /* initial bucket not in a chain! */
while (BUCKET_IN_CHAIN (class, i_aux))
{
if (BUCKET_VALUE (class, i_aux) < min_value_any)
min_value_any = BUCKET_VALUE (class, i_aux);
if (BUCKET_VALUE (class, i_aux) < min_value &&
!BUCKET_IS_LOCKED (class, i_aux))
min_value = BUCKET_VALUE (class, i_aux);
i_aux = PREV_BUCKET (class, i_aux);
if (i_aux == j_aux)
break; /* don't hang if we have a 100% full .css file */
/* fprintf (stderr, "-"); */
}
/* now, move the index to the first bucket in this chain. */
i_aux = NEXT_BUCKET (class, i_aux);
packstart = i_aux;
/* find the end of the chain */
while (BUCKET_IN_CHAIN (class, i_aux))
{
i_aux = NEXT_BUCKET (class, i_aux);
if (i_aux == packstart)
break; /* don't hang if we have a 100% full .cfc file */
}
/* now, the index is right after the last bucket in this chain. */
/* only >, not >= in this case, otherwise the packlen would be 0
* instead of NUM_BUCKETS (class).
*/
if (i_aux > packstart)
packlen = i_aux - packstart;
else /* if i_aux == packstart, packlen = header->buckets */
packlen = NUM_BUCKETS (class) + i_aux - packstart;
/* if no unlocked bucket can be zeroed, zero any */
if (groom_locked > 0 || min_value == OSBF_MAX_BUCKET_VALUE)
{
groom_locked = 1;
min_value = min_value_any;
}
else
groom_locked = 0;
/*
* This pruning method zeroes buckets with minimum count in the chain.
* It tries first buckets with minimum distance to their right position,
* to increase the chance of zeroing older buckets first. If none with
* distance 0 is found, the distance is increased until at least one
* bucket is zeroed.
*
* We keep track of how many buckets we've marked to be zeroed and we
* stop marking additional buckets after that point. That messes up
* the tail length, and if we don't repack the tail, then features in
* the tail can become permanently inaccessible! Therefore, we really
* can't stop in the middle of the tail (well, we could stop marking,
* but we need to pass the full length of the tail in).
*
* This is a statistics report of microgroomings for 4147 messages
* of the SpamAssassin corpus. It shows that 77% is done in a single
* pass, 95.2% in 1 or 2 passes and 99% in at most 3 passes.
*
* # microgrommings passes % accum. %
* 232584 1 76.6 76.6
* 56396 2 18.6 95.2
* 11172 3 3.7 98.9
* 2502 4 0.8 99.7
* 726 5 0.2 99.9
* ...
* -----------
* 303773
*
* If we consider only the last 100 microgroomings, when the cfc
* file is full, we'll have the following numbers showing that most
* microgroomings (61%) are still done in a single pass, almost 90%
* is done in 1 or 2 passes and 97% are done in at most 3 passes:
*
* # microgrommings passes % accum. %
* 61 1 61 61
* 27 2 27 88
* 9 3 9 97
* 3 4 3 100
* ---
* 100
*
* So, it's not so slow. Anyway, a better algorithm could be
* implemented using 2 additional arrays, with MICROGROOM_STOP_AFTER
* positions each, to store the indexes of the candidate buckets
* found with distance equal to 1 or 2 while we scan for distance 0.
* Those with distance 0 are zeroed immediatelly. If none with
* distance 0 is found, we'll zero the indexes stored in the first
* array. Again, if none is found in the first array, we'll try the
* second one. Finally, if none is found in both arrays, the loop
* will continue until one bucket is zeroed.
*
* But now comes the question: do the numbers above justify the
* additional code/work? I'll try to find out the answer
* implementing it :), but this has low priority for now.
*
*/
/* try features in their right place first */
max_distance = 1;
/* fprintf(stderr, "packstart: %ld, packlen: %ld, max_zeroed_buckets: %ld\n",
packstart, packlen, microgroom_stop_after); */
/* while no bucket is zeroed... */
while (zeroed_countdown == microgroom_stop_after)
{
/*
fprintf(stderr, "Start: %lu, stop_after: %u, max_distance: %lu,
min_value: %lu\n", packstart,
microgroom_stop_after, max_distance, min_value);
*/
i_aux = packstart;
while (BUCKET_IN_CHAIN (class, i_aux) && zeroed_countdown > 0)
{
/* check if it's a candidate */
if ((BUCKET_VALUE (class, i_aux) == min_value) &&
(!BUCKET_IS_LOCKED (class, i_aux) || (groom_locked != 0)))
{
/* if it is, check the distance */
right_position = HASH_INDEX (class, BUCKET_HASH (class, i_aux));
if (right_position <= i_aux)
distance = i_aux - right_position;
else
distance = NUM_BUCKETS (class) + i_aux - right_position;
if (distance < max_distance)
{
MARK_IT_FREE (class, i_aux);
zeroed_countdown--;
}
}
i_aux++;
if (i_aux >= NUM_BUCKETS (class))
i_aux = 0;
}
/* if none was zeroed, increase the allowed distance between the */
/* candidade's position and its right place. */
if (zeroed_countdown == microgroom_stop_after)
max_distance++;
}
/*
fprintf (stderr,
"Leaving microgroom: %ld buckets with value %ld zeroed at distance %ld\n",
microgroom_stop_after - zeroed_countdown, h[i].value, max_distance - 1);
*/
/* now we pack the chains */
osbf_packchain (class, packstart, packlen);
/* return the number of zeroed buckets */
return (microgroom_stop_after - zeroed_countdown);
}
/*****************************************************************/
/* get next bucket index */
uint32_t
osbf_next_bindex (CLASS_STRUCT * class, uint32_t bindex)
{
bindex++;
if (bindex >= NUM_BUCKETS (class))
bindex = 0;
return bindex;
}
/*****************************************************************/
/* get the index of the last bucket in a chain */
uint32_t
osbf_last_in_chain (CLASS_STRUCT * class, uint32_t bindex)
{
uint32_t wraparound;
/* if the bucket is not in a chain, return an index */
/* out of the buckets space, equal to the number of */
/* buckets in the file to indicate an empty chain */
if (!BUCKET_IN_CHAIN (class, bindex))
return NUM_BUCKETS (class);
wraparound = bindex;
while (BUCKET_IN_CHAIN (class, bindex))
{
bindex++;
if (bindex >= NUM_BUCKETS (class))
bindex = 0;
/* if .cfc file is full return an index out of */
/* the buckets space, equal to number of buckets */
/* in the file, plus one */
if (bindex == wraparound)
return NUM_BUCKETS (class) + 1;
}
if (bindex == 0)
bindex = NUM_BUCKETS (class) - 1;
else
bindex--;
return bindex;
}
/*****************************************************************/
/* get previous bucket index */
uint32_t
osbf_prev_bindex (CLASS_STRUCT * class, uint32_t bindex)
{
if (bindex == 0)
bindex = NUM_BUCKETS (class) - 1;
else
bindex--;
return bindex;
}
/*****************************************************************/
/* get the index of the first bucket in a chain */
uint32_t
osbf_first_in_chain (CLASS_STRUCT * class, uint32_t bindex)
{
uint32_t wraparound;
/* if the bucket is not in a chain, return an index */
/* out of the buckets space, equal to the number of */
/* buckets in the file to indicate an empty chain */
if (!BUCKET_IN_CHAIN (class, bindex))
return NUM_BUCKETS (class);
wraparound = bindex;
while (BUCKET_IN_CHAIN (class, bindex))
{
if (bindex == 0)
bindex = NUM_BUCKETS (class) - 1;
else
bindex--;
/* if .cfc file is full return an index out of */
/* the buckets space, equal to number of buckets */
/* in the file, plus one */
if (bindex == wraparound)
return NUM_BUCKETS (class) + 1;
}
bindex++;
if (bindex >= NUM_BUCKETS (class))
bindex = 0;
return bindex;
}
/*****************************************************************/
uint32_t
osbf_find_bucket (CLASS_STRUCT * class, uint32_t hash, uint32_t key)
{
uint32_t bindex, start;
bindex = start = HASH_INDEX (class, hash);
while (BUCKET_IN_CHAIN (class, bindex) &&
!BUCKET_HASH_COMPARE (class, bindex, hash, key))
{
bindex = NEXT_BUCKET (class, bindex);
/* if .cfc file is completely full return an index */
/* out of the buckets space, equal to number of buckets */
/* in the file, plus one */
if (bindex == start)
return NUM_BUCKETS (class) + 1;
}
/* return the index of the found bucket or, if not found,
* the index of a free bucket where it could be put
*/
return bindex;
}
/*****************************************************************/
void
osbf_update_bucket (CLASS_STRUCT * class, uint32_t bindex, int delta)
{
/*
* fprintf (stderr, "Bucket updated at %lu, hash: %lu, key: %lu, value: %d\n",
* bindex, hashes[bindex].hash, hashes[bindex].key, delta);
*/
if (delta > 0 &&
BUCKET_VALUE (class, bindex) + delta >= OSBF_MAX_BUCKET_VALUE)
{
SETL_BUCKET_VALUE (class, bindex, OSBF_MAX_BUCKET_VALUE);
}
else if (delta < 0 && BUCKET_VALUE (class, bindex) <= (uint32_t) (-delta))
{
if (BUCKET_VALUE (class, bindex) != 0)
{
uint32_t i, packlen;
MARK_IT_FREE (class, bindex);
/* pack chain */
i = osbf_last_in_chain (class, bindex);
if (i >= bindex)
packlen = i - bindex + 1;
else
packlen = NUM_BUCKETS (class) - (bindex - i) + 1;
/*
fprintf (stderr, "packing: %" PRIu32 ", %" PRIu32 "\n", i,
bindex);
*/
osbf_packchain (class, bindex, packlen);
}
}
else
{
SETL_BUCKET_VALUE (class, bindex, BUCKET_VALUE (class, bindex) + delta);
}
}
/*****************************************************************/
void
osbf_insert_bucket (CLASS_STRUCT * class,
uint32_t bindex, uint32_t hash, uint32_t key, int value)
{
uint32_t right_index, distance;
int microgroom = 1;
/* "right" bucket index */
right_index = HASH_INDEX (class, hash);
/* distance from right position to free position */
distance = (bindex >= right_index) ? bindex - right_index :
NUM_BUCKETS (class) - (right_index - bindex);
/* if not specified, max chain len is automatically specified */
if (microgroom_chain_length == 0)
{
/* from experimental values */
microgroom_chain_length = 14.85 + 1.5E-4 * NUM_BUCKETS (class);
/* not less than 29 */
if (microgroom_chain_length < 29)
microgroom_chain_length = 29;
}
if (microgroom && (value > 0))
while (distance > microgroom_chain_length)
{
/*
* fprintf (stderr, "hindex: %lu, bindex: %lu, distance: %lu\n",
* hindex, bindex, distance);
*/
osbf_microgroom (class, PREV_BUCKET (class, bindex));
/* get new free bucket index */
bindex = osbf_find_bucket (class, hash, key);
distance = (bindex >= right_index) ? bindex - right_index :
NUM_BUCKETS (class) - (right_index - bindex);
}
/*
* fprintf (stderr,
* "new bucket at %lu, hash: %lu, key: %lu, distance: %lu\n",
* bindex, hash, key, distance);
*/
SETL_BUCKET_VALUE (class, bindex, value);
BUCKET_HASH (class, bindex) = hash;
BUCKET_KEY (class, bindex) = key;
}
/*****************************************************************/
uint32_t
strnhash (unsigned char *str, uint32_t len)
{
uint32_t i;
#ifdef CRM114_COMPATIBILITY
int32_t hval; /* signed int for CRM114 compatibility */
#else
uint32_t hval;
#endif
uint32_t tmp;
/* initialize hval */
hval = len;
/* for each character in the incoming text: */
for (i = 0; i < len; i++)
{
/*
* xor in the current byte against each byte of hval
* (which alone gaurantees that every bit of input will have
* an effect on the output)
*/
tmp = str[i];
tmp = tmp | (tmp << 8) | (tmp << 16) | (tmp << 24);
hval ^= tmp;
/* add some bits out of the middle as low order bits. */
hval = hval + ((hval >> 12) & 0x0000ffff);
/* swap most and min significative bytes */
tmp = (hval << 24) | ((hval >> 24) & 0xff);
hval &= 0x00ffff00; /* zero most and least significative bytes of hval */
hval |= tmp; /* OR with swapped bytes */
/* rotate hval 3 bits to the left (thereby making the */
/* 3rd msb of the above mess the hsb of the output hash) */
hval = (hval << 3) + (hval >> 29);
}
return (uint32_t) hval;
}
/*****************************************************************/
static OSBF_HEADER_BUCKET_UNION hu;
int
osbf_create_cfcfile (const char *cfcfile, uint32_t num_buckets,
uint32_t major, uint32_t minor, char *errmsg)
{
FILE *f;
uint32_t i_aux;
OSBF_BUCKET_STRUCT bucket = { 0, 0, 0 };
if (cfcfile == NULL || *cfcfile == '\0')
{
if (cfcfile != NULL)
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Invalid file name: '%s'", cfcfile);
else
strncpy (errmsg, "Invalid (NULL) pointer to cfc file name",
OSBF_ERROR_MESSAGE_LEN);
return -1;
}
f = fopen (cfcfile, "r");
if (f)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"File already exists: '%s'", cfcfile);
fclose(f);
return -1;
}
f = fopen (cfcfile, "wb");
if (!f)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't create the file: '%s'", cfcfile);
return -1;
}
/* Set the header. */
hu.header.version = major;
hu.header.db_flags = minor;
hu.header.buckets_start = OSBF_CFC_HEADER_SIZE;
hu.header.num_buckets = num_buckets;
hu.header.learnings = 0;
/* Write header */
if (fwrite (&hu, sizeof (hu), 1, f) != 1)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't initialize the file header: '%s'", cfcfile);
return -1;
}
/* Initialize CFC hashes - zero all buckets */
for (i_aux = 0; i_aux < num_buckets; i_aux++)
{
/* Write buckets */
if (fwrite (&bucket, sizeof (bucket), 1, f) != 1)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't write to: '%s'", cfcfile);
return -1;
}
}
fclose (f);
return 0;
}
/*****************************************************************/
/* Check if a file exists. Return its length if yes and < 0 if no */
off_t
check_file (const char *file)
{
int fd;
off_t fsize;
fd = open (file, O_RDONLY);
if (fd < 0)
return -1;
fsize = lseek (fd, 0L, SEEK_END);
if (fsize < 0)
return -2;
close (fd);
return fsize;
}
/*****************************************************************/
int
osbf_open_class (const char *classname, int flags, CLASS_STRUCT * class,
char *errmsg)
{
int prot;
off_t fsize;
/* clear class structure */
class->fd = -1;
class->flags = O_RDONLY;
class->classname = NULL;
class->header = NULL;
class->buckets = NULL;
class->bflags = NULL;
fsize = check_file (classname);
if (fsize < 0)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN, "Couldn't open %s.",
classname);
return (-1);
}
/* open the class to be trained and mmap it into memory */
class->fd = open (classname, flags);
if (class->fd < 0)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't open the file %s.", classname);
return -2;
}
if (flags == O_RDWR)
{
class->flags = O_RDWR;
prot = PROT_READ + PROT_WRITE;
#if !defined(OSBF_NO_FILE_LOCKING)
if (osbf_lock_file (class->fd, 0, 0) != 0)
{
fprintf (stderr, "Couldn't lock the file %s.", classname);
close (class->fd);
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't lock the file %s.", classname);
return -3;
}
#endif
}
else
{
class->flags = O_RDONLY;
prot = PROT_READ;
}
class->header = (OSBF_HEADER_STRUCT *) mmap (NULL, fsize, prot,
MAP_SHARED, class->fd, 0);
if (class->header == MAP_FAILED)
{
close (class->fd);
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN, "Couldn't mmap %s.",
classname);
return (-4);
}
/* check file version */
if (class->header->version != OSBF_VERSION || class->header->db_flags != 0)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"%s is not an OSBF_Bayes-spectrum file.", classname);
return (-5);
}
class->bflags = calloc (class->header->num_buckets, sizeof (unsigned char));
if (!class->bflags)
{
close (class->fd);
munmap ((void *) class->header, (class->header->buckets_start +
class->header->num_buckets) *
sizeof (OSBF_BUCKET_STRUCT));
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't allocate memory for seen features array.");
return (-6);
}
class->classname = classname;
class->buckets = (OSBF_BUCKET_STRUCT *) class->header +
class->header->buckets_start;
return 0;
}
/*****************************************************************/
int
osbf_close_class (CLASS_STRUCT * class, char *errmsg)
{
int err = 0;
if (class->header)
{
munmap ((void *) class->header, (class->header->buckets_start +
class->header->num_buckets) *
sizeof (OSBF_BUCKET_STRUCT));
class->header = NULL;
class->buckets = NULL;
}
if (class->bflags)
{
free (class->bflags);
class->bflags = NULL;
}
if (class->fd >= 0)
{
if (class->flags == O_RDWR)
{
/* "touch" the file */
OSBF_HEADER_STRUCT foo;
read (class->fd, &foo, sizeof (foo));
lseek (class->fd, 0, SEEK_SET);
write (class->fd, &foo, sizeof (foo));
#if !defined(OSBF_NO_FILE_LOCKING)
if (osbf_unlock_file (class->fd, 0, 0) != 0)
{
snprintf (errmsg, OSBF_ERROR_MESSAGE_LEN,
"Couldn't unlock file: %s", class->classname);
err = -1;
}
#endif
}
close (class->fd);
class->fd = -1;
}
return err;
}
/*****************************************************************/
int
osbf_lock_file (int fd, uint32_t start, uint32_t len)
{
struct flock fl;
int max_lock_attempts = 20;
int errsv = 0;
fl.l_type = F_WRLCK; /* write lock */
fl.l_whence = SEEK_SET;
fl.l_start = start;
fl.l_len = len;
while (max_lock_attempts > 0)
{
errsv = 0;
if (fcntl (fd, F_SETLK, &fl) < 0)
{
errsv = errno;
if (errsv == EAGAIN || errsv == EACCES)
{
max_lock_attempts--;
sleep (1);
}
else
break;
}
else
break;
}
return errsv;
}
/*****************************************************************/
int
osbf_unlock_file (int fd, uint32_t start, uint32_t len)
{
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = start;
fl.l_len = len;
if (fcntl (fd, F_SETLK, &fl) == -1)
return -1;
else
return 0;
}
/*****************************************************************/
int
osbf_dump (const char *cfcfile, const char *csvfile, char *errmsg)
{
FILE *fp_cfc, *fp_csv;
OSBF_BUCKET_STRUCT buckets[BUCKET_BUFFER_SIZE];
OSBF_HEADER_STRUCT header;
int32_t i, size_in_buckets;
int error = 0;
fp_cfc = fopen (cfcfile, "rb");
if (fp_cfc != NULL)
{
int32_t num_buckets;
if (1 == fread (&header, sizeof (header), 1, fp_cfc))
{
size_in_buckets = header.num_buckets + header.buckets_start;
fp_csv = fopen (csvfile, "w");
if (fp_csv != NULL)
{
fseek (fp_cfc, 0, SEEK_SET);
while (size_in_buckets > 0)
{
num_buckets = fread (buckets, sizeof (OSBF_BUCKET_STRUCT),
BUCKET_BUFFER_SIZE, fp_cfc);
if (num_buckets > 0)
{
for (i = 0; i < num_buckets; i++)
{
fprintf (fp_csv,
"%" PRIu32 ";%" PRIu32 ";%" PRIu32 "\n",
buckets[i].hash, buckets[i].key,
buckets[i].value);
}
}
size_in_buckets -= num_buckets;
}
fclose (fp_cfc);
fclose (fp_csv);
if (size_in_buckets != 0)
{
error = 1;
strncpy (errmsg, "Not a valid cfc file",
OSBF_ERROR_MESSAGE_LEN);
}
}
else
{
error = 1;
strncpy (errmsg, "Can't create csv file",
OSBF_ERROR_MESSAGE_LEN);
}
}
else
{
error = 1;
strncpy (errmsg, "Error reading cfc file", OSBF_ERROR_MESSAGE_LEN);
}
}
else
{
error = 1;
strncpy (errmsg, "Can't open cfc file", OSBF_ERROR_MESSAGE_LEN);
}
return error;
}
/*****************************************************************/
int
osbf_restore (const char *cfcfile, const char *csvfile, char *errmsg)
{
FILE *fp_cfc, *fp_csv;
OSBF_BUCKET_STRUCT buckets[BUCKET_BUFFER_SIZE];
OSBF_HEADER_STRUCT *header = (OSBF_HEADER_STRUCT *) buckets;
int32_t size_in_buckets;
int error = 0;
/*
if (check_file(cfcfile) >= 0)
{
strncpy (errmsg, "The .cfc file already exists!", OSBF_ERROR_MESSAGE_LEN);
return 1;
}
*/
fp_csv = fopen (csvfile, "r");
if (fp_csv != NULL)
{
/* read header */
if (5 ==
fscanf (fp_csv,
"%" SCNu32 ";%" SCNu32 ";%" SCNu32 "\n%" SCNu32 ";%" SCNu32
"\n", &header->version, &header->db_flags,
&header->buckets_start, &header->num_buckets,
&header->learnings))
{
size_in_buckets = header->buckets_start + header->num_buckets;
fp_cfc = fopen (cfcfile, "wb");
fseek (fp_csv, 0, SEEK_SET);