isomorfeus-ferret 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +612 -0
  3. data/README.md +44 -0
  4. data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
  5. data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
  6. data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
  7. data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
  8. data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
  9. data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
  10. data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
  11. data/ext/isomorfeus_ferret_ext/email.rl +21 -0
  12. data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
  13. data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
  14. data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
  15. data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
  16. data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
  17. data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
  18. data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
  19. data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
  20. data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
  21. data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
  22. data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
  23. data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
  24. data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
  25. data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
  26. data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
  27. data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
  28. data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
  29. data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
  30. data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
  31. data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
  32. data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
  33. data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
  34. data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
  35. data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
  36. data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
  37. data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
  38. data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
  39. data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
  40. data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
  41. data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
  42. data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
  43. data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
  44. data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
  45. data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
  46. data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
  47. data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
  48. data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
  49. data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
  50. data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
  51. data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
  52. data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
  53. data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
  54. data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
  55. data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
  56. data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
  57. data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
  58. data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
  59. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
  60. data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
  61. data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
  62. data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
  63. data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
  64. data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
  65. data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
  66. data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
  67. data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
  68. data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
  69. data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
  70. data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
  71. data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
  72. data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
  73. data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
  74. data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
  75. data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
  76. data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
  77. data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
  78. data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
  79. data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
  80. data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
  81. data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
  82. data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
  83. data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
  84. data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
  85. data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
  86. data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
  87. data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
  88. data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
  89. data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
  90. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
  91. data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
  92. data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
  93. data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
  94. data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
  95. data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
  96. data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
  97. data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
  98. data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
  99. data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
  100. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
  101. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
  102. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
  103. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
  104. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
  105. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
  106. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
  107. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
  108. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
  109. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
  110. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
  111. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
  112. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
  113. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
  114. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
  115. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
  116. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
  117. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
  118. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
  119. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
  120. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
  121. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
  122. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
  123. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
  124. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
  125. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
  126. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
  127. data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
  128. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
  129. data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
  130. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
  131. data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
  132. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
  133. data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
  134. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
  135. data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
  136. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
  137. data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
  138. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
  139. data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
  140. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
  141. data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
  142. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
  143. data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
  144. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
  145. data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
  146. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
  147. data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
  148. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
  149. data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
  150. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
  151. data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
  152. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
  153. data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
  154. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
  155. data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
  156. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
  157. data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
  158. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
  159. data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
  160. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
  161. data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
  162. data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
  163. data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
  164. data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
  165. data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
  166. data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
  167. data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
  168. data/ext/isomorfeus_ferret_ext/test.c +850 -0
  169. data/ext/isomorfeus_ferret_ext/test.h +416 -0
  170. data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
  171. data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
  172. data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
  173. data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
  174. data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
  175. data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
  176. data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
  177. data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
  178. data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
  179. data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
  180. data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
  181. data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
  182. data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
  183. data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
  184. data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
  185. data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
  186. data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
  187. data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
  188. data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
  189. data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
  190. data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
  191. data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
  192. data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
  193. data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
  194. data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
  195. data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
  196. data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
  197. data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
  198. data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
  199. data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
  200. data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
  201. data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
  202. data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
  203. data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
  204. data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
  205. data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
  206. data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
  207. data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
  208. data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
  209. data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
  210. data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
  211. data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
  212. data/ext/isomorfeus_ferret_ext/url.rl +27 -0
  213. data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
  214. data/lib/isomorfeus/ferret/document.rb +132 -0
  215. data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
  216. data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
  217. data/lib/isomorfeus/ferret/index/index.rb +970 -0
  218. data/lib/isomorfeus/ferret/monitor.rb +323 -0
  219. data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
  220. data/lib/isomorfeus/ferret/version.rb +5 -0
  221. data/lib/isomorfeus-ferret.rb +8 -0
  222. metadata +307 -0
@@ -0,0 +1,460 @@
1
+ #include "frt_store.h"
2
+ #include <string.h>
3
+
4
+ static FrtRAMFile *rf_new(const char *name)
5
+ {
6
+ FrtRAMFile *rf = FRT_ALLOC(FrtRAMFile);
7
+ rf->buffers = FRT_ALLOC(frt_uchar *);
8
+ rf->buffers[0] = FRT_ALLOC_N(frt_uchar, FRT_BUFFER_SIZE);
9
+ rf->name = frt_estrdup(name);
10
+ rf->len = 0;
11
+ rf->bufcnt = 1;
12
+ rf->ref_cnt = 1;
13
+ return rf;
14
+ }
15
+
16
+ static void rf_extend_if_necessary(FrtRAMFile *rf, int buf_num)
17
+ {
18
+ while (rf->bufcnt <= buf_num) {
19
+ FRT_REALLOC_N(rf->buffers, frt_uchar *, (rf->bufcnt + 1));
20
+ rf->buffers[rf->bufcnt++] = FRT_ALLOC_N(frt_uchar, FRT_BUFFER_SIZE);
21
+ }
22
+ }
23
+
24
+ static void rf_close(void *p)
25
+ {
26
+ int i;
27
+ FrtRAMFile *rf = (FrtRAMFile *)p;
28
+ if (rf->ref_cnt > 0) {
29
+ return;
30
+ }
31
+ free(rf->name);
32
+ for (i = 0; i < rf->bufcnt; i++) {
33
+ free(rf->buffers[i]);
34
+ }
35
+ free(rf->buffers);
36
+ free(rf);
37
+ }
38
+
39
+ static void ram_touch(FrtStore *store, const char *filename)
40
+ {
41
+ if (frt_h_get(store->dir.ht, filename) == NULL) {
42
+ frt_h_set(store->dir.ht, filename, rf_new(filename));
43
+ }
44
+ }
45
+
46
+ static int ram_exists(FrtStore *store, const char *filename)
47
+ {
48
+ if (frt_h_get(store->dir.ht, filename) != NULL) {
49
+ return true;
50
+ }
51
+ else {
52
+ return false;
53
+ }
54
+ }
55
+
56
+ static int ram_remove(FrtStore *store, const char *filename)
57
+ {
58
+ FrtRAMFile *rf = (FrtRAMFile *)frt_h_rem(store->dir.ht, filename, false);
59
+ if (rf != NULL) {
60
+ FRT_DEREF(rf);
61
+ rf_close(rf);
62
+ return true;
63
+ }
64
+ else {
65
+ return false;
66
+ }
67
+ }
68
+
69
+ static void ram_rename(FrtStore *store, const char *from, const char *to)
70
+ {
71
+ FrtRAMFile *rf = (FrtRAMFile *)frt_h_rem(store->dir.ht, from, false);
72
+ FrtRAMFile *tmp;
73
+
74
+ if (rf == NULL) {
75
+ FRT_RAISE(FRT_IO_ERROR, "couldn't rename \"%s\" to \"%s\". \"%s\""
76
+ " doesn't exist", from, to, from);
77
+ }
78
+
79
+ free(rf->name);
80
+
81
+ rf->name = frt_estrdup(to);
82
+
83
+ /* clean up the file we are overwriting */
84
+ tmp = (FrtRAMFile *)frt_h_get(store->dir.ht, to);
85
+ if (tmp != NULL) {
86
+ FRT_DEREF(tmp);
87
+ }
88
+
89
+ frt_h_set(store->dir.ht, rf->name, rf);
90
+ }
91
+
92
+ static int ram_count(FrtStore *store)
93
+ {
94
+ return store->dir.ht->size;
95
+ }
96
+
97
+ static void ram_each(FrtStore *store,
98
+ void (*func)(const char *fname, void *arg), void *arg)
99
+ {
100
+ FrtHash *ht = store->dir.ht;
101
+ int i;
102
+ for (i = 0; i <= ht->mask; i++) {
103
+ FrtRAMFile *rf = (FrtRAMFile *)ht->table[i].value;
104
+ if (rf) {
105
+ if (strncmp(rf->name, FRT_LOCK_PREFIX, strlen(FRT_LOCK_PREFIX)) == 0) {
106
+ continue;
107
+ }
108
+ func(rf->name, arg);
109
+ }
110
+ }
111
+ }
112
+
113
+ static void ram_close_i(FrtStore *store)
114
+ {
115
+ FrtHash *ht = store->dir.ht;
116
+ int i;
117
+ for (i = 0; i <= ht->mask; i++) {
118
+ FrtRAMFile *rf = (FrtRAMFile *)ht->table[i].value;
119
+ if (rf) {
120
+ FRT_DEREF(rf);
121
+ }
122
+ }
123
+ frt_h_destroy(store->dir.ht);
124
+ frt_store_destroy(store);
125
+ }
126
+
127
+ /*
128
+ * Be sure to keep the locks
129
+ */
130
+ static void ram_clear(FrtStore *store)
131
+ {
132
+ int i;
133
+ FrtHash *ht = store->dir.ht;
134
+ for (i = 0; i <= ht->mask; i++) {
135
+ FrtRAMFile *rf = (FrtRAMFile *)ht->table[i].value;
136
+ if (rf && !frt_file_is_lock(rf->name)) {
137
+ FRT_DEREF(rf);
138
+ frt_h_del(ht, rf->name);
139
+ }
140
+ }
141
+ }
142
+
143
+ static void ram_clear_locks(FrtStore *store)
144
+ {
145
+ int i;
146
+ FrtHash *ht = store->dir.ht;
147
+ for (i = 0; i <= ht->mask; i++) {
148
+ FrtRAMFile *rf = (FrtRAMFile *)ht->table[i].value;
149
+ if (rf && frt_file_is_lock(rf->name)) {
150
+ FRT_DEREF(rf);
151
+ frt_h_del(ht, rf->name);
152
+ }
153
+ }
154
+ }
155
+
156
+ static void ram_clear_all(FrtStore *store)
157
+ {
158
+ int i;
159
+ FrtHash *ht = store->dir.ht;
160
+ for (i = 0; i <= ht->mask; i++) {
161
+ FrtRAMFile *rf = (FrtRAMFile *)ht->table[i].value;
162
+ if (rf) {
163
+ FRT_DEREF(rf);
164
+ frt_h_del(ht, rf->name);
165
+ }
166
+ }
167
+ }
168
+
169
+ static off_t ram_length(FrtStore *store, const char *filename)
170
+ {
171
+ FrtRAMFile *rf = (FrtRAMFile *)frt_h_get(store->dir.ht, filename);
172
+ if (rf != NULL) {
173
+ return rf->len;
174
+ }
175
+ else {
176
+ return 0;
177
+ }
178
+ }
179
+
180
+ static void ramo_flush_i(FrtOutStream *os, const frt_uchar *src, int len)
181
+ {
182
+ frt_uchar *buffer;
183
+ FrtRAMFile *rf = os->file.rf;
184
+ int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
185
+ int src_offset;
186
+ off_t pointer = os->pointer;
187
+
188
+ buffer_number = (int)(pointer / FRT_BUFFER_SIZE);
189
+ buffer_offset = pointer % FRT_BUFFER_SIZE;
190
+ bytes_in_buffer = FRT_BUFFER_SIZE - buffer_offset;
191
+ bytes_to_copy = bytes_in_buffer < len ? bytes_in_buffer : len;
192
+
193
+ rf_extend_if_necessary(rf, buffer_number);
194
+
195
+ buffer = rf->buffers[buffer_number];
196
+ memcpy(buffer + buffer_offset, src, bytes_to_copy);
197
+
198
+ if (bytes_to_copy < len) {
199
+ src_offset = bytes_to_copy;
200
+ bytes_to_copy = len - bytes_to_copy;
201
+ buffer_number += 1;
202
+ rf_extend_if_necessary(rf, buffer_number);
203
+ buffer = rf->buffers[buffer_number];
204
+
205
+ memcpy(buffer, src + src_offset, bytes_to_copy);
206
+ }
207
+ os->pointer += len;
208
+
209
+ if (os->pointer > rf->len) {
210
+ rf->len = os->pointer;
211
+ }
212
+ }
213
+
214
+ static void ramo_seek_i(FrtOutStream *os, off_t pos)
215
+ {
216
+ os->pointer = pos;
217
+ }
218
+
219
+ void frt_ramo_reset(FrtOutStream *os)
220
+ {
221
+ frt_os_seek(os, 0);
222
+ os->file.rf->len = 0;
223
+ }
224
+
225
+ static void ramo_close_i(FrtOutStream *os)
226
+ {
227
+ FrtRAMFile *rf = os->file.rf;
228
+ FRT_DEREF(rf);
229
+ rf_close(rf);
230
+ }
231
+
232
+ void frt_ramo_write_to(FrtOutStream *os, FrtOutStream *other_o)
233
+ {
234
+ int i, len;
235
+ FrtRAMFile *rf = os->file.rf;
236
+ int last_buffer_number;
237
+ int last_buffer_offset;
238
+
239
+ frt_os_flush(os);
240
+ last_buffer_number = (int) (rf->len / FRT_BUFFER_SIZE);
241
+ last_buffer_offset = rf->len % FRT_BUFFER_SIZE;
242
+ for (i = 0; i <= last_buffer_number; i++) {
243
+ len = (i == last_buffer_number ? last_buffer_offset : FRT_BUFFER_SIZE);
244
+ frt_os_write_bytes(other_o, rf->buffers[i], len);
245
+ }
246
+ }
247
+
248
+ static const struct FrtOutStreamMethods RAM_OUT_STREAM_METHODS = {
249
+ ramo_flush_i,
250
+ ramo_seek_i,
251
+ ramo_close_i
252
+ };
253
+
254
+ FrtOutStream *frt_ram_new_buffer()
255
+ {
256
+ FrtRAMFile *rf = rf_new("");
257
+ FrtOutStream *os = frt_os_new();
258
+
259
+ FRT_DEREF(rf);
260
+ os->file.rf = rf;
261
+ os->pointer = 0;
262
+ os->m = &RAM_OUT_STREAM_METHODS;
263
+ return os;
264
+ }
265
+
266
+ void frt_ram_destroy_buffer(FrtOutStream *os)
267
+ {
268
+ rf_close(os->file.rf);
269
+ free(os);
270
+ }
271
+
272
+ static FrtOutStream *ram_new_output(FrtStore *store, const char *filename)
273
+ {
274
+ FrtRAMFile *rf = (FrtRAMFile *)frt_h_get(store->dir.ht, filename);
275
+ FrtOutStream *os = frt_os_new();
276
+
277
+ if (rf == NULL) {
278
+ rf = rf_new(filename);
279
+ frt_h_set(store->dir.ht, rf->name, rf);
280
+ }
281
+ FRT_REF(rf);
282
+ os->pointer = 0;
283
+ os->file.rf = rf;
284
+ os->m = &RAM_OUT_STREAM_METHODS;
285
+ return os;
286
+ }
287
+
288
+ static void rami_read_i(FrtInStream *is, frt_uchar *b, int len)
289
+ {
290
+ FrtRAMFile *rf = is->file.rf;
291
+
292
+ int offset = 0;
293
+ int buffer_number, buffer_offset, bytes_in_buffer, bytes_to_copy;
294
+ int remainder = len;
295
+ off_t start = is->d.pointer;
296
+ frt_uchar *buffer;
297
+
298
+ while (remainder > 0) {
299
+ buffer_number = (int) (start / FRT_BUFFER_SIZE);
300
+ buffer_offset = start % FRT_BUFFER_SIZE;
301
+ bytes_in_buffer = FRT_BUFFER_SIZE - buffer_offset;
302
+
303
+ if (bytes_in_buffer >= remainder) {
304
+ bytes_to_copy = remainder;
305
+ }
306
+ else {
307
+ bytes_to_copy = bytes_in_buffer;
308
+ }
309
+ buffer = rf->buffers[buffer_number];
310
+ memcpy(b + offset, buffer + buffer_offset, bytes_to_copy);
311
+ offset += bytes_to_copy;
312
+ start += bytes_to_copy;
313
+ remainder -= bytes_to_copy;
314
+ }
315
+
316
+ is->d.pointer += len;
317
+ }
318
+
319
+ static off_t rami_length_i(FrtInStream *is)
320
+ {
321
+ return is->file.rf->len;
322
+ }
323
+
324
+ static void rami_seek_i(FrtInStream *is, off_t pos)
325
+ {
326
+ is->d.pointer = pos;
327
+ }
328
+
329
+ static void rami_close_i(FrtInStream *is)
330
+ {
331
+ FrtRAMFile *rf = is->file.rf;
332
+ FRT_DEREF(rf);
333
+ rf_close(rf);
334
+ }
335
+
336
+ static const struct FrtInStreamMethods RAM_IN_STREAM_METHODS = {
337
+ rami_read_i,
338
+ rami_seek_i,
339
+ rami_length_i,
340
+ rami_close_i
341
+ };
342
+
343
+ static FrtInStream *ram_open_input(FrtStore *store, const char *filename)
344
+ {
345
+ FrtRAMFile *rf = (FrtRAMFile *)frt_h_get(store->dir.ht, filename);
346
+ FrtInStream *is = NULL;
347
+
348
+ if (rf == NULL) {
349
+ FRT_RAISE(FRT_FILE_NOT_FOUND_ERROR, "tried to open \"%s\" but it doesn't exist", filename);
350
+ }
351
+ FRT_REF(rf);
352
+ is = frt_is_new();
353
+ is->file.rf = rf;
354
+ is->d.pointer = 0;
355
+ is->m = &RAM_IN_STREAM_METHODS;
356
+
357
+ return is;
358
+ }
359
+
360
+ #define LOCK_OBTAIN_TIMEOUT 5
361
+
362
+ static int ram_lock_obtain(FrtLock *lock)
363
+ {
364
+ int ret = true;
365
+ if (ram_exists(lock->store, lock->name)) {
366
+ ret = false;
367
+ }
368
+ ram_touch(lock->store, lock->name);
369
+ return ret;
370
+ }
371
+
372
+ static int ram_lock_is_locked(FrtLock *lock)
373
+ {
374
+ return ram_exists(lock->store, lock->name);
375
+ }
376
+
377
+ static void ram_lock_release(FrtLock *lock)
378
+ {
379
+ ram_remove(lock->store, lock->name);
380
+ }
381
+
382
+ static FrtLock *ram_open_lock_i(FrtStore *store, const char *lockname)
383
+ {
384
+ FrtLock *lock = FRT_ALLOC(FrtLock);
385
+ char lname[100];
386
+ snprintf(lname, 100, "%s%s.lck", FRT_LOCK_PREFIX, lockname);
387
+ lock->name = frt_estrdup(lname);
388
+ lock->store = store;
389
+ lock->obtain = &ram_lock_obtain;
390
+ lock->release = &ram_lock_release;
391
+ lock->is_locked = &ram_lock_is_locked;
392
+ return lock;
393
+ }
394
+
395
+ static void ram_close_lock_i(FrtLock *lock)
396
+ {
397
+ free(lock->name);
398
+ free(lock);
399
+ }
400
+
401
+
402
+ FrtStore *frt_open_ram_store()
403
+ {
404
+ FrtStore *new_store = frt_store_new();
405
+
406
+ new_store->dir.ht = frt_h_new_str(NULL, rf_close);
407
+ new_store->touch = &ram_touch;
408
+ new_store->exists = &ram_exists;
409
+ new_store->remove = &ram_remove;
410
+ new_store->rename = &ram_rename;
411
+ new_store->count = &ram_count;
412
+ new_store->clear = &ram_clear;
413
+ new_store->clear_all = &ram_clear_all;
414
+ new_store->clear_locks = &ram_clear_locks;
415
+ new_store->length = &ram_length;
416
+ new_store->each = &ram_each;
417
+ new_store->new_output = &ram_new_output;
418
+ new_store->open_input = &ram_open_input;
419
+ new_store->open_lock_i = &ram_open_lock_i;
420
+ new_store->close_lock_i = &ram_close_lock_i;
421
+ new_store->close_i = &ram_close_i;
422
+ return new_store;
423
+ }
424
+
425
+ struct CopyFileArg
426
+ {
427
+ FrtStore *to_store, *from_store;
428
+ };
429
+
430
+ static void copy_files(const char *fname, void *arg)
431
+ {
432
+ struct CopyFileArg *cfa = (struct CopyFileArg *)arg;
433
+ FrtOutStream *os = cfa->to_store->new_output(cfa->to_store, fname);
434
+ FrtInStream *is = cfa->from_store->open_input(cfa->from_store, fname);
435
+ int len = (int)frt_is_length(is);
436
+ frt_uchar *buffer = FRT_ALLOC_N(frt_uchar, len + 1);
437
+
438
+ frt_is_read_bytes(is, buffer, len);
439
+ frt_os_write_bytes(os, buffer, len);
440
+
441
+ frt_is_close(is);
442
+ frt_os_close(os);
443
+ free(buffer);
444
+ }
445
+
446
+ FrtStore *frt_open_ram_store_and_copy(FrtStore *from_store, bool close_dir)
447
+ {
448
+ FrtStore *store = frt_open_ram_store();
449
+ struct CopyFileArg cfa;
450
+ cfa.to_store = store;
451
+ cfa.from_store = from_store;
452
+
453
+ from_store->each(from_store, &copy_files, &cfa);
454
+
455
+ if (close_dir) {
456
+ frt_store_deref(from_store);
457
+ }
458
+
459
+ return store;
460
+ }