forked from FooSoft/zero-epwing-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
1641 lines (1471 loc) · 49.8 KB
/
search.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) 1997-2006 Motoyuki Kasahara
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "build-pre.h"
#include "eb.h"
#include "error.h"
#include "text.h"
#include "build-post.h"
/*
* Page-ID macros.
*/
#define PAGE_ID_IS_LEAF_LAYER(page_id) (((page_id) & 0x80) == 0x80)
#define PAGE_ID_IS_LAYER_START(page_id) (((page_id) & 0x40) == 0x40)
#define PAGE_ID_IS_LAYER_END(page_id) (((page_id) & 0x20) == 0x20)
#define PAGE_ID_HAVE_GROUP_ENTRY(page_id) (((page_id) & 0x10) == 0x10)
/*
* The maximum number of hit entries for tomporary hit lists.
* This is used in eb_hit_list().
*/
#define EB_TMP_MAX_HITS 64
/*
* Book-code of the book in which you want to search a word.
*/
static EB_Book_Code cache_book_code = EB_BOOK_NONE;
/*
* Cache buffer for the current page.
*/
static char cache_buffer[EB_SIZE_PAGE];
/*
* Cache buffer for the current page.
*/
static int cache_page;
/*
* Unexported functions.
*/
static EB_Error_Code eb_hit_list_word(EB_Book *book,
EB_Search_Context *context, int max_hit_count, EB_Hit *hit_list,
int *hit_count);
static EB_Error_Code eb_hit_list_keyword(EB_Book *book,
EB_Search_Context *context, int max_hit_count, EB_Hit *hit_list,
int *hit_count);
static EB_Error_Code eb_hit_list_multi(EB_Book *book,
EB_Search_Context *context, int max_hit_count, EB_Hit *hit_list,
int *hit_count);
static void eb_and_hit_lists(EB_Hit and_list[EB_TMP_MAX_HITS],
int *and_count, int max_and_count, int hit_list_count,
EB_Hit hit_lists[EB_NUMBER_OF_SEARCH_CONTEXTS][EB_TMP_MAX_HITS],
int hit_counts[EB_NUMBER_OF_SEARCH_CONTEXTS]);
/*
* Intialize search contexts of `book'.
*/
void
eb_initialize_search_contexts(EB_Book *book)
{
EB_Search_Context *context;
int i;
LOG(("in: eb_initialize_search_context(book=%d)", (int)book->code));
for (i = 0, context = book->search_contexts;
i < EB_NUMBER_OF_SEARCH_CONTEXTS; i++, context++) {
context->code = EB_SEARCH_NONE;
context->compare_pre = NULL;
context->compare_single = NULL;
context->compare_group = NULL;
context->comparison_result = -1;
context->word[0] = '\0';
context->canonicalized_word[0] = '\0';
context->page = 0;
context->offset = 0;
context->page_id = 0;
context->entry_count = 0;
context->entry_index = 0;
context->entry_length = 0;
context->entry_arrangement = EB_ARRANGE_INVALID;
context->in_group_entry = 0;
context->keyword_heading.page = 0;
context->keyword_heading.offset = 0;
}
LOG(("out: eb_initialize_search_context()"));
}
/*
* Finalize search contexts of `book'.
*/
void
eb_finalize_search_contexts(EB_Book *book)
{
LOG(("in+out: eb_finalize_search_context(book=%d)", (int)book->code));
/* nothing to be done */
}
/*
* Reset search context of `book'.
*/
void
eb_reset_search_contexts(EB_Book *book)
{
LOG(("in: eb_reset_search_context(book=%d)", (int)book->code));
eb_initialize_search_contexts(book);
LOG(("out: eb_reset_search_context()"));
}
/*
* Intialize a search element.
*/
void
eb_initialize_search(EB_Search *search)
{
search->index_id = 0;
search->start_page = 0;
search->end_page = 0;
search->candidates_page = 0;
search->katakana = EB_INDEX_STYLE_CONVERT;
search->lower = EB_INDEX_STYLE_CONVERT;
search->mark = EB_INDEX_STYLE_DELETE;
search->long_vowel = EB_INDEX_STYLE_CONVERT;
search->double_consonant = EB_INDEX_STYLE_CONVERT;
search->contracted_sound = EB_INDEX_STYLE_CONVERT;
search->voiced_consonant = EB_INDEX_STYLE_CONVERT;
search->small_vowel = EB_INDEX_STYLE_CONVERT;
search->p_sound = EB_INDEX_STYLE_CONVERT;
search->space = EB_INDEX_STYLE_DELETE;
search->label[0] = '\0';
}
/*
* Finalize a search element.
*/
void
eb_finalize_search(EB_Search *search)
{
/* nothing to be done */
}
/*
* Initialize all search elements in the current subbook.
*/
void
eb_initialize_searches(EB_Book *book)
{
EB_Subbook *subbook;
EB_Multi_Search *multi;
EB_Search *entry;
int i, j;
LOG(("in: eb_initialize_searches(book=%d)", (int)book->code));
subbook = book->subbook_current;
eb_initialize_search(&subbook->word_alphabet);
eb_initialize_search(&subbook->word_asis);
eb_initialize_search(&subbook->word_kana);
eb_initialize_search(&subbook->endword_alphabet);
eb_initialize_search(&subbook->endword_asis);
eb_initialize_search(&subbook->endword_kana);
eb_initialize_search(&subbook->keyword);
eb_initialize_search(&subbook->cross);
eb_initialize_search(&subbook->menu);
eb_initialize_search(&subbook->image_menu);
eb_initialize_search(&subbook->copyright);
eb_initialize_search(&subbook->text);
eb_initialize_search(&subbook->sound);
for (i = 0, multi = subbook->multis; i < EB_MAX_MULTI_SEARCHES;
i++, multi++) {
eb_initialize_search(&multi->search);
multi->title[0] = '\0';
multi->entry_count = 0;
for (j = 0, entry = multi->entries;
j < EB_MAX_MULTI_ENTRIES; j++, entry++) {
eb_initialize_search(entry);
}
}
LOG(("out: eb_initialize_searches(book=%d)", (int)book->code));
}
/*
* Finalize all search elements in the current subbook.
*/
void
eb_finalize_searches(EB_Book *book)
{
EB_Subbook *subbook;
EB_Multi_Search *multi;
EB_Search *entry;
int i, j;
LOG(("in: eb_finalize_searches(book=%d)", (int)book->code));
subbook = book->subbook_current;
eb_finalize_search(&subbook->word_alphabet);
eb_finalize_search(&subbook->word_asis);
eb_finalize_search(&subbook->word_kana);
eb_finalize_search(&subbook->endword_alphabet);
eb_finalize_search(&subbook->endword_asis);
eb_finalize_search(&subbook->endword_kana);
eb_finalize_search(&subbook->keyword);
eb_finalize_search(&subbook->menu);
eb_finalize_search(&subbook->image_menu);
eb_finalize_search(&subbook->copyright);
eb_finalize_search(&subbook->text);
eb_finalize_search(&subbook->sound);
for (i = 0, multi = subbook->multis; i < EB_MAX_KEYWORDS;
i++, multi++) {
eb_finalize_search(&multi->search);
multi->entry_count = 0;
for (j = 0, entry = multi->entries;
j < multi->entry_count; j++, entry++) {
eb_finalize_search(entry);
}
}
LOG(("out: eb_finalize_searches()"));
}
/*
* Pre-search for a word described in the current search context.
* It descends intermediate indexes and reached to a leaf page that
* may have the word.
* If succeeded, 0 is returned. Otherwise -1 is returned.
*/
EB_Error_Code
eb_presearch_word(EB_Book *book, EB_Search_Context *context)
{
EB_Error_Code error_code;
int next_page;
int index_depth;
char *cache_p;
LOG(("in: eb_presearch_word(book=%d)", (int)book->code));
/*
* Discard cache data.
*/
cache_book_code = EB_BOOK_NONE;
/*
* Search the word in intermediate indexes.
* Find a page number of the leaf index page.
*/
for (index_depth = 0; index_depth < EB_MAX_INDEX_DEPTH; index_depth++) {
next_page = context->page;
/*
* Seek and read a page.
*/
if (zio_lseek(&book->subbook_current->text_zio,
((off_t) context->page - 1) * EB_SIZE_PAGE, SEEK_SET) < 0) {
cache_book_code = EB_BOOK_NONE;
error_code = EB_ERR_FAIL_SEEK_TEXT;
goto failed;
}
if (zio_read(&book->subbook_current->text_zio, cache_buffer,
EB_SIZE_PAGE) != EB_SIZE_PAGE) {
cache_book_code = EB_BOOK_NONE;
error_code = EB_ERR_FAIL_READ_TEXT;
goto failed;
}
/*
* Get some data from the read page.
*/
context->page_id = eb_uint1(cache_buffer);
context->entry_length = eb_uint1(cache_buffer + 1);
if (context->entry_length == 0)
context->entry_arrangement = EB_ARRANGE_VARIABLE;
else
context->entry_arrangement = EB_ARRANGE_FIXED;
context->entry_count = eb_uint2(cache_buffer + 2);
context->offset = 4;
cache_p = cache_buffer + 4;
LOG(("aux: eb_presearch_word(page=%d, page_id=0x%02x, \
entry_length=%d, entry_arrangement=%d, entry_count=%d)",
context->page, context->page_id, context->entry_length,
context->entry_arrangement, context->entry_count));
/*
* Exit the loop if it reached to the leaf index.
*/
if (PAGE_ID_IS_LEAF_LAYER(context->page_id))
break;
/*
* Search a page of next level index.
*/
for (context->entry_index = 0;
context->entry_index < context->entry_count;
context->entry_index++) {
if (EB_SIZE_PAGE < context->offset + context->entry_length + 4) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
if (context->compare_pre(context->canonicalized_word, cache_p,
context->entry_length) <= 0) {
next_page = eb_uint4(cache_p + context->entry_length);
break;
}
cache_p += context->entry_length + 4;
context->offset += context->entry_length + 4;
}
if (context->entry_count <= context->entry_index
|| context->page == next_page) {
context->comparison_result = -1;
goto succeeded;
}
context->page = next_page;
}
/*
* Check for the index depth.
*/
if (index_depth == EB_MAX_INDEX_DEPTH) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Update search context and cache information.
*/
context->entry_index = 0;
context->comparison_result = 1;
context->in_group_entry = 0;
cache_book_code = book->code;
cache_page = context->page;
succeeded:
LOG(("out: eb_presearch_word() = %s", eb_error_string(EB_SUCCESS)));
return EB_SUCCESS;
/*
* An error occurs...
*/
failed:
LOG(("out: eb_presearch_word() = %s", eb_error_string(error_code)));
return error_code;
}
/*
* Get hit entries of a submitted search request.
*/
EB_Error_Code
eb_hit_list(EB_Book *book, int max_hit_count, EB_Hit *hit_list, int *hit_count)
{
EB_Error_Code error_code;
EB_Search_Context temporary_context;
EB_Hit temporary_hit_lists[EB_NUMBER_OF_SEARCH_CONTEXTS][EB_TMP_MAX_HITS];
int temporary_hit_counts[EB_NUMBER_OF_SEARCH_CONTEXTS];
int more_hit_count;
int i;
/*
* Lock cache data and the book.
*/
LOG(("in: eb_hit_list(book=%d, max_hit_count=%d)", (int)book->code,
max_hit_count));
if (max_hit_count == 0)
goto succeeded;
*hit_count = 0;
/*
* Current subbook must have been set.
*/
if (book->subbook_current == NULL) {
error_code = EB_ERR_NO_CUR_SUB;
goto failed;
}
/*
* Get a list of hit entries.
*/
switch (book->search_contexts->code) {
case EB_SEARCH_EXACTWORD:
case EB_SEARCH_WORD:
case EB_SEARCH_ALL:
case EB_SEARCH_ENDWORD:
/*
* In case of exactword, word of endword search.
*/
error_code = eb_hit_list_word(book, book->search_contexts,
max_hit_count, hit_list, hit_count);
if (error_code != EB_SUCCESS)
goto failed;
break;
case EB_SEARCH_KEYWORD:
case EB_SEARCH_CROSS:
/*
* In case of keyword or cross search.
*/
for (;;) {
int search_is_over = 0;
for (i = 0; i < EB_MAX_KEYWORDS; i++) {
if (book->search_contexts[i].code != EB_SEARCH_KEYWORD
&& book->search_contexts[i].code != EB_SEARCH_CROSS)
break;
memcpy(&temporary_context, book->search_contexts + i,
sizeof(EB_Search_Context));
error_code = eb_hit_list_keyword(book, &temporary_context,
EB_TMP_MAX_HITS, temporary_hit_lists[i],
temporary_hit_counts + i);
if (error_code != EB_SUCCESS)
goto failed;
if (temporary_hit_counts[i] == 0) {
search_is_over = 1;
break;
}
}
if (search_is_over)
break;
eb_and_hit_lists(hit_list + *hit_count, &more_hit_count,
max_hit_count - *hit_count, i, temporary_hit_lists,
temporary_hit_counts);
for (i = 0; i < EB_MAX_MULTI_ENTRIES; i++) {
if (book->search_contexts[i].code != EB_SEARCH_KEYWORD
&& book->search_contexts[i].code != EB_SEARCH_CROSS)
break;
error_code = eb_hit_list_keyword(book,
book->search_contexts + i, temporary_hit_counts[i],
temporary_hit_lists[i], temporary_hit_counts + i);
if (error_code != EB_SUCCESS)
goto failed;
}
*hit_count += more_hit_count;
if (max_hit_count <= *hit_count)
break;
}
break;
case EB_SEARCH_MULTI:
/*
* In case of multi search.
*/
for (;;) {
int search_is_over = 0;
for (i = 0; i < EB_MAX_MULTI_ENTRIES; i++) {
if (book->search_contexts[i].code != EB_SEARCH_MULTI)
break;
memcpy(&temporary_context, book->search_contexts + i,
sizeof(EB_Search_Context));
error_code = eb_hit_list_multi(book, &temporary_context,
EB_TMP_MAX_HITS, temporary_hit_lists[i],
temporary_hit_counts + i);
if (error_code != EB_SUCCESS)
goto failed;
if (temporary_hit_counts[i] == 0) {
search_is_over = 1;
break;
}
}
if (search_is_over)
break;
eb_and_hit_lists(hit_list + *hit_count, &more_hit_count,
max_hit_count - *hit_count, i, temporary_hit_lists,
temporary_hit_counts);
for (i = 0; i < EB_MAX_MULTI_ENTRIES; i++) {
if (book->search_contexts[i].code != EB_SEARCH_MULTI)
break;
error_code = eb_hit_list_multi(book,
book->search_contexts + i, temporary_hit_counts[i],
temporary_hit_lists[i], temporary_hit_counts + i);
if (error_code != EB_SUCCESS)
goto failed;
}
*hit_count += more_hit_count;
if (max_hit_count <= *hit_count)
break;
}
break;
default:
/* not reached */
error_code = EB_ERR_NO_PREV_SEARCH;
goto failed;
}
/*
* Unlock cache data and the book.
*/
succeeded:
LOG(("out: eb_hit_list(hit_count=%d) = %s",
*hit_count, eb_error_string(EB_SUCCESS)));
return EB_SUCCESS;
/*
* An error occurs...
*/
failed:
*hit_count = 0;
LOG(("out: eb_hit_list() = %s", eb_error_string(error_code)));
return error_code;
}
/*
* Get hit entries of a submitted exactword/word/endword search request.
*/
static EB_Error_Code
eb_hit_list_word(EB_Book *book, EB_Search_Context *context, int max_hit_count,
EB_Hit *hit_list, int *hit_count)
{
EB_Error_Code error_code;
EB_Hit *hit;
int group_id;
char *cache_p;
LOG(("in: eb_hit_list_word(book=%d, max_hit_count=%d)", (int)book->code,
max_hit_count));
hit = hit_list;
*hit_count = 0;
/*
* If the result of previous comparison is negative value, all
* matched entries have been found.
*/
if (context->comparison_result < 0 || max_hit_count <= 0)
goto succeeded;
for (;;) {
/*
* Read a page to search, if the page is not on the cache buffer.
*
* Cache may be missed by the two reasons:
* 1. the search process reaches to the end of an index page,
* and tries to read the next page.
* 2. Someone else used the cache buffer.
*
* At the case of 1, the search process reads the page and update
* the search context. At the case of 2. it reads the page but
* must not update the context!
*/
if (cache_book_code != book->code || cache_page != context->page) {
if (zio_lseek(&book->subbook_current->text_zio,
((off_t) context->page - 1) * EB_SIZE_PAGE, SEEK_SET) < 0) {
error_code = EB_ERR_FAIL_SEEK_TEXT;
goto failed;
}
if (zio_read(&book->subbook_current->text_zio,
cache_buffer, EB_SIZE_PAGE) != EB_SIZE_PAGE) {
error_code = EB_ERR_FAIL_READ_TEXT;
goto failed;
}
/*
* Update search context.
*/
if (context->entry_index == 0) {
context->page_id = eb_uint1(cache_buffer);
context->entry_length = eb_uint1(cache_buffer + 1);
if (context->entry_length == 0)
context->entry_arrangement = EB_ARRANGE_VARIABLE;
else
context->entry_arrangement = EB_ARRANGE_FIXED;
context->entry_count = eb_uint2(cache_buffer + 2);
context->entry_index = 0;
context->offset = 4;
}
cache_book_code = book->code;
cache_page = context->page;
}
cache_p = cache_buffer + context->offset;
LOG(("aux: eb_hit_list_word(page=%d, page_id=0x%02x, \
entry_length=%d, entry_arrangement=%d, entry_count=%d)",
context->page, context->page_id, context->entry_length,
context->entry_arrangement, context->entry_count));
if (!PAGE_ID_IS_LEAF_LAYER(context->page_id)) {
/*
* Not a leaf index. It is an error.
*/
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
if (!PAGE_ID_HAVE_GROUP_ENTRY(context->page_id)
&& context->entry_arrangement == EB_ARRANGE_FIXED) {
/*
* The leaf index doesn't have a group entry.
* Find text and heading locations.
*/
while (context->entry_index < context->entry_count) {
if (EB_SIZE_PAGE
< context->offset + context->entry_length + 12) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Compare word and pattern.
* If matched, add it to a hit list.
*/
context->comparison_result
= context->compare_single(context->word, cache_p,
context->entry_length);
if (context->comparison_result == 0) {
hit->heading.page
= eb_uint4(cache_p + context->entry_length + 6);
hit->heading.offset
= eb_uint2(cache_p + context->entry_length + 10);
hit->text.page
= eb_uint4(cache_p + context->entry_length);
hit->text.offset
= eb_uint2(cache_p + context->entry_length + 4);
hit++;
*hit_count += 1;
}
context->entry_index++;
context->offset += context->entry_length + 12;
cache_p += context->entry_length + 12;
if (context->comparison_result < 0
|| max_hit_count <= *hit_count)
goto succeeded;
}
} else if (!PAGE_ID_HAVE_GROUP_ENTRY(context->page_id)
&& context->entry_arrangement == EB_ARRANGE_VARIABLE) {
/*
* The leaf index doesn't have a group entry.
* Find text and heading locations.
*/
while (context->entry_index < context->entry_count) {
if (EB_SIZE_PAGE < context->offset + 1) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
context->entry_length = eb_uint1(cache_p);
if (EB_SIZE_PAGE
< context->offset + context->entry_length + 13) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Compare word and pattern.
* If matched, add it to a hit list.
*/
context->comparison_result
= context->compare_single(context->word, cache_p + 1,
context->entry_length);
if (context->comparison_result == 0) {
hit->heading.page
= eb_uint4(cache_p + context->entry_length + 7);
hit->heading.offset
= eb_uint2(cache_p + context->entry_length + 11);
hit->text.page
= eb_uint4(cache_p + context->entry_length + 1);
hit->text.offset
= eb_uint2(cache_p + context->entry_length + 5);
hit++;
*hit_count += 1;
}
context->entry_index++;
context->offset += context->entry_length + 13;
cache_p += context->entry_length + 13;
if (context->comparison_result < 0
|| max_hit_count <= *hit_count)
goto succeeded;
}
} else {
/*
* The leaf index have a group entry.
* Find text and heading locations.
*/
while (context->entry_index < context->entry_count) {
if (EB_SIZE_PAGE < context->offset + 2) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
group_id = eb_uint1(cache_p);
if (group_id == 0x00) {
/*
* 0x00 -- Single entry.
*/
context->entry_length = eb_uint1(cache_p + 1);
if (EB_SIZE_PAGE
< context->offset + context->entry_length + 14) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Compare word and pattern.
* If matched, add it to a hit list.
*/
context->comparison_result
= context->compare_single(context->canonicalized_word,
cache_p + 2, context->entry_length);
if (context->comparison_result == 0) {
hit->heading.page
= eb_uint4(cache_p + context->entry_length + 8);
hit->heading.offset
= eb_uint2(cache_p + context->entry_length + 12);
hit->text.page
= eb_uint4(cache_p + context->entry_length + 2);
hit->text.offset
= eb_uint2(cache_p + context->entry_length + 6);
hit++;
*hit_count += 1;
}
context->in_group_entry = 0;
context->offset += context->entry_length + 14;
cache_p += context->entry_length + 14;
} else if (group_id == 0x80) {
/*
* 0x80 -- Start of group entry.
*/
context->entry_length = eb_uint1(cache_p + 1);
if (EB_SIZE_PAGE
< context->offset + context->entry_length + 4) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
context->comparison_result
= context->compare_single(context->canonicalized_word,
cache_p + 4, context->entry_length);
context->in_group_entry = 1;
cache_p += context->entry_length + 4;
context->offset += context->entry_length + 4;
} else if (group_id == 0xc0) {
/*
* Element of the group entry
*/
context->entry_length = eb_uint1(cache_p + 1);
if (EB_SIZE_PAGE < context->offset + 14) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Compare word and pattern.
* If matched, add it to a hit list.
*/
if (context->comparison_result == 0
&& context->in_group_entry
&& context->compare_group(context->word, cache_p + 2,
context->entry_length) == 0) {
hit->heading.page
= eb_uint4(cache_p + context->entry_length + 8);
hit->heading.offset
= eb_uint2(cache_p + context->entry_length + 12);
hit->text.page
= eb_uint4(cache_p + context->entry_length + 2);
hit->text.offset
= eb_uint2(cache_p + context->entry_length + 6);
hit++;
*hit_count += 1;
}
context->offset += context->entry_length + 14;
cache_p += context->entry_length + 14;
} else {
/*
* Unknown group ID.
*/
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
context->entry_index++;
if (context->comparison_result < 0
|| max_hit_count <= *hit_count)
goto succeeded;
}
}
/*
* Go to a next page if available.
*/
if (PAGE_ID_IS_LAYER_END(context->page_id)) {
context->comparison_result = -1;
goto succeeded;
}
context->page++;
context->entry_index = 0;
}
succeeded:
LOG(("out: eb_hit_list_word(hit_count=%d) = %s",
*hit_count, eb_error_string(EB_SUCCESS)));
return EB_SUCCESS;
/*
* An error occurs...
* Discard cache if read error occurs.
*/
failed:
if (error_code == EB_ERR_FAIL_READ_TEXT)
cache_book_code = EB_BOOK_NONE;
*hit_count = 0;
LOG(("out: eb_hit_list_word() = %s", eb_error_string(error_code)));
return error_code;
}
/*
* Get hit entries of a submitted keyword search request.
*/
static EB_Error_Code
eb_hit_list_keyword(EB_Book *book, EB_Search_Context *context,
int max_hit_count, EB_Hit *hit_list, int *hit_count)
{
EB_Error_Code error_code;
EB_Text_Context text_context;
EB_Hit *hit;
int group_id;
char *cache_p;
LOG(("in: eb_hit_list_keyword(book=%d, max_hit_count=%d)",
(int)book->code, max_hit_count));
hit = hit_list;
*hit_count = 0;
/*
* Backup the text context in `book'.
*/
memcpy(&text_context, &book->text_context, sizeof(EB_Text_Context));
/*
* Seek text file.
*/
if (context->in_group_entry && context->comparison_result == 0) {
error_code = eb_seek_text(book, &context->keyword_heading);
if (error_code != EB_SUCCESS)
goto failed;
}
/*
* If the result of previous comparison is negative value, all
* matched entries have been found.
*/
if (context->comparison_result < 0 || max_hit_count <= 0)
goto succeeded;
for (;;) {
/*
* Read a page to search, if the page is not on the cache buffer.
*
* Cache may be missed by the two reasons:
* 1. the search process reaches to the end of an index page,
* and tries to read the next page.
* 2. Someone else used the cache buffer.
*
* At the case of 1, the search process reads the page and update
* the search context. At the case of 2. it reads the page but
* must not update the context!
*/
if (cache_book_code != book->code || cache_page != context->page) {
if (zio_lseek(&book->subbook_current->text_zio,
((off_t) context->page - 1) * EB_SIZE_PAGE, SEEK_SET) < 0) {
error_code = EB_ERR_FAIL_SEEK_TEXT;
goto failed;
}
if (zio_read(&book->subbook_current->text_zio, cache_buffer,
EB_SIZE_PAGE) != EB_SIZE_PAGE) {
error_code = EB_ERR_FAIL_READ_TEXT;
goto failed;
}
/*
* Update search context.
*/
if (context->entry_index == 0) {
context->page_id = eb_uint1(cache_buffer);
context->entry_length = eb_uint1(cache_buffer + 1);
if (context->entry_length == 0)
context->entry_arrangement = EB_ARRANGE_VARIABLE;
else
context->entry_arrangement = EB_ARRANGE_FIXED;
context->entry_count = eb_uint2(cache_buffer + 2);
context->entry_index = 0;
context->offset = 4;
}
cache_book_code = book->code;
cache_page = context->page;
}
cache_p = cache_buffer + context->offset;
LOG(("aux: eb_hit_list_keyword(page=%d, page_id=0x%02x, \
entry_length=%d, entry_arrangement=%d, entry_count=%d)",
context->page, context->page_id, context->entry_length,
context->entry_arrangement, context->entry_count));
if (!PAGE_ID_IS_LEAF_LAYER(context->page_id)) {
/*
* Not a leaf index. It is an error.
*/
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
if (!PAGE_ID_HAVE_GROUP_ENTRY(context->page_id)
&& context->entry_arrangement == EB_ARRANGE_FIXED) {
/*
* The leaf index doesn't have a group entry.
* Find text and heading locations.
*/
while (context->entry_index < context->entry_count) {
if (EB_SIZE_PAGE
< context->offset + context->entry_length + 12) {
error_code = EB_ERR_UNEXP_TEXT;
goto failed;
}
/*
* Compare word and pattern.
* If matched, add it to a hit list.
*/
context->comparison_result
= context->compare_single(context->word, cache_p,
context->entry_length);
if (context->comparison_result == 0) {
hit->heading.page
= eb_uint4(cache_p + context->entry_length + 6);
hit->heading.offset
= eb_uint2(cache_p + context->entry_length + 10);
hit->text.page
= eb_uint4(cache_p + context->entry_length);
hit->text.offset
= eb_uint2(cache_p + context->entry_length + 4);
hit++;
*hit_count += 1;
}
context->entry_index++;
context->offset += context->entry_length + 12;
cache_p += context->entry_length + 12;
if (context->comparison_result < 0
|| max_hit_count <= *hit_count)