jk-ferret 0.11.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. data/CHANGELOG +24 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +90 -0
  4. data/RELEASE_CHANGES +137 -0
  5. data/RELEASE_NOTES +60 -0
  6. data/Rakefile +443 -0
  7. data/TODO +109 -0
  8. data/TUTORIAL +231 -0
  9. data/bin/ferret-browser +79 -0
  10. data/ext/BZLIB_blocksort.c +1094 -0
  11. data/ext/BZLIB_bzlib.c +1578 -0
  12. data/ext/BZLIB_compress.c +672 -0
  13. data/ext/BZLIB_crctable.c +104 -0
  14. data/ext/BZLIB_decompress.c +626 -0
  15. data/ext/BZLIB_huffman.c +205 -0
  16. data/ext/BZLIB_randtable.c +84 -0
  17. data/ext/STEMMER_api.c +66 -0
  18. data/ext/STEMMER_libstemmer.c +93 -0
  19. data/ext/STEMMER_stem_ISO_8859_1_danish.c +337 -0
  20. data/ext/STEMMER_stem_ISO_8859_1_dutch.c +624 -0
  21. data/ext/STEMMER_stem_ISO_8859_1_english.c +1117 -0
  22. data/ext/STEMMER_stem_ISO_8859_1_finnish.c +762 -0
  23. data/ext/STEMMER_stem_ISO_8859_1_french.c +1246 -0
  24. data/ext/STEMMER_stem_ISO_8859_1_german.c +503 -0
  25. data/ext/STEMMER_stem_ISO_8859_1_hungarian.c +1230 -0
  26. data/ext/STEMMER_stem_ISO_8859_1_italian.c +1065 -0
  27. data/ext/STEMMER_stem_ISO_8859_1_norwegian.c +297 -0
  28. data/ext/STEMMER_stem_ISO_8859_1_porter.c +749 -0
  29. data/ext/STEMMER_stem_ISO_8859_1_portuguese.c +1017 -0
  30. data/ext/STEMMER_stem_ISO_8859_1_spanish.c +1093 -0
  31. data/ext/STEMMER_stem_ISO_8859_1_swedish.c +307 -0
  32. data/ext/STEMMER_stem_ISO_8859_2_romanian.c +998 -0
  33. data/ext/STEMMER_stem_KOI8_R_russian.c +700 -0
  34. data/ext/STEMMER_stem_UTF_8_danish.c +339 -0
  35. data/ext/STEMMER_stem_UTF_8_dutch.c +634 -0
  36. data/ext/STEMMER_stem_UTF_8_english.c +1125 -0
  37. data/ext/STEMMER_stem_UTF_8_finnish.c +768 -0
  38. data/ext/STEMMER_stem_UTF_8_french.c +1256 -0
  39. data/ext/STEMMER_stem_UTF_8_german.c +509 -0
  40. data/ext/STEMMER_stem_UTF_8_hungarian.c +1234 -0
  41. data/ext/STEMMER_stem_UTF_8_italian.c +1073 -0
  42. data/ext/STEMMER_stem_UTF_8_norwegian.c +299 -0
  43. data/ext/STEMMER_stem_UTF_8_porter.c +755 -0
  44. data/ext/STEMMER_stem_UTF_8_portuguese.c +1023 -0
  45. data/ext/STEMMER_stem_UTF_8_romanian.c +1004 -0
  46. data/ext/STEMMER_stem_UTF_8_russian.c +694 -0
  47. data/ext/STEMMER_stem_UTF_8_spanish.c +1097 -0
  48. data/ext/STEMMER_stem_UTF_8_swedish.c +309 -0
  49. data/ext/STEMMER_stem_UTF_8_turkish.c +2205 -0
  50. data/ext/STEMMER_utilities.c +478 -0
  51. data/ext/analysis.c +1710 -0
  52. data/ext/analysis.h +266 -0
  53. data/ext/api.h +26 -0
  54. data/ext/array.c +125 -0
  55. data/ext/array.h +62 -0
  56. data/ext/bitvector.c +96 -0
  57. data/ext/bitvector.h +594 -0
  58. data/ext/bzlib.h +282 -0
  59. data/ext/bzlib_private.h +503 -0
  60. data/ext/compound_io.c +384 -0
  61. data/ext/config.h +52 -0
  62. data/ext/document.c +159 -0
  63. data/ext/document.h +63 -0
  64. data/ext/except.c +102 -0
  65. data/ext/except.h +176 -0
  66. data/ext/extconf.rb +15 -0
  67. data/ext/ferret.c +416 -0
  68. data/ext/ferret.h +94 -0
  69. data/ext/field_index.c +262 -0
  70. data/ext/field_index.h +52 -0
  71. data/ext/filter.c +157 -0
  72. data/ext/fs_store.c +493 -0
  73. data/ext/global.c +458 -0
  74. data/ext/global.h +302 -0
  75. data/ext/hash.c +524 -0
  76. data/ext/hash.h +515 -0
  77. data/ext/hashset.c +192 -0
  78. data/ext/hashset.h +215 -0
  79. data/ext/header.h +58 -0
  80. data/ext/helper.c +63 -0
  81. data/ext/helper.h +21 -0
  82. data/ext/index.c +6804 -0
  83. data/ext/index.h +935 -0
  84. data/ext/internal.h +1019 -0
  85. data/ext/lang.c +10 -0
  86. data/ext/lang.h +68 -0
  87. data/ext/libstemmer.h +79 -0
  88. data/ext/mempool.c +88 -0
  89. data/ext/mempool.h +43 -0
  90. data/ext/modules.h +190 -0
  91. data/ext/multimapper.c +351 -0
  92. data/ext/multimapper.h +60 -0
  93. data/ext/posh.c +1006 -0
  94. data/ext/posh.h +973 -0
  95. data/ext/priorityqueue.c +149 -0
  96. data/ext/priorityqueue.h +155 -0
  97. data/ext/q_boolean.c +1621 -0
  98. data/ext/q_const_score.c +162 -0
  99. data/ext/q_filtered_query.c +212 -0
  100. data/ext/q_fuzzy.c +280 -0
  101. data/ext/q_match_all.c +149 -0
  102. data/ext/q_multi_term.c +673 -0
  103. data/ext/q_parser.c +3103 -0
  104. data/ext/q_phrase.c +1206 -0
  105. data/ext/q_prefix.c +98 -0
  106. data/ext/q_range.c +682 -0
  107. data/ext/q_span.c +2390 -0
  108. data/ext/q_term.c +337 -0
  109. data/ext/q_wildcard.c +167 -0
  110. data/ext/r_analysis.c +2626 -0
  111. data/ext/r_index.c +3468 -0
  112. data/ext/r_qparser.c +635 -0
  113. data/ext/r_search.c +4490 -0
  114. data/ext/r_store.c +513 -0
  115. data/ext/r_utils.c +1131 -0
  116. data/ext/ram_store.c +476 -0
  117. data/ext/scanner.c +895 -0
  118. data/ext/scanner.h +36 -0
  119. data/ext/scanner_mb.c +6701 -0
  120. data/ext/scanner_utf8.c +4415 -0
  121. data/ext/search.c +1864 -0
  122. data/ext/search.h +953 -0
  123. data/ext/similarity.c +151 -0
  124. data/ext/similarity.h +89 -0
  125. data/ext/sort.c +786 -0
  126. data/ext/stem_ISO_8859_1_danish.h +16 -0
  127. data/ext/stem_ISO_8859_1_dutch.h +16 -0
  128. data/ext/stem_ISO_8859_1_english.h +16 -0
  129. data/ext/stem_ISO_8859_1_finnish.h +16 -0
  130. data/ext/stem_ISO_8859_1_french.h +16 -0
  131. data/ext/stem_ISO_8859_1_german.h +16 -0
  132. data/ext/stem_ISO_8859_1_hungarian.h +16 -0
  133. data/ext/stem_ISO_8859_1_italian.h +16 -0
  134. data/ext/stem_ISO_8859_1_norwegian.h +16 -0
  135. data/ext/stem_ISO_8859_1_porter.h +16 -0
  136. data/ext/stem_ISO_8859_1_portuguese.h +16 -0
  137. data/ext/stem_ISO_8859_1_spanish.h +16 -0
  138. data/ext/stem_ISO_8859_1_swedish.h +16 -0
  139. data/ext/stem_ISO_8859_2_romanian.h +16 -0
  140. data/ext/stem_KOI8_R_russian.h +16 -0
  141. data/ext/stem_UTF_8_danish.h +16 -0
  142. data/ext/stem_UTF_8_dutch.h +16 -0
  143. data/ext/stem_UTF_8_english.h +16 -0
  144. data/ext/stem_UTF_8_finnish.h +16 -0
  145. data/ext/stem_UTF_8_french.h +16 -0
  146. data/ext/stem_UTF_8_german.h +16 -0
  147. data/ext/stem_UTF_8_hungarian.h +16 -0
  148. data/ext/stem_UTF_8_italian.h +16 -0
  149. data/ext/stem_UTF_8_norwegian.h +16 -0
  150. data/ext/stem_UTF_8_porter.h +16 -0
  151. data/ext/stem_UTF_8_portuguese.h +16 -0
  152. data/ext/stem_UTF_8_romanian.h +16 -0
  153. data/ext/stem_UTF_8_russian.h +16 -0
  154. data/ext/stem_UTF_8_spanish.h +16 -0
  155. data/ext/stem_UTF_8_swedish.h +16 -0
  156. data/ext/stem_UTF_8_turkish.h +16 -0
  157. data/ext/stopwords.c +410 -0
  158. data/ext/store.c +698 -0
  159. data/ext/store.h +799 -0
  160. data/ext/symbol.c +10 -0
  161. data/ext/symbol.h +23 -0
  162. data/ext/term_vectors.c +73 -0
  163. data/ext/threading.h +31 -0
  164. data/ext/win32.h +62 -0
  165. data/lib/ferret.rb +30 -0
  166. data/lib/ferret/browser.rb +246 -0
  167. data/lib/ferret/browser/s/global.js +192 -0
  168. data/lib/ferret/browser/s/style.css +148 -0
  169. data/lib/ferret/browser/views/document/list.rhtml +49 -0
  170. data/lib/ferret/browser/views/document/show.rhtml +27 -0
  171. data/lib/ferret/browser/views/error/index.rhtml +7 -0
  172. data/lib/ferret/browser/views/help/index.rhtml +8 -0
  173. data/lib/ferret/browser/views/home/index.rhtml +29 -0
  174. data/lib/ferret/browser/views/layout.rhtml +22 -0
  175. data/lib/ferret/browser/views/term-vector/index.rhtml +4 -0
  176. data/lib/ferret/browser/views/term/index.rhtml +199 -0
  177. data/lib/ferret/browser/views/term/termdocs.rhtml +1 -0
  178. data/lib/ferret/browser/webrick.rb +14 -0
  179. data/lib/ferret/document.rb +130 -0
  180. data/lib/ferret/field_infos.rb +44 -0
  181. data/lib/ferret/field_symbol.rb +87 -0
  182. data/lib/ferret/index.rb +973 -0
  183. data/lib/ferret/number_tools.rb +157 -0
  184. data/lib/ferret/version.rb +3 -0
  185. data/setup.rb +1555 -0
  186. data/test/long_running/largefile/tc_largefile.rb +46 -0
  187. data/test/test_all.rb +5 -0
  188. data/test/test_helper.rb +29 -0
  189. data/test/test_installed.rb +1 -0
  190. data/test/threading/number_to_spoken.rb +132 -0
  191. data/test/threading/thread_safety_index_test.rb +88 -0
  192. data/test/threading/thread_safety_read_write_test.rb +73 -0
  193. data/test/threading/thread_safety_test.rb +133 -0
  194. data/test/unit/analysis/tc_analyzer.rb +550 -0
  195. data/test/unit/analysis/tc_token_stream.rb +653 -0
  196. data/test/unit/index/tc_index.rb +867 -0
  197. data/test/unit/index/tc_index_reader.rb +699 -0
  198. data/test/unit/index/tc_index_writer.rb +447 -0
  199. data/test/unit/index/th_doc.rb +332 -0
  200. data/test/unit/query_parser/tc_query_parser.rb +238 -0
  201. data/test/unit/search/tc_filter.rb +156 -0
  202. data/test/unit/search/tc_fuzzy_query.rb +147 -0
  203. data/test/unit/search/tc_index_searcher.rb +67 -0
  204. data/test/unit/search/tc_multi_searcher.rb +128 -0
  205. data/test/unit/search/tc_multiple_search_requests.rb +58 -0
  206. data/test/unit/search/tc_search_and_sort.rb +179 -0
  207. data/test/unit/search/tc_sort.rb +49 -0
  208. data/test/unit/search/tc_sort_field.rb +27 -0
  209. data/test/unit/search/tc_spans.rb +190 -0
  210. data/test/unit/search/tm_searcher.rb +436 -0
  211. data/test/unit/store/tc_fs_store.rb +115 -0
  212. data/test/unit/store/tc_ram_store.rb +35 -0
  213. data/test/unit/store/tm_store.rb +34 -0
  214. data/test/unit/store/tm_store_lock.rb +68 -0
  215. data/test/unit/tc_document.rb +81 -0
  216. data/test/unit/tc_field_symbol.rb +26 -0
  217. data/test/unit/ts_analysis.rb +2 -0
  218. data/test/unit/ts_index.rb +2 -0
  219. data/test/unit/ts_largefile.rb +4 -0
  220. data/test/unit/ts_query_parser.rb +2 -0
  221. data/test/unit/ts_search.rb +2 -0
  222. data/test/unit/ts_store.rb +2 -0
  223. data/test/unit/ts_utils.rb +2 -0
  224. data/test/unit/utils/tc_bit_vector.rb +295 -0
  225. data/test/unit/utils/tc_number_tools.rb +117 -0
  226. data/test/unit/utils/tc_priority_queue.rb +106 -0
  227. data/test/utils/content_generator.rb +226 -0
  228. metadata +319 -0
data/ext/store.c ADDED
@@ -0,0 +1,698 @@
1
+ #include "store.h"
2
+ #include <string.h>
3
+ #include "internal.h"
4
+
5
+ #define VINT_MAX_LEN 10
6
+ #define VINT_END BUFFER_SIZE - VINT_MAX_LEN
7
+
8
+ /*
9
+ * TODO: add try finally
10
+ */
11
+ void with_lock(Lock *lock, void (*func)(void *arg), void *arg)
12
+ {
13
+ if (!lock->obtain(lock)) {
14
+ RAISE(IO_ERROR, "couldn't obtain lock \"%s\"", lock->name);
15
+ }
16
+ func(arg);
17
+ lock->release(lock);
18
+ }
19
+
20
+ /*
21
+ * TODO: add try finally
22
+ */
23
+ void with_lock_name(Store *store, const char *lock_name,
24
+ void (*func)(void *arg), void *arg)
25
+ {
26
+ Lock *lock = store->open_lock_i(store, lock_name);
27
+ if (!lock->obtain(lock)) {
28
+ RAISE(LOCK_ERROR, "couldn't obtain lock \"%s\"", lock->name);
29
+ }
30
+ func(arg);
31
+ lock->release(lock);
32
+ store->close_lock_i(lock);
33
+ }
34
+
35
+ void store_deref(Store *store)
36
+ {
37
+ mutex_lock(&store->mutex_i);
38
+ if (--store->ref_cnt <= 0) {
39
+ store->close_i(store);
40
+ }
41
+ else {
42
+ mutex_unlock(&store->mutex_i);
43
+ }
44
+ }
45
+
46
+ Lock *open_lock(Store *store, const char *lockname)
47
+ {
48
+ Lock *lock = store->open_lock_i(store, lockname);
49
+ hs_add(store->locks, lock);
50
+ return lock;
51
+ }
52
+
53
+ void close_lock(Lock *lock)
54
+ {
55
+ hs_del(lock->store->locks, lock);
56
+ }
57
+
58
+ static void close_lock_i(Lock *lock)
59
+ {
60
+ lock->store->close_lock_i(lock);
61
+ }
62
+
63
+ /**
64
+ * Create a store struct initializing the mutex.
65
+ */
66
+ Store *store_new()
67
+ {
68
+ Store *store = ALLOC(Store);
69
+ store->ref_cnt = 1;
70
+ mutex_init(&store->mutex_i, NULL);
71
+ mutex_init(&store->mutex, NULL);
72
+ store->locks = hs_new_ptr((free_ft)&close_lock_i);
73
+ return store;
74
+ }
75
+
76
+ /**
77
+ * Destroy the store freeing allocated resources
78
+ *
79
+ * @param store the store struct to free
80
+ */
81
+ void store_destroy(Store *store)
82
+ {
83
+ mutex_destroy(&store->mutex_i);
84
+ mutex_destroy(&store->mutex);
85
+ hs_destroy(store->locks);
86
+ free(store);
87
+ }
88
+
89
+ /**
90
+ * Create a newly allocated and initialized OutStream object
91
+ *
92
+ * @return a newly allocated and initialized OutStream object
93
+ */
94
+ OutStream *os_new()
95
+ {
96
+ OutStream *os = ALLOC(OutStream);
97
+ os->buf.start = 0;
98
+ os->buf.pos = 0;
99
+ os->buf.len = 0;
100
+ return os;
101
+ }
102
+
103
+ /**
104
+ * Flush the countents of the OutStream's buffers
105
+ *
106
+ * @param the OutStream to flush
107
+ */
108
+ INLINE void os_flush(OutStream *os)
109
+ {
110
+ os->m->flush_i(os, os->buf.buf, os->buf.pos);
111
+ os->buf.start += os->buf.pos;
112
+ os->buf.pos = 0;
113
+ }
114
+
115
+ void os_close(OutStream *os)
116
+ {
117
+ os_flush(os);
118
+ os->m->close_i(os);
119
+ free(os);
120
+ }
121
+
122
+ off_t os_pos(OutStream *os)
123
+ {
124
+ return os->buf.start + os->buf.pos;
125
+ }
126
+
127
+ void os_seek(OutStream *os, off_t new_pos)
128
+ {
129
+ os_flush(os);
130
+ os->buf.start = new_pos;
131
+ os->m->seek_i(os, new_pos);
132
+ }
133
+
134
+ /**
135
+ * Unsafe alternative to os_write_byte. Only use this method if you know there
136
+ * is no chance of buffer overflow.
137
+ */
138
+ #define write_byte(os, b) os->buf.buf[os->buf.pos++] = (uchar)b
139
+
140
+ /**
141
+ * Write a single byte +b+ to the OutStream +os+
142
+ *
143
+ * @param os the OutStream to write to
144
+ * @param b the byte to write
145
+ * @raise IO_ERROR if there is an IO error writing to the filesystem
146
+ */
147
+ INLINE void os_write_byte(OutStream *os, uchar b)
148
+ {
149
+ if (os->buf.pos >= BUFFER_SIZE) {
150
+ os_flush(os);
151
+ }
152
+ write_byte(os, b);
153
+ }
154
+
155
+ void os_write_bytes(OutStream *os, const uchar *buf, int len)
156
+ {
157
+ if (os->buf.pos > 0) { /* flush buffer */
158
+ os_flush(os);
159
+ }
160
+
161
+ if (len < BUFFER_SIZE) {
162
+ os->m->flush_i(os, buf, len);
163
+ os->buf.start += len;
164
+ }
165
+ else {
166
+ int pos = 0;
167
+ int size;
168
+ while (pos < len) {
169
+ if (len - pos < BUFFER_SIZE) {
170
+ size = len - pos;
171
+ }
172
+ else {
173
+ size = BUFFER_SIZE;
174
+ }
175
+ os->m->flush_i(os, buf + pos, size);
176
+ pos += size;
177
+ os->buf.start += size;
178
+ }
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Create a newly allocated and initialized InStream
184
+ *
185
+ * @return a newly allocated and initialized InStream
186
+ */
187
+ InStream *is_new()
188
+ {
189
+ InStream *is = ALLOC(InStream);
190
+ is->buf.start = 0;
191
+ is->buf.pos = 0;
192
+ is->buf.len = 0;
193
+ is->ref_cnt_ptr = ALLOC_AND_ZERO(int);
194
+ return is;
195
+ }
196
+
197
+ /**
198
+ * Refill the InStream's buffer from the store source (filesystem or memory).
199
+ *
200
+ * @param is the InStream to refill
201
+ * @raise IO_ERROR if there is a error reading from the filesystem
202
+ * @raise EOF_ERROR if there is an attempt to read past the end of the file
203
+ */
204
+ static void is_refill(InStream *is)
205
+ {
206
+ off_t start = is->buf.start + is->buf.pos;
207
+ off_t last = start + BUFFER_SIZE;
208
+ off_t flen = is->m->length_i(is);
209
+
210
+ if (last > flen) { /* don't read past EOF */
211
+ last = flen;
212
+ }
213
+
214
+ is->buf.len = last - start;
215
+ if (is->buf.len <= 0) {
216
+ RAISE(EOF_ERROR, "current pos = %"OFF_T_PFX"d, "
217
+ "file length = %"OFF_T_PFX"d", start, flen);
218
+ }
219
+
220
+ is->m->read_i(is, is->buf.buf, is->buf.len);
221
+
222
+ is->buf.start = start;
223
+ is->buf.pos = 0;
224
+ }
225
+
226
+ /**
227
+ * Unsafe alternative to is_read_byte. Only use this method when you know
228
+ * there is no chance that you will read past the end of the InStream's
229
+ * buffer.
230
+ */
231
+ #define read_byte(is) is->buf.buf[is->buf.pos++]
232
+
233
+ /**
234
+ * Read a singly byte (unsigned char) from the InStream +is+.
235
+ *
236
+ * @param is the Instream to read from
237
+ * @return a single unsigned char read from the InStream +is+
238
+ * @raise IO_ERROR if there is a error reading from the filesystem
239
+ * @raise EOF_ERROR if there is an attempt to read past the end of the file
240
+ */
241
+ INLINE uchar is_read_byte(InStream *is)
242
+ {
243
+ if (is->buf.pos >= is->buf.len) {
244
+ is_refill(is);
245
+ }
246
+
247
+ return read_byte(is);
248
+ }
249
+
250
+ off_t is_pos(InStream *is)
251
+ {
252
+ return is->buf.start + is->buf.pos;
253
+ }
254
+
255
+ uchar *is_read_bytes(InStream *is, uchar *buf, int len)
256
+ {
257
+ int i;
258
+ off_t start;
259
+
260
+ if ((is->buf.pos + len) < is->buf.len) {
261
+ for (i = 0; i < len; i++) {
262
+ buf[i] = read_byte(is);
263
+ }
264
+ }
265
+ else { /* read all-at-once */
266
+ start = is_pos(is);
267
+ is->m->seek_i(is, start);
268
+ is->m->read_i(is, buf, len);
269
+
270
+ is->buf.start = start + len; /* adjust stream variables */
271
+ is->buf.pos = 0;
272
+ is->buf.len = 0; /* trigger refill on read */
273
+ }
274
+ return buf;
275
+ }
276
+
277
+ void is_seek(InStream *is, off_t pos)
278
+ {
279
+ if (pos >= is->buf.start && pos < (is->buf.start + is->buf.len)) {
280
+ is->buf.pos = pos - is->buf.start; /* seek within buffer */
281
+ }
282
+ else {
283
+ is->buf.start = pos;
284
+ is->buf.pos = 0;
285
+ is->buf.len = 0; /* trigger refill() on read() */
286
+ is->m->seek_i(is, pos);
287
+ }
288
+ }
289
+
290
+ void is_close(InStream *is)
291
+ {
292
+ if (--(*(is->ref_cnt_ptr)) < 0) {
293
+ is->m->close_i(is);
294
+ free(is->ref_cnt_ptr);
295
+ }
296
+ free(is);
297
+ }
298
+
299
+ InStream *is_clone(InStream *is)
300
+ {
301
+ InStream *new_index_i = ALLOC(InStream);
302
+ memcpy(new_index_i, is, sizeof(InStream));
303
+ (*(new_index_i->ref_cnt_ptr))++;
304
+ return new_index_i;
305
+ }
306
+
307
+ i32 is_read_i32(InStream *is)
308
+ {
309
+ return ((i32)is_read_byte(is) << 24) |
310
+ ((i32)is_read_byte(is) << 16) |
311
+ ((i32)is_read_byte(is) << 8) |
312
+ ((i32)is_read_byte(is));
313
+ }
314
+
315
+ i64 is_read_i64(InStream *is)
316
+ {
317
+ return ((i64)is_read_byte(is) << 56) |
318
+ ((i64)is_read_byte(is) << 48) |
319
+ ((i64)is_read_byte(is) << 40) |
320
+ ((i64)is_read_byte(is) << 32) |
321
+ ((i64)is_read_byte(is) << 24) |
322
+ ((i64)is_read_byte(is) << 16) |
323
+ ((i64)is_read_byte(is) << 8) |
324
+ ((i64)is_read_byte(is));
325
+ }
326
+
327
+ u32 is_read_u32(InStream *is)
328
+ {
329
+ return ((u32)is_read_byte(is) << 24) |
330
+ ((u32)is_read_byte(is) << 16) |
331
+ ((u32)is_read_byte(is) << 8) |
332
+ ((u32)is_read_byte(is));
333
+ }
334
+
335
+ u64 is_read_u64(InStream *is)
336
+ {
337
+ return ((u64)is_read_byte(is) << 56) |
338
+ ((u64)is_read_byte(is) << 48) |
339
+ ((u64)is_read_byte(is) << 40) |
340
+ ((u64)is_read_byte(is) << 32) |
341
+ ((u64)is_read_byte(is) << 24) |
342
+ ((u64)is_read_byte(is) << 16) |
343
+ ((u64)is_read_byte(is) << 8) |
344
+ ((u64)is_read_byte(is));
345
+ }
346
+
347
+ /* optimized to use unchecked read_byte if there is definitely space */
348
+ INLINE unsigned int is_read_vint(InStream *is)
349
+ {
350
+ register unsigned int res, b;
351
+ register int shift = 7;
352
+
353
+ if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
354
+ b = is_read_byte(is);
355
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
356
+
357
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
358
+ b = is_read_byte(is);
359
+ res |= (b & 0x7F) << shift;
360
+ shift += 7;
361
+ }
362
+ }
363
+ else { /* unchecked optimization */
364
+ b = read_byte(is);
365
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
366
+
367
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
368
+ b = read_byte(is);
369
+ res |= (b & 0x7F) << shift;
370
+ shift += 7;
371
+ }
372
+ }
373
+
374
+ return res;
375
+ }
376
+
377
+ /* optimized to use unchecked read_byte if there is definitely space */
378
+ INLINE off_t is_read_voff_t(InStream *is)
379
+ {
380
+ register off_t res, b;
381
+ register int shift = 7;
382
+
383
+ if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
384
+ b = is_read_byte(is);
385
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
386
+
387
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
388
+ b = is_read_byte(is);
389
+ res |= (b & 0x7F) << shift;
390
+ shift += 7;
391
+ }
392
+ }
393
+ else { /* unchecked optimization */
394
+ b = read_byte(is);
395
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
396
+
397
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
398
+ b = read_byte(is);
399
+ res |= (b & 0x7F) << shift;
400
+ shift += 7;
401
+ }
402
+ }
403
+
404
+ return res;
405
+ }
406
+
407
+ /* optimized to use unchecked read_byte if there is definitely space */
408
+ INLINE u64 is_read_vll(InStream *is)
409
+ {
410
+ register u64 res, b;
411
+ register int shift = 7;
412
+
413
+ if (is->buf.pos > (is->buf.len - VINT_MAX_LEN)) {
414
+ b = is_read_byte(is);
415
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
416
+
417
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
418
+ b = is_read_byte(is);
419
+ res |= (b & 0x7F) << shift;
420
+ shift += 7;
421
+ }
422
+ }
423
+ else { /* unchecked optimization */
424
+ b = read_byte(is);
425
+ res = b & 0x7F; /* 0x7F = 0b01111111 */
426
+
427
+ while ((b & 0x80) != 0) { /* 0x80 = 0b10000000 */
428
+ b = read_byte(is);
429
+ res |= (b & 0x7F) << shift;
430
+ shift += 7;
431
+ }
432
+ }
433
+
434
+ return res;
435
+ }
436
+
437
+ INLINE void is_skip_vints(InStream *is, register int cnt)
438
+ {
439
+ for (; cnt > 0; cnt--) {
440
+ while ((is_read_byte(is) & 0x80) != 0) {
441
+ }
442
+ }
443
+ }
444
+
445
+ /*
446
+ * FIXME: Not used. Do we need/want this?
447
+ static INLINE void is_read_chars(InStream *is, char *buffer,
448
+ int off, int len)
449
+ {
450
+ int end, i;
451
+
452
+ end = off + len;
453
+
454
+ for (i = off; i < end; i++) {
455
+ buffer[i] = is_read_byte(is);
456
+ }
457
+ }
458
+ */
459
+
460
+ char *is_read_string(InStream *is)
461
+ {
462
+ register int length = (int) is_read_vint(is);
463
+ char *str = ALLOC_N(char, length + 1);
464
+ str[length] = '\0';
465
+
466
+ if (is->buf.pos > (is->buf.len - length)) {
467
+ register int i;
468
+ for (i = 0; i < length; i++) {
469
+ str[i] = is_read_byte(is);
470
+ }
471
+ }
472
+ else { /* unchecked optimization */
473
+ memcpy(str, is->buf.buf + is->buf.pos, length);
474
+ is->buf.pos += length;
475
+ }
476
+
477
+ return str;
478
+ }
479
+
480
+ char *is_read_string_safe(InStream *is)
481
+ {
482
+ register int length = (int) is_read_vint(is);
483
+ char *str = ALLOC_N(char, length + 1);
484
+ str[length] = '\0';
485
+
486
+ TRY
487
+ if (is->buf.pos > (is->buf.len - length)) {
488
+ register int i;
489
+ for (i = 0; i < length; i++) {
490
+ str[i] = is_read_byte(is);
491
+ }
492
+ }
493
+ else { /* unchecked optimization */
494
+ memcpy(str, is->buf.buf + is->buf.pos, length);
495
+ is->buf.pos += length;
496
+ }
497
+ XCATCHALL
498
+ free(str);
499
+ XENDTRY
500
+
501
+ return str;
502
+ }
503
+
504
+ void os_write_i32(OutStream *os, i32 num)
505
+ {
506
+ os_write_byte(os, (uchar)((num >> 24) & 0xFF));
507
+ os_write_byte(os, (uchar)((num >> 16) & 0xFF));
508
+ os_write_byte(os, (uchar)((num >> 8) & 0xFF));
509
+ os_write_byte(os, (uchar)(num & 0xFF));
510
+ }
511
+
512
+ void os_write_i64(OutStream *os, i64 num)
513
+ {
514
+ os_write_byte(os, (uchar)((num >> 56) & 0xFF));
515
+ os_write_byte(os, (uchar)((num >> 48) & 0xFF));
516
+ os_write_byte(os, (uchar)((num >> 40) & 0xFF));
517
+ os_write_byte(os, (uchar)((num >> 32) & 0xFF));
518
+ os_write_byte(os, (uchar)((num >> 24) & 0xFF));
519
+ os_write_byte(os, (uchar)((num >> 16) & 0xFF));
520
+ os_write_byte(os, (uchar)((num >> 8) & 0xFF));
521
+ os_write_byte(os, (uchar)(num & 0xFF));
522
+ }
523
+
524
+ void os_write_u32(OutStream *os, u32 num)
525
+ {
526
+ os_write_byte(os, (uchar)((num >> 24) & 0xFF));
527
+ os_write_byte(os, (uchar)((num >> 16) & 0xFF));
528
+ os_write_byte(os, (uchar)((num >> 8) & 0xFF));
529
+ os_write_byte(os, (uchar)(num & 0xFF));
530
+ }
531
+
532
+ void os_write_u64(OutStream *os, u64 num)
533
+ {
534
+ os_write_byte(os, (uchar)((num >> 56) & 0xFF));
535
+ os_write_byte(os, (uchar)((num >> 48) & 0xFF));
536
+ os_write_byte(os, (uchar)((num >> 40) & 0xFF));
537
+ os_write_byte(os, (uchar)((num >> 32) & 0xFF));
538
+ os_write_byte(os, (uchar)((num >> 24) & 0xFF));
539
+ os_write_byte(os, (uchar)((num >> 16) & 0xFF));
540
+ os_write_byte(os, (uchar)((num >> 8) & 0xFF));
541
+ os_write_byte(os, (uchar)(num & 0xFF));
542
+ }
543
+
544
+ /* optimized to use an unchecked write if there is space */
545
+ INLINE void os_write_vint(OutStream *os, register unsigned int num)
546
+ {
547
+ if (os->buf.pos > VINT_END) {
548
+ while (num > 127) {
549
+ os_write_byte(os, (uchar)((num & 0x7f) | 0x80));
550
+ num >>= 7;
551
+ }
552
+ os_write_byte(os, (uchar)(num));
553
+ }
554
+ else {
555
+ while (num > 127) {
556
+ write_byte(os, (uchar)((num & 0x7f) | 0x80));
557
+ num >>= 7;
558
+ }
559
+ write_byte(os, (uchar)(num));
560
+ }
561
+ }
562
+
563
+ /* optimized to use an unchecked write if there is space */
564
+ INLINE void os_write_voff_t(OutStream *os, register off_t num)
565
+ {
566
+ if (os->buf.pos > VINT_END) {
567
+ while (num > 127) {
568
+ os_write_byte(os, (uchar)((num & 0x7f) | 0x80));
569
+ num >>= 7;
570
+ }
571
+ os_write_byte(os, (uchar)num);
572
+ }
573
+ else {
574
+ while (num > 127) {
575
+ write_byte(os, (uchar)((num & 0x7f) | 0x80));
576
+ num >>= 7;
577
+ }
578
+ write_byte(os, (uchar)num);
579
+ }
580
+ }
581
+
582
+ /* optimized to use an unchecked write if there is space */
583
+ INLINE void os_write_vll(OutStream *os, register u64 num)
584
+ {
585
+ if (os->buf.pos > VINT_END) {
586
+ while (num > 127) {
587
+ os_write_byte(os, (uchar)((num & 0x7f) | 0x80));
588
+ num >>= 7;
589
+ }
590
+ os_write_byte(os, (uchar)num);
591
+ }
592
+ else {
593
+ while (num > 127) {
594
+ write_byte(os, (uchar)((num & 0x7f) | 0x80));
595
+ num >>= 7;
596
+ }
597
+ write_byte(os, (uchar)num);
598
+ }
599
+ }
600
+
601
+ INLINE void os_write_string_len(OutStream *os, const char *str, int len)
602
+ {
603
+ os_write_vint(os, len);
604
+ os_write_bytes(os, (uchar *)str, len);
605
+ }
606
+ void os_write_string(OutStream *os, const char *str)
607
+ {
608
+ os_write_string_len(os, str, (int)strlen(str));
609
+ }
610
+
611
+ /**
612
+ * Determine if the filename is the name of a lock file. Return 1 if it is, 0
613
+ * otherwise.
614
+ *
615
+ * @param filename the name of the file to check
616
+ * @return 1 (true) if the file is a lock file, 0 (false) otherwise
617
+ */
618
+ int file_is_lock(const char *filename)
619
+ {
620
+ int start = (int) strlen(filename) - 4;
621
+ return ((start > 0) && (strcmp(LOCK_EXT, &filename[start]) == 0));
622
+ }
623
+
624
+ void is2os_copy_bytes(InStream *is, OutStream *os, int cnt)
625
+ {
626
+ int len;
627
+ uchar buf[BUFFER_SIZE];
628
+
629
+ for (; cnt > 0; cnt -= BUFFER_SIZE) {
630
+ len = ((cnt > BUFFER_SIZE) ? BUFFER_SIZE : cnt);
631
+ is_read_bytes(is, buf, len);
632
+ os_write_bytes(os, buf, len);
633
+ }
634
+ }
635
+
636
+ void is2os_copy_vints(InStream *is, OutStream *os, int cnt)
637
+ {
638
+ uchar b;
639
+ for (; cnt > 0; cnt--) {
640
+ while (((b = is_read_byte(is)) & 0x80) != 0) {
641
+ os_write_byte(os, b);
642
+ }
643
+ os_write_byte(os, b);
644
+ }
645
+ }
646
+
647
+ /**
648
+ * Test argument used to test the store->each function
649
+ */
650
+ struct FileNameListArg
651
+ {
652
+ int count;
653
+ int size;
654
+ int total_len;
655
+ char **files;
656
+ };
657
+
658
+ /**
659
+ * Test function used to test store->each function
660
+ */
661
+ static void add_file_name(const char *fname, void *arg)
662
+ {
663
+ struct FileNameListArg *fnl = (struct FileNameListArg *)arg;
664
+ if (fnl->count >= fnl->size) {
665
+ fnl->size *= 2;
666
+ REALLOC_N(fnl->files, char *, fnl->size);
667
+ }
668
+ fnl->files[fnl->count++] = estrdup(fname);
669
+ fnl->total_len += strlen(fname) + 2;
670
+ }
671
+
672
+ char *store_to_s(Store *store)
673
+ {
674
+ struct FileNameListArg fnl;
675
+ char *buf, *b;
676
+ int i;
677
+ fnl.count = 0;
678
+ fnl.size = 16;
679
+ fnl.total_len = 10;
680
+ fnl.files = ALLOC_N(char *, 16);
681
+
682
+ store->each(store, &add_file_name, &fnl);
683
+ qsort(fnl.files, fnl.count, sizeof(char *), &scmp);
684
+ b = buf = ALLOC_N(char, fnl.total_len);
685
+
686
+ for (i = 0; i < fnl.count; i++) {
687
+ char *fn = fnl.files[i];
688
+ int len = strlen(fn);
689
+ memcpy(b, fn, len);
690
+ b += len;
691
+ *b++ = '\n';
692
+ free(fn);
693
+ }
694
+ *b = '\0';
695
+ free(fnl.files);
696
+
697
+ return buf;
698
+ }