-
Notifications
You must be signed in to change notification settings - Fork 22
/
get_index.c
1453 lines (1384 loc) · 50.5 KB
/
get_index.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
/* Copyright (c) 1994 Burra Gopal, Udi Manber. All Rights Reserved. */
#include "glimpse.h"
#include "defs.h"
#include <errno.h>
#if BG_DEBUG
extern FILE *debug;
#endif /*BG_DEBUG*/
extern char INDEX_DIR[MAX_LINE_LEN];
extern int Only_first;
extern int PRINTAPPXFILEMATCH;
extern int OneFilePerBlock;
extern int StructuredIndex;
extern int WHOLEFILESCOPE;
extern unsigned int *dest_index_set;
extern unsigned char *dest_index_buf;
extern int mask_int[32];
extern int errno;
extern int ByteLevelIndex;
extern int RecordLevelIndex;
extern int rdelim_len;
extern char rdelim[MAX_LINE_LEN];
extern char old_rdelim[MAX_LINE_LEN];
extern int NOBYTELEVEL;
extern int OPTIMIZEBYTELEVEL;
extern int RegionLimit;
extern int PRINTINDEXLINE;
extern struct offsets **src_offset_table;
extern unsigned int *multi_dest_index_set[MAXNUM_PAT];
extern struct offsets **multi_dest_offset_table[MAXNUM_PAT];
extern char *index_argv[MAX_ARGS];
extern int index_argc;
extern CHAR GProgname[MAXNAME];
extern FILE *indexfp, *minifp;
extern int REAL_PARTITION, REAL_INDEX_BUF, MAX_ALL_INDEX, FILEMASK_SIZE;
extern int p_table[MAX_PARTITION];
extern int GNumpartitions;
extern int INVERSE; /* agrep's global: need here to implement ~ in index-search */
extern int last_Y_filenumber;
#define USEFREQUENCIES 0 /* set to one if we want to stop collecting offsets sometimes since words "look" like they are in the stop list... */
free_list(p1)
struct offsets **p1;
{
struct offsets *tp1;
while (*p1 != NULL) {
tp1 = *p1;
*p1 = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
}
}
/* Unions offset lists list2 with list1 sorted in increasing order (deletes elements from list2) => changes both list1 and list2: f += #elems added */
sorted_union(list1, list2, f, pf, cf)
struct offsets **list1, **list2;
int *f, pf, cf;
{
register struct offsets **p1 = list1, *p2;
register int count = *f; /* don't update *f if setting NOBYTELEVEL */
if (!RecordLevelIndex && NOBYTELEVEL) { /* cannot come here! */
free_list(list1);
free_list(list2);
return;
}
#if USEFREQUENCIES
if (!RecordLevelIndex && ( ((pf > MIN_OCCURRENCES) && (count > MAX_UNION * pf)) || (count > MAX_ABSOLUTE) ||
((count > MIN_OCCURRENCES) && (pf > MAX_UNION * count)) || (pf > MAX_ABSOLUTE) )) {
/* enough if we check the second condition at the beginning since it won't surely be satisfied after this when count ++ */
NOBYTELEVEL = 1;
return;
}
#endif
while (*list2 != NULL) {
/* extract 1st element, update list2 */
p2 = *list2;
*list2 = (*list2)->next;
p2->next = NULL;
/* find position to insert p2, and do so */
p1 = list1;
while (((*p1) != NULL) && ((*p1)->offset < p2->offset)) p1 = &(*p1)->next;
if (*p1 == NULL) { /* end of list1: append list2 to it and return */
*p1 = p2;
p2->next = *list2;
*list2 = NULL;
if (cf > 0) count = *f + cf;
#if USEFREQUENCIES
if (!RecordLevelIndex && ( ((pf > MIN_OCCURRENCES) && (count > MAX_UNION * pf)) || (count > MAX_ABSOLUTE))) {
NOBYTELEVEL = 1;
return;
}
#endif
*f = count;
return;
}
else if (p2->offset == (*p1)->offset) my_free(p2, sizeof(struct offsets));
else {
p2->next = *p1;
*p1 = p2;
count ++;
#if USEFREQUENCIES
if (!RecordLevelIndex && ( ((pf > MIN_OCCURRENCES) && (count > MAX_UNION * pf)) || (count > MAX_ABSOLUTE) )) {
NOBYTELEVEL = 1;
return;
}
#endif
/* update list1 */
list1 = &(*p1)->next;
}
}
*f = count;
}
/* Intersects offset lists list2 with list1 sorted in increasing order (deletes elements from list2) => changes both list1 and list2 */
sorted_intersection(filenum, list1, list2, f)
struct offsets **list1, **list2;
int *f;
{
register struct offsets **p1 = list1, *p2, *tp1;
register int diff;
struct offsets *tp;
if (!RecordLevelIndex && NOBYTELEVEL) { /* cannot come here! */
free_list(list1);
free_list(list2);
return;
}
/*
NOT NECESSARY SINCE done INITIALIZED TO 0 ON CREATION AND MADE 0 BELOW
tp = *list1;
while (tp != NULL) {
tp->done = 0;
tp = tp->next;
}
*/
#if 0
printf("sorted_intersection BEGIN: list1=\n\t");
tp = *list1;
while (tp != NULL) {
printf("%d ", tp->offset);
tp = tp->next;
}
printf("\n");
printf("list2=\n\t");
tp = *list2;
while (tp != NULL) {
printf("%d ", tp->offset);
tp = tp->next;
}
printf("\n");
#endif
/* find position to intersect list2, and do so: REMEBER: list1 is in increasing order, and so is list2 !!! */
p1 = list1;
while ( ((*p1) != NULL) && (*list2 != NULL) ) {
diff = (*list2)->offset - (*p1)->offset;
if ( (diff == 0) || (!RecordLevelIndex && (diff >= -RegionLimit) && (diff <= RegionLimit)) ) {
(*p1)->done = 1; /* p1 is in */
p1 = &(*p1)->next;
/* Can't increment p2 here since it might keep others after p1 also in */
}
else {
if (diff < 0) {
p2 = *list2;
*list2 = (*list2)->next;
my_free(p2, sizeof(struct offsets));
/* p1 can intersect with list2's next */
}
else {
if((*p1)->done && 0) p1 = &(*p1)->next; /* imposs */ /* THIS CHECK ALWAYS YEILDS 0 FROM 25/08/1996: bgopal@cs.arizona.edu */
else {
tp1 = *p1;
*p1 = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
(*f) --;
}
/* list2 can intersect with p1's next */
}
}
}
while (*list2 != NULL) {
p2 = *list2;
*list2 = (*list2)->next;
my_free(p2, sizeof(struct offsets));
}
p1 = list1;
while (*p1 != NULL) {
if ((*p1)->done == 0) {
tp1 = *p1;
*p1 = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
(*f) --;
}
else {
(*p1)->done = 0; /* for the next round! */
p1 = &(*p1)->next;
}
}
#if 0
printf("sorted_intersection END: list1=\n\t");
tp = *list1;
while (tp != NULL) {
printf("%d ", tp->offset);
tp = tp->next;
}
printf("\n");
printf("list2=\n\t");
tp = *list2;
while (tp != NULL) {
printf("%d ", tp->offset);
tp = tp->next;
}
printf("\n");
#endif
}
purge_offsets(p1)
struct offsets **p1;
{
struct offsets *tp1;
while (*p1 != NULL) {
if ((*p1)->sign == 0) {
tp1 = *p1;
(*p1) = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
}
else p1 = &(*p1)->next;
}
}
/* Returns 1 if it is a Universal set, 0 otherwise. Constraint: WORD_END_MARK/ALL_INDEX_MARK must occur at or after buffer[0] */
get_set(buffer, set, offset_table, patlen, pattern, patattr, outfile, partfp, frequency, prevfreq)
unsigned char *buffer;
unsigned int *set;
struct offsets **offset_table;
int patlen;
char *pattern;
int patattr;
FILE *outfile;
FILE *partfp;
int *frequency, prevfreq;
{
int bdx2, j;
int ret;
int x=0, y=0, diff, even_words=1, prevy;
int indexattr = 0;
struct offsets *o, *tailo, *heado;
int delim = encode8b(0);
int curfreq = 0;
unsigned char c;
/* buffer[0] is '\n', search must start from buffer[1] */
bdx2 = 1;
if (OneFilePerBlock)
while((bdx2<REAL_INDEX_BUF+1) && (buffer[bdx2] != WORD_END_MARK) && (buffer[bdx2] != ALL_INDEX_MARK)) bdx2++;
else while((bdx2<REAL_INDEX_BUF+1) && (buffer[bdx2] != WORD_END_MARK)) bdx2++;
if (bdx2 >= REAL_INDEX_BUF+1) return 0;
if (StructuredIndex) {
if (StructuredIndex < MaxNum8bPartition - 1) {
indexattr = decode8b(buffer[bdx2+1]);
}
else {
indexattr = decode16b((buffer[bdx2+1] << 8) | (buffer[bdx2 + 2]));
}
/* printf("i=%d p=%d\n", indexattr, patattr); */
if ((patattr > 0) && (indexattr != patattr)) {
#if BG_DEBUG
fprintf(debug, "indexattr=%d DOES NOT MATCH patattr=%d\n", indexattr, patattr);
#endif /*BG_DEBUG*/
return 0;
}
}
if (PRINTINDEXLINE) {
c = buffer[bdx2];
buffer[bdx2] = '\0';
printf("%s %d", &buffer[1], indexattr);
buffer[bdx2] = c;
if (c == ALL_INDEX_MARK) printf(" ! ");
else printf(" : ");
}
if (OneFilePerBlock && (buffer[bdx2] == ALL_INDEX_MARK)) {
/* A intersection Univ-set = A: so src_index_set won't change; A union Univ-set = Univ-set: so src_index_set = all 1s */
#if BG_DEBUG
buffer[bdx2] = '\0';
fprintf(debug, "All indices search for %s\n", buffer + 1);
buffer[bdx2] = ALL_INDEX_MARK;
#endif /*BG_DEBUG*/
set[REAL_PARTITION - 1] = 1;
for(bdx2=0; bdx2<round(OneFilePerBlock, 8*sizeof(int)) - 1; bdx2++) {
set[bdx2] = 0xffffffff;
}
set[bdx2] = 0;
for (j=0; j<8*sizeof(int); j++) {
if (bdx2*8*sizeof(int) + j >= OneFilePerBlock) break;
set[bdx2] |= mask_int[j];
}
set[REAL_PARTITION - 1] = 1;
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1; /* With RecordLevelIndex, I want NOBYTELEVEL to be unused (i.e., !NOBYTELEVEL is always true) */
return 1;
}
else if (!OneFilePerBlock) { /* check only if index+partitions are NOT split */
#if BG_DEBUG
buffer[bdx2] = '\0';
fprintf(debug, "memagrep-line: %s\t\tpattern: %s\n", buffer, pattern);
#endif /*BG_DEBUG*/
/* ignore if pattern with all its options matches block number sequence: bg+udi: Feb/16/93 */
buffer[bdx2] = '\n'; /* memagrep needs buffer to end with '\n' */
if ((ret = memagrep_search(patlen, pattern, bdx2+1, buffer, 0, outfile)) <= 0) return 0;
else buffer[bdx2] = WORD_END_MARK;
}
if ((StructuredIndex > 0) && (StructuredIndex < MaxNum8bPartition - 1)) bdx2 ++;
else if (StructuredIndex > 0) bdx2 += 2;
bdx2++; /* bdx2 now points to the first byte of the offset */
even_words = 1;
/* Code identical to that in merge_in() in glimpseindex */
if (OneFilePerBlock) {
get_block_numbers(&buffer[bdx2], &buffer[bdx2], partfp);
while((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0')) {
/* First get the file name */
x = 0;
if (ByteLevelIndex) {
if (OneFilePerBlock <= MaxNum8bPartition) {
x = decode8b(buffer[bdx2]);
bdx2 ++;
}
else if (OneFilePerBlock <= MaxNum16bPartition) {
x = (buffer[bdx2] << 8) | buffer[bdx2+1];
x = decode16b(x);
bdx2 += 2;
}
else {
x = (buffer[bdx2] << 16) | (buffer[bdx2+1] << 8) | buffer[bdx2+2];
x = decode24b(x);
bdx2 += 3;
}
}
else if (OneFilePerBlock <= MaxNum8bPartition) {
x = decode8b(buffer[bdx2]);
bdx2 ++;
}
else if (OneFilePerBlock <= MaxNum12bPartition) {
if (even_words) {
x = ((buffer[bdx2+1] & 0x0000000f) << 8) | buffer[bdx2];
x = decode12b(x);
bdx2 += 2;
even_words = 0;
}
else { /* odd number of words so far */
x = ((buffer[bdx2-1] & 0x000000f0) << 4) | buffer[bdx2];
x = decode12b(x);
bdx2 ++;
even_words = 1;
}
}
else if (OneFilePerBlock <= MaxNum16bPartition) {
x = (buffer[bdx2] << 8) | buffer[bdx2+1];
x = decode16b(x);
bdx2 += 2;
}
else {
x = (buffer[bdx2] << 16) | (buffer[bdx2+1] << 8) | buffer[bdx2+2];
x = decode24b(x);
bdx2 += 3;
}
if ((last_Y_filenumber > 0) && (x >= last_Y_filenumber)) continue;
set[block2index(x)] |= block2mask(x);
if (PRINTINDEXLINE) {
printf("%d [", x);
}
prevy = 0;
if (ByteLevelIndex) {
heado = tailo = NULL;
curfreq = 0;
while ((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0')) {
y = decode8b(buffer[bdx2]);
if ((y & 0x000000c0) == 0) { /* one byte offset */
diff = y&0x0000003f;
y = prevy + diff;
bdx2 ++;
}
else if ((y & 0x000000c0) == 0x40) { /* two byte offset */
diff = decode8b(buffer[bdx2+1]);
y = prevy + (((y & 0x0000003f) * MaxNum8bPartition) + diff);
bdx2 += 2;
}
else if ((y & 0x000000c0) == 0x80) { /* three byte offset */
diff = decode16b((buffer[bdx2+1] << 8) | buffer[bdx2+2]);
y = prevy + (((y & 0x0000003f) * MaxNum16bPartition) + diff);
bdx2 += 3;
}
else { /* four byte offset */
diff = decode24b((buffer[bdx2+1] << 16) | (buffer[bdx2+2] << 8) | buffer[bdx2+3]);
y = prevy + (((y & 0x0000003f) * MaxNum24bPartition) + diff);
bdx2 += 4;
}
prevy = y;
if (PRINTINDEXLINE) {
printf(" %d", y);
}
curfreq ++;
if(RecordLevelIndex ||
(!(Only_first && !PRINTAPPXFILEMATCH) && !NOBYTELEVEL && /* below borrowed from sorted_union */
#if USEFREQUENCIES
!(((prevfreq>MIN_OCCURRENCES)&&(curfreq+*frequency > MAX_UNION*prevfreq)) || (curfreq+*frequency > MAX_ABSOLUTE))
#else
1
#endif
)
) {
/* These o's will be in sorted order. Just collect all of them and merge with &offset_table[x]. */
o = (struct offsets *)my_malloc(sizeof(struct offsets));
o->offset = y;
o->next = NULL;
o->sign = o->done = 0;
if (heado == NULL) {
heado = o;
tailo = o;
}
else {
tailo->next = o;
tailo = o;
}
}
else if (!RecordLevelIndex) {
if (heado != NULL) free_list(&heado);
/* printf("1 "); */
NOBYTELEVEL = 1; /* can't return since have to or the bitmasks */
}
if ((bdx2<REAL_INDEX_BUF) && (buffer[bdx2] == delim)) { /* look at offsets corr. to a new file now */
bdx2 ++;
break;
}
}
if (heado == NULL) *frequency += curfreq;
else if (RecordLevelIndex || (!(Only_first && !PRINTAPPXFILEMATCH) && !NOBYTELEVEL)) {
sorted_union(&offset_table[x], &heado, frequency, prevfreq, curfreq); /* this will free heado's elements and ++ *frequency */
if (!RecordLevelIndex && NOBYTELEVEL) *frequency += curfreq; /* can't return since have to or the bitmasks */
if (heado != NULL) free_list(&heado);
}
}
if (PRINTINDEXLINE) {
printf("] ");
}
}
}
else {
while((bdx2<MAX_INDEX_BUF) && (buffer[bdx2] != '\n') && (buffer[bdx2] != '\0') && (buffer[bdx2] < MAX_PARTITION)) {
if ((last_Y_filenumber > 0) && (p_table[buffer[bdx2]] >= last_Y_filenumber)) {
bdx2 ++;
continue;
}
if (PRINTINDEXLINE) {
for (j=p_table[buffer[bdx2]]; j<p_table[buffer[bdx2] + 1]; j++)
if ((last_Y_filenumber > 0) && (j >= last_Y_filenumber)) break;
else printf("%d [] ", j);
}
set[buffer[bdx2]] = 1;
bdx2++;
}
}
if (PRINTINDEXLINE) {
printf("\n");
}
return 0;
}
/*
* This is a very simple function: it gets the list of matched lines from the index,
* and sets the block numbers corr. to files that need to be searched in "index_tab".
* It also sets the file-offsets that have to be searched in "offset_tab" (byte-level).
*/
get_index(infile, index_tab, offset_tab, pattern, patlen, patattr, index_argv, index_argc, outfile, partfp, parse, first_time)
char *infile;
unsigned int *index_tab;
struct offsets **offset_tab;
char *pattern;
int patlen;
int patattr;
char *index_argv[];
int index_argc;
FILE *outfile;
FILE *partfp;
int parse;
int first_time;
{
int i=0, j, iii;
FILE *f_in;
struct offsets **offsetptr = multi_dest_offset_table[0]; /* cannot be NULL if ByteLevelIndex: main.c takes care of that */
int ret=0;
if (OneFilePerBlock && (parse & OR_EXP) && (index_tab[REAL_PARTITION - 1] == 1)) return 0;
if (((infile == NULL) || !strcmp(infile, "")) /* || (index_tab == NULL) || (offset_tab == NULL) || (pattern == NULL)*/) return -1;
if((f_in = fopen(infile, "r")) == NULL) {
fprintf(stderr, "%s: can't open for reading: %s/%s\n", GProgname, INDEX_DIR, infile);
return -1;
}
if (OneFilePerBlock)
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) {
dest_index_set[i] = 0;
}
else for(i=0; i<MAX_PARTITION; i++) {
dest_index_set[i] = 0;
}
dest_index_buf[0] = '\n'; /* memagrep needs buffer to begin with '\n' */
dest_index_set[REAL_PARTITION - 2] = 0;
while(fgets(dest_index_buf+1, REAL_INDEX_BUF-1, f_in)) {
#if BG_DEBUG
fprintf(debug, "index-line: %s", dest_index_buf+1);
#endif /*BG_DEBUG*/
if ((ret = get_set(&dest_index_buf[0], dest_index_set, offsetptr, patlen, pattern, patattr, outfile, partfp, &dest_index_set[REAL_PARTITION - 2], index_tab[REAL_PARTITION - 2])) != 0)
break; /* all index mark touched */
}
if (!RecordLevelIndex && NOBYTELEVEL) {
for (iii=0; iii<OneFilePerBlock; iii++) {
free_list(&offset_tab[iii]);
free_list(&offsetptr[iii]);
}
}
if (INVERSE) {
if (OneFilePerBlock) {
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1; /* can't collect all offsets where pattern DOES NOT occur! */
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)) - 1; i++) dest_index_set[i] = ~dest_index_set[i];
for (j=0; j<8*sizeof(int); j++) {
if (i*8*sizeof(int) + j >= OneFilePerBlock) break;
if (dest_index_set[i] & mask_int[j]) dest_index_set[i] &= ~mask_int[j];
else dest_index_set[i] |= mask_int[j];
}
}
else {
for(i=0; i<MAX_PARTITION; i++) {
if (i>=GNumpartitions-1) break; /* STUPID: get_table returns 1 + part_num, where part_num was no. of partitions glimpseindex found */
if ((i == 0) || (i == '\n')) continue;
if (dest_index_set[i]) dest_index_set[i] = 0;
else dest_index_set[i] = 1;
}
}
}
/* Take intersection if parse=ANDPAT or 0 (one terminal pattern), union if OR_EXP; Take care of universal sets in index_tab[REAL_PARTITION - 1] */
if (OneFilePerBlock) {
if (parse & OR_EXP) {
if (ret) {
ret_is_1:
index_tab[REAL_PARTITION - 1] = 1;
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)) - 1; i++) {
index_tab[i] = 0xffffffff;
}
index_tab[i] = 0;
for (j=0; j<8*sizeof(int); j++) {
if (i*8*sizeof(int) + j >= OneFilePerBlock) break;
index_tab[i] |= mask_int[j];
}
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) for (i=0; i<OneFilePerBlock; i++) { /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
free_list(&offsetptr[i]);
free_list(&offset_tab[i]);
}
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1;
fclose(f_in);
return 0;
}
index_tab[REAL_PARTITION - 1] = 0;
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] |= dest_index_set[i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
for (i=0; i<OneFilePerBlock; i++) {
sorted_union(&offset_tab[i], &offsetptr[i], &index_tab[REAL_PARTITION - 2], dest_index_set[REAL_PARTITION - 2], 0);
if (!RecordLevelIndex && NOBYTELEVEL) { /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (iii=0; iii<OneFilePerBlock; iii++) {
free_list(&offset_tab[iii]);
free_list(&offsetptr[iii]);
}
break;
}
}
}
}
else {
if (((index_tab[REAL_PARTITION - 1] == 1) || first_time) && (ret)) {
both_are_1:
if (first_time) {
index_tab[REAL_PARTITION - 1] = 1;
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)) - 1; i++) {
index_tab[i] = 0xffffffff;
}
index_tab[i] = 0;
for (j=0; j<8*sizeof(int); j++) {
if (i*8*sizeof(int) + j >= OneFilePerBlock) break;
index_tab[i] |= mask_int[j];
}
}
first_time = 0;
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) for (i=0; i<OneFilePerBlock; i++) { /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
free_list(&offsetptr[i]);
free_list(&offset_tab[i]);
}
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1;
/*
fclose(f_in);
return 0;
*/
}
else if ((index_tab[REAL_PARTITION - 1] == 1) || first_time) {
first_time = 0;
index_tab[REAL_PARTITION - 1] = 0;
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] = dest_index_set[i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
for (i=0; i<OneFilePerBlock; i++) {
free_list(&offset_tab[i]);
offset_tab[i] = offsetptr[i];
offsetptr[i] = NULL;
}
}
}
else if (ret) {
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) for (i=0; i<OneFilePerBlock; i++) free_list(&offsetptr[i]); /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
}
else {
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] &= dest_index_set[i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
if (first_time || WHOLEFILESCOPE) {
first_time = 0;
for (i=0; i<OneFilePerBlock; i++) {
sorted_union(&offset_tab[i], &offsetptr[i], &index_tab[REAL_PARTITION - 2], dest_index_set[REAL_PARTITION - 2], 0);
if (!RecordLevelIndex && NOBYTELEVEL) { /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (iii=0; iii<OneFilePerBlock; iii++) {
free_list(&offset_tab[iii]);
free_list(&offsetptr[iii]);
}
break;
}
}
}
else {
for (i=0; i<OneFilePerBlock; i++) {
if ((index_tab[block2index(i)] & mask_int[i % (8*sizeof(int))]))
sorted_intersection(i, &offset_tab[i], &offsetptr[i], &index_tab[REAL_PARTITION - 2]);
else free_list(&offsetptr[i]);
/*
if (index_tab[REAL_PARTITION - 2] < MIN_OCCURRENCES) {
if (!NOBYTELEVEL) {
for (iii=0; iii<OneFilePerBlock; iii++) {
free_list(&offset_tab[iii]);
free_list(&offsetptr[iii]);
}
}
NOBYTELEVEL = 1;
OPTIMIZEBYTELEVEL = 1;
break;
}
*/
}
}
}
}
}
}
else {
if (parse & OR_EXP)
for(i=0; i<MAX_PARTITION; i++) index_tab[i] |= dest_index_set[i];
else
for(i=0; i<MAX_PARTITION; i++) index_tab[i] &= dest_index_set[i];
}
#if BG_DEBUG
fprintf(debug, "get_index(): the following partitions are ON\n");
for(i=0; i<((OneFilePerBlock > 0) ? round(OneFilePerBlock, 8*sizeof(int)) : MAX_PARTITION); i++) {
if(index_tab[i]) fprintf(debug, "%d,%x\n", i, index_tab[i]);
}
#endif /*BG_DEBUG*/
fclose(f_in);
return 0;
}
/*
* Same as above, but uses mgrep to search the index for many patterns at one go,
* and interprets the output obtained from the -M and -P options (set in main.c).
*/
mgrep_get_index(infile, index_tab, offset_tab, pat_list, pat_lens, pat_attr, mgrep_pat_index, num_mgrep_pat, patbufpos, index_argv, index_argc, outfile, partfp, parse, first_time)
char *infile;
unsigned int *index_tab;
struct offsets **offset_tab;
char *pat_list[];
int pat_lens[];
int pat_attr[];
int mgrep_pat_index[];
int num_mgrep_pat;
int patbufpos;
char *index_argv[];
int index_argc;
FILE *outfile;
FILE *partfp;
int parse;
int first_time;
{
int i=0, j, temp, iii, jjj;
FILE *f_in;
int ret;
int x=0, y=0, even_words=1;
int patnum;
unsigned int *setptr;
struct offsets **offsetptr;
CHAR dummypat[MAX_PAT];
int dummylen=0;
char allindexmark[MAXNUM_PAT];
int k;
int sorted[MAXNUM_PAT], min, max;
if (OneFilePerBlock && (parse & OR_EXP) && (index_tab[REAL_PARTITION - 1] == 1)) return 0;
/* Do the mgrep() */
if ((f_in = fopen(infile, "w")) == NULL) {
fprintf(stderr, "%s: run out of file descriptors!\n", GProgname);
return -1;
}
errno = 0;
if ((ret = fileagrep(index_argc, index_argv, 0, f_in)) < 0) {
fprintf(stderr, "%s: error in searching index\n", HARVEST_PREFIX);
fclose(f_in);
return -1;
}
fflush(f_in);
fclose(f_in);
f_in = NULL;
index_argv[patbufpos] = NULL;
/* For index-search with memgrep and get-filenames */
dummypat[0] = '\0';
if ((dummylen = memagrep_init(index_argc, index_argv, MAX_PAT, dummypat)) <= 0) {
fclose(f_in);
return -1;
}
/* Interpret the result */
if((f_in = fopen(infile, "r")) == NULL) {
fprintf(stderr, "%s: can't open for reading: %s/%s\n", GProgname, INDEX_DIR, infile);
return -1;
}
if (OneFilePerBlock) {
for (patnum=0; patnum<num_mgrep_pat; patnum ++) {
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) {
multi_dest_index_set[patnum][i] = 0;
}
if (ByteLevelIndex) for(i=0; i<OneFilePerBlock; i++) {
free_list(&multi_dest_offset_table[patnum][i]);
/* multi_dest_offset_table[patnum][i] = NULL; bg, 28/9/1995 */
}
multi_dest_index_set[patnum][REAL_PARTITION - 1] = 0;
multi_dest_index_set[patnum][REAL_PARTITION - 2] = 0;
}
}
else {
for (patnum=0; patnum<num_mgrep_pat; patnum ++)
for(i=0; i<MAX_PARTITION; i++) {
multi_dest_index_set[patnum][i] = 0;
}
}
dest_index_buf[0] = '\n'; /* memagrep needs buffer to begin with '\n' */
memset(allindexmark, '\0', num_mgrep_pat);
min = (index_tab[REAL_PARTITION - 1] == 1) ? 0 : index_tab[REAL_PARTITION - 2];
while(fgets(dest_index_buf+1, REAL_INDEX_BUF, f_in)) {
patnum=0;
sscanf(&dest_index_buf[1], "%d-", &patnum);
#if BG_DEBUG
fprintf(debug, "patnum=%d len=%d pat=%s attr=%d index-line: %s\n", patnum, pat_lens[mgrep_pat_index[patnum-1]], pat_list[mgrep_pat_index[patnum-1]], pat_attr[mgrep_pat_index[patnum-1]], dest_index_buf+1);
#endif /*BG_DEBUG*/
if ((patnum < 1) || (patnum > num_mgrep_pat)) continue; /* error! */
setptr = multi_dest_index_set[patnum - 1];
offsetptr = multi_dest_offset_table[patnum - 1];
for(k=0; dest_index_buf[k] != ' '; k++);
dest_index_buf[k] = '\n';
if (!allindexmark[patnum - 1])
allindexmark[patnum - 1] = (char)get_set(&dest_index_buf[k], setptr, offsetptr, pat_lens[mgrep_pat_index[patnum-1]],
pat_list[mgrep_pat_index[patnum-1]], pat_attr[mgrep_pat_index[patnum-1]], outfile, partfp,
&setptr[REAL_PARTITION - 2], min);
/* To test the maximum disparity to stop unions within above */
if (!allindexmark[patnum-1]) min = setptr[REAL_PARTITION - 2];
for (patnum=0; patnum<num_mgrep_pat; patnum ++) {
if ((multi_dest_index_set[patnum][REAL_PARTITION - 2] < min) && (multi_dest_index_set[patnum][REAL_PARTITION - 1] != 1))
min = multi_dest_index_set[patnum][REAL_PARTITION - 2];
}
min += (index_tab[REAL_PARTITION - 1] == 1) ? 0 : index_tab[REAL_PARTITION - 2];
}
#if 0
for (patnum=0; patnum<num_mgrep_pat; patnum++)
printf("%d=%d,%d\n", patnum, multi_dest_index_set[patnum][REAL_PARTITION - 1], multi_dest_index_set[patnum][REAL_PARTITION - 2]);
#endif /*0*/
for (patnum=0; patnum<num_mgrep_pat; patnum++)
sorted[patnum] = patnum;
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
max = 0;
for (patnum=1; patnum<num_mgrep_pat; patnum++) {
if (multi_dest_index_set[patnum][REAL_PARTITION - 2] > multi_dest_index_set[max][REAL_PARTITION - 2])
max = patnum;
}
/* Sort them according to the lengths of the lists in increasing order: min first */
for (patnum=0; patnum<num_mgrep_pat; patnum++) {
min = patnum;
for (j=patnum+1; j<num_mgrep_pat; j++)
if (multi_dest_index_set[sorted[j]][REAL_PARTITION - 2] < multi_dest_index_set[sorted[min]][REAL_PARTITION - 2])
min = j;
if (min != patnum) {
temp = sorted[patnum];
sorted[patnum] = sorted[min];
sorted[min] = temp;
}
}
#if USEFREQUENCIES
if (!RecordLevelIndex && (multi_dest_index_set[sorted[max]][REAL_PARTITION - 2] > MAX_DISPARITY * multi_dest_index_set[sorted[0]][REAL_PARTITION - 2])) {
NOBYTELEVEL = 1;
/* printf("4 "); */
for (iii=0; iii<OneFilePerBlock; iii++) {
for (jjj=0; jjj<num_mgrep_pat; jjj++)
free_list(&multi_dest_offset_table[jjj][iii]);
free_list(&offset_tab[iii]);
}
}
#endif
}
else if (!RecordLevelIndex && NOBYTELEVEL) {
for (iii=0; iii<OneFilePerBlock; iii++) {
for (jjj=0; jjj<num_mgrep_pat; jjj++)
free_list(&multi_dest_offset_table[jjj][iii]);
free_list(&offset_tab[iii]);
}
}
/* Take intersection if parse=ANDPAT or 0 (one terminal pattern), union if OR_EXP; Take care of universal sets in offset_tab[REAL_PARTITION - 1] */
for (patnum=0; patnum<num_mgrep_pat; patnum++) {
if (OneFilePerBlock) {
if (parse & OR_EXP) {
if (allindexmark[sorted[patnum]]) {
ret_is_1:
index_tab[REAL_PARTITION - 1] = 1;
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)) - 1; i++) {
index_tab[i] = 0xffffffff;
}
index_tab[i] = 0;
for (j=0; j<8*sizeof(int); j++) {
if (i*8*sizeof(int) + j >= OneFilePerBlock) break;
index_tab[i] |= mask_int[j];
}
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (i=0; i<OneFilePerBlock; i++) {
for (patnum=0;patnum<num_mgrep_pat;patnum++)
free_list(&multi_dest_offset_table[sorted[patnum]][i]);
free_list(&offset_tab[i]);
}
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1;
fclose(f_in);
return 0;
}
index_tab[REAL_PARTITION - 1] = 0;
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] |= multi_dest_index_set[sorted[patnum]][i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
for (i=0; i<OneFilePerBlock; i++) {
sorted_union(&offset_tab[i], &multi_dest_offset_table[sorted[patnum]][i], &index_tab[REAL_PARTITION - 2], multi_dest_index_set[sorted[patnum]][REAL_PARTITION - 2], 0);
if (!RecordLevelIndex && NOBYTELEVEL) { /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (iii=0; iii<OneFilePerBlock; iii++) {
for (jjj=0; jjj<num_mgrep_pat; jjj++)
free_list(&multi_dest_offset_table[jjj][iii]);
free_list(&offset_tab[iii]);
}
break;
}
}
}
}
else {
if (((index_tab[REAL_PARTITION - 1] == 1) || first_time) && (allindexmark[sorted[patnum]])) {
both_are_1:
if (first_time) {
index_tab[REAL_PARTITION - 1] = 1;
for(i=0; i<round(OneFilePerBlock, 8*sizeof(int)) - 1; i++) {
index_tab[i] = 0xffffffff;
}
index_tab[i] = 0;
for (j=0; j<8*sizeof(int); j++) {
if (i*8*sizeof(int) + j >= OneFilePerBlock) break;
index_tab[i] |= mask_int[j];
}
}
first_time = 0;
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (i=0; i<OneFilePerBlock; i++) {
for (patnum=0;patnum<num_mgrep_pat;patnum++)
free_list(&multi_dest_offset_table[sorted[patnum]][i]);
free_list(&offset_tab[i]);
}
if (ByteLevelIndex && !RecordLevelIndex) NOBYTELEVEL = 1;
/*
fclose(f_in);
return 0;
*/
}
else if ((index_tab[REAL_PARTITION - 1] == 1) || first_time) {
first_time = 0;
index_tab[REAL_PARTITION - 1] = 0;
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] = multi_dest_index_set[sorted[patnum]][i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
for (i=0; i<OneFilePerBlock; i++) {
free_list(&offset_tab[i]);
offset_tab[i] = multi_dest_offset_table[sorted[patnum]][i];
multi_dest_offset_table[sorted[patnum]][i] = NULL;
}
}
}
else if (allindexmark[sorted[patnum]]) {
if (ByteLevelIndex && !RecordLevelIndex && !NOBYTELEVEL && !(Only_first && !PRINTAPPXFILEMATCH)) /* collect as many offsets as possible with RecordLevelIndex: free offset_tables at the end of process_query() */
for (i=0; i<OneFilePerBlock; i++) free_list(&multi_dest_offset_table[sorted[patnum]][i]);
}
else {
for (i=0; i<round(OneFilePerBlock, 8*sizeof(int)); i++) index_tab[i] &= multi_dest_index_set[sorted[patnum]][i];
if (ByteLevelIndex && !NOBYTELEVEL && (RecordLevelIndex || !(Only_first && !PRINTAPPXFILEMATCH))) {
if (first_time || WHOLEFILESCOPE) {
first_time = 0;
for (i=0; i<OneFilePerBlock; i++) {
sorted_union(&offset_tab[i], &multi_dest_offset_table[sorted[patnum]][i], &index_tab[REAL_PARTITION - 2], multi_dest_index_set[sorted[patnum]][REAL_PARTITION - 2], 0);
if (!RecordLevelIndex && NOBYTELEVEL) {
for (iii=0; iii<OneFilePerBlock; iii++) {
for (jjj=0; jjj<num_mgrep_pat; jjj++)
free_list(&multi_dest_offset_table[jjj][iii]);
free_list(&offset_tab[iii]);
}
break;
}
}
}
else {
for (i=0; i<OneFilePerBlock; i++) {
if ((index_tab[block2index(i)] & mask_int[i % (8*sizeof(int))]))
sorted_intersection(i, &offset_tab[i], &multi_dest_offset_table[sorted[patnum]][i], &index_tab[REAL_PARTITION - 2]);
else free_list(&multi_dest_offset_table[sorted[patnum]][i]);
/*
if (index_tab[REAL_PARTITION - 2] < MIN_OCCURRENCES) {
if (!NOBYTELEVEL) {
for (iii=0; iii<OneFilePerBlock; iii++) {
for (jjj=0; jjj<num_mgrep_pat; jjj++)
free_list(&multi_dest_offset_table[jjj][iii]);
free_list(&offset_tab[iii]);
}
}
NOBYTELEVEL = 1;
OPTIMIZEBYTELEVEL = 1;
break;
}
*/
}
}
}
}
}
}
else {
if (parse & OR_EXP) {
for (patnum=0; patnum<num_mgrep_pat; patnum++)
for(i=0; i<MAX_PARTITION; i++) index_tab[i] |= multi_dest_index_set[patnum][i];
}
else {
for (patnum=0; patnum<num_mgrep_pat; patnum++)
for(i=0; i<MAX_PARTITION; i++) index_tab[i] &= multi_dest_index_set[patnum][i];
}
}
}
#if BG_DEBUG
fprintf(debug, "get_index(): the following partitions are ON\n");
for(i=0; i<((OneFilePerBlock > 0) ? round(OneFilePerBlock, 8*sizeof(int)) : MAX_PARTITION); i++) {
if(index_tab[i]) fprintf(debug, "%d,%x\n", i, index_tab[i]);
}
#endif /*BG_DEBUG*/
fclose(f_in);
return 0;
}
/* All borrowed from main.c and are needed for searching the index */
extern CHAR *pat_list[MAXNUM_PAT]; /* complete words within global pattern */
extern int pat_lens[MAXNUM_PAT]; /* their lengths */
extern int pat_attr[MAXNUM_PAT]; /* set of attributes */