sdsykes-ferret 0.11.6.19

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