isomorfeus-ferret 0.13.7 → 0.13.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -4
  3. data/ext/isomorfeus_ferret_ext/bm_store.c +3 -3
  4. data/ext/isomorfeus_ferret_ext/frb_analysis.c +7 -4
  5. data/ext/isomorfeus_ferret_ext/frb_index.c +18 -24
  6. data/ext/isomorfeus_ferret_ext/frb_qparser.c +2 -1
  7. data/ext/isomorfeus_ferret_ext/frb_search.c +23 -19
  8. data/ext/isomorfeus_ferret_ext/frb_store.c +34 -36
  9. data/ext/isomorfeus_ferret_ext/frt_analysis.c +4 -4
  10. data/ext/isomorfeus_ferret_ext/frt_analysis.h +9 -9
  11. data/ext/isomorfeus_ferret_ext/frt_bitvector.c +1 -1
  12. data/ext/isomorfeus_ferret_ext/frt_bitvector.h +1 -1
  13. data/ext/isomorfeus_ferret_ext/frt_compound_io.c +41 -66
  14. data/ext/isomorfeus_ferret_ext/frt_config.h +8 -0
  15. data/ext/isomorfeus_ferret_ext/frt_except.c +1 -0
  16. data/ext/isomorfeus_ferret_ext/frt_field_index.c +1 -1
  17. data/ext/isomorfeus_ferret_ext/frt_filter.c +2 -4
  18. data/ext/isomorfeus_ferret_ext/frt_fs_store.c +24 -19
  19. data/ext/isomorfeus_ferret_ext/frt_global.c +6 -4
  20. data/ext/isomorfeus_ferret_ext/frt_global.h +1 -1
  21. data/ext/isomorfeus_ferret_ext/frt_hash.c +40 -48
  22. data/ext/isomorfeus_ferret_ext/frt_hash.h +14 -16
  23. data/ext/isomorfeus_ferret_ext/frt_ind.c +3 -4
  24. data/ext/isomorfeus_ferret_ext/frt_index.c +152 -222
  25. data/ext/isomorfeus_ferret_ext/frt_index.h +31 -31
  26. data/ext/isomorfeus_ferret_ext/frt_lang.c +1 -4
  27. data/ext/isomorfeus_ferret_ext/frt_multimapper.c +8 -9
  28. data/ext/isomorfeus_ferret_ext/frt_multimapper.h +1 -1
  29. data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +7 -7
  30. data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +1 -1
  31. data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +8 -3
  32. data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +2 -2
  33. data/ext/isomorfeus_ferret_ext/frt_q_parser.c +1742 -1742
  34. data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +2 -3
  35. data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +1 -1
  36. data/ext/isomorfeus_ferret_ext/frt_q_range.c +1 -1
  37. data/ext/isomorfeus_ferret_ext/frt_q_span.c +12 -11
  38. data/ext/isomorfeus_ferret_ext/frt_q_term.c +2 -2
  39. data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +1 -1
  40. data/ext/isomorfeus_ferret_ext/frt_ram_store.c +24 -40
  41. data/ext/isomorfeus_ferret_ext/frt_search.c +30 -29
  42. data/ext/isomorfeus_ferret_ext/frt_search.h +18 -19
  43. data/ext/isomorfeus_ferret_ext/frt_sort.c +1 -1
  44. data/ext/isomorfeus_ferret_ext/frt_store.c +47 -40
  45. data/ext/isomorfeus_ferret_ext/frt_store.h +45 -47
  46. data/ext/isomorfeus_ferret_ext/frt_threading.h +12 -5
  47. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +4 -3
  48. data/ext/isomorfeus_ferret_ext/test_1710.c +1 -2
  49. data/ext/isomorfeus_ferret_ext/test_compound_io.c +8 -8
  50. data/ext/isomorfeus_ferret_ext/test_fields.c +7 -7
  51. data/ext/isomorfeus_ferret_ext/test_file_deleter.c +1 -1
  52. data/ext/isomorfeus_ferret_ext/test_filter.c +5 -4
  53. data/ext/isomorfeus_ferret_ext/test_fs_store.c +1 -1
  54. data/ext/isomorfeus_ferret_ext/test_highlighter.c +4 -2
  55. data/ext/isomorfeus_ferret_ext/test_index.c +63 -63
  56. data/ext/isomorfeus_ferret_ext/test_q_const_score.c +3 -2
  57. data/ext/isomorfeus_ferret_ext/test_q_filtered.c +4 -3
  58. data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +4 -2
  59. data/ext/isomorfeus_ferret_ext/test_q_span.c +9 -2
  60. data/ext/isomorfeus_ferret_ext/test_ram_store.c +4 -4
  61. data/ext/isomorfeus_ferret_ext/test_search.c +10 -5
  62. data/ext/isomorfeus_ferret_ext/test_segments.c +4 -3
  63. data/ext/isomorfeus_ferret_ext/test_sort.c +18 -10
  64. data/ext/isomorfeus_ferret_ext/test_store.c +1 -1
  65. data/ext/isomorfeus_ferret_ext/test_term.c +5 -3
  66. data/ext/isomorfeus_ferret_ext/test_term_vectors.c +2 -2
  67. data/ext/isomorfeus_ferret_ext/test_threading.c +5 -4
  68. data/lib/isomorfeus/ferret/index/index.rb +8 -3
  69. data/lib/isomorfeus/ferret/version.rb +1 -1
  70. metadata +16 -2
@@ -11,8 +11,8 @@
11
11
  #include "bzlib.h"
12
12
  #include "lz4frame.h"
13
13
 
14
- #undef close
15
- #undef read
14
+ // #undef close
15
+ // #undef read
16
16
 
17
17
  extern rb_encoding *utf8_encoding;
18
18
  extern void frt_micro_sleep(const int micro_seconds);
@@ -313,11 +313,8 @@ FrtFieldInfo *frt_fi_new(ID name, FrtStoreValue store, FrtCompressionType compre
313
313
  return frt_fi_init(fi, name, store, compression, index, term_vector);
314
314
  }
315
315
 
316
- void frt_fi_deref(FrtFieldInfo *fi)
317
- {
318
- if (0 == --(fi->ref_cnt)) {
319
- free(fi);
320
- }
316
+ void frt_fi_deref(FrtFieldInfo *fi) {
317
+ if (FRT_DEREF(fi) == 0) free(fi);
321
318
  }
322
319
 
323
320
  FrtCompressionType frt_fi_get_compression(FrtFieldInfo *fi) {
@@ -369,13 +366,13 @@ FrtFieldInfos *frt_fis_alloc(void) {
369
366
  return FRT_ALLOC(FrtFieldInfos);
370
367
  }
371
368
 
372
- FrtFieldInfos *frt_fis_init(FrtFieldInfos *fis, FrtStoreValue store, FrtCompressionType compression, FrtIndexValue index, FrtTermVectorValue term_vector) {
373
- fi_check_params(store, compression, index, term_vector);
369
+ FrtFieldInfos *frt_fis_init(FrtFieldInfos *fis, FrtStoreValue store_val, FrtCompressionType compression, FrtIndexValue index, FrtTermVectorValue term_vector) {
370
+ fi_check_params(store_val, compression, index, term_vector);
374
371
  fis->field_dict = frt_h_new_ptr((frt_free_ft)&frt_fi_deref);
375
372
  fis->size = 0;
376
373
  fis->capa = FIELD_INFOS_INIT_CAPA;
377
374
  fis->fields = FRT_ALLOC_N(FrtFieldInfo *, fis->capa);
378
- fis->store = store;
375
+ fis->store_val = store_val;
379
376
  fis->compression = compression;
380
377
  fis->index = index;
381
378
  fis->term_vector = term_vector;
@@ -416,7 +413,7 @@ int frt_fis_get_field_num(FrtFieldInfos *fis, ID name) {
416
413
  FrtFieldInfo *frt_fis_get_or_add_field(FrtFieldInfos *fis, ID name) {
417
414
  FrtFieldInfo *fi = (FrtFieldInfo *)frt_h_get(fis->field_dict, (void *)name);
418
415
  if (!fi) {
419
- fi = (FrtFieldInfo*)frt_fi_new(name, fis->store, fis->compression, fis->index, fis->term_vector);
416
+ fi = (FrtFieldInfo*)frt_fi_new(name, fis->store_val, fis->compression, fis->index, fis->term_vector);
420
417
  frt_fis_add_field(fis, fi);
421
418
  }
422
419
  return fi;
@@ -465,7 +462,7 @@ void frt_fis_write(FrtFieldInfos *fis, FrtOutStream *os)
465
462
  FrtFieldInfo *fi;
466
463
  const int fis_size = fis->size;
467
464
 
468
- frt_os_write_vint(os, fis->store);
465
+ frt_os_write_vint(os, fis->store_val);
469
466
  frt_os_write_vint(os, fis->index);
470
467
  frt_os_write_vint(os, fis->term_vector);
471
468
  frt_os_write_vint(os, fis->size);
@@ -537,7 +534,7 @@ char *frt_fis_to_s(FrtFieldInfos *fis)
537
534
  " index: %s\n"
538
535
  " term_vector: %s\n"
539
536
  "fields:\n",
540
- store_str[fis->store],
537
+ store_str[fis->store_val],
541
538
  index_str[fis->index],
542
539
  term_vector_str[fis->term_vector]);
543
540
  for (i = 0; i < fis_size; i++) {
@@ -555,9 +552,8 @@ char *frt_fis_to_s(FrtFieldInfos *fis)
555
552
  return buf;
556
553
  }
557
554
 
558
- void frt_fis_deref(FrtFieldInfos *fis)
559
- {
560
- if (0 == --(fis->ref_cnt)) {
555
+ void frt_fis_deref(FrtFieldInfos *fis) {
556
+ if (FRT_DEREF(fis) == 0) {
561
557
  frt_h_destroy(fis->field_dict);
562
558
  free(fis->fields);
563
559
  free(fis);
@@ -589,6 +585,7 @@ FrtSegmentInfo *frt_si_new(char *name, int doc_cnt, FrtStore *store)
589
585
  si->name = name;
590
586
  si->doc_cnt = doc_cnt;
591
587
  si->store = store;
588
+ FRT_REF(store);
592
589
  si->del_gen = -1;
593
590
  si->norm_gens = NULL;
594
591
  si->norm_gens_size = 0;
@@ -602,6 +599,7 @@ static FrtSegmentInfo *si_read(FrtStore *store, FrtInStream *is)
602
599
  FrtSegmentInfo *volatile si = FRT_ALLOC_AND_ZERO(FrtSegmentInfo);
603
600
  FRT_TRY
604
601
  si->store = store;
602
+ FRT_REF(store);
605
603
  si->name = frt_is_read_string_safe(is);
606
604
  si->doc_cnt = frt_is_read_vint(is);
607
605
  si->del_gen = frt_is_read_vint(is);
@@ -616,6 +614,7 @@ static FrtSegmentInfo *si_read(FrtStore *store, FrtInStream *is)
616
614
  }
617
615
  si->use_compound_file = (bool)frt_is_read_byte(is);
618
616
  FRT_XCATCHALL
617
+ frt_store_close(si->store);
619
618
  free(si->name);
620
619
  free(si);
621
620
  FRT_XENDTRY
@@ -637,9 +636,9 @@ static void si_write(FrtSegmentInfo *si, FrtOutStream *os)
637
636
  frt_os_write_byte(os, (frt_uchar)si->use_compound_file);
638
637
  }
639
638
 
640
- void frt_si_deref(FrtSegmentInfo *si)
641
- {
642
- if (--si->ref_cnt <= 0) {
639
+ void frt_si_close(FrtSegmentInfo *si) {
640
+ if (FRT_DEREF(si) == 0) {
641
+ frt_store_close(si->store);
643
642
  free(si->name);
644
643
  free(si->norm_gens);
645
644
  free(si);
@@ -760,8 +759,7 @@ static void which_gen_i(const char *file_name, void *arg)
760
759
  }
761
760
  }
762
761
 
763
- static void si_put(FrtSegmentInfo *si, FILE *stream)
764
- {
762
+ static void si_put(FrtSegmentInfo *si, FILE *stream) {
765
763
  int i;
766
764
  fprintf(stream, "\tSegmentInfo {\n");
767
765
  fprintf(stream, "\t\tname = %s\n", si->name);
@@ -777,8 +775,7 @@ static void si_put(FrtSegmentInfo *si, FILE *stream)
777
775
  fprintf(stream, "\t}\n");
778
776
  }
779
777
 
780
- void frt_sis_put(FrtSegmentInfos *sis, FILE *stream)
781
- {
778
+ void frt_sis_put(FrtSegmentInfos *sis, FILE *stream) {
782
779
  int i;
783
780
  fprintf(stream, "SegmentInfos {\n");
784
781
  fprintf(stream, "\tcounter = %"POSH_I64_PRINTF_PREFIX"d\n", sis->counter);
@@ -860,7 +857,6 @@ static void sis_find_segments_file(FrtStore *store, FindSegmentsFile *fsf, void
860
857
  if (0 == method) {
861
858
  gen = frt_sis_current_segment_generation(store);
862
859
  if (gen == -1) {
863
- // fprintf(stderr, ">>\n%s\n>>\n", frt_store_to_s(store));
864
860
  FRT_RAISE(FRT_FILE_NOT_FOUND_ERROR, "couldn't find segments file");
865
861
  }
866
862
  }
@@ -946,32 +942,6 @@ static void sis_find_segments_file(FrtStore *store, FindSegmentsFile *fsf, void
946
942
  return;
947
943
  case FRT_IO_ERROR: case FRT_FILE_NOT_FOUND_ERROR: case FRT_EOF_ERROR:
948
944
  FRT_HANDLED();
949
- /*
950
- if (gen != frt_sis_current_segment_generation(store)) {
951
- fprintf(stderr, "%lld != %lld\n",
952
- gen, frt_sis_current_segment_generation(store));
953
- fprintf(stderr, "%s\n", xcontext.msg);
954
- }
955
- else {
956
- char *sl = frt_store_to_s(store);
957
- bool done = false;
958
- fprintf(stderr, "%s\n>>>\n%s", xcontext.msg, sl);
959
- free(sl);
960
- while (!done) {
961
- FRT_TRY
962
- frt_sis_put(frt_sis_read(store), stderr);
963
- done = true;
964
- FRT_XCATCHALL
965
- FRT_HANDLED();
966
- FRT_XENDTRY
967
- }
968
- }
969
-
970
- char *sl = frt_store_to_s(store);
971
- fprintf(stderr, "%s\n>>>\n%s", xcontext.msg, sl);
972
- free(sl);
973
- */
974
-
975
945
  /* Save the original root cause: */
976
946
  /* TODO:LOG "primary Exception on '" + segmentFileName + "': " +
977
947
  * err + "'; will retry: retry=" + retry + "; gen = " + gen */
@@ -1031,8 +1001,9 @@ void frt_sis_destroy(FrtSegmentInfos *sis)
1031
1001
  int i;
1032
1002
  const int sis_size = sis->size;
1033
1003
  for (i = 0; i < sis_size; i++) {
1034
- frt_si_deref(sis->segs[i]);
1004
+ frt_si_close(sis->segs[i]);
1035
1005
  }
1006
+ if (sis->store) frt_store_close(sis->store);
1036
1007
  if (sis->fis) frt_fis_deref(sis->fis);
1037
1008
  free(sis->segs);
1038
1009
  free(sis);
@@ -1052,7 +1023,7 @@ void frt_sis_del_at(FrtSegmentInfos *sis, int at)
1052
1023
  {
1053
1024
  int i;
1054
1025
  const int sis_size = --(sis->size);
1055
- frt_si_deref(sis->segs[at]);
1026
+ frt_si_close(sis->segs[at]);
1056
1027
  for (i = at; i < sis_size; i++) {
1057
1028
  sis->segs[i] = sis->segs[i+1];
1058
1029
  }
@@ -1063,7 +1034,7 @@ void frt_sis_del_from_to(FrtSegmentInfos *sis, int from, int to)
1063
1034
  int i, num_to_del = to - from;
1064
1035
  const int sis_size = sis->size -= num_to_del;
1065
1036
  for (i = from; i < to; i++) {
1066
- frt_si_deref(sis->segs[i]);
1037
+ frt_si_close(sis->segs[i]);
1067
1038
  }
1068
1039
  for (i = from; i < sis_size; i++) {
1069
1040
  sis->segs[i] = sis->segs[i+num_to_del];
@@ -1083,6 +1054,7 @@ static void frt_sis_read_i(FrtStore *store, FindSegmentsFile *fsf, FrtIndexReade
1083
1054
  FRT_TRY
1084
1055
  is = store->open_input(store, seg_file_name);
1085
1056
  sis->store = store;
1057
+ FRT_REF(store);
1086
1058
  sis->generation = fsf->generation;
1087
1059
  sis->format = frt_is_read_u32(is); /* do nothing. it's the first version */
1088
1060
  sis->version = frt_is_read_u64(is);
@@ -1530,7 +1502,6 @@ FrtLazyDocField *frt_lazy_doc_get(FrtLazyDoc *self, ID field) {
1530
1502
 
1531
1503
  FrtFieldsReader *frt_fr_open(FrtStore *store, const char *segment, FrtFieldInfos *fis) {
1532
1504
  FrtFieldsReader *fr = FRT_ALLOC(FrtFieldsReader);
1533
- FrtInStream *fdx_in;
1534
1505
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
1535
1506
  size_t segment_len = strlen(segment);
1536
1507
 
@@ -1541,9 +1512,10 @@ FrtFieldsReader *frt_fr_open(FrtStore *store, const char *segment, FrtFieldInfos
1541
1512
  strcpy(file_name + segment_len, ".fdt");
1542
1513
  fr->fdt_in = store->open_input(store, file_name);
1543
1514
  strcpy(file_name + segment_len, ".fdx");
1544
- fdx_in = fr->fdx_in = store->open_input(store, file_name);
1545
- fr->size = frt_is_length(fdx_in) / FIELDS_IDX_PTR_SIZE;
1515
+ fr->fdx_in = store->open_input(store, file_name);
1516
+ fr->size = frt_is_length(fr->fdx_in) / FIELDS_IDX_PTR_SIZE;
1546
1517
  fr->store = store;
1518
+ FRT_REF(store);
1547
1519
 
1548
1520
  return fr;
1549
1521
  }
@@ -1552,6 +1524,7 @@ FrtFieldsReader *frt_fr_clone(FrtFieldsReader *orig) {
1552
1524
  FrtFieldsReader *fr = FRT_ALLOC(FrtFieldsReader);
1553
1525
 
1554
1526
  memcpy(fr, orig, sizeof(FrtFieldsReader));
1527
+ FRT_REF(fr->store);
1555
1528
  fr->fdx_in = frt_is_clone(orig->fdx_in);
1556
1529
  fr->fdt_in = frt_is_clone(orig->fdt_in);
1557
1530
 
@@ -1561,6 +1534,7 @@ FrtFieldsReader *frt_fr_clone(FrtFieldsReader *orig) {
1561
1534
  void frt_fr_close(FrtFieldsReader *fr) {
1562
1535
  frt_is_close(fr->fdt_in);
1563
1536
  frt_is_close(fr->fdx_in);
1537
+ frt_store_close(fr->store);
1564
1538
  free(fr);
1565
1539
  }
1566
1540
 
@@ -1591,7 +1565,7 @@ static void frt_fr_read_compressed_fields(FrtFieldsReader *fr, FrtDocField *df,
1591
1565
  FrtDocument *frt_fr_get_doc(FrtFieldsReader *fr, int doc_num)
1592
1566
  {
1593
1567
  int i, j;
1594
- off_t pos;
1568
+ frt_off_t pos;
1595
1569
  int stored_cnt;
1596
1570
  FrtDocument *doc = frt_doc_new();
1597
1571
  FrtInStream *fdx_in = fr->fdx_in;
@@ -1638,7 +1612,7 @@ FrtLazyDoc *frt_fr_get_lazy_doc(FrtFieldsReader *fr, int doc_num)
1638
1612
  {
1639
1613
  int start = 0;
1640
1614
  int i, j;
1641
- off_t pos;
1615
+ frt_off_t pos;
1642
1616
  int stored_cnt;
1643
1617
  FrtLazyDoc *lazy_doc;
1644
1618
  FrtInStream *fdx_in = fr->fdx_in;
@@ -1668,7 +1642,7 @@ FrtLazyDoc *frt_fr_get_lazy_doc(FrtFieldsReader *fr, int doc_num)
1668
1642
  lazy_doc_add_field(lazy_doc, lazy_df, i);
1669
1643
  }
1670
1644
  /* correct the starts to their correct absolute positions */
1671
- const off_t abs_start = frt_is_pos(fdt_in);
1645
+ const frt_off_t abs_start = frt_is_pos(fdt_in);
1672
1646
  for (i = 0; i < stored_cnt; i++) {
1673
1647
  FrtLazyDocField *lazy_df = lazy_doc->fields[i];
1674
1648
  const int df_size = lazy_df->size;
@@ -1745,7 +1719,7 @@ FrtHash *frt_fr_get_tv(FrtFieldsReader *fr, int doc_num)
1745
1719
  int i;
1746
1720
  FrtInStream *fdx_in = fr->fdx_in;
1747
1721
  FrtInStream *fdt_in = fr->fdt_in;
1748
- off_t data_ptr, field_index_ptr;
1722
+ frt_off_t data_ptr, field_index_ptr;
1749
1723
  int field_cnt;
1750
1724
  int *field_nums;
1751
1725
 
@@ -1778,13 +1752,12 @@ FrtHash *frt_fr_get_tv(FrtFieldsReader *fr, int doc_num)
1778
1752
  return term_vectors;
1779
1753
  }
1780
1754
 
1781
- FrtTermVector *frt_fr_get_field_tv(FrtFieldsReader *fr, int doc_num, int field_num)
1782
- {
1755
+ FrtTermVector *frt_fr_get_field_tv(FrtFieldsReader *fr, int doc_num, int field_num) {
1783
1756
  FrtTermVector *tv = NULL;
1784
1757
 
1785
1758
  if (doc_num >= 0 && doc_num < fr->size) {
1786
1759
  int i, fnum = -1;
1787
- off_t field_index_ptr;
1760
+ frt_off_t field_index_ptr;
1788
1761
  int field_cnt;
1789
1762
  int offset = 0;
1790
1763
  FrtInStream *fdx_in = fr->fdx_in;
@@ -2065,7 +2038,7 @@ void frt_fw_add_postings(FrtFieldsWriter *fw,
2065
2038
  int i, delta_start, delta_length;
2066
2039
  const char *last_term = FRT_EMPTY_STRING;
2067
2040
  FrtOutStream *fdt_out = fw->fdt_out;
2068
- off_t fdt_start_pos = frt_os_pos(fdt_out);
2041
+ frt_off_t fdt_start_pos = frt_os_pos(fdt_out);
2069
2042
  FrtPostingList *plist;
2070
2043
  FrtPosting *posting;
2071
2044
  FrtOccurence *occ;
@@ -2175,13 +2148,11 @@ static void sti_destroy(FrtSegmentTermIndex *sti)
2175
2148
  free(sti);
2176
2149
  }
2177
2150
 
2178
- static void sti_ensure_index_is_read(FrtSegmentTermIndex *sti,
2179
- FrtTermEnum *index_te)
2180
- {
2151
+ static void sti_ensure_index_is_read(FrtSegmentTermIndex *sti, FrtTermEnum *index_te) {
2181
2152
  if (NULL == sti->index_terms) {
2182
2153
  int i;
2183
2154
  int index_cnt = sti->index_cnt;
2184
- off_t index_ptr = 0;
2155
+ frt_off_t index_ptr = 0;
2185
2156
  ste_reset(index_te);
2186
2157
  frt_is_seek(STE(index_te)->is, sti->index_ptr);
2187
2158
  STE(index_te)->size = sti->index_cnt;
@@ -2241,8 +2212,7 @@ static int sti_get_index_offset(FrtSegmentTermIndex *sti, const char *term)
2241
2212
  }\
2242
2213
  } while (0)
2243
2214
 
2244
- FrtSegmentFieldIndex *frt_sfi_open(FrtStore *store, const char *segment)
2245
- {
2215
+ FrtSegmentFieldIndex *frt_sfi_open(FrtStore *store, const char *segment) {
2246
2216
  int field_count;
2247
2217
  FrtSegmentFieldIndex *sfi = FRT_ALLOC(FrtSegmentFieldIndex);
2248
2218
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
@@ -2271,12 +2241,12 @@ FrtSegmentFieldIndex *frt_sfi_open(FrtStore *store, const char *segment)
2271
2241
 
2272
2242
  sprintf(file_name, "%s.tix", segment);
2273
2243
  is = store->open_input(store, file_name);
2244
+ FRT_DEREF(is);
2274
2245
  sfi->index_te = frt_ste_new(is, sfi);
2275
2246
  return sfi;
2276
2247
  }
2277
2248
 
2278
- void frt_sfi_close(FrtSegmentFieldIndex *sfi)
2279
- {
2249
+ void frt_sfi_close(FrtSegmentFieldIndex *sfi) {
2280
2250
  frt_mutex_destroy(&sfi->mutex);
2281
2251
  frt_ste_close(sfi->index_te);
2282
2252
  frt_h_destroy(sfi->field_dict);
@@ -2287,8 +2257,7 @@ void frt_sfi_close(FrtSegmentFieldIndex *sfi)
2287
2257
  * SegmentTermEnum
2288
2258
  ****************************************************************************/
2289
2259
 
2290
- static int term_read(char *buf, FrtInStream *is)
2291
- {
2260
+ static int term_read(char *buf, FrtInStream *is) {
2292
2261
  int start = (int)frt_is_read_vint(is);
2293
2262
  int length = (int)frt_is_read_vint(is);
2294
2263
  int total_length = start + length;
@@ -2297,8 +2266,7 @@ static int term_read(char *buf, FrtInStream *is)
2297
2266
  return total_length;
2298
2267
  }
2299
2268
 
2300
- static char *ste_next(FrtTermEnum *te)
2301
- {
2269
+ static char *ste_next(FrtTermEnum *te) {
2302
2270
  FrtTermInfo *ti;
2303
2271
  FrtInStream *is = STE(te)->is;
2304
2272
 
@@ -2323,32 +2291,27 @@ static char *ste_next(FrtTermEnum *te)
2323
2291
  return te->curr_term;
2324
2292
  }
2325
2293
 
2326
- static void ste_reset(FrtTermEnum *te)
2327
- {
2294
+ static void ste_reset(FrtTermEnum *te) {
2328
2295
  STE(te)->pos = -1;
2329
2296
  te->curr_term[0] = '\0';
2330
2297
  te->curr_term_len = 0;
2331
2298
  FRT_ZEROSET(&(te->curr_ti), FrtTermInfo);
2332
2299
  }
2333
2300
 
2334
- static FrtTermEnum *ste_set_field(FrtTermEnum *te, int field_num)
2335
- {
2336
- FrtSegmentTermIndex *sti
2337
- = (FrtSegmentTermIndex *)frt_h_get_int(STE(te)->sfi->field_dict, field_num);
2301
+ static FrtTermEnum *ste_set_field(FrtTermEnum *te, int field_num) {
2302
+ FrtSegmentTermIndex *sti = (FrtSegmentTermIndex *)frt_h_get_int(STE(te)->sfi->field_dict, field_num);
2338
2303
  ste_reset(te);
2339
2304
  te->field_num = field_num;
2340
2305
  if (sti) {
2341
2306
  STE(te)->size = sti->size;
2342
2307
  frt_is_seek(STE(te)->is, sti->ptr);
2343
- }
2344
- else {
2308
+ } else {
2345
2309
  STE(te)->size = 0;
2346
2310
  }
2347
2311
  return te;
2348
2312
  }
2349
2313
 
2350
- static void frt_ste_index_seek(FrtTermEnum *te, FrtSegmentTermIndex *sti, int idx_offset)
2351
- {
2314
+ static void frt_ste_index_seek(FrtTermEnum *te, FrtSegmentTermIndex *sti, int idx_offset) {
2352
2315
  int term_len = sti->index_term_lens[idx_offset];
2353
2316
  frt_is_seek(STE(te)->is, sti->index_ptrs[idx_offset]);
2354
2317
  STE(te)->pos = STE(te)->sfi->index_interval * idx_offset - 1;
@@ -2359,11 +2322,9 @@ static void frt_ste_index_seek(FrtTermEnum *te, FrtSegmentTermIndex *sti, int id
2359
2322
  te->curr_ti = sti->index_term_infos[idx_offset];
2360
2323
  }
2361
2324
 
2362
- static char *ste_scan_to(FrtTermEnum *te, const char *term)
2363
- {
2325
+ static char *ste_scan_to(FrtTermEnum *te, const char *term) {
2364
2326
  FrtSegmentFieldIndex *sfi = STE(te)->sfi;
2365
- FrtSegmentTermIndex *sti
2366
- = (FrtSegmentTermIndex *)frt_h_get_int(sfi->field_dict, te->field_num);
2327
+ FrtSegmentTermIndex *sti = (FrtSegmentTermIndex *)frt_h_get_int(sfi->field_dict, te->field_num);
2367
2328
  if (sti && sti->size > 0) {
2368
2329
  SFI_ENSURE_INDEX_IS_READ(sfi, sti);
2369
2330
  if (term[0] == '\0') {
@@ -2383,9 +2344,7 @@ static char *ste_scan_to(FrtTermEnum *te, const char *term)
2383
2344
  frt_ste_index_seek(te, sti, sti_get_index_offset(sti, term));
2384
2345
  return te_skip_to(te, term);
2385
2346
  }
2386
- else {
2387
- return NULL;
2388
- }
2347
+ return NULL;
2389
2348
  }
2390
2349
 
2391
2350
  static FrtSegmentTermEnum *ste_allocate(void) {
@@ -2398,8 +2357,7 @@ static FrtSegmentTermEnum *ste_allocate(void) {
2398
2357
  return ste;
2399
2358
  }
2400
2359
 
2401
- FrtTermEnum *frt_ste_clone(FrtTermEnum *other_te)
2402
- {
2360
+ FrtTermEnum *frt_ste_clone(FrtTermEnum *other_te) {
2403
2361
  FrtSegmentTermEnum *ste = ste_allocate();
2404
2362
 
2405
2363
  memcpy(ste, other_te, sizeof(FrtSegmentTermEnum));
@@ -2407,8 +2365,7 @@ FrtTermEnum *frt_ste_clone(FrtTermEnum *other_te)
2407
2365
  return TE(ste);
2408
2366
  }
2409
2367
 
2410
- void frt_ste_close(FrtTermEnum *te)
2411
- {
2368
+ void frt_ste_close(FrtTermEnum *te) {
2412
2369
  frt_is_close(STE(te)->is);
2413
2370
  free(te);
2414
2371
  }
@@ -2422,8 +2379,7 @@ static char *frt_ste_get_term(FrtTermEnum *te, int pos)
2422
2379
  else if (pos != ste->pos) {
2423
2380
  int idx_int = ste->sfi->index_interval;
2424
2381
  if ((pos < ste->pos) || pos > (1 + ste->pos / idx_int) * idx_int) {
2425
- FrtSegmentTermIndex *sti = (FrtSegmentTermIndex *)frt_h_get_int(
2426
- ste->sfi->field_dict, te->field_num);
2382
+ FrtSegmentTermIndex *sti = (FrtSegmentTermIndex *)frt_h_get_int(ste->sfi->field_dict, te->field_num);
2427
2383
  SFI_ENSURE_INDEX_IS_READ(ste->sfi, sti);
2428
2384
  frt_ste_index_seek(te, sti, pos / idx_int);
2429
2385
  }
@@ -2443,6 +2399,7 @@ FrtTermEnum *frt_ste_new(FrtInStream *is, FrtSegmentFieldIndex *sfi)
2443
2399
 
2444
2400
  TE(ste)->field_num = -1;
2445
2401
  ste->is = is;
2402
+ FRT_REF(is);
2446
2403
  ste->size = 0;
2447
2404
  ste->pos = -1;
2448
2405
  ste->sfi = sfi;
@@ -2490,25 +2447,6 @@ static bool tew_lt(const TermEnumWrapper *tew1, const TermEnumWrapper *tew2)
2490
2447
  }
2491
2448
  }
2492
2449
 
2493
- /*
2494
- static void tew_load_doc_map(TermEnumWrapper *tew)
2495
- {
2496
- int j = 0, i;
2497
- FrtIndexReader *ir = tew->ir;
2498
- int max_doc = ir->max_doc(ir);
2499
- int *doc_map = tew->doc_map = FRT_ALLOC_N(int, max_doc);
2500
-
2501
- for (i = 0; i < max_doc; i++) {
2502
- if (ir->is_deleted(ir, i)) {
2503
- doc_map[i] = -1;
2504
- }
2505
- else {
2506
- doc_map[i] = j++;
2507
- }
2508
- }
2509
- }
2510
- */
2511
-
2512
2450
  static char *tew_next(TermEnumWrapper *tew)
2513
2451
  {
2514
2452
  return (tew->term = tew->te->next(tew->te));
@@ -2521,6 +2459,7 @@ static char *tew_skip_to(TermEnumWrapper *tew, const char *term)
2521
2459
 
2522
2460
  static void tew_destroy(TermEnumWrapper *tew)
2523
2461
  {
2462
+ frt_ir_close(tew->ir);
2524
2463
  if (tew->doc_map) {
2525
2464
  free(tew->doc_map);
2526
2465
  }
@@ -2530,6 +2469,7 @@ static void tew_destroy(TermEnumWrapper *tew)
2530
2469
  static TermEnumWrapper *tew_setup(TermEnumWrapper *tew, int index, FrtTermEnum *te, FrtIndexReader *ir) {
2531
2470
  tew->index = index;
2532
2471
  tew->ir = ir;
2472
+ FRT_REF(ir);
2533
2473
  tew->te = te;
2534
2474
  tew->term = te->curr_term;
2535
2475
  tew->doc_map = NULL;
@@ -2689,11 +2629,13 @@ FrtTermInfosReader *frt_tir_open(FrtStore *store, FrtSegmentFieldIndex *sfi, con
2689
2629
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
2690
2630
 
2691
2631
  sprintf(file_name, "%s.tis", segment);
2692
- tir->orig_te = frt_ste_new(store->open_input(store, file_name), sfi);
2632
+ FrtInStream *is = store->open_input(store, file_name);
2633
+ FRT_DEREF(is);
2634
+ tir->orig_te = frt_ste_new(is, sfi);
2635
+ tir->thread_te = 0;
2693
2636
  frt_thread_key_create(&tir->thread_te, NULL);
2694
2637
  tir->te_bucket = frt_ary_new();
2695
2638
  tir->field_num = -1;
2696
-
2697
2639
  return tir;
2698
2640
  }
2699
2641
 
@@ -2751,14 +2693,9 @@ char *frt_tir_get_term(FrtTermInfosReader *tir, int pos) {
2751
2693
  }
2752
2694
  }
2753
2695
 
2754
-
2755
2696
  void frt_tir_close(FrtTermInfosReader *tir) {
2756
2697
  frt_ary_destroy(tir->te_bucket, (frt_free_ft)&frt_ste_close);
2757
2698
  frt_ste_close(tir->orig_te);
2758
-
2759
- /* fix for some dodgy old versions of pthread */
2760
- frt_thread_setspecific(tir->thread_te, NULL);
2761
-
2762
2699
  frt_thread_key_delete(tir->thread_te);
2763
2700
  free(tir);
2764
2701
  }
@@ -2852,7 +2789,7 @@ static void tw_add(FrtTermWriter *tw, const char *term, int term_len, FrtTermInf
2852
2789
  }
2853
2790
 
2854
2791
  void frt_tiw_add(FrtTermInfosWriter *tiw, const char *term, int term_len, FrtTermInfo *ti) {
2855
- off_t tis_pos;
2792
+ frt_off_t tis_pos;
2856
2793
 
2857
2794
  if (0 == (tiw->tis_writer->counter % tiw->index_interval)) {
2858
2795
  /* add an index term */
@@ -3025,8 +2962,8 @@ static bool stde_skip_to(FrtTermDocEnum *tde, int target_doc_num) {
3025
2962
  if (stde->doc_freq >= stde->skip_interval
3026
2963
  && target_doc_num > stde->doc_num) { /* optimized case */
3027
2964
  int last_skip_doc;
3028
- off_t last_frq_ptr;
3029
- off_t last_prx_ptr;
2965
+ frt_off_t last_frq_ptr;
2966
+ frt_off_t last_prx_ptr;
3030
2967
  int num_skipped;
3031
2968
 
3032
2969
  if (NULL == stde->skip_in) {
@@ -3097,7 +3034,7 @@ static void stde_skip_prox(FrtSegmentTermDocEnum *stde) {
3097
3034
  (void)stde;
3098
3035
  }
3099
3036
 
3100
- static void stde_seek_prox(FrtSegmentTermDocEnum *stde, off_t prx_ptr) {
3037
+ static void stde_seek_prox(FrtSegmentTermDocEnum *stde, frt_off_t prx_ptr) {
3101
3038
  (void)stde;
3102
3039
  (void)prx_ptr;
3103
3040
  }
@@ -3193,7 +3130,7 @@ static void stpe_skip_prox(FrtSegmentTermDocEnum *stde)
3193
3130
  frt_is_skip_vints(stde->prx_in, stde->freq);
3194
3131
  }
3195
3132
 
3196
- static void stpe_seek_prox(FrtSegmentTermDocEnum *stde, off_t prx_ptr)
3133
+ static void stpe_seek_prox(FrtSegmentTermDocEnum *stde, frt_off_t prx_ptr)
3197
3134
  {
3198
3135
  frt_is_seek(stde->prx_in, prx_ptr);
3199
3136
  stde->prx_cnt = 0;
@@ -3696,11 +3633,13 @@ FrtDeleter *frt_deleter_new(FrtSegmentInfos *sis, FrtStore *store) {
3696
3633
  FrtDeleter *dlr = FRT_ALLOC(FrtDeleter);
3697
3634
  dlr->sis = sis;
3698
3635
  dlr->store = store;
3636
+ FRT_REF(store);
3699
3637
  dlr->pending = frt_hs_new_str(&free);
3700
3638
  return dlr;
3701
3639
  }
3702
3640
 
3703
3641
  void frt_deleter_destroy(FrtDeleter *dlr) {
3642
+ frt_store_close(dlr->store);
3704
3643
  frt_hs_destroy(dlr->pending);
3705
3644
  free(dlr);
3706
3645
  }
@@ -3821,7 +3760,7 @@ void frt_deleter_find_deletable_files(FrtDeleter *dlr) {
3821
3760
  FrtStore *store = dlr->store;
3822
3761
  struct DelFilesArg dfa;
3823
3762
  FrtHash *current = dfa.current
3824
- = frt_h_new_str((frt_free_ft)NULL, (frt_free_ft)frt_si_deref);
3763
+ = frt_h_new_str((frt_free_ft)NULL, (frt_free_ft)frt_si_close);
3825
3764
  dfa.dlr = dlr;
3826
3765
 
3827
3766
  for(i = 0; i < sis->size; i++) {
@@ -3913,9 +3852,10 @@ static FrtIndexReader *ir_setup(FrtIndexReader *ir, FrtStore *store, FrtSegmentI
3913
3852
  ir->store = store;
3914
3853
  FRT_REF(store);
3915
3854
  }
3916
- ir->sis = sis;
3917
- ir->fis = fis;
3855
+ ir->sis = sis;
3856
+ ir->fis = fis;
3918
3857
  ir->ref_cnt = 1;
3858
+ ir->rir = Qnil;
3919
3859
 
3920
3860
  ir->is_owner = is_owner;
3921
3861
  if (is_owner) {
@@ -4095,37 +4035,28 @@ void frt_ir_commit(FrtIndexReader *ir)
4095
4035
  frt_mutex_unlock(&ir->mutex);
4096
4036
  }
4097
4037
 
4098
- void frt_ir_close(FrtIndexReader *ir)
4099
- {
4100
- frt_mutex_lock(&ir->mutex);
4101
- if (0 == --(ir->ref_cnt)) {
4038
+ void frt_ir_close(FrtIndexReader *ir) {
4039
+ if (ir->ref_cnt == 0) {
4040
+ fprintf(stderr, "ir ref_cnt to low\n");
4041
+ FRT_RAISE(FRT_STATE_ERROR, "ir ref_cnt to low\n");
4042
+ }
4043
+
4044
+ if (FRT_DEREF(ir) == 0) {
4045
+ frt_mutex_lock(&ir->mutex);
4102
4046
  ir_commit_i(ir);
4103
4047
  ir->close_i(ir);
4104
- if (ir->store) {
4105
- frt_store_deref(ir->store);
4106
- }
4107
- if (ir->is_owner && ir->sis) {
4108
- frt_sis_destroy(ir->sis);
4109
- }
4110
- if (ir->cache) {
4111
- frt_h_destroy(ir->cache);
4112
- }
4113
- if (ir->field_index_cache) {
4114
- frt_h_destroy(ir->field_index_cache);
4115
- }
4116
- if (ir->deleter && ir->is_owner) {
4117
- frt_deleter_destroy(ir->deleter);
4118
- }
4048
+ if (ir->store) frt_store_close(ir->store);
4049
+ if (ir->is_owner && ir->sis) frt_sis_destroy(ir->sis);
4050
+ if (ir->cache) frt_h_destroy(ir->cache);
4051
+ if (ir->field_index_cache) frt_h_destroy(ir->field_index_cache);
4052
+ if (ir->deleter && ir->is_owner) frt_deleter_destroy(ir->deleter);
4119
4053
  free(ir->fake_norms);
4120
4054
 
4121
- frt_mutex_destroy(&ir->mutex);
4122
4055
  frt_mutex_destroy(&ir->field_index_mutex);
4123
- free(ir);
4124
- }
4125
- else {
4126
4056
  frt_mutex_unlock(&ir->mutex);
4057
+ frt_mutex_destroy(&ir->mutex);
4058
+ free(ir);
4127
4059
  }
4128
-
4129
4060
  }
4130
4061
 
4131
4062
  /**
@@ -4159,6 +4090,7 @@ static Norm *norm_create(FrtInStream *is, int field_num)
4159
4090
  Norm *norm = FRT_ALLOC(Norm);
4160
4091
 
4161
4092
  norm->is = is;
4093
+ FRT_REF(is);
4162
4094
  norm->field_num = field_num;
4163
4095
  norm->bytes = NULL;
4164
4096
  norm->is_dirty = false;
@@ -4201,10 +4133,8 @@ static void norm_rewrite(Norm *norm, FrtStore *store, FrtDeleter *dlr,
4201
4133
  #define SR(ir) ((FrtSegmentReader *)(ir))
4202
4134
  #define SR_SIZE(ir) (SR(ir)->fr->size)
4203
4135
 
4204
- static FrtFieldsReader *sr_fr(FrtSegmentReader *sr)
4205
- {
4136
+ static FrtFieldsReader *sr_fr(FrtSegmentReader *sr) {
4206
4137
  FrtFieldsReader *fr;
4207
-
4208
4138
  if (NULL == (fr = (FrtFieldsReader *)frt_thread_getspecific(sr->thread_fr))) {
4209
4139
  fr = frt_fr_clone(sr->fr);
4210
4140
  frt_ary_push(sr->fr_bucket, fr);
@@ -4218,17 +4148,13 @@ static bool sr_is_deleted_i(FrtSegmentReader *sr, int doc_num)
4218
4148
  return (NULL != sr->deleted_docs && frt_bv_get(sr->deleted_docs, doc_num));
4219
4149
  }
4220
4150
 
4221
- static void sr_get_norms_into_i(FrtSegmentReader *sr, int field_num,
4222
- frt_uchar *buf)
4223
- {
4151
+ static void sr_get_norms_into_i(FrtSegmentReader *sr, int field_num, frt_uchar *buf) {
4224
4152
  Norm *norm = (Norm *)frt_h_get_int(sr->norms, field_num);
4225
4153
  if (NULL == norm) {
4226
4154
  memset(buf, 0, SR_SIZE(sr));
4227
- }
4228
- else if (NULL != norm->bytes) { /* can copy from cache */
4155
+ } else if (NULL != norm->bytes) { /* can copy from cache */
4229
4156
  memcpy(buf, norm->bytes, SR_SIZE(sr));
4230
- }
4231
- else {
4157
+ } else {
4232
4158
  FrtInStream *norm_in = frt_is_clone(norm->is);
4233
4159
  /* read from disk */
4234
4160
  frt_is_seek(norm_in, 0);
@@ -4237,8 +4163,7 @@ static void sr_get_norms_into_i(FrtSegmentReader *sr, int field_num,
4237
4163
  }
4238
4164
  }
4239
4165
 
4240
- static frt_uchar *sr_get_norms_i(FrtSegmentReader *sr, int field_num)
4241
- {
4166
+ static frt_uchar *sr_get_norms_i(FrtSegmentReader *sr, int field_num) {
4242
4167
  Norm *norm = (Norm *)frt_h_get_int(sr->norms, field_num);
4243
4168
  if (NULL == norm) { /* not an indexed field */
4244
4169
  return NULL;
@@ -4252,8 +4177,7 @@ static frt_uchar *sr_get_norms_i(FrtSegmentReader *sr, int field_num)
4252
4177
  return norm->bytes;
4253
4178
  }
4254
4179
 
4255
- static void sr_set_norm_i(FrtIndexReader *ir, int doc_num, int field_num, frt_uchar b)
4256
- {
4180
+ static void sr_set_norm_i(FrtIndexReader *ir, int doc_num, int field_num, frt_uchar b) {
4257
4181
  Norm *norm = (Norm *)frt_h_get_int(SR(ir)->norms, field_num);
4258
4182
  if (NULL != norm) { /* has_norms */
4259
4183
  ir->has_changes = true;
@@ -4263,8 +4187,7 @@ static void sr_set_norm_i(FrtIndexReader *ir, int doc_num, int field_num, frt_uc
4263
4187
  }
4264
4188
  }
4265
4189
 
4266
- static void sr_delete_doc_i(FrtIndexReader *ir, int doc_num)
4267
- {
4190
+ static void sr_delete_doc_i(FrtIndexReader *ir, int doc_num) {
4268
4191
  if (NULL == SR(ir)->deleted_docs) {
4269
4192
  SR(ir)->deleted_docs = frt_bv_new();
4270
4193
  }
@@ -4275,8 +4198,7 @@ static void sr_delete_doc_i(FrtIndexReader *ir, int doc_num)
4275
4198
  frt_bv_set(SR(ir)->deleted_docs, doc_num);
4276
4199
  }
4277
4200
 
4278
- static void sr_undelete_all_i(FrtIndexReader *ir)
4279
- {
4201
+ static void sr_undelete_all_i(FrtIndexReader *ir) {
4280
4202
  SR(ir)->undelete_all = true;
4281
4203
  SR(ir)->deleted_docs_dirty = false;
4282
4204
  ir->has_changes = true;
@@ -4286,8 +4208,7 @@ static void sr_undelete_all_i(FrtIndexReader *ir)
4286
4208
  SR(ir)->deleted_docs = NULL;
4287
4209
  }
4288
4210
 
4289
- static void sr_set_deleter_i(FrtIndexReader *ir, FrtDeleter *deleter)
4290
- {
4211
+ static void sr_set_deleter_i(FrtIndexReader *ir, FrtDeleter *deleter) {
4291
4212
  ir->deleter = deleter;
4292
4213
  }
4293
4214
 
@@ -4371,8 +4292,7 @@ static void sr_commit_i(FrtIndexReader *ir)
4371
4292
  }
4372
4293
  }
4373
4294
 
4374
- static void sr_close_i(FrtIndexReader *ir)
4375
- {
4295
+ static void sr_close_i(FrtIndexReader *ir) {
4376
4296
  FrtSegmentReader *sr = SR(ir);
4377
4297
 
4378
4298
  if (sr->fr) frt_fr_close(sr->fr);
@@ -4382,9 +4302,8 @@ static void sr_close_i(FrtIndexReader *ir)
4382
4302
  if (sr->prx_in) frt_is_close(sr->prx_in);
4383
4303
  if (sr->norms) frt_h_destroy(sr->norms);
4384
4304
  if (sr->deleted_docs) frt_bv_destroy(sr->deleted_docs);
4385
- if (sr->cfs_store) frt_store_deref(sr->cfs_store);
4305
+ if (sr->cfs_store) frt_store_close(sr->cfs_store);
4386
4306
  if (sr->fr_bucket) {
4387
- frt_thread_setspecific(sr->thread_fr, NULL);
4388
4307
  frt_thread_key_delete(sr->thread_fr);
4389
4308
  frt_ary_destroy(sr->fr_bucket, (frt_free_ft)&frt_fr_close);
4390
4309
  }
@@ -4491,8 +4410,7 @@ static FrtTermVector *sr_term_vector(FrtIndexReader *ir, int doc_num, ID field)
4491
4410
  FrtFieldInfo *fi = (FrtFieldInfo *)frt_h_get(ir->fis->field_dict, (void *)field);
4492
4411
  FrtFieldsReader *fr;
4493
4412
 
4494
- if (!fi || !fi_store_term_vector(fi) || !SR(ir)->fr ||
4495
- !(fr = sr_fr(SR(ir)))) {
4413
+ if (!fi || !fi_store_term_vector(fi) || !SR(ir)->fr || !(fr = sr_fr(SR(ir)))) {
4496
4414
  return NULL;
4497
4415
  }
4498
4416
 
@@ -4535,8 +4453,9 @@ static void sr_open_norms(FrtIndexReader *ir, FrtStore *cfs_store)
4535
4453
  FrtStore *store = (si->use_compound_file && si->norm_gens[i] == 0) ?
4536
4454
  cfs_store : ir->store;
4537
4455
  if (si_norm_file_name(si, file_name, i)) {
4538
- frt_h_set_int(SR(ir)->norms, i,
4539
- norm_create(store->open_input(store, file_name), i));
4456
+ FrtInStream *is = store->open_input(store, file_name);
4457
+ FRT_DEREF(is);
4458
+ frt_h_set_int(SR(ir)->norms, i, norm_create(is, i));
4540
4459
  }
4541
4460
  }
4542
4461
  SR(ir)->norms_dirty = false;
@@ -4575,6 +4494,7 @@ static FrtIndexReader *sr_setup_i(FrtSegmentReader *sr)
4575
4494
 
4576
4495
  ir->type = FRT_SEGMENT_READER;
4577
4496
 
4497
+ sr->thread_fr = 0;
4578
4498
  sr->cfs_store = NULL;
4579
4499
 
4580
4500
  FRT_TRY
@@ -4908,11 +4828,11 @@ FrtMultiReader *frt_mr_init(FrtMultiReader *mr, FrtIndexReader **sub_readers, co
4908
4828
  mr->max_doc = 0;
4909
4829
  mr->num_docs_cache = -1;
4910
4830
  mr->has_deletions = false;
4911
-
4912
4831
  mr->starts = FRT_ALLOC_N(int, (r_cnt+1));
4913
4832
 
4914
4833
  for (i = 0; i < r_cnt; i++) {
4915
4834
  FrtIndexReader *sub_reader = sub_readers[i];
4835
+ FRT_REF(sub_reader);
4916
4836
  mr->starts[i] = mr->max_doc;
4917
4837
  mr->max_doc += sub_reader->max_doc(sub_reader); /* compute max_docs */
4918
4838
 
@@ -5028,7 +4948,6 @@ FrtIndexReader *frt_mr_open(FrtIndexReader *ir, FrtIndexReader **sub_readers, co
5028
4948
 
5029
4949
  static void ir_open_i(FrtStore *store, FindSegmentsFile *fsf, FrtIndexReader *ir) {
5030
4950
  volatile bool success = false;
5031
- // FrtIndexReader *volatile ir = NULL;
5032
4951
  FrtSegmentInfos *volatile sis = NULL;
5033
4952
  FRT_TRY
5034
4953
  do {
@@ -5039,21 +4958,24 @@ static void ir_open_i(FrtStore *store, FindSegmentsFile *fsf, FrtIndexReader *ir
5039
4958
  fis = sis->fis;
5040
4959
  if (sis->size == 1) {
5041
4960
  ir = sr_open(sis, fis, 0, true, (FrtSegmentReader *)ir);
5042
- }
5043
- else {
4961
+ } else {
5044
4962
  volatile int i;
5045
- FrtIndexReader **readers = FRT_ALLOC_N(FrtIndexReader *, sis->size);
4963
+ FrtIndexReader **readers = FRT_ALLOC_AND_ZERO_N(FrtIndexReader *, sis->size);
5046
4964
  int num_segments = sis->size;
5047
- for (i = num_segments - 1; i >= 0; i--) {
5048
- FRT_TRY
4965
+ FRT_TRY
4966
+ for (i = num_segments - 1; i >= 0; i--) {
5049
4967
  readers[i] = sr_open(sis, fis, i, false, NULL);
5050
- FRT_XCATCHALL
5051
- for (i++; i < num_segments; i++) {
4968
+ FRT_DEREF(readers[i]);
4969
+ }
4970
+ FRT_XCATCHALL
4971
+ for (i++; i < num_segments; i++) {
4972
+ if (readers[i]) {
4973
+ FRT_REF(readers[i]);
5052
4974
  frt_ir_close(readers[i]);
5053
4975
  }
5054
- free(readers);
5055
- FRT_XENDTRY
5056
- }
4976
+ }
4977
+ free(readers);
4978
+ FRT_XENDTRY
5057
4979
  ir = frt_mr_open_i(store, sis, fis, readers, sis->size, ir);
5058
4980
  }
5059
4981
  fsf->ret.ir = ir;
@@ -5186,8 +5108,8 @@ typedef struct SkipBuffer
5186
5108
  FrtOutStream *frq_out;
5187
5109
  FrtOutStream *prx_out;
5188
5110
  int last_doc;
5189
- off_t last_frq_ptr;
5190
- off_t last_prx_ptr;
5111
+ frt_off_t last_frq_ptr;
5112
+ frt_off_t last_prx_ptr;
5191
5113
  } SkipBuffer;
5192
5114
 
5193
5115
  static void skip_buf_reset(SkipBuffer *skip_buf)
@@ -5209,8 +5131,8 @@ static SkipBuffer *skip_buf_new(FrtOutStream *frq_out, FrtOutStream *prx_out)
5209
5131
 
5210
5132
  static void skip_buf_add(SkipBuffer *skip_buf, int doc)
5211
5133
  {
5212
- off_t frq_ptr = frt_os_pos(skip_buf->frq_out);
5213
- off_t prx_ptr = frt_os_pos(skip_buf->prx_out);
5134
+ frt_off_t frq_ptr = frt_os_pos(skip_buf->frq_out);
5135
+ frt_off_t prx_ptr = frt_os_pos(skip_buf->prx_out);
5214
5136
 
5215
5137
  frt_os_write_vint(skip_buf->buf, doc - skip_buf->last_doc);
5216
5138
  frt_os_write_vint(skip_buf->buf, frq_ptr - skip_buf->last_frq_ptr);
@@ -5221,9 +5143,9 @@ static void skip_buf_add(SkipBuffer *skip_buf, int doc)
5221
5143
  skip_buf->last_prx_ptr = prx_ptr;
5222
5144
  }
5223
5145
 
5224
- static off_t skip_buf_write(SkipBuffer *skip_buf)
5146
+ static frt_off_t skip_buf_write(SkipBuffer *skip_buf)
5225
5147
  {
5226
- off_t skip_ptr = frt_os_pos(skip_buf->frq_out);
5148
+ frt_off_t skip_ptr = frt_os_pos(skip_buf->frq_out);
5227
5149
  frt_ramo_write_to(skip_buf->buf, skip_buf->frq_out);
5228
5150
  return skip_ptr;
5229
5151
  }
@@ -5293,8 +5215,7 @@ static void dw_flush(FrtDocWriter *dw)
5293
5215
  FrtPosting *p;
5294
5216
  FrtOccurence *occ;
5295
5217
  FrtStore *store = dw->store;
5296
- FrtTermInfosWriter *tiw = frt_tiw_open(store, dw->si->name,
5297
- dw->index_interval, skip_interval);
5218
+ FrtTermInfosWriter *tiw = frt_tiw_open(store, dw->si->name, dw->index_interval, skip_interval);
5298
5219
  FrtTermInfo ti;
5299
5220
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
5300
5221
  FrtOutStream *frq_out, *prx_out;
@@ -5373,6 +5294,7 @@ FrtDocWriter *frt_dw_open(FrtIndexWriter *iw, FrtSegmentInfo *si)
5373
5294
  dw->analyzer = iw->analyzer;
5374
5295
  dw->fis = iw->fis;
5375
5296
  dw->store = store;
5297
+ FRT_REF(store);
5376
5298
  dw->fw = frt_fw_open(store, si->name, iw->fis);
5377
5299
  dw->si = si;
5378
5300
 
@@ -5407,6 +5329,7 @@ void frt_dw_close(FrtDocWriter *dw)
5407
5329
  if (dw->fw) {
5408
5330
  frt_fw_close(dw->fw);
5409
5331
  }
5332
+ frt_store_close(dw->store);
5410
5333
  frt_h_destroy(dw->curr_plists);
5411
5334
  frt_h_destroy(dw->fields);
5412
5335
  frt_mp_destroy(dw->mp);
@@ -5455,7 +5378,7 @@ static void dw_add_posting(FrtMemoryPool *mp,
5455
5378
  }
5456
5379
  }
5457
5380
 
5458
- static void dw_add_offsets(FrtDocWriter *dw, int pos, off_t start, off_t end)
5381
+ static void dw_add_offsets(FrtDocWriter *dw, int pos, frt_off_t start, frt_off_t end)
5459
5382
  {
5460
5383
  if (pos >= dw->offsets_capa) {
5461
5384
  int old_capa = dw->offsets_capa;
@@ -5479,7 +5402,7 @@ FrtHash *frt_dw_invert_field(FrtDocWriter *dw, FrtFieldInverter *fld_inv, FrtDoc
5479
5402
  int doc_num = dw->doc_num;
5480
5403
  int i;
5481
5404
  const int df_size = df->size;
5482
- off_t start_offset = 0;
5405
+ frt_off_t start_offset = 0;
5483
5406
 
5484
5407
  if (fld_inv->is_tokenized) {
5485
5408
  FrtToken *tk;
@@ -5658,6 +5581,7 @@ static SegmentMergeInfo *smi_new(int base, FrtStore *store, FrtSegmentInfo *si)
5658
5581
  smi->base = base;
5659
5582
  smi->si = si;
5660
5583
  smi->orig_store = smi->store = store;
5584
+ FRT_REF(smi->orig_store);
5661
5585
  sprintf(file_name, "%s.cfs", segment);
5662
5586
  if (store->exists(store, file_name)) {
5663
5587
  smi->store = frt_open_cmpd_store(store, file_name);
@@ -5682,7 +5606,9 @@ static void smi_load_term_input(SegmentMergeInfo *smi)
5682
5606
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
5683
5607
  smi->sfi = frt_sfi_open(store, segment);
5684
5608
  sprintf(file_name, "%s.tis", segment);
5685
- smi->te = TE(frt_ste_new(store->open_input(store, file_name), smi->sfi));
5609
+ FrtInStream *is = store->open_input(store, file_name);
5610
+ FRT_DEREF(is);
5611
+ smi->te = TE(frt_ste_new(is, smi->sfi));
5686
5612
  sprintf(file_name, "%s.frq", segment);
5687
5613
  smi->frq_in = store->open_input(store, file_name);
5688
5614
  sprintf(file_name, "%s.prx", segment);
@@ -5703,8 +5629,9 @@ static void smi_close_term_input(SegmentMergeInfo *smi)
5703
5629
  static void smi_destroy(SegmentMergeInfo *smi)
5704
5630
  {
5705
5631
  if (smi->store != smi->orig_store) {
5706
- frt_store_deref(smi->store);
5632
+ frt_store_close(smi->store);
5707
5633
  }
5634
+ frt_store_close(smi->orig_store);
5708
5635
  if (smi->deleted_docs) {
5709
5636
  frt_bv_destroy(smi->deleted_docs);
5710
5637
  free(smi->doc_map);
@@ -5740,19 +5667,18 @@ typedef struct SegmentMerger {
5740
5667
  FrtOutStream *prx_out;
5741
5668
  } SegmentMerger;
5742
5669
 
5743
- static SegmentMerger *sm_create(FrtIndexWriter *iw, FrtSegmentInfo *si,
5744
- FrtSegmentInfo **seg_infos, const int seg_cnt)
5670
+ static SegmentMerger *sm_create(FrtIndexWriter *iw, FrtSegmentInfo *si, FrtSegmentInfo **seg_infos, const int seg_cnt)
5745
5671
  {
5746
5672
  int i;
5747
5673
  SegmentMerger *sm = FRT_ALLOC_AND_ZERO_N(SegmentMerger, seg_cnt);
5748
5674
  sm->store = iw->store;
5675
+ FRT_REF(sm->store);
5749
5676
  sm->fis = iw->fis;
5750
5677
  sm->si = si;
5751
5678
  sm->doc_cnt = 0;
5752
5679
  sm->smis = FRT_ALLOC_N(SegmentMergeInfo *, seg_cnt);
5753
5680
  for (i = 0; i < seg_cnt; i++) {
5754
- sm->smis[i] = smi_new(sm->doc_cnt, seg_infos[i]->store,
5755
- seg_infos[i]);
5681
+ sm->smis[i] = smi_new(sm->doc_cnt, seg_infos[i]->store, seg_infos[i]);
5756
5682
  sm->doc_cnt += sm->smis[i]->doc_cnt;
5757
5683
  }
5758
5684
  sm->seg_cnt = seg_cnt;
@@ -5767,6 +5693,7 @@ static void sm_destroy(SegmentMerger *sm)
5767
5693
  for (i = 0; i < seg_cnt; i++) {
5768
5694
  smi_destroy(sm->smis[i]);
5769
5695
  }
5696
+ frt_store_close(sm->store);
5770
5697
  free(sm->smis);
5771
5698
  free(sm);
5772
5699
  }
@@ -5774,7 +5701,7 @@ static void sm_destroy(SegmentMerger *sm)
5774
5701
  static void sm_merge_fields(SegmentMerger *sm)
5775
5702
  {
5776
5703
  int i, j;
5777
- off_t start, end = 0;
5704
+ frt_off_t start, end = 0;
5778
5705
  char file_name[FRT_SEGMENT_NAME_MAX_LENGTH];
5779
5706
  FrtOutStream *fdt_out, *fdx_out;
5780
5707
  FrtStore *store = sm->store;
@@ -5891,12 +5818,12 @@ static char *sm_cache_term(SegmentMerger *sm, char *term, int term_len)
5891
5818
  static void sm_merge_term_info(SegmentMerger *sm, SegmentMergeInfo **matches,
5892
5819
  int match_size)
5893
5820
  {
5894
- off_t frq_ptr = frt_os_pos(sm->frq_out);
5895
- off_t prx_ptr = frt_os_pos(sm->prx_out);
5821
+ frt_off_t frq_ptr = frt_os_pos(sm->frq_out);
5822
+ frt_off_t prx_ptr = frt_os_pos(sm->prx_out);
5896
5823
 
5897
5824
  int df = sm_append_postings(sm, matches, match_size); /* append posting data */
5898
5825
 
5899
- off_t skip_ptr = skip_buf_write(sm->skip_buf);
5826
+ frt_off_t skip_ptr = skip_buf_write(sm->skip_buf);
5900
5827
 
5901
5828
  if (df > 0) {
5902
5829
  /* add an entry to the dictionary with ptrs to prox and freq files */
@@ -6351,8 +6278,9 @@ void frt_iw_close(FrtIndexWriter *iw)
6351
6278
  iw->write_lock->release(iw->write_lock);
6352
6279
  frt_close_lock(iw->write_lock);
6353
6280
  iw->write_lock = NULL;
6354
- frt_store_deref(iw->store);
6281
+ frt_store_close(iw->store);
6355
6282
  frt_deleter_destroy(iw->deleter);
6283
+ frt_mutex_unlock(&iw->mutex);
6356
6284
  frt_mutex_destroy(&iw->mutex);
6357
6285
  free(iw);
6358
6286
  }
@@ -6366,6 +6294,7 @@ FrtIndexWriter *frt_iw_open(FrtIndexWriter *iw, FrtStore *store, FrtAnalyzer *vo
6366
6294
  iw = frt_iw_alloc();
6367
6295
  frt_mutex_init(&iw->mutex, NULL);
6368
6296
  iw->store = store;
6297
+ FRT_REF(store);
6369
6298
  if (!config) {
6370
6299
  config = &frt_default_config;
6371
6300
  }
@@ -6374,14 +6303,14 @@ FrtIndexWriter *frt_iw_open(FrtIndexWriter *iw, FrtStore *store, FrtAnalyzer *vo
6374
6303
  FRT_TRY
6375
6304
  iw->write_lock = frt_open_lock(store, FRT_WRITE_LOCK_NAME);
6376
6305
  if (!iw->write_lock->obtain(iw->write_lock)) {
6377
- FRT_RAISE(FRT_LOCK_ERROR,
6378
- "Couldn't obtain write lock when opening IndexWriter");
6306
+ FRT_RAISE(FRT_LOCK_ERROR, "Couldn't obtain write lock when opening IndexWriter");
6379
6307
  }
6380
6308
 
6381
6309
  iw->sis = frt_sis_read(store);
6382
6310
  iw->fis = iw->sis->fis;
6383
6311
  FRT_REF(iw->fis);
6384
6312
  FRT_XCATCHALL
6313
+ frt_store_close(iw->store);
6385
6314
  if (iw->write_lock) {
6386
6315
  iw->write_lock->release(iw->write_lock);
6387
6316
  frt_close_lock(iw->write_lock);
@@ -6399,7 +6328,6 @@ FrtIndexWriter *frt_iw_open(FrtIndexWriter *iw, FrtStore *store, FrtAnalyzer *vo
6399
6328
  iw->deleter = frt_deleter_new(iw->sis, store);
6400
6329
  deleter_delete_deletable_files(iw->deleter);
6401
6330
 
6402
- FRT_REF(store);
6403
6331
  return iw;
6404
6332
  }
6405
6333
 
@@ -6431,6 +6359,8 @@ static void iw_cp_fields(FrtIndexWriter *iw, FrtSegmentReader *sr, const char *s
6431
6359
  sprintf(file_name, "%s.del", segment);
6432
6360
  del_out = store_out->new_output(store_out, file_name);
6433
6361
  frt_is2os_copy_bytes(del_in, del_out, frt_is_length(del_in));
6362
+ frt_os_close(del_out);
6363
+ frt_is_close(del_in);
6434
6364
  }
6435
6365
 
6436
6366
  if (map) {
@@ -6440,7 +6370,7 @@ static void iw_cp_fields(FrtIndexWriter *iw, FrtSegmentReader *sr, const char *s
6440
6370
  int j, data_len = 0;
6441
6371
  const int field_cnt = frt_is_read_vint(fdt_in);
6442
6372
  int tv_cnt;
6443
- off_t doc_start_ptr = frt_os_pos(fdt_out);
6373
+ frt_off_t doc_start_ptr = frt_os_pos(fdt_out);
6444
6374
 
6445
6375
  frt_os_write_u64(fdx_out, doc_start_ptr);
6446
6376
  frt_os_write_vint(fdt_out, field_cnt);