-
Notifications
You must be signed in to change notification settings - Fork 8
/
index_external.c
993 lines (834 loc) · 21.7 KB
/
index_external.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
/**
* index_external.c - an external string index implementation for SimpleMail.
* Copyright (C) 2013 Sebastian Bauer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index_external.c
*
* This is a naive implementation of an external memory data structure that
* can be used for full text search. It is basically a b tree that stores all
* suffixes of each document that is inserted.
*
* It is very slow at the moment, in particular as the strings are read multiple
* times from the external media.
*/
#include "index_external.h"
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "index_private.h"
#include "lists.h"
#include "support.h"
/* Get va_vopy() with pre-C99 compilers */
#ifdef __SASC
#define va_copy(dest,src) ((dest) = (src))
#elif defined(__GNUC__) && (__GNUC__ < 3)
#include <varargs.h>
#endif
/* Define this if some debug options should be compiled in */
/*#define DEBUG_FUNCTIONS*/
struct bnode_element
{
unsigned int str_offset;
unsigned int str_len;
union
{
struct
{
int did;
} leaf;
/** Child containing values that are all greater or equal */
struct
{
int gchild;
} internal;
};
};
struct bnode_header
{
int leaf;
int num_elements; /* Number of following bnode_elements */
int lchild; /* Child containing keys/strings that are all (lexicographical) smaller */
int sibling;
};
typedef struct bnode_header bnode;
struct index_external
{
struct index index;
struct list document_list;
unsigned int block_size;
int max_elements_per_node;
int number_of_blocks;
FILE *string_file;
FILE *offset_file;
FILE *index_file;
/** Identifies the block for the root node. TODO: Make this persistent */
int root_node;
/**
* Defines the maximum length for which a substring search is accurate.
* Searches with longer substring will shorten the substring to this length.
*/
unsigned int max_substring_len;
bnode *tmp;
bnode *tmp2;
bnode *tmp3;
};
/**
* Create a new node for the given index/btree.
*
* @param idx
* @return
*/
static bnode *bnode_create(struct index_external *idx)
{
struct bnode_header *bnode;
if (!(bnode = (struct bnode_header*)malloc(idx->block_size + sizeof(struct bnode_element))))
return NULL;
memset(bnode, 0, idx->block_size + sizeof(struct bnode_element));
bnode->sibling = -1;
return bnode;
}
/**
* Free the memory associated to the given bnode.
*
* @param idx
* @param bnode
*/
static void bnode_free(struct index_external *idx, bnode *bnode)
{
free(bnode);
}
/**
* Return the i'th element of the given bnode.
*
* @param idx
* @param bnode_header
* @param elem
* @return
*/
static struct bnode_element *bnode_get_ith_element_of_node(struct index_external *idx, bnode *bnode, int i)
{
return &(((struct bnode_element*)(bnode+1))[i]);
}
/**
* Writes the given block to the given address.
*
* @param idx
* @param node
* @param address
* @return
*/
static int bnode_write_block(struct index_external *idx, const bnode *node, int address)
{
unsigned int offset = address * idx->block_size;
if (fseek(idx->index_file, offset, SEEK_SET))
return 0;
if (fwrite(node, 1, idx->block_size, idx->index_file) != idx->block_size)
return 0;
return 1;
}
/**
* Adds the given block to the index and return the block number.
*
* @param idx
* @param node
* @return -1 for an error.
*/
static int bnode_add_block(struct index_external *idx, const bnode *node)
{
int new_address = idx->number_of_blocks++;
if (!bnode_write_block(idx, node, new_address))
return -1;
return new_address;
}
/**
* Reads the given block for the given address to the given node.
*
* @param idx
* @param node
* @param address
* @return
*/
static int bnode_read_block(struct index_external *idx, bnode *node, int address)
{
size_t rc;
unsigned int offset = address * idx->block_size;
if (fseek(idx->index_file, offset, SEEK_SET))
return 0;
rc = fread(node, 1, idx->block_size, idx->index_file);
if (rc != idx->block_size)
return 0;
return 1;
}
/**
* Reads the entire string for the given element,
*
* @param idx
* @param element
* @return the string which must be freed with free() when no longer in use.
*/
static char *bnode_read_string(struct index_external *idx, struct bnode_element *element)
{
char *str;
unsigned int str_len = element->str_len;
if (str_len > idx->max_substring_len)
str_len = idx->max_substring_len;
fseek(idx->string_file, element->str_offset, SEEK_SET);
if (!(str = (char *)malloc(str_len + 1)))
return 0;
if (fread(str, 1, str_len, idx->string_file) != str_len)
return 0;
str[str_len] = 0;
return str;
}
/**
* Compares contents represented by the given element with the given text. If str is the contents,
* then the function places the return value of strcmp(str, text) into *out_cmp.
*
* @param idx
* @param e
* @param text
* @param out_cmp
* @return 0 on failure, 1 on success.
*/
static int bnode_compare_string(struct index_external *idx, struct bnode_element *e, const char *text, int *out_cmp)
{
char *str = bnode_read_string(idx, e);
if (!str) return 0;
*out_cmp = strncmp(str, text, idx->max_substring_len);
free(str);
return 1;
}
#define BNODE_PATH_MAX_NODES 24
/**
* Represents a path in the bnode tree.
*/
struct bnode_path
{
int max_level;
struct
{
int block;
int key_index;
} node[BNODE_PATH_MAX_NODES];
};
/**
* Find the slot with the separation key for the given text. The slot with
* the separation key for a given text is the slot whose value is not
* lexicographically smaller than the text but whose left neighbor slot value
* is lexicographically smaller than the text.
*
* @param idx
* @param tmp
* @param text
* @param out_slot
* @param out_directmatch
* @return
*/
static int bnode_find_separation_slot(struct index_external *idx, bnode *tmp, const char *text, int *out_slot, int *out_directmatch)
{
int l, h, hcmp;
/* Variable l is inclusive, h is exclusive */
l = 0;
h = tmp->num_elements;
hcmp = 1;
while (l < h)
{
int cmp;
int m = (l + h)/2;
struct bnode_element *e = bnode_get_ith_element_of_node(idx, tmp, m);
if (!bnode_compare_string(idx, e, text, &cmp))
return 0;
/* < results in getting the first element. We could also use <= to get the last element */
if (cmp < 0)
{
l = m + 1;
} else
{
h = m;
hcmp = cmp;
}
}
*out_slot = h;
*out_directmatch = hcmp == 0;
return 1;
}
/**
* Lookup the given text.
*
* @param idx
* @param text
* @param out_block_array
* @param out_index
* @return
*/
static int bnode_lookup(struct index_external *idx, const char *text, struct bnode_path *path)
{
int i;
bnode *tmp = idx->tmp;
int block = idx->root_node;
int level = 0;
do
{
int lchild;
int direct_match = 0;
bnode_read_block(idx, tmp, block);
if (!bnode_find_separation_slot(idx, tmp, text, &i, &direct_match))
return 0;
path->max_level = level;
path->node[level].block = block;
path->node[level].key_index = i;
if (tmp->leaf)
break;
if (i == 0) lchild = tmp->lchild;
else lchild = bnode_get_ith_element_of_node(idx, tmp, i - 1)->internal.gchild;
if (block == lchild && !tmp->leaf)
{
#ifdef DEBUG_FUNCTIONS
fprintf(stderr, "Endless loop detected!\n");
#endif
return 0;
}
/* Otherwise, it was no direct match, and the separation key is lexicographically strictly
* greater hence the sub tree defined by the left child, which is lexicographically strictly
* smaller. will contain the key (or a better suited value if the key is not contained).
*/
block = lchild;
level++;
if (level == BNODE_PATH_MAX_NODES)
return 0;
} while (!tmp->leaf);
return 1;
}
/**
* Clear all elements of the given node begining at start.
*
* @param idx
* @param n
* @param start
*/
static void bnode_clear_elements(struct index_external *idx, bnode *n, int start)
{
struct bnode_element *e = bnode_get_ith_element_of_node(idx, n, start);
memset(e, 0, (idx->max_elements_per_node - start)*sizeof(struct bnode_element));
}
#ifdef DEBUG_FUNCTIONS
/**
* Dump the children of the given nodes.
*/
static void dump_node_children(struct index_external *idx, bnode *node, const char *prefix)
{
int i;
printf("%s (%d): ", prefix, node->lchild);
for (i=0;i<node->num_elements;i++)
{
struct bnode_element *be;
be = bnode_get_ith_element_of_node(idx, node, i);
printf("%d ", node->leaf?be->leaf.did:be->internal.gchild);
}
printf("\n");
}
/**
* Dump the entire index.
*
* @param idx
* @param block
* @param level
*/
static void dump_index(struct index_external *idx, int block, int level)
{
int i;
bnode *tmp = bnode_create(idx);
if (!bnode_read_block(idx, tmp, block))
return;
printf("dump_index(block=%d, level=%d, leaf=%d, sibling=%d) elements=%d\n", block, level, tmp->leaf, tmp->sibling, tmp->num_elements);
if (!tmp->leaf)
dump_index(idx, tmp->lchild, level + 1);
for (i=0; i<tmp->num_elements; i++)
{
char *str;
struct bnode_element *e;
char buf[16];
e = bnode_get_ith_element_of_node(idx, tmp, i);
if (!(str = bnode_read_string(idx, e)))
{
fprintf(stderr, "Couldn't read string!\n");
exit(-1);
}
snprintf(buf,sizeof(buf),"%s", str);
{
int k;
for (k=0;k<level;k++)
printf(" ");
}
printf("l%d: b%d: i%d: o%04d ", level, block, i, e->str_offset);
{
int k;
for (k=0;k<strlen(buf);k++)
{
if (!buf[k]) break;
if (buf[k] == 10) buf[k] = ' ';
printf("%c", buf[k]);
}
printf("\n");
}
if (!tmp->leaf)
dump_index(idx, e->internal.gchild, level + 1);
free(str);
}
bnode_free(idx, tmp);
}
/**
* Verify whether the index is consitent, i.e., whether all strings are in increasing order.
*
* @param
* @param block
* @param level
*/
static void verify_index(struct index_external *idx, int block, int level)
{
int i;
bnode *tmp = bnode_create(idx);
char *str1 = strdup("");
char *str2;
int offset1 = -1;
if (!bnode_read_block(idx, tmp, block))
return;
if (!tmp->leaf)
verify_index(idx, tmp->lchild, level + 1);
for (i=0; i<tmp->num_elements; i++)
{
struct bnode_element *e;
int rc;
e = bnode_get_ith_element_of_node(idx, tmp, i);
str2 = bnode_read_string(idx, e);
if ((rc = strcmp(str1,str2)) > 0)
{
int *a = 0;
printf("violation\n");
printf("%d: %d: %d: %d-%d=%d |%s |%s (%d %d)\n", level, block, i, str1[0], str2[0], rc, str1, str2, offset1, e->str_offset);
*a = 0;
exit(1);
}
if (!tmp->leaf)
verify_index(idx, e->internal.gchild, level + 1);
free(str1);
str1 = str2;
offset1= e->str_offset;
}
free(str1);
bnode_free(idx, tmp);
}
/**
* Count the total number of strings to which the index refers.
* This includes the strings of the internal nodes as well as the leaves.
*
* @param idx
* @param block
* @param level
*/
static int count_index(struct index_external *idx, int block, int level)
{
int i, count = 0;
bnode *tmp = bnode_create(idx);
if (!bnode_read_block(idx, tmp, block))
return 0;
if (!tmp->leaf)
count += count_index(idx, tmp->lchild, level + 1);
for (i=0; i<tmp->num_elements; i++)
{
struct bnode_element *e;
int rc;
e = bnode_get_ith_element_of_node(idx, tmp, i);
if (!tmp->leaf)
count += count_index(idx, e->internal.gchild, level + 1);
count++;
}
bnode_free(idx, tmp);
return count;
}
/**
* Count the number of strings, to which all the leaves refer.
*
* @param idx
* @param block
* @param level
*/
static int count_index_leaves(struct index_external *idx, int block, int level)
{
int i, count = 0;
bnode *tmp = bnode_create(idx);
if (!bnode_read_block(idx, tmp, block))
return -1;
if (!tmp->leaf)
count += count_index(idx, tmp->lchild, level + 1);
for (i=0; i<tmp->num_elements; i++)
{
struct bnode_element *e;
int rc;
e = bnode_get_ith_element_of_node(idx, tmp, i);
if (!tmp->leaf)
{
int c = count_index_leaves(idx, e->internal.gchild, level + 1);
if (c == -1)
{
return -1;
}
count += c;
} else
{
count++;
}
}
bnode_free(idx, tmp);
return count;
}
#endif
/**
* Inserts the given string into the bnode tree.
*
* @param idx
* @param did
* @param txt
* @return
*/
static int bnode_insert_string(struct index_external *idx, int did, int offset, const char *text)
{
int i;
int current_level;
int block;
bnode *tmp = idx->tmp;
struct bnode_path path;
struct bnode_element new_element;
if (!bnode_lookup(idx, text, &path))
return 0;
current_level = path.max_level;
new_element.str_offset = offset;
new_element.str_len = strlen(text);
new_element.leaf.did = did;
while (current_level >= 0)
{
struct bnode_element *e;
block = path.node[current_level].block;
i = path.node[current_level].key_index;
bnode_read_block(idx, tmp, block);
if (!tmp->leaf && current_level == path.max_level)
{
#ifdef DEBUG_FUNCTIONS
fprintf(stderr, "Cannot insert into a non-leaf\n");
exit(1);
#endif
}
/* Recall that we allocate in our temps one more entry than it would fit in the block, so we surly can
* insert the entry now */
e = bnode_get_ith_element_of_node(idx, tmp, i);
memmove(e + 1, e, (tmp->num_elements - i) * sizeof(struct bnode_element));
/* New element */
*e = new_element;
tmp->num_elements++;
if (tmp->num_elements == idx->max_elements_per_node)
{
int tmp3block;
/* Now we split the node into two nodes. We keep the median in but also
* insert it as a separation value for the two nodes on the parent.
*/
int median = tmp->num_elements / 2;
int start_of_2nd_node = median;
struct bnode_element *me = bnode_get_ith_element_of_node(idx, tmp, median);
struct bnode_element me_copy = *me;
/* First node */
tmp->num_elements = median;
/* Second node */
idx->tmp3->num_elements = idx->max_elements_per_node - start_of_2nd_node;
idx->tmp3->lchild = me->internal.gchild;
idx->tmp3->leaf = tmp->leaf;
idx->tmp3->sibling = tmp->sibling;
memcpy(bnode_get_ith_element_of_node(idx, idx->tmp3, 0), bnode_get_ith_element_of_node(idx, tmp, start_of_2nd_node), idx->tmp3->num_elements * sizeof(struct bnode_element));
bnode_clear_elements(idx, idx->tmp3, idx->tmp3->num_elements);
tmp3block = bnode_add_block(idx, idx->tmp3);
/* This should be done as late as possible */
tmp->sibling = tmp3block;
bnode_clear_elements(idx, tmp, median);
bnode_write_block(idx, tmp, block);
if (block == idx->root_node)
{
#ifdef DEBUG_FUNCTIONS
assert(current_level == 0);
#endif
/* Create a new root block if the root was getting full */
tmp->num_elements = 1;
tmp->lchild = idx->root_node;
tmp->leaf = 0;
tmp->sibling = -1;
me_copy.internal.gchild = tmp3block;
*bnode_get_ith_element_of_node(idx, tmp, 0) = me_copy;
bnode_clear_elements(idx, tmp, 1);
idx->root_node = bnode_add_block(idx, tmp);
break;
}
new_element = me_copy;
new_element.internal.gchild = tmp3block;
} else
{
/* There was enough space in the block */
bnode_write_block(idx, tmp, block);
break;
}
current_level--;
}
return 1;
}
/**
* The iterator data.
*/
struct bnode_string_iter_data
{
/** Allocated bnode */
bnode *node;
/** The current index */
int index;
/** The document id */
int did;
};
/**
* The document did to which the iter points.
*
* @param iter
* @return
*/
int bnode_string_iter_data_get_did(struct bnode_string_iter_data *iter)
{
return iter->did;
}
/**
* Sets up an iterator to find the given text.
*
* @param idx the index.
* @param text the text to look for.
* @param iter the previous iterator or NULL if this is the first iterator.
* @return the new iterator or NULL, if no more matches could have been found.
*/
static struct bnode_string_iter_data *bnode_find_string_iter(struct index_external *idx, const char *text, struct bnode_string_iter_data *iter)
{
int cmp;
int index;
struct bnode_element *be;
if (!iter)
{
struct bnode_path path;
int block;
if (!(iter = (struct bnode_string_iter_data *)malloc(sizeof(*iter))))
return NULL;
if (!(iter->node = bnode_create(idx)))
{
free(iter);
return NULL;
}
if (!bnode_lookup(idx, text, &path))
return 0;
block = path.node[path.max_level].block;
index = path.node[path.max_level].key_index;
if (!bnode_read_block(idx, iter->node, block))
return 0;
} else
{
index = iter->index + 1;
}
if (index == iter->node->num_elements)
{
if (iter->node->sibling == -1)
goto done;
if (!bnode_read_block(idx, iter->node, iter->node->sibling))
goto done;
index = 0;
}
be = bnode_get_ith_element_of_node(idx, iter->node, index);
if (!bnode_compare_string(idx, be, text, &cmp))
goto done;
if (cmp) goto done;
iter->index = index;
iter->did = be->leaf.did;
return iter;
done:
bnode_free(idx, iter->node);
free(iter);
return NULL;
}
/**
* Tries to find the given string.
*
* @param idx
* @param text
* @return
*/
static int bnode_find_string(struct index_external *idx, const char *text, int (*callback)(int did, void *userdata), void *userdata)
{
struct bnode_string_iter_data *iter = NULL;
int nd = 0;
while ((iter = bnode_find_string_iter(idx, text, iter)))
{
callback(bnode_string_iter_data_get_did(iter), userdata);
nd++;
}
return nd;
}
static void index_external_dispose(struct index *index)
{
struct index_external *idx;
idx = (struct index_external*)index;
if (idx->offset_file) fclose(idx->offset_file);
if (idx->string_file) fclose(idx->string_file);
if (idx->index_file) fclose(idx->index_file);
if (idx->tmp3) bnode_free(idx, idx->tmp3);
if (idx->tmp2) bnode_free(idx, idx->tmp2);
if (idx->tmp) bnode_free(idx, idx->tmp);
free(idx);
}
static struct index *index_external_create_with_opts(const char *filename, int block_size)
{
struct index_external *idx;
char buf[380];
if (!(idx = (struct index_external*)malloc(sizeof(*idx))))
return NULL;
memset(idx,0,sizeof(*idx));
list_init(&idx->document_list);
idx->block_size = block_size;
idx->max_elements_per_node = (idx->block_size - sizeof(struct bnode_header)) / sizeof(struct bnode_element);
if (!(idx->tmp = bnode_create(idx)))
goto bailout;
if (!(idx->tmp2 = bnode_create(idx)))
goto bailout;
if (!(idx->tmp3 = bnode_create(idx)))
goto bailout;
sm_snprintf(buf, sizeof(buf), "%s.index", filename);
if (!(idx->index_file = fopen(buf, "w+b")))
goto bailout;
sm_snprintf(buf, sizeof(buf), "%s.strings", filename);
if (!(idx->string_file = fopen(buf, "w+b")))
goto bailout;
sm_snprintf(buf, sizeof(buf), "%s.offsets", filename);
if (!(idx->offset_file = fopen(buf, "w+b")))
goto bailout;
idx->tmp->leaf = 1;
idx->root_node = bnode_add_block(idx, idx->tmp);
idx->max_substring_len = 32;
return &idx->index;
bailout:
index_external_dispose(&idx->index);
return NULL;
}
static struct index *index_external_create(const char *filename)
{
return index_external_create_with_opts(filename, 16384);
}
/**
* Appends the given string to the string file. The string will be zero-byte terminated.
*
* @param idx the index.
* @param text the text to be inserted.
* @param offset returns the starting offset of the string that has been inserted.
* @return 0 on failure, else something different.
*/
static int index_external_append_string(struct index_external *idx, const char *text, long *offset)
{
/* Determine position and write text */
if (fseek(idx->string_file, 0, SEEK_END) != 0)
return 0;
*offset = ftell(idx->string_file);
fputs(text, idx->string_file);
fputc(0, idx->string_file);
return 1;
}
/**
* Structure representing an offset/did pair.
*/
struct offset_entry
{
int offset;
int did;
};
/**
* Appends the given offset did pair to the offsets file.
*/
static int index_external_append_offset_did_pair(struct index_external *idx, int offset, int did)
{
struct offset_entry entry;
if (fseek(idx->offset_file, 0, SEEK_END) != 0)
return 0;
entry.offset = offset;
entry.did = did;
if (fwrite(&entry, 1, sizeof(entry), idx->offset_file) != sizeof(entry))
return 0;
return 1;
}
int index_external_put_document(struct index *index, int did, const char *text)
{
struct index_external *idx;
int i;
int l = strlen(text);
long offset;
idx = (struct index_external*)index;
if (!index_external_append_string(idx, text, &offset))
return 0;
if (!index_external_append_offset_did_pair(idx, offset, did))
return 0;
for (i=0; i < l; i++)
{
if (!(bnode_insert_string((struct index_external*)index, did, offset + i, text + i)))
return 0;
}
return 1;
}
int index_external_remove_document(struct index *index, int did)
{
return 0;
}
int index_external_find_documents(struct index *index, int (*callback)(int did, void *userdata), void *userdata, int num_substrings, va_list substrings)
{
int i;
int nd = 0;
struct index_external *idx;
va_list substrings_copy;
idx = (struct index_external*)index;
if (num_substrings != 1)
{
#ifdef DEBUG_FUNCTIONS
fprintf(stderr, "Searching with only one sub string is supported for now.\n");
exit(-1);
#else
return 0;
#endif
}
va_copy(substrings_copy,substrings);
for (i=0;i<num_substrings;i++)
{
nd = bnode_find_string(idx, va_arg(substrings_copy, char *), callback, userdata);
}
va_end(substrings_copy);
return nd;
}
/*****************************************************/
struct index_algorithm index_external =
{
index_external_create,
index_external_dispose,
index_external_put_document,
index_external_remove_document,
index_external_find_documents
};