-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
2205 lines (1957 loc) · 57.8 KB
/
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
/*
* Index file format, index file generation and index file usage
* are complex operations indeed. You have been warned.
*
* This stuff really would need some documentation
*/
/*
* Here are some design principles:
* - There will be one IndexBuffer for every index term. That's why
* IndexBufferStruct must be kept small
* - There will migh be millions of postings in one IndexBuffer.
* That's why the memory needed by one posting must be kept small
* (compression is used)
* - The size of the whole index will be greater than available RAM-memory.
* So it cannot be constructed in main memory. It will be divided
* to memory loads.
*/
/*
* Here are some assumptions:
* - The underlying OS supports memory mapped files
* - There is enough main memory for one IndexBufferStruct and for
* index spool (INDEX_SPOOL_SIZE) for each term
*/
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#define SGREP_LIBRARY
#include "sgrep.h"
/*
* How many regions we need to handle, before we add a dot
*/
#define DOT_REGIONS (1<<17) /* 65536*2 */
#define INDEX_VERSION_MAGIC ("sgrep-index v0")
/* If we would need larger index than MAX_INDEX_SIZE we would have
* to deal with 64 bit wide integers.
*/
#define MAX_INDEX_SIZE (INT_MAX)
#define EXTERNAL_INDEX_BLOCK_SIZE 32
#define max_term_len 256
/* We will run out of filedescriptors, before running out of memory
* loads. Anyway, this enough for creating MAX_INDEX_SIZE sized index
* with 32M memory load.
*/
#define MAX_MEMORY_LOADS 256
#define INDEX_BUFFER_ARRAY_SIZE 1024
const static IndexOptions default_index_options= {
NULL,IM_NONE,0,0,NULL,NULL,DEFAULT_HASH_TABLE_SIZE,
DEFAULT_INDEXER_MEMORY,NULL
};
struct IndexBlock {
int next;
unsigned char buf[EXTERNAL_INDEX_BLOCK_SIZE];
};
struct ExternalIndexBuffer {
int first;
int current;
int bytes;
};
/* #define INTERNAL_BLOCK_SIZE (sizeof(struct ExternalIndexBuffer)) */
#define INTERNAL_INDEX_BLOCK_SIZE 12
struct IndexBufferStruct {
char *str;
struct IndexBufferStruct *next;
union {
/* This struct is used when building index and all entries of a
* term fit inside IndexBuffer */
struct {
unsigned char ibuf[INTERNAL_INDEX_BLOCK_SIZE];
} internal;
/* This struct is used when building index and entries of a term
* do not fit inside IndexBuffer */
struct ExternalIndexBuffer external;
/* This is used, when reading index from a file */
struct {
const unsigned char *buf;
int ind;
} map;
} list;
/* Last index added to this buffer. This will be zero when the buffer
* is created, INT_MAX when the buffer has been scanner and -1
* if this buffer corresponds to stop word (and therefore is not used )
*/
int last_index;
int saved_bytes; /* How many bytes of this entry have been saved to a
* memory load file */
/* block_used will >= 0 for internal buffer, <0 for external buffers
* and SHORT_MIN when reading */
short block_used;
short last_len;
unsigned char lcp;
};
typedef struct IndexBufferStruct IndexBuffer;
struct IndexReaderStruct {
SgrepData *sgrep;
const char *filename;
void *map;
size_t size;
int len;
const unsigned char *array;
const void *entries;
};
struct IndexBufferArray {
IndexBuffer bufs[INDEX_BUFFER_ARRAY_SIZE];
struct IndexBufferArray *next;
};
typedef struct IndexWriterStruct {
struct SgrepStruct *sgrep;
const IndexOptions *options;
/* FileList of the indexed files */
FileList *file_list;
/* To speed up index buffer allocation and to reduce memory usage of
* the index buffers, they are allocated in chunks. */
struct IndexBufferArray *free_index_buffers;
int first_free_index_buffer;
/* Points to hash table of IndexBuffers when scanning indexed files */
int hash_size; /* Size of the hash table */
IndexBuffer **htable;
/* Points to list of sorted IndexBuffers when writing index file */
IndexBuffer *sorted_buffers;
/* Size, usage and pointer to postings spool (the one in main memory) */
int spool_size;
int spool_used;
struct IndexBlock *spool;
/* Array of memory load files */
TempFile *memory_load_files[MAX_MEMORY_LOADS];
int memory_loads;
/* The stream to which index is written */
FILE *stream;
/* Statistics */
int terms;
int postings;
int total_postings_bytes;
int total_string_bytes;
int strings_lcps_compressed;
int entry_lengths[8];
int flist_start;
int flist_size;
int total_index_file_size;
int failed;
} IndexWriter;
/*
* Used for creating IndexEntryLists
*/
struct IndexEntryListStruct {
int hits;
IndexReader *reader;
IndexEntry *first;
IndexEntry *last;
};
struct IndexEntryStruct {
char *term;
const unsigned char *postings;
struct IndexEntryStruct *next;
};
/*
* Used for reading index postings
*/
struct SortingReaderStruct {
Region *regions[32];
int sizes[32];
int lists_merged;
int regions_merged;
int max;
Region one;
Region *saved_array;
int saved_size;
int dots;
};
/*
* Looking up something in index requires one of these
*/
struct LookupStruct {
SgrepData *sgrep;
const char *begin;
const char *end;
IndexReader *map;
void (*callback)(const char *str, const unsigned char *regions,
struct LookupStruct *data);
int stop_words;
union {
/* This one is for looking up only entries */
struct IndexEntryListStruct *entry_list;
/* This one is for creating possibly unsorted region list from all
* postings */
RegionList *reader;
/* This is used for sorting postings while reading them */
struct SortingReaderStruct sorting_reader;
/* This is for dumping postings to a file stream */
FILE *stream;
} data;
};
/*
* The real stuff
*/
static int put_int(int i,FILE* stream) {
putc(i>>24,stream);
putc(i>>16,stream);
putc(i>>8,stream);
putc(i&255,stream);
return 4;
}
static int get_int(const unsigned char *ptr, int ind) {
ptr+=ind*4;
return (ptr[0]<<24) | (ptr[1]<<16) | (ptr[2]<<8) | ptr[3];
}
/*
* Writes postings of from given IndexBuffer to given stream.
* Does NOT check write errors: they have to be checked later.
*/
static int fwrite_postings(IndexWriter *writer, IndexBuffer *tmp,
FILE *stream) {
int bytes=0;
/* Now the regions */
if (tmp->block_used==0) {
/* This is possible, when this buffer was written out
* in some previous memory load, and there has been no new
* entries in this buffer since or when this is a stop word
* buffer and therefore contains no entries */
return 0;
} else if (tmp->block_used>0) {
bytes+=tmp->block_used;
fwrite(tmp->list.internal.ibuf,tmp->block_used,1,stream);
} else {
int esize;
struct IndexBlock *ind=&writer->spool[tmp->list.external.first];
esize=tmp->list.external.bytes;
bytes=esize;
while(ind->next!=INT_MIN) {
esize-=EXTERNAL_INDEX_BLOCK_SIZE;
fwrite(ind->buf,EXTERNAL_INDEX_BLOCK_SIZE,1,stream);
ind=&writer->spool[ind->next];
}
assert(esize<=EXTERNAL_INDEX_BLOCK_SIZE);
fwrite(ind->buf,esize,1,stream);
}
return bytes;
}
/* FIXME: needs better hash function */
unsigned int hash_function(int size,const char *str) {
int i;
unsigned int r=0;
for(i=0;str[i];i++) {
r=((unsigned char)str[i])+61*r;
}
/* printf("%s=%d,",str,r%size); */
return r%size;
}
void display_index_statistics(IndexWriter *writer) {
int i;
int size;
FILE *f;
f=writer->sgrep->progress_stream;
size=writer->spool_size*sizeof(struct IndexBlock);
fprintf(f,"Indexer memory usage:\n");
fprintf(f,"%dK bytes postings, %dK postings spool size, %dK used\n",
writer->total_postings_bytes/1024,
size/1024,
writer->spool_used/1024);
fprintf(f,"%d individual terms of %d term postings (%d%%)\n",
writer->terms,writer->postings,
writer->terms*100/writer->postings);
fprintf(f,"Postings lengths:\n");
for(i=0;i<8;i++) {
if (writer->entry_lengths[i]>0) {
fprintf(f,"%8d:%8d, %8dK (%d%%)\n",i+1,
writer->entry_lengths[i],
(i+1)*writer->entry_lengths[i]/1024,
(i+1)*writer->entry_lengths[i]*100/writer->total_postings_bytes);
}
}
fprintf(f,"Hash array size %dK\n",
writer->hash_size*sizeof(IndexBuffer*)/1024);
fprintf(f,"Term entries total size %dK\n",
writer->terms*sizeof(IndexBuffer)/1024);
fprintf(f,"Strings total size %dK\n",writer->total_string_bytes/1024);
}
int index_buffer_compare(const void *first, const void *next) {
return strcmp(
(*(const IndexBuffer **)first)->str,
(*(const IndexBuffer **)next)->str
);
}
void index_spool_overflow(IndexWriter *writer) {
int i,j;
IndexBuffer *l;
IndexBuffer **term_array;
int esize;
TempFile *temp_file;
FILE *load_file;
SgrepData *sgrep=writer->sgrep;
sgrep_progress(sgrep,"Postings spool overflow. Sorting terms..\n");
term_array=(IndexBuffer **)sgrep_malloc(sizeof(IndexBuffer *)*writer->terms);
if (writer->htable) {
/* Make an array of the hash table */
j=0;
for(i=0;i<writer->hash_size;i++) {
for(l=writer->htable[i];l;l=l->next) {
term_array[j++]=l;
}
}
qsort(term_array,writer->terms,
sizeof(IndexBuffer *),index_buffer_compare);
} else {
/* Make an array of the sorted buffers */
j=0;
for(l=writer->sorted_buffers;l;l=l->next) {
assert(j<writer->terms);
term_array[j++]=l;
}
assert(j==writer->terms);
}
temp_file=create_temp_file(sgrep);
if (!temp_file) {
sgrep_error(sgrep,"Can't write memory load\n");
writer->failed=1;
writer->spool_used=0;
sgrep_free(term_array);
return;
}
load_file=temp_file_stream(temp_file);
for(i=0;i<writer->terms;i++) {
if ( (i&1023)==0 ) {
sgrep_progress(sgrep,"saving memory load: %d/%d entries (%d%%)\r",
i,writer->terms,i*100/writer->terms);
}
if (term_array[i]->block_used<0) {
/* Only write external buffers. First the entry string. */
fputs(term_array[i]->str,load_file);
fputc(0,load_file);
put_int(term_array[i]->list.external.bytes,load_file);
/* Then the postings */
esize=fwrite_postings(writer,term_array[i],load_file);
term_array[i]->saved_bytes+=esize;
assert(esize==term_array[i]->list.external.bytes);
/* Now empty the entry */
term_array[i]->block_used=0;
}
}
sgrep_free(term_array);
sgrep_progress(sgrep,"\n");
fflush(load_file);
if (ferror(load_file)) {
sgrep_error(sgrep,"Failed to write memory load: %s\n",strerror(errno));
delete_temp_file(temp_file);
writer->failed=1;
} else {
writer->memory_load_files[writer->memory_loads++]=temp_file;
}
writer->spool_used=0;
}
/* FIXME: Here we assume that sizeof(int) is 4 */
void new_block(IndexWriter *writer,IndexBuffer *buf, unsigned char byte) {
assert(writer->spool_used<writer->spool_size);
assert(writer->spool[buf->list.external.current].next==INT_MIN);
writer->spool[buf->list.external.current].next=writer->spool_used;
buf->list.external.current=writer->spool_used;
writer->spool[writer->spool_used].buf[0]=byte;
writer->spool[writer->spool_used].next=INT_MIN;
buf->list.external.bytes++;
buf->block_used=-1;
writer->spool_used++;
}
void add_byte(IndexWriter *writer,IndexBuffer *buf, unsigned char byte) {
int used;
writer->total_postings_bytes++;
if (buf->block_used>=0) {
/* Internal block */
if (buf->block_used<INTERNAL_INDEX_BLOCK_SIZE) {
buf->list.internal.ibuf[buf->block_used++]=byte;
return;
}
/* Does not fit into internal block anymore. Make it external */
assert(writer->spool_used<writer->spool_size);
writer->spool[writer->spool_used].next=INT_MIN;
memcpy(writer->spool[writer->spool_used].buf,
buf->list.internal.ibuf,INTERNAL_INDEX_BLOCK_SIZE);
buf->list.external.first=writer->spool_used;
buf->list.external.current=writer->spool_used;
buf->list.external.bytes=buf->block_used;
buf->block_used=-INTERNAL_INDEX_BLOCK_SIZE;
writer->spool_used++;
}
/* External block */
used=-buf->block_used;
if (used==EXTERNAL_INDEX_BLOCK_SIZE) {
new_block(writer,buf,byte);
} else {
writer->spool[buf->list.external.current].buf[used]=byte;
buf->block_used--;
buf->list.external.bytes++;
}
assert(writer->spool_used<=writer->spool_size);
if (writer->spool_used==writer->spool_size) index_spool_overflow(writer);
}
/*
* Here is the core of the index compression technique: small numbers
* take less space than large numbers. This function does the actual
* compression. The mapping of regions (or whateever data is stored
* under the index term) to small numbers is done elsewhere
*/
#define NEGATIVE_NUMBER_TAG ((unsigned char)255)
#define END_OF_POSTINGS_TAG ((unsigned char)127)
void add_integer(IndexWriter *writer, IndexBuffer *buf, int num) {
if (num<0) {
/* Negative number: Add the NEGATIVE_NUMBER tag */
add_byte(writer,buf,NEGATIVE_NUMBER_TAG);
/* Add the number as positive integer */
num=-num;
}
if (num<127) {
/* Eight bits with being 0 */
/* 01111111 is reserved for end of buffer. Zero is OK */
add_byte(writer,buf,num);
writer->entry_lengths[0]++;
} else if (num<(1<<14)) {
/* 16 bits with first being 10 */
add_byte(writer,buf,(num>>8)|128);
add_byte(writer,buf,num&255);
writer->entry_lengths[1]++;
} else if (num<(1<<21)) {
/* 24 bits with first being 110 */
add_byte(writer,buf,(num>>16)|(128+64));
add_byte(writer,buf,(num>>8)&255);
add_byte(writer,buf,num&255);
writer->entry_lengths[2]++;
} else if (num<(1<<28)) {
/* 32 bits with first being 1110 */
add_byte(writer,buf,(num>>24)|(128+64+32));
add_byte(writer,buf,(num>>16)&255);
add_byte(writer,buf,(num>>8)&255);
add_byte(writer,buf,num&255);
writer->entry_lengths[3]++;
} else if (num<=0x7fffffff) {
/* First byte 11110000*/
add_byte(writer,buf,0xf0);
add_byte(writer,buf,(num>>24)&255);
add_byte(writer,buf,(num>>16)&255);
add_byte(writer,buf,(num>>8)&255);
add_byte(writer,buf,num&255);
} else {
/* More than 32 bits. Shouldn't happen with ints. */
sgrep_error(writer->sgrep,"Index value %u is too big!\n",num);
}
}
void add_entry(IndexWriter *writer,IndexBuffer *buf, int index) {
assert(index>=0);
index-=buf->last_index;
buf->last_index+=index;
add_integer(writer,buf,index);
}
unsigned char get_next_block(IndexWriter *writer,IndexBuffer *buf) {
assert(buf->block_used<0);
if (buf->block_used==-EXTERNAL_INDEX_BLOCK_SIZE-1) {
/* Start scan */
buf->list.external.current=buf->list.external.first;
} else {
/* Next block */
assert(-buf->block_used==EXTERNAL_INDEX_BLOCK_SIZE);
assert(writer->spool[buf->list.external.current].next>0);
buf->list.external.current=writer->spool[buf->list.external.current].next;
}
buf->block_used=-1;
return writer->spool[buf->list.external.current].buf[0];
}
unsigned char get_byte(IndexBuffer *buf) {
assert(buf->block_used==SHRT_MIN);
return buf->list.map.buf[buf->list.map.ind++];
#if 0
if (buf->block_used==SHRT_MIN) {
return buf->list.map.buf[buf->list.map.ind++];
}
if (buf->block_used>=0) {
assert(buf->block_used<INTERNAL_INDEX_BLOCK_SIZE);
return buf->list.internal.ibuf[buf->block_used++];
}
if (buf->block_used<=-EXTERNAL_INDEX_BLOCK_SIZE)
return get_next_block(writer,buf);
return writer->spool[buf->list.external.current].buf[-(buf->block_used--)];
#endif /* 0 */
}
int get_integer(IndexBuffer *buf) {
unsigned char i;
int r;
int negative=0;
i=get_byte(buf);
if (i==NEGATIVE_NUMBER_TAG) {
negative=1;
i=get_byte(buf);
}
if (i==END_OF_POSTINGS_TAG) {
/* Found end of index */
return INT_MAX;
}
else if (i<127) r=i; /* 8 bits starting with 0 */
else if ((i&(128+64))==128) {
/* 16 bits starting with 10 */
r=((i&63)<<8)|get_byte(buf);
}
else if ((i&(128+64+32))==128+64) {
/* 24 bits starting with 110 */
r=(i&31)<<16|(get_byte(buf)<<8);
r=r|get_byte(buf);
}
else if ((i&(128+64+32+16))==128+64+32) {
/* 32 bits starting with 1110 */
r=(i&15)<<24|(get_byte(buf)<<16);
r|=get_byte(buf)<<8;
r=r|get_byte(buf);
} else if(i==0xf0) {
/* 40 bits starting with 0xf0 */
r=get_byte(buf)<<24;
r|=get_byte(buf)<<16;
r|=get_byte(buf)<<8;
r|=get_byte(buf);
} else {
assert(0 && "Corrupted index file");
abort();
}
return (negative)?-r:r;
}
unsigned int get_entry(IndexBuffer *buf) {
int r=get_integer(buf);
if (r==INT_MAX) return r;
buf->last_index+=r;
assert(buf->last_index>=0);
/* fprintf(stderr,"%d\n",buf->last_index); */
return buf->last_index;
}
static IndexBuffer *new_writer_index_buffer(IndexWriter *writer) {
struct SgrepStruct *sgrep=writer->sgrep;
if (writer->free_index_buffers==NULL ||
writer->first_free_index_buffer==INDEX_BUFFER_ARRAY_SIZE) {
struct IndexBufferArray *b;
b=(struct IndexBufferArray *)sgrep_calloc(1,
sizeof(struct IndexBufferArray));
b->next=writer->free_index_buffers;
writer->first_free_index_buffer=0;
writer->free_index_buffers=b;
}
return &writer->free_index_buffers->bufs[writer->first_free_index_buffer++];
}
IndexBuffer *find_index_buffer(IndexWriter *writer, const char *str) {
IndexBuffer **n;
int h;
SgrepData *sgrep=writer->sgrep;
h=hash_function(writer->hash_size,str);
n=&writer->htable[h];
while(*n!=NULL) {
/* There already is entries with same hash value */
if (strcmp(str,(*n)->str)!=0) {
/* No match */
n=&(*n)->next;
} else {
/* Found existing entry */
return *n;
}
}
writer->terms++;
if ((writer->terms==writer->hash_size*2)) {
sgrep_error(sgrep,"Warning: There is more than 2*%d (hash table size) unique index terms.\n",writer->hash_size);
sgrep_error(sgrep,"Warning: Suggest using larger hash table (-H option).\n");
}
*n=new_writer_index_buffer(writer);
(*n)->str=sgrep_strdup(str);
(*n)->last_len=strlen(str)-1;
writer->total_string_bytes+=strlen(str)+1;
return *n;
}
int read_stop_word_file(IndexWriter *writer, const char *filename) {
char entry[max_term_len];
int term_len;
int ch;
FILE *stop_file;
IndexBuffer *ib;
SGREPDATA(writer);
stop_file=fopen(filename,"r");
if (stop_file==NULL) {
sgrep_error(sgrep,"Failed to read stop word file '%s':%s\n",
filename,strerror(errno));
return SGREP_ERROR;
}
ch=getc(stop_file);
while(ch!=EOF) {
/* If the line starts with a number, ignore them */
if (ch>='0' && ch<='9') {
while(ch>='0' && ch<='9') ch=getc(stop_file);
/* And the space, if there was one */
if (ch==' ') ch=getc(stop_file);
}
for(term_len=0;
term_len<max_term_len-1 && ch!=EOF && ch!='\n';
ch=getc(stop_file)) entry[term_len++]=ch;
entry[term_len]=0;
if (term_len>0) {
/* fprintf(stderr,"'%s'\n",entry); */
ib=find_index_buffer(writer,entry);
/* We can't unwind already added postings (because it is not
* implemented) so we check that there is none */
assert(ib->last_index==0 || ib->last_index==-1);
ib->last_index=-1;
}
/* Finally, skip the LF */
if (ch=='\n') ch=getc(stop_file);
}
fclose(stop_file);
return SGREP_OK;
}
int add_region_to_index(IndexWriter *writer,
const char *str, int start, int end) {
IndexBuffer *ib;
int len;
SGREPDATA(writer);
if (end<start) {
sgrep_error(sgrep,"BUG: ignoring zero sized region\n");
return SGREP_OK;
}
ib=find_index_buffer(writer,str);
writer->postings++;
/* Check for stopword */
if (ib->last_index==-1) return SGREP_OK;
len=end-start+1;
/* FIXME: the start!=0 condition should be removed, but removing
* it needs change in index file format! (In other words: a design
* bug. Sorry about that. */
if (ib->last_len==len && start!=0) {
/* This is the compression hack: skip the end point in entries
* having same length */
if (start==ib->last_index) {
/* Bad luck: we have to add zero entry tag. So we duplicate
* it */
add_entry(writer,ib,start);
add_entry(writer,ib,start);
} else {
add_entry(writer,ib,start);
}
} else if (len==-ib->last_len) {
/* Previous was same lenght as this. Switch to compression hack
* state */
ib->last_len=len;
add_entry(writer,ib,start);
add_entry(writer,ib,end);
} else {
/* Lengths did not match. If we we're in compression hack
* state, need to add tag to switch state */
if (ib->last_len>0) {
/* Add the zero entry tag to switch state */
add_entry(writer,ib,ib->last_index);
}
/* Normal entry */
ib->last_len=-len;
add_entry(writer,ib,start);
add_entry(writer,ib,end);
}
if (writer->failed) {
return SGREP_ERROR;
} else {
return SGREP_OK;
}
}
int get_region_index(IndexBuffer *buf, Region *region) {
int saved_index;
int s,e;
saved_index=buf->last_index;
assert(saved_index!=INT_MAX);
s=get_entry(buf);
if (s==INT_MAX) {
buf->last_index=INT_MAX;
return 0;
}
if (buf->last_len>0) {
/* We are in compression hack state */
if (s==saved_index) {
/* The zero tag: either switch to normal state or this is
* escaped zero tag */
s=get_entry(buf);
if (s==saved_index && s!=0) {
/* It was an escaped zero tag */
region->start=s;
region->end=s+buf->last_len-1;
return 1;
}
/* Switch to normal state. Need to read also end index */
e=get_entry(buf);
assert(e!=INT_MAX);
buf->last_len=0-(e-s+1);
region->start=s;
region->end=e;
return 1;
}
/* Ending point was compressed out */
region->start=s;
region->end=s+buf->last_len-1;
return 1;
}
/* Normal state. Read also end point */
e=get_entry(buf);
assert(e!=INT_MAX);
if (e-s+1==-buf->last_len) {
/* Same length twice. Switch to CH state */
buf->last_len=e-s+1;
} else {
/* Different length. Just save the length */
buf->last_len=0-(e-s+1);
}
region->start=s;
region->end=e;
return 1;
}
void rewind_index_buffer(IndexBuffer *buf) {
if (buf->block_used<0) {
buf->block_used=-EXTERNAL_INDEX_BLOCK_SIZE-1;
} else {
buf->block_used=0;
}
buf->last_index=0;
}
IndexWriter *new_index_writer(const IndexOptions *options) {
int i;
IndexWriter *writer;
SgrepData *sgrep=options->sgrep;
writer=sgrep_new(IndexWriter);
writer->sgrep=options->sgrep;
writer->options=options;
writer->file_list=NULL;
writer->free_index_buffers=NULL;
writer->first_free_index_buffer=0;
writer->total_postings_bytes=0;
writer->terms=0;
writer->postings=0;
writer->total_string_bytes=0;
for(i=0;i<8;i++) writer->entry_lengths[i]=0;
writer->htable=(IndexBuffer **)sgrep_malloc(
options->hash_table_size*sizeof(IndexBuffer*));
/* Mark the hash entries as unused */
writer->hash_size=options->hash_table_size;
for(i=0;i<writer->hash_size;i++) writer->htable[i]=NULL;
writer->spool_size=options->available_memory/
sizeof(struct IndexBlock);
writer->spool_used=0;
writer->spool=(struct IndexBlock*)sgrep_calloc(1,writer->spool_size*sizeof(struct IndexBlock));
if (writer->spool==NULL) {
sgrep_error(sgrep,"Could not allocate %dK memory for postings spool\n",
writer->spool_size*sizeof(struct IndexBlock)/1024);
sgrep_free(writer->htable);
sgrep_free(writer);
return NULL;
}
writer->memory_loads=0;
writer->stream=NULL;
writer->failed=0;
return writer;
}
/*
* This frees all resources (memory, files, what else?) allocated by
* IndexWriter. This can be called either because indexing has
* been successfully completed, or because indexing has failed
*/
void delete_index_writer(IndexWriter *writer) {
struct IndexBufferArray *b;
int i;
SgrepData *sgrep=writer->sgrep;
/* Close the index file stream */
if (writer->stream) {
fclose(writer->stream);
writer->stream=NULL;
}
/* Close all temporary file stream */
for (i=0;i<writer->memory_loads;i++) {
if (writer->memory_load_files[i]!=NULL) {
delete_temp_file(writer->memory_load_files[i]);
writer->memory_load_files[i]=NULL;
}
}
/* Free all the IndexBuffers */
while (writer->free_index_buffers) {
while(--writer->first_free_index_buffer>=0) {
sgrep_free(writer->free_index_buffers->bufs[writer->first_free_index_buffer].str);
}
b=writer->free_index_buffers;
writer->free_index_buffers=writer->free_index_buffers->next;
writer->first_free_index_buffer=INDEX_BUFFER_ARRAY_SIZE;
sgrep_free(b);
}
/* Free the postings spool */
if (writer->spool) {
sgrep_free(writer->spool);
}
/* Free the hash table */
if (writer->htable) {
sgrep_free(writer->htable);
}
/* Free the writer itself */
sgrep_free(writer);
}
IndexBuffer *merge_sort_index_buffer(IndexBuffer *list) {
IndexBuffer *l;
IndexBuffer *next,*first,*second;
IndexBuffer *sorted=NULL;
if (list==NULL) return list;
/* Split */
first=NULL;
second=NULL;
while(list) {
next=list->next;
list->next=first;
first=list;
list=next;
if (list) {
next=list->next;
list->next=second;
second=list;
list=next;
}
}
if (second==NULL) return first;
/* Recursion */
first=merge_sort_index_buffer(first);
second=merge_sort_index_buffer(second);
/* Merge */
l=NULL;
while(first&&second) {
if (first && (!second || strcmp(first->str,second->str)<=0)) {
if (l) l=l->next=first;
else l=sorted=first;
first=first->next;
} else {
if (l) l=l->next=second;
else l=sorted=second;
second=second->next;
}
}
assert(first||second);
if (first) l->next=first;
else l->next=second;
return sorted;
}
void sort_index_buffers(IndexWriter *writer) {
IndexBuffer *list;
IndexBuffer *l,*next;
IndexBuffer *sorted_buffer;
int i;
int state;
SGREPDATA(writer);
/* Link buffers from hash table to list */
list=NULL;
state=0;
for(i=0;i<writer->hash_size;i++) {
for(l=writer->htable[i];l;l=next) {
next=l->next;
l->next=list;
list=l;
}
}
/* Since the hash table is not valid anymore, free it now */
sgrep_free(writer->htable);
/* Now sort the buffers */
sorted_buffer=merge_sort_index_buffer(list);
writer->htable=NULL;
writer->sorted_buffers=sorted_buffer;
}
/* There exists a faster algorithm for this, but i don't think that it
* would give us any noticiable speed advantage in this particular
* application, since this isn't the crucial part anyway. */
void count_lcps_recursion(IndexBuffer **array,int len,const char *str) {
const char *middle_str;
int middle_ind;
unsigned int i;
assert(len!=0);
middle_ind=len/2;
middle_str=array[middle_ind]->str;
for(i=0;str[i]==middle_str[i] && middle_str[i] && str[i]; i++);
array[middle_ind]->lcp=(i<256)?i:255;
array[middle_ind]=NULL;
if (len==1) return;
if (len==2) {
count_lcps_recursion(array,1,middle_str);
return;
}
count_lcps_recursion(array,middle_ind,middle_str);
count_lcps_recursion(array+middle_ind+1,len-middle_ind-1,middle_str);
}
void count_common_prefixes(IndexWriter *writer) {
int i;
IndexBuffer *tmp;
IndexBuffer **sorted_array;
SGREPDATA(writer);
sorted_array=(IndexBuffer **)sgrep_malloc(
writer->terms*sizeof(IndexBuffer *));
for(tmp=writer->sorted_buffers,i=0;tmp;tmp=tmp->next,i++) {
assert(i<writer->terms);
sorted_array[i]=tmp;
}
count_lcps_recursion(sorted_array,i,"");
sgrep_free(sorted_array);
}