isomorfeus-ferret 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +612 -0
- data/README.md +44 -0
- data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
- data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
- data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
- data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
- data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
- data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
- data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
- data/ext/isomorfeus_ferret_ext/email.rl +21 -0
- data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
- data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
- data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
- data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
- data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
- data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
- data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
- data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
- data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
- data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
- data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
- data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
- data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
- data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
- data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
- data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
- data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
- data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
- data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
- data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
- data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
- data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
- data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
- data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
- data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
- data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
- data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
- data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
- data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
- data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
- data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
- data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
- data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
- data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
- data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
- data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
- data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
- data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
- data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
- data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
- data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
- data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
- data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
- data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
- data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
- data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
- data/ext/isomorfeus_ferret_ext/test.c +850 -0
- data/ext/isomorfeus_ferret_ext/test.h +416 -0
- data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
- data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
- data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
- data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
- data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
- data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
- data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
- data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
- data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
- data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
- data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
- data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
- data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
- data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
- data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
- data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
- data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
- data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
- data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
- data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
- data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
- data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
- data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
- data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
- data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
- data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
- data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
- data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
- data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
- data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
- data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
- data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
- data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
- data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
- data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
- data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
- data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
- data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
- data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
- data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
- data/ext/isomorfeus_ferret_ext/url.rl +27 -0
- data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
- data/lib/isomorfeus/ferret/document.rb +132 -0
- data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
- data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
- data/lib/isomorfeus/ferret/index/index.rb +970 -0
- data/lib/isomorfeus/ferret/monitor.rb +323 -0
- data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
- data/lib/isomorfeus/ferret/version.rb +5 -0
- data/lib/isomorfeus-ferret.rb +8 -0
- metadata +307 -0
@@ -0,0 +1,323 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
# = monitor.rb
|
3
|
+
#
|
4
|
+
# Copyright (C) 2001 Shugo Maeda <shugo@ruby-lang.org>
|
5
|
+
#
|
6
|
+
# This library is distributed under the terms of the Ruby license.
|
7
|
+
# You can freely distribute/modify this library.
|
8
|
+
#
|
9
|
+
|
10
|
+
#
|
11
|
+
# In concurrent programming, a monitor is an object or module intended to be
|
12
|
+
# used safely by more than one thread. The defining characteristic of a
|
13
|
+
# monitor is that its methods are executed with mutual exclusion. That is, at
|
14
|
+
# each point in time, at most one thread may be executing any of its methods.
|
15
|
+
# This mutual exclusion greatly simplifies reasoning about the implementation
|
16
|
+
# of monitors compared to reasoning about parallel code that updates a data
|
17
|
+
# structure.
|
18
|
+
#
|
19
|
+
# You can read more about the general principles on the Wikipedia page for
|
20
|
+
# Monitors[http://en.wikipedia.org/wiki/Monitor_%28synchronization%29]
|
21
|
+
#
|
22
|
+
# == Examples
|
23
|
+
#
|
24
|
+
# === Simple object.extend
|
25
|
+
#
|
26
|
+
# require 'monitor.rb'
|
27
|
+
#
|
28
|
+
# buf = []
|
29
|
+
# buf.extend(MonitorMixin)
|
30
|
+
# empty_cond = buf.new_cond
|
31
|
+
#
|
32
|
+
# # consumer
|
33
|
+
# Thread.start do
|
34
|
+
# loop do
|
35
|
+
# buf.synchronize do
|
36
|
+
# empty_cond.wait_while { buf.empty? }
|
37
|
+
# print buf.shift
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # producer
|
43
|
+
# while line = ARGF.gets
|
44
|
+
# buf.synchronize do
|
45
|
+
# buf.push(line)
|
46
|
+
# empty_cond.signal
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# The consumer thread waits for the producer thread to push a line to buf
|
51
|
+
# while <tt>buf.empty?</tt>. The producer thread (main thread) reads a
|
52
|
+
# line from ARGF and pushes it into buf then calls <tt>empty_cond.signal</tt>
|
53
|
+
# to notify the consumer thread of new data.
|
54
|
+
#
|
55
|
+
# === Simple Class include
|
56
|
+
#
|
57
|
+
# require 'monitor'
|
58
|
+
#
|
59
|
+
# class SynchronizedArray < Array
|
60
|
+
#
|
61
|
+
# include MonitorMixin
|
62
|
+
#
|
63
|
+
# def initialize(*args)
|
64
|
+
# super(*args)
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# alias :old_shift :shift
|
68
|
+
# alias :old_unshift :unshift
|
69
|
+
#
|
70
|
+
# def shift(n=1)
|
71
|
+
# self.synchronize do
|
72
|
+
# self.old_shift(n)
|
73
|
+
# end
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# def unshift(item)
|
77
|
+
# self.synchronize do
|
78
|
+
# self.old_unshift(item)
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # other methods ...
|
83
|
+
# end
|
84
|
+
#
|
85
|
+
# +SynchronizedArray+ implements an Array with synchronized access to items.
|
86
|
+
# This Class is implemented as subclass of Array which includes the
|
87
|
+
# MonitorMixin module.
|
88
|
+
#
|
89
|
+
module Isomorfeus
|
90
|
+
module Ferret
|
91
|
+
module MonitorMixin
|
92
|
+
#
|
93
|
+
# FIXME: This isn't documented in Nutshell.
|
94
|
+
#
|
95
|
+
# Since MonitorMixin.new_cond returns a ConditionVariable, and the example
|
96
|
+
# above calls while_wait and signal, this class should be documented.
|
97
|
+
#
|
98
|
+
class ConditionVariable
|
99
|
+
class Timeout < Exception; end
|
100
|
+
|
101
|
+
#
|
102
|
+
# Releases the lock held in the associated monitor and waits; reacquires the lock on wakeup.
|
103
|
+
#
|
104
|
+
# If +timeout+ is given, this method returns after +timeout+ seconds passed,
|
105
|
+
# even if no other thread doesn't signal.
|
106
|
+
#
|
107
|
+
def wait(timeout = nil)
|
108
|
+
Thread.handle_interrupt(Exception => :never) do
|
109
|
+
@monitor.__send__(:mon_check_owner)
|
110
|
+
count = @monitor.__send__(:mon_exit_for_cond)
|
111
|
+
begin
|
112
|
+
Thread.handle_interrupt(Exception => :immediate) do
|
113
|
+
@cond.wait(@monitor.instance_variable_get(:@mon_mutex), timeout)
|
114
|
+
end
|
115
|
+
return true
|
116
|
+
ensure
|
117
|
+
@monitor.__send__(:mon_enter_for_cond, count)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Calls wait repeatedly while the given block yields a truthy value.
|
124
|
+
#
|
125
|
+
def wait_while
|
126
|
+
while yield
|
127
|
+
wait
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
#
|
132
|
+
# Calls wait repeatedly until the given block yields a truthy value.
|
133
|
+
#
|
134
|
+
def wait_until
|
135
|
+
until yield
|
136
|
+
wait
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
#
|
141
|
+
# Wakes up the first thread in line waiting for this lock.
|
142
|
+
#
|
143
|
+
def signal
|
144
|
+
@monitor.__send__(:mon_check_owner)
|
145
|
+
@cond.signal
|
146
|
+
end
|
147
|
+
|
148
|
+
#
|
149
|
+
# Wakes up all threads waiting for this lock.
|
150
|
+
#
|
151
|
+
def broadcast
|
152
|
+
@monitor.__send__(:mon_check_owner)
|
153
|
+
@cond.broadcast
|
154
|
+
end
|
155
|
+
|
156
|
+
private
|
157
|
+
|
158
|
+
def initialize(monitor)
|
159
|
+
@monitor = monitor
|
160
|
+
@cond = Thread::ConditionVariable.new
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.extend_object(obj)
|
165
|
+
super(obj)
|
166
|
+
obj.__send__(:mon_initialize)
|
167
|
+
end
|
168
|
+
|
169
|
+
#
|
170
|
+
# Attempts to enter exclusive section. Returns +false+ if lock fails.
|
171
|
+
#
|
172
|
+
def mon_try_enter
|
173
|
+
if @mon_owner != Thread.current
|
174
|
+
unless @mon_mutex.try_lock
|
175
|
+
return false
|
176
|
+
end
|
177
|
+
@mon_owner = Thread.current
|
178
|
+
@mon_count = 0
|
179
|
+
end
|
180
|
+
@mon_count += 1
|
181
|
+
return true
|
182
|
+
end
|
183
|
+
# For backward compatibility
|
184
|
+
alias try_mon_enter mon_try_enter
|
185
|
+
|
186
|
+
#
|
187
|
+
# Enters exclusive section.
|
188
|
+
#
|
189
|
+
def mon_enter
|
190
|
+
if @mon_owner != Thread.current
|
191
|
+
@mon_mutex.lock
|
192
|
+
@mon_owner = Thread.current
|
193
|
+
@mon_count = 0
|
194
|
+
end
|
195
|
+
@mon_count += 1
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# Leaves exclusive section.
|
200
|
+
#
|
201
|
+
def mon_exit
|
202
|
+
mon_check_owner
|
203
|
+
@mon_count -=1
|
204
|
+
if @mon_count == 0
|
205
|
+
@mon_owner = nil
|
206
|
+
@mon_mutex.unlock
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
#
|
211
|
+
# Returns true if this monitor is locked by any thread
|
212
|
+
#
|
213
|
+
def mon_locked?
|
214
|
+
@mon_mutex.locked?
|
215
|
+
end
|
216
|
+
|
217
|
+
#
|
218
|
+
# Returns true if this monitor is locked by current thread.
|
219
|
+
#
|
220
|
+
def mon_owned?
|
221
|
+
@mon_mutex.locked? && @mon_owner == Thread.current
|
222
|
+
end
|
223
|
+
|
224
|
+
#
|
225
|
+
# Enters exclusive section and executes the block. Leaves the exclusive
|
226
|
+
# section automatically when the block exits. See example under
|
227
|
+
# +MonitorMixin+.
|
228
|
+
#
|
229
|
+
def mon_synchronize
|
230
|
+
# Prevent interrupt on handling interrupts; for example timeout errors
|
231
|
+
# it may break locking state.
|
232
|
+
Thread.handle_interrupt(Exception => :never){ mon_enter }
|
233
|
+
begin
|
234
|
+
yield
|
235
|
+
ensure
|
236
|
+
Thread.handle_interrupt(Exception => :never){ mon_exit }
|
237
|
+
end
|
238
|
+
end
|
239
|
+
alias synchronize mon_synchronize
|
240
|
+
|
241
|
+
#
|
242
|
+
# Creates a new MonitorMixin::ConditionVariable associated with the
|
243
|
+
# receiver.
|
244
|
+
#
|
245
|
+
def new_cond
|
246
|
+
return ConditionVariable.new(self)
|
247
|
+
end
|
248
|
+
|
249
|
+
private
|
250
|
+
|
251
|
+
# Use <tt>extend MonitorMixin</tt> or <tt>include MonitorMixin</tt> instead
|
252
|
+
# of this constructor. Have look at the examples above to understand how to
|
253
|
+
# use this module.
|
254
|
+
def initialize(*args)
|
255
|
+
super
|
256
|
+
mon_initialize
|
257
|
+
end
|
258
|
+
|
259
|
+
# Initializes the MonitorMixin after being included in a class or when an
|
260
|
+
# object has been extended with the MonitorMixin
|
261
|
+
def mon_initialize
|
262
|
+
if defined?(@mon_mutex) && @mon_mutex_owner_object_id == object_id
|
263
|
+
raise ThreadError, "already initialized"
|
264
|
+
end
|
265
|
+
@mon_mutex = Thread::Mutex.new
|
266
|
+
@mon_mutex_owner_object_id = object_id
|
267
|
+
@mon_owner = nil
|
268
|
+
@mon_count = 0
|
269
|
+
end
|
270
|
+
|
271
|
+
def mon_check_owner
|
272
|
+
if @mon_owner != Thread.current
|
273
|
+
raise ThreadError, "current thread not owner"
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def mon_enter_for_cond(count)
|
278
|
+
@mon_owner = Thread.current
|
279
|
+
@mon_count = count
|
280
|
+
end
|
281
|
+
|
282
|
+
def mon_exit_for_cond
|
283
|
+
count = @mon_count
|
284
|
+
@mon_owner = nil
|
285
|
+
@mon_count = 0
|
286
|
+
return count
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
# Use the Monitor class when you want to have a lock object for blocks with
|
291
|
+
# mutual exclusion.
|
292
|
+
#
|
293
|
+
# require 'monitor'
|
294
|
+
#
|
295
|
+
# lock = Monitor.new
|
296
|
+
# lock.synchronize do
|
297
|
+
# # exclusive access
|
298
|
+
# end
|
299
|
+
#
|
300
|
+
class Monitor
|
301
|
+
include MonitorMixin
|
302
|
+
alias try_enter try_mon_enter
|
303
|
+
alias enter mon_enter
|
304
|
+
alias exit mon_exit
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
# Documentation comments:
|
310
|
+
# - All documentation comes from Nutshell.
|
311
|
+
# - MonitorMixin.new_cond appears in the example, but is not documented in
|
312
|
+
# Nutshell.
|
313
|
+
# - All the internals (internal modules Accessible and Initializable, class
|
314
|
+
# ConditionVariable) appear in RDoc. It might be good to hide them, by
|
315
|
+
# making them private, or marking them :nodoc:, etc.
|
316
|
+
# - RDoc doesn't recognise aliases, so we have mon_synchronize documented, but
|
317
|
+
# not synchronize.
|
318
|
+
# - mon_owner is in Nutshell, but appears as an accessor in a separate module
|
319
|
+
# here, so is hard/impossible to RDoc. Some other useful accessors
|
320
|
+
# (mon_count and some queue stuff) are also in this module, and don't appear
|
321
|
+
# directly in the RDoc output.
|
322
|
+
# - in short, it may be worth changing the code layout in this file to make the
|
323
|
+
# documentation easier
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
# Provides support for converting integers to Strings, and back again. The
|
5
|
+
# strings are structured so that lexicographic sorting order is preserved.
|
6
|
+
#
|
7
|
+
# That is, if integer1 is less than integer2 for any two integers integer1 and
|
8
|
+
# integer2, then integer1.to_s_lex is lexicographically less than
|
9
|
+
# integer2.to_s_lex. (Similarly for "greater than" and "equals".)
|
10
|
+
#
|
11
|
+
# This class handles numbers between - 10 ** 10,000 and 10 ** 10,000
|
12
|
+
# which should cover all practical numbers. If you need bigger numbers,
|
13
|
+
# increase Integer::LEN_STR_SIZE.
|
14
|
+
class Integer
|
15
|
+
# LEN_SIZE of 4 should handle most numbers that can practically be held in
|
16
|
+
# memory.
|
17
|
+
LEN_STR_SIZE = 4
|
18
|
+
NEG_LEN_MASK = 10 ** LEN_STR_SIZE
|
19
|
+
LEN_STR_TEMPLATE = "%0#{LEN_STR_SIZE}d"
|
20
|
+
|
21
|
+
# Convert the number to a lexicographically sortable string. This string will
|
22
|
+
# use printable characters only but will not be human readable.
|
23
|
+
def to_s_lex
|
24
|
+
if (self >= 0)
|
25
|
+
num_str = self.to_s
|
26
|
+
len_str = LEN_STR_TEMPLATE % num_str.size
|
27
|
+
return len_str + num_str
|
28
|
+
else
|
29
|
+
num = self * -1
|
30
|
+
num_str = num.to_s
|
31
|
+
num_len = num_str.size
|
32
|
+
len_str = LEN_STR_TEMPLATE % (NEG_LEN_MASK - num_len)
|
33
|
+
num = (10 ** num_str.size) - num
|
34
|
+
return "-#{len_str}%0#{num_len}d" % num
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert the number to a lexicographically sortable string by padding with
|
39
|
+
# 0s. You should make sure that you set the width to a number large enough to
|
40
|
+
# accommodate all possible values. Also note that this method will not work
|
41
|
+
# with negative numbers. That is negative numbers will sort in the opposite
|
42
|
+
# direction as positive numbers. If you have very large numbers or a mix of
|
43
|
+
# positive and negative numbers you should use the Integer#to_s_lex method
|
44
|
+
#
|
45
|
+
# width:: number of characters in the string returned. Default is 10. So
|
46
|
+
# 123.to_s_pad(5) => 00123 and -123.to_s_pad(5) => -0123
|
47
|
+
# return:: padding string representation of the number.
|
48
|
+
def to_s_pad(width = 10)
|
49
|
+
"%#{width}d" % self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Date
|
54
|
+
# Convert the Date to a lexicographically sortable string with the required
|
55
|
+
# precision. The format used is %Y%m%d
|
56
|
+
#
|
57
|
+
# precision:: the precision required in the string version of the date. The
|
58
|
+
# options are :year, :month and :day
|
59
|
+
# return:: a lexicographically sortable string representing the date
|
60
|
+
def to_s_lex(precision = :day)
|
61
|
+
self.strftime(Time::LEX_FORMAT[precision])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class DateTime
|
66
|
+
# Convert the DateTime to a lexicographically sortable string with the
|
67
|
+
# required precision. The format used is %Y%m%d %H:%M:%S.
|
68
|
+
#
|
69
|
+
# precision:: the precision required in the string version of the date. The
|
70
|
+
# options are :year, :month, :day, :hour, :minute and :second
|
71
|
+
# return:: a lexicographically sortable string representing the date
|
72
|
+
def to_s_lex(precision = :day)
|
73
|
+
self.strftime(Time::LEX_FORMAT[precision])
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Time
|
78
|
+
LEX_FORMAT = {
|
79
|
+
:year => "%Y",
|
80
|
+
:month => "%Y-%m",
|
81
|
+
:day => "%Y-%m-%d",
|
82
|
+
:hour => "%Y-%m-%d %H",
|
83
|
+
:minute => "%Y-%m-%d %H:%M",
|
84
|
+
:second => "%Y-%m-%d %H:%M:%S",
|
85
|
+
:millisecond => "%Y-%m-%d %H:%M:%S"
|
86
|
+
}
|
87
|
+
|
88
|
+
# Convert the Time to a lexicographically sortable string with the required
|
89
|
+
# precision. The format used is %Y%m%d %H:%M:%S.
|
90
|
+
#
|
91
|
+
# precision:: the precision required in the string version of the time. The
|
92
|
+
# options are :year, :month, :day, :hour, :minute and :second
|
93
|
+
# return:: a lexicographically sortable string representing the date
|
94
|
+
def to_s_lex(precision = :day)
|
95
|
+
self.strftime(LEX_FORMAT[precision])
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class String
|
100
|
+
# Convert a string to an integer. This method will only work on strings that
|
101
|
+
# were previously created with Integer#to_s_lex, otherwise the result will be
|
102
|
+
# unpredictable.
|
103
|
+
def to_i_lex
|
104
|
+
if (self[0] == ?-)
|
105
|
+
return self[(Integer::LEN_STR_SIZE + 1)..-1].to_i -
|
106
|
+
10 ** (self.size - Integer::LEN_STR_SIZE - 1)
|
107
|
+
else
|
108
|
+
return self[Integer::LEN_STR_SIZE..-1].to_i
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Convert a string to a Time. This method will only work on strings that
|
113
|
+
# match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
|
114
|
+
def to_time_lex
|
115
|
+
vals = []
|
116
|
+
self.gsub(/(?:^|[- :])(\d+)/) {vals << $1.to_i; $&}
|
117
|
+
Time.mktime(*vals)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Convert a string to a Date. This method will only work on strings that
|
121
|
+
# match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
|
122
|
+
def to_date_lex
|
123
|
+
return Date.strptime(self + "-02-01", "%Y-%m-%d")
|
124
|
+
end
|
125
|
+
|
126
|
+
# Convert a string to a DateTime. This method will only work on strings that
|
127
|
+
# match the format %Y%m%d %H%M%S, otherwise the result will be unpredictable.
|
128
|
+
def to_date_time_lex
|
129
|
+
return DateTime.strptime(self + "-01-01", "%Y-%m-%d %H:%M:%S")
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def get_lex_format(len)
|
135
|
+
case len
|
136
|
+
when 0.. 3 then ""
|
137
|
+
when 4.. 5 then "%Y"
|
138
|
+
when 6.. 7 then "%Y%m"
|
139
|
+
when 8.. 9 then "%Y%m%d"
|
140
|
+
when 10..11 then "%Y%m%d%H"
|
141
|
+
when 12..13 then "%Y%m%d%H%M"
|
142
|
+
else "%Y%m%d%H%M%S"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# See FieldSymbolMethods
|
148
|
+
class Symbol
|
149
|
+
include Isomorfeus::Ferret::FieldSymbolMethods
|
150
|
+
end
|
151
|
+
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'isomorfeus_ferret_ext'
|
2
|
+
require 'isomorfeus/ferret/version'
|
3
|
+
require 'isomorfeus/ferret/field_symbol'
|
4
|
+
require 'isomorfeus/ferret/stdlib_patches'
|
5
|
+
require 'isomorfeus/ferret/monitor'
|
6
|
+
require 'isomorfeus/ferret/index/field_infos'
|
7
|
+
require 'isomorfeus/ferret/index/index'
|
8
|
+
require 'isomorfeus/ferret/document'
|