picky 2.7.0 → 3.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/picky/adapters/rack/base.rb +20 -16
- data/lib/picky/adapters/rack/live_parameters.rb +28 -24
- data/lib/picky/adapters/rack/search.rb +67 -0
- data/lib/picky/adapters/rack.rb +27 -23
- data/lib/picky/application.rb +246 -236
- data/lib/picky/backend/base.rb +115 -119
- data/lib/picky/backend/file/basic.rb +102 -98
- data/lib/picky/backend/file/json.rb +27 -23
- data/lib/picky/backend/file/marshal.rb +32 -28
- data/lib/picky/backend/file/text.rb +45 -41
- data/lib/picky/backend/files.rb +19 -15
- data/lib/picky/backend/redis/basic.rb +76 -72
- data/lib/picky/backend/redis/list_hash.rb +40 -36
- data/lib/picky/backend/redis/string_hash.rb +30 -26
- data/lib/picky/backend/redis.rb +32 -28
- data/lib/picky/bundle.rb +82 -57
- data/lib/{bundling.rb → picky/bundling.rb} +0 -0
- data/lib/picky/calculations/location.rb +51 -47
- data/lib/picky/categories.rb +60 -56
- data/lib/picky/categories_indexed.rb +73 -82
- data/lib/picky/categories_indexing.rb +12 -8
- data/lib/picky/category.rb +109 -120
- data/lib/picky/category_indexed.rb +39 -41
- data/lib/picky/category_indexing.rb +123 -125
- data/lib/picky/character_substituters/west_european.rb +32 -26
- data/lib/{constants.rb → picky/constants.rb} +0 -0
- data/lib/picky/cores.rb +96 -92
- data/lib/{deployment.rb → picky/deployment.rb} +0 -0
- data/lib/picky/frontend_adapters/rack.rb +133 -118
- data/lib/picky/generators/aliases.rb +5 -3
- data/lib/picky/generators/base.rb +11 -7
- data/lib/picky/generators/partial/default.rb +7 -3
- data/lib/picky/generators/partial/none.rb +24 -20
- data/lib/picky/generators/partial/strategy.rb +20 -16
- data/lib/picky/generators/partial/substring.rb +94 -90
- data/lib/picky/generators/partial_generator.rb +11 -7
- data/lib/picky/generators/similarity/default.rb +9 -5
- data/lib/picky/generators/similarity/double_metaphone.rb +20 -16
- data/lib/picky/generators/similarity/metaphone.rb +20 -16
- data/lib/picky/generators/similarity/none.rb +23 -19
- data/lib/picky/generators/similarity/phonetic.rb +49 -45
- data/lib/picky/generators/similarity/soundex.rb +20 -16
- data/lib/picky/generators/similarity/strategy.rb +10 -6
- data/lib/picky/generators/similarity_generator.rb +11 -7
- data/lib/picky/generators/strategy.rb +14 -10
- data/lib/picky/generators/weights/default.rb +9 -5
- data/lib/picky/generators/weights/logarithmic.rb +30 -26
- data/lib/picky/generators/weights/strategy.rb +10 -6
- data/lib/picky/generators/weights_generator.rb +11 -7
- data/lib/picky/helpers/measuring.rb +20 -16
- data/lib/picky/indexed/bundle/base.rb +39 -37
- data/lib/picky/indexed/bundle/memory.rb +68 -64
- data/lib/picky/indexed/bundle/redis.rb +73 -69
- data/lib/picky/indexed/wrappers/bundle/calculation.rb +26 -22
- data/lib/picky/indexed/wrappers/bundle/location.rb +30 -26
- data/lib/picky/indexed/wrappers/bundle/wrapper.rb +36 -32
- data/lib/picky/indexed/wrappers/category/location.rb +17 -13
- data/lib/picky/indexed/wrappers/exact_first.rb +46 -42
- data/lib/picky/indexers/base.rb +26 -22
- data/lib/picky/indexers/parallel.rb +62 -58
- data/lib/picky/indexers/serial.rb +41 -37
- data/lib/picky/indexes/index.rb +400 -0
- data/lib/picky/indexes/index_indexed.rb +24 -0
- data/lib/picky/indexes/index_indexing.rb +138 -0
- data/lib/picky/indexes/memory.rb +20 -0
- data/lib/picky/indexes/redis.rb +20 -0
- data/lib/picky/indexes.rb +68 -61
- data/lib/picky/indexes_indexed.rb +16 -12
- data/lib/picky/indexes_indexing.rb +41 -37
- data/lib/picky/indexing/bundle/base.rb +216 -205
- data/lib/picky/indexing/bundle/memory.rb +16 -11
- data/lib/picky/indexing/bundle/redis.rb +14 -12
- data/lib/picky/indexing/wrappers/category/location.rb +17 -13
- data/lib/picky/interfaces/live_parameters.rb +159 -154
- data/lib/picky/loader.rb +267 -304
- data/lib/picky/loggers/search.rb +20 -13
- data/lib/picky/no_source_specified_exception.rb +7 -3
- data/lib/picky/performant.rb +6 -2
- data/lib/picky/query/allocation.rb +71 -67
- data/lib/picky/query/allocations.rb +99 -94
- data/lib/picky/query/combination.rb +70 -66
- data/lib/picky/query/combinations/base.rb +56 -52
- data/lib/picky/query/combinations/memory.rb +36 -32
- data/lib/picky/query/combinations/redis.rb +66 -62
- data/lib/picky/query/indexes.rb +175 -160
- data/lib/picky/query/qualifier_category_mapper.rb +43 -0
- data/lib/picky/query/token.rb +165 -172
- data/lib/picky/query/tokens.rb +86 -82
- data/lib/picky/query/weights.rb +44 -48
- data/lib/picky/query.rb +5 -1
- data/lib/picky/rack/harakiri.rb +51 -47
- data/lib/picky/results.rb +81 -77
- data/lib/picky/search.rb +169 -158
- data/lib/picky/sinatra.rb +34 -0
- data/lib/picky/sources/base.rb +73 -70
- data/lib/picky/sources/couch.rb +61 -57
- data/lib/picky/sources/csv.rb +68 -64
- data/lib/picky/sources/db.rb +139 -135
- data/lib/picky/sources/delicious.rb +52 -48
- data/lib/picky/sources/mongo.rb +68 -63
- data/lib/picky/sources/wrappers/base.rb +20 -16
- data/lib/picky/sources/wrappers/location.rb +37 -33
- data/lib/picky/statistics.rb +46 -43
- data/lib/picky/tasks.rb +3 -0
- data/lib/picky/tokenizers/base.rb +192 -187
- data/lib/picky/tokenizers/index.rb +25 -21
- data/lib/picky/tokenizers/location.rb +33 -29
- data/lib/picky/tokenizers/query.rb +49 -43
- data/lib/picky.rb +21 -13
- data/lib/tasks/application.rake +1 -1
- data/lib/tasks/index.rake +3 -3
- data/lib/tasks/routes.rake +1 -1
- data/lib/tasks/server.rake +1 -1
- data/spec/lib/adapters/rack/base_spec.rb +1 -1
- data/spec/lib/adapters/rack/live_parameters_spec.rb +1 -1
- data/spec/lib/adapters/rack/query_spec.rb +1 -1
- data/spec/lib/application_spec.rb +39 -32
- data/spec/lib/backend/file/basic_spec.rb +2 -2
- data/spec/lib/backend/file/json_spec.rb +2 -2
- data/spec/lib/backend/file/marshal_spec.rb +2 -2
- data/spec/lib/backend/file/text_spec.rb +1 -1
- data/spec/lib/backend/files_spec.rb +14 -24
- data/spec/lib/backend/redis/basic_spec.rb +2 -2
- data/spec/lib/backend/redis/list_hash_spec.rb +3 -3
- data/spec/lib/backend/redis/string_hash_spec.rb +3 -3
- data/spec/lib/backend/redis_spec.rb +20 -13
- data/spec/lib/calculations/location_spec.rb +1 -1
- data/spec/lib/categories_indexed_spec.rb +16 -34
- data/spec/lib/category_indexed_spec.rb +9 -27
- data/spec/lib/category_indexing_spec.rb +2 -3
- data/spec/lib/category_spec.rb +10 -10
- data/spec/lib/character_substituters/west_european_spec.rb +6 -5
- data/spec/lib/cores_spec.rb +17 -17
- data/spec/lib/extensions/symbol_spec.rb +15 -1
- data/spec/lib/frontend_adapters/rack_spec.rb +20 -20
- data/spec/lib/generators/aliases_spec.rb +3 -3
- data/spec/lib/generators/cacher_strategy_spec.rb +1 -1
- data/spec/lib/generators/partial/default_spec.rb +3 -3
- data/spec/lib/generators/partial/none_spec.rb +2 -2
- data/spec/lib/generators/partial/substring_spec.rb +1 -1
- data/spec/lib/generators/partial_generator_spec.rb +3 -3
- data/spec/lib/generators/similarity/double_metaphone_spec.rb +1 -1
- data/spec/lib/generators/similarity/metaphone_spec.rb +1 -1
- data/spec/lib/generators/similarity/none_spec.rb +1 -1
- data/spec/lib/generators/similarity/phonetic_spec.rb +1 -1
- data/spec/lib/generators/similarity/soundex_spec.rb +1 -1
- data/spec/lib/generators/similarity_generator_spec.rb +2 -2
- data/spec/lib/generators/weights/logarithmic_spec.rb +1 -1
- data/spec/lib/generators/weights_generator_spec.rb +1 -1
- data/spec/lib/helpers/measuring_spec.rb +2 -2
- data/spec/lib/indexed/bundle/memory_spec.rb +6 -6
- data/spec/lib/indexed/bundle/redis_spec.rb +4 -4
- data/spec/lib/indexed/wrappers/bundle/calculation_spec.rb +2 -3
- data/spec/lib/indexed/wrappers/bundle/wrapper_spec.rb +2 -2
- data/spec/lib/indexed/wrappers/exact_first_spec.rb +5 -5
- data/spec/lib/indexers/base_spec.rb +1 -1
- data/spec/lib/indexers/parallel_spec.rb +1 -1
- data/spec/lib/indexers/serial_spec.rb +1 -1
- data/spec/lib/{index/base_indexed_spec.rb → indexes/index_indexed_spec.rb} +3 -3
- data/spec/lib/{index/base_indexing_spec.rb → indexes/index_indexing_spec.rb} +19 -2
- data/spec/lib/{index/base_spec.rb → indexes/index_spec.rb} +6 -25
- data/spec/lib/{index → indexes}/redis_spec.rb +1 -1
- data/spec/lib/indexes_class_spec.rb +2 -2
- data/spec/lib/indexes_indexed_spec.rb +1 -1
- data/spec/lib/indexes_indexing_spec.rb +1 -1
- data/spec/lib/indexes_spec.rb +1 -1
- data/spec/lib/indexing/bundle/base_spec.rb +7 -5
- data/spec/lib/indexing/bundle/memory_partial_generation_speed_spec.rb +4 -4
- data/spec/lib/indexing/bundle/memory_spec.rb +15 -15
- data/spec/lib/indexing/bundle/redis_spec.rb +9 -9
- data/spec/lib/interfaces/live_parameters_spec.rb +5 -5
- data/spec/lib/loader_spec.rb +17 -19
- data/spec/lib/loggers/search_spec.rb +2 -2
- data/spec/lib/query/allocation_spec.rb +1 -1
- data/spec/lib/query/allocations_spec.rb +1 -1
- data/spec/lib/query/combination_spec.rb +4 -4
- data/spec/lib/query/combinations/base_spec.rb +1 -1
- data/spec/lib/query/combinations/memory_spec.rb +1 -1
- data/spec/lib/query/combinations/redis_spec.rb +1 -1
- data/spec/lib/query/indexes_spec.rb +7 -2
- data/spec/lib/query/qualifier_category_mapper_spec.rb +34 -0
- data/spec/lib/query/token_spec.rb +32 -53
- data/spec/lib/query/tokens_spec.rb +30 -35
- data/spec/lib/query/weights_spec.rb +16 -16
- data/spec/lib/rack/harakiri_spec.rb +5 -5
- data/spec/lib/results_spec.rb +1 -1
- data/spec/lib/search_spec.rb +24 -22
- data/spec/lib/sinatra_spec.rb +36 -0
- data/spec/lib/sources/base_spec.rb +1 -1
- data/spec/lib/sources/couch_spec.rb +9 -9
- data/spec/lib/sources/csv_spec.rb +7 -7
- data/spec/lib/sources/db_spec.rb +2 -2
- data/spec/lib/sources/delicious_spec.rb +5 -5
- data/spec/lib/sources/mongo_spec.rb +7 -7
- data/spec/lib/sources/wrappers/base_spec.rb +2 -2
- data/spec/lib/sources/wrappers/location_spec.rb +1 -1
- data/spec/lib/statistics_spec.rb +1 -1
- data/spec/lib/tokenizers/base_spec.rb +2 -2
- data/spec/lib/tokenizers/index_spec.rb +1 -1
- data/spec/lib/tokenizers/query_spec.rb +1 -1
- metadata +30 -30
- data/lib/picky/adapters/rack/query.rb +0 -65
- data/lib/picky/index/base.rb +0 -409
- data/lib/picky/index/base_indexed.rb +0 -29
- data/lib/picky/index/base_indexing.rb +0 -127
- data/lib/picky/index/memory.rb +0 -16
- data/lib/picky/index/redis.rb +0 -16
- data/lib/picky/query/qualifiers.rb +0 -76
- data/lib/picky/query/solr.rb +0 -60
- data/lib/picky/signals.rb +0 -8
- data/lib/picky-tasks.rb +0 -6
- data/lib/tasks/spec.rake +0 -11
- data/spec/lib/query/qualifiers_spec.rb +0 -31
@@ -1,148 +1,146 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
class Category
|
1
|
+
module Picky
|
4
2
|
|
5
|
-
attr_reader :indexing_exact,
|
6
|
-
:indexing_partial
|
7
|
-
|
8
|
-
# Prepares and caches this category.
|
9
3
|
#
|
10
|
-
# This one should be used by users.
|
11
4
|
#
|
12
|
-
|
13
|
-
prepare
|
14
|
-
cache
|
15
|
-
end
|
5
|
+
class Category
|
16
6
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
7
|
+
attr_reader :indexing_exact,
|
8
|
+
:indexing_partial
|
9
|
+
|
10
|
+
# Prepares and caches this category.
|
11
|
+
#
|
12
|
+
# This one should be used by users.
|
13
|
+
#
|
14
|
+
def index
|
15
|
+
prepare
|
16
|
+
cache
|
22
17
|
end
|
23
|
-
end
|
24
18
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
19
|
+
# Indexes, creates the "prepared_..." file.
|
20
|
+
#
|
21
|
+
def prepare
|
22
|
+
with_data_snapshot do
|
23
|
+
indexer.index [self]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Take a data snapshot if the source offers it.
|
28
|
+
#
|
29
|
+
def with_data_snapshot
|
30
|
+
if source.respond_to? :with_snapshot
|
31
|
+
source.with_snapshot(@index) do
|
32
|
+
yield
|
33
|
+
end
|
34
|
+
else
|
30
35
|
yield
|
31
36
|
end
|
32
|
-
else
|
33
|
-
yield
|
34
37
|
end
|
35
|
-
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
39
|
+
# Generates all caches for this category.
|
40
|
+
#
|
41
|
+
def cache
|
42
|
+
generate_caches_from_source
|
43
|
+
generate_partial
|
44
|
+
generate_caches_from_memory
|
45
|
+
dump_caches
|
46
|
+
timed_exclaim %Q{"#{identifier}": Caching finished.}
|
47
|
+
end
|
48
|
+
# Generate the cache data.
|
49
|
+
#
|
50
|
+
def generate_caches_from_source
|
51
|
+
indexing_exact.generate_caches_from_source
|
52
|
+
end
|
53
|
+
def generate_partial
|
54
|
+
indexing_partial.generate_partial_from indexing_exact.inverted
|
55
|
+
end
|
56
|
+
def generate_caches_from_memory
|
57
|
+
indexing_partial.generate_caches_from_memory
|
58
|
+
end
|
59
|
+
def dump_caches
|
60
|
+
indexing_exact.dump
|
61
|
+
indexing_partial.dump
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
# Return an appropriate source.
|
65
|
+
#
|
66
|
+
# If we have no explicit source, we'll check the index for one.
|
67
|
+
#
|
68
|
+
def source
|
69
|
+
(@source && extract_source) || @index.source
|
70
|
+
end
|
71
|
+
# Extract the actual source if it is wrapped in a time
|
72
|
+
# capsule, i.e. a block/lambda.
|
73
|
+
#
|
74
|
+
# TODO Extract into module.
|
75
|
+
#
|
76
|
+
def extract_source
|
77
|
+
@source = @source.respond_to?(:call) ? @source.call : @source
|
78
|
+
end
|
70
79
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
end
|
80
|
+
# Return the key format.
|
81
|
+
#
|
82
|
+
# If the source has no key format, and
|
83
|
+
# none is defined on this category, ask
|
84
|
+
# the index for one.
|
85
|
+
#
|
86
|
+
def key_format
|
87
|
+
source.respond_to?(:key_format) && source.key_format || @key_format || @index.key_format
|
88
|
+
end
|
81
89
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
90
|
+
# Where the data is taken from.
|
91
|
+
#
|
92
|
+
def from
|
93
|
+
@from || name
|
94
|
+
end
|
87
95
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
@indexer ||= source.respond_to?(:each) ? Indexers::Parallel.new(self) : Indexers::Serial.new(self)
|
94
|
-
end
|
96
|
+
# The indexer is lazily generated and cached.
|
97
|
+
#
|
98
|
+
def indexer
|
99
|
+
@indexer ||= source.respond_to?(:each) ? Indexers::Parallel.new(self) : Indexers::Serial.new(self)
|
100
|
+
end
|
95
101
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
102
|
+
# Returns an appropriate tokenizer.
|
103
|
+
# If one isn't set on this category, will try the index,
|
104
|
+
# and finally the default index tokenizer.
|
105
|
+
#
|
106
|
+
def tokenizer
|
107
|
+
@tokenizer || @index.tokenizer
|
108
|
+
end
|
103
109
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
indexing_partial[:key_format] = self.key_format
|
112
|
-
end
|
110
|
+
# Checks the caches for existence.
|
111
|
+
#
|
112
|
+
def check
|
113
|
+
timed_exclaim "Checking #{identifier}."
|
114
|
+
indexing_exact.raise_unless_cache_exists
|
115
|
+
indexing_partial.raise_unless_cache_exists
|
116
|
+
end
|
113
117
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
118
|
+
# Deletes the caches.
|
119
|
+
#
|
120
|
+
def clear
|
121
|
+
timed_exclaim "Deleting #{identifier}."
|
122
|
+
indexing_exact.delete
|
123
|
+
indexing_partial.delete
|
124
|
+
end
|
121
125
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
126
|
+
# Backup the caches.
|
127
|
+
# (Revert with restore_caches)
|
128
|
+
#
|
129
|
+
def backup
|
130
|
+
timed_exclaim "Backing up #{identifier}."
|
131
|
+
indexing_exact.backup
|
132
|
+
indexing_partial.backup
|
133
|
+
end
|
129
134
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
135
|
+
# Restore the caches.
|
136
|
+
# (Revert with backup_caches)
|
137
|
+
#
|
138
|
+
def restore
|
139
|
+
timed_exclaim "Restoring #{identifier}."
|
140
|
+
indexing_exact.restore
|
141
|
+
indexing_partial.restore
|
142
|
+
end
|
138
143
|
|
139
|
-
# Restore the caches.
|
140
|
-
# (Revert with backup_caches)
|
141
|
-
#
|
142
|
-
def restore
|
143
|
-
timed_exclaim "Restoring #{identifier}."
|
144
|
-
indexing_exact.restore
|
145
|
-
indexing_partial.restore
|
146
144
|
end
|
147
145
|
|
148
146
|
end
|
@@ -1,37 +1,43 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
#
|
3
|
-
module
|
4
|
-
# Substitutes Umlauts like
|
5
|
-
# ä, ö, ü => ae, oe, ue.
|
6
|
-
# (and more, see specs)
|
7
|
-
#
|
8
|
-
class WestEuropean
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@chars = ActiveSupport::Multibyte.proxy_class
|
12
|
-
end
|
3
|
+
module Picky
|
13
4
|
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
module CharacterSubstituters # :nodoc:all
|
6
|
+
|
7
|
+
# Substitutes Umlauts like
|
8
|
+
# ä, ö, ü => ae, oe, ue.
|
9
|
+
# (and more, see specs)
|
10
|
+
#
|
11
|
+
class WestEuropean
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@chars = ActiveSupport::Multibyte.proxy_class
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
def to_s
|
18
|
+
self.class.name
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
trans.gsub!('ß', 'ss')
|
21
|
+
def substitute text
|
22
|
+
trans = @chars.new(text).normalize(:kd)
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
# substitute special cases
|
25
|
+
#
|
26
|
+
trans.gsub!('ß', 'ss')
|
27
|
+
|
28
|
+
# substitute umlauts (of A,O,U,a,o,u)
|
29
|
+
#
|
30
|
+
trans.gsub!(/([AOUaou])\314\210/u, '\1e')
|
31
|
+
|
32
|
+
# get rid of ecutes, graves and …
|
33
|
+
#
|
34
|
+
trans.unpack('U*').select { |cp|
|
35
|
+
cp < 0x0300 || cp > 0x035F
|
36
|
+
}.pack('U*')
|
37
|
+
end
|
28
38
|
|
29
|
-
# get rid of ecutes, graves and …
|
30
|
-
#
|
31
|
-
trans.unpack('U*').select { |cp|
|
32
|
-
cp < 0x0300 || cp > 0x035F
|
33
|
-
}.pack('U*')
|
34
39
|
end
|
35
40
|
|
36
41
|
end
|
42
|
+
|
37
43
|
end
|
File without changes
|
data/lib/picky/cores.rb
CHANGED
@@ -1,104 +1,108 @@
|
|
1
|
-
|
1
|
+
module Picky
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# Pass it an ary or generator.
|
8
|
-
#
|
9
|
-
# generator = (1..10).each
|
10
|
-
# forked generator, :max => 5 do |element|
|
11
|
-
#
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# Options include:
|
15
|
-
# * max: Maximum # of processors to use. Default is all it can get.
|
3
|
+
Infinity = 1.0/0
|
4
|
+
|
5
|
+
# Handles processing over multiple cores.
|
16
6
|
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
ary_or_generator = ary_or_generator.sort_by { rand } if options[:randomly]
|
22
|
-
generator = ary_or_generator.each
|
23
|
-
|
24
|
-
# Get the maximum number of processors.
|
7
|
+
class Cores # :nodoc:all
|
8
|
+
|
9
|
+
# Pass it an ary or generator.
|
25
10
|
#
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
#
|
11
|
+
# generator = (1..10).each
|
12
|
+
# forked generator, :max => 5 do |element|
|
30
13
|
#
|
31
|
-
|
32
|
-
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# Options include:
|
17
|
+
# * max: Maximum # of processors to use. Default is all it can get.
|
18
|
+
#
|
19
|
+
def self.forked ary_or_generator, options = {}
|
20
|
+
return if ary_or_generator.empty?
|
21
|
+
raise "Block argument needed when running Cores.forked" unless block_given?
|
22
|
+
|
23
|
+
ary_or_generator = ary_or_generator.sort_by { rand } if options[:randomly]
|
24
|
+
generator = ary_or_generator.each
|
25
|
+
|
26
|
+
# Get the maximum number of processors.
|
27
|
+
#
|
28
|
+
max = max_processors options
|
29
|
+
currently_processing = 0
|
30
|
+
|
33
31
|
#
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
32
|
+
#
|
33
|
+
loop do
|
34
|
+
# Ramp it up to num processors.
|
35
|
+
#
|
36
|
+
while currently_processing < max
|
37
|
+
|
38
|
+
currently_processing = currently_processing + 1
|
39
|
+
|
40
|
+
element = nil
|
41
|
+
begin
|
42
|
+
element = generator.next
|
43
|
+
rescue StopIteration => si
|
44
|
+
break
|
45
|
+
end
|
46
|
+
break unless element
|
47
|
+
|
48
|
+
Process.fork do
|
49
|
+
sleep 0.01*currently_processing
|
50
|
+
yield element
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
39
55
|
begin
|
40
|
-
|
41
|
-
rescue
|
56
|
+
Process.wait 0 # Block and wait for any child to finish.
|
57
|
+
rescue Errno::ECHILD => e
|
42
58
|
break
|
59
|
+
ensure
|
60
|
+
currently_processing = currently_processing - 1
|
43
61
|
end
|
44
|
-
break unless element
|
45
|
-
|
46
|
-
Process.fork do
|
47
|
-
sleep 0.01*currently_processing
|
48
|
-
yield element
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
begin
|
54
|
-
Process.wait 0 # Block and wait for any child to finish.
|
55
|
-
rescue Errno::ECHILD => e
|
56
|
-
break
|
57
|
-
ensure
|
58
|
-
currently_processing = currently_processing - 1
|
59
62
|
end
|
60
63
|
end
|
64
|
+
|
65
|
+
# Return the number of maximum usable processors.
|
66
|
+
#
|
67
|
+
def self.max_processors options = {}
|
68
|
+
options[:amount] || [number_of_cores, (options[:max] || Infinity)].min
|
69
|
+
end
|
70
|
+
|
71
|
+
# Gets the number of cores depending on OS.
|
72
|
+
#
|
73
|
+
def self.number_of_cores
|
74
|
+
extract_cores_for actual_platform
|
75
|
+
end
|
76
|
+
# Extracts the platform os from the platform.
|
77
|
+
#
|
78
|
+
# Note: Could also use 'rbconfig'.
|
79
|
+
#
|
80
|
+
def self.actual_platform
|
81
|
+
matched = platform.match(/-\b([a-z]*)/)
|
82
|
+
matched && matched[1]
|
83
|
+
end
|
84
|
+
# Returns a mapping
|
85
|
+
# os_name => lambda_which_returns_a_number_of_cores
|
86
|
+
#
|
87
|
+
@@number_of_cores = {
|
88
|
+
'darwin' => lambda { `system_profiler SPHardwareDataType | grep -i 'Total Number Of Cores'`.gsub(/[^\d]/, '') },
|
89
|
+
'linux' => lambda { `grep -ci ^processor /proc/cpuinfo` }
|
90
|
+
}
|
91
|
+
def self.os_to_core_mapping
|
92
|
+
@@number_of_cores
|
93
|
+
end
|
94
|
+
# Extracts the number of cores for the given os name.
|
95
|
+
#
|
96
|
+
# Note: Default is 1.
|
97
|
+
#
|
98
|
+
def self.extract_cores_for os
|
99
|
+
code_to_execute = os_to_core_mapping[os]
|
100
|
+
code_to_execute && code_to_execute.call.to_i || 1
|
101
|
+
end
|
102
|
+
def self.platform
|
103
|
+
RUBY_PLATFORM
|
104
|
+
end
|
105
|
+
|
61
106
|
end
|
62
|
-
|
63
|
-
# Return the number of maximum usable processors.
|
64
|
-
#
|
65
|
-
def self.max_processors options = {}
|
66
|
-
options[:amount] || [number_of_cores, (options[:max] || Infinity)].min
|
67
|
-
end
|
68
|
-
|
69
|
-
# Gets the number of cores depending on OS.
|
70
|
-
#
|
71
|
-
def self.number_of_cores
|
72
|
-
extract_cores_for actual_platform
|
73
|
-
end
|
74
|
-
# Extracts the platform os from the platform.
|
75
|
-
#
|
76
|
-
# Note: Could also use 'rbconfig'.
|
77
|
-
#
|
78
|
-
def self.actual_platform
|
79
|
-
matched = platform.match(/-\b([a-z]*)/)
|
80
|
-
matched && matched[1]
|
81
|
-
end
|
82
|
-
# Returns a mapping
|
83
|
-
# os_name => lambda_which_returns_a_number_of_cores
|
84
|
-
#
|
85
|
-
@@number_of_cores = {
|
86
|
-
'darwin' => lambda { `system_profiler SPHardwareDataType | grep -i 'Total Number Of Cores'`.gsub(/[^\d]/, '') },
|
87
|
-
'linux' => lambda { `grep -ci ^processor /proc/cpuinfo` }
|
88
|
-
}
|
89
|
-
def self.os_to_core_mapping
|
90
|
-
@@number_of_cores
|
91
|
-
end
|
92
|
-
# Extracts the number of cores for the given os name.
|
93
|
-
#
|
94
|
-
# Note: Default is 1.
|
95
|
-
#
|
96
|
-
def self.extract_cores_for os
|
97
|
-
code_to_execute = os_to_core_mapping[os]
|
98
|
-
code_to_execute && code_to_execute.call.to_i || 1
|
99
|
-
end
|
100
|
-
def self.platform
|
101
|
-
RUBY_PLATFORM
|
102
|
-
end
|
103
|
-
|
107
|
+
|
104
108
|
end
|
File without changes
|