picky 2.5.2 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/lib/picky/adapters/rack/base.rb +23 -0
  2. data/lib/picky/adapters/rack/live_parameters.rb +33 -0
  3. data/lib/picky/adapters/rack/query.rb +65 -0
  4. data/lib/picky/adapters/rack.rb +30 -0
  5. data/lib/picky/application.rb +5 -5
  6. data/lib/picky/backend/backend.rb +108 -0
  7. data/lib/picky/backend/file/basic.rb +101 -0
  8. data/lib/picky/backend/file/json.rb +34 -0
  9. data/lib/picky/backend/file/marshal.rb +34 -0
  10. data/lib/picky/backend/file/text.rb +56 -0
  11. data/lib/picky/backend/files.rb +30 -0
  12. data/lib/picky/backend/redis/basic.rb +85 -0
  13. data/lib/picky/backend/redis/list_hash.rb +49 -0
  14. data/lib/picky/backend/redis/string_hash.rb +40 -0
  15. data/lib/picky/backend/redis.rb +40 -0
  16. data/lib/picky/calculations/location.rb +57 -0
  17. data/lib/picky/categories.rb +62 -0
  18. data/lib/picky/categories_indexed.rb +93 -0
  19. data/lib/picky/categories_indexing.rb +12 -0
  20. data/lib/picky/category.rb +127 -0
  21. data/lib/picky/category_indexed.rb +64 -0
  22. data/lib/picky/category_indexing.rb +145 -0
  23. data/lib/picky/{internals/ext → ext}/maybe_compile.rb +0 -0
  24. data/lib/picky/{internals/ext → ext}/ruby19/extconf.rb +0 -0
  25. data/lib/picky/{internals/ext → ext}/ruby19/performant.c +0 -0
  26. data/lib/picky/{internals/extensions → extensions}/array.rb +0 -0
  27. data/lib/picky/extensions/class.rb +11 -0
  28. data/lib/picky/{internals/extensions → extensions}/hash.rb +0 -0
  29. data/lib/picky/{internals/extensions → extensions}/module.rb +0 -0
  30. data/lib/picky/{internals/extensions → extensions}/object.rb +0 -0
  31. data/lib/picky/{internals/extensions → extensions}/symbol.rb +0 -0
  32. data/lib/picky/frontend_adapters/rack.rb +146 -0
  33. data/lib/picky/generators/aliases.rb +3 -3
  34. data/lib/picky/generators/base.rb +15 -0
  35. data/lib/picky/generators/partial/default.rb +5 -0
  36. data/lib/picky/generators/partial/none.rb +31 -0
  37. data/lib/picky/generators/partial/strategy.rb +25 -0
  38. data/lib/picky/generators/partial/substring.rb +118 -0
  39. data/lib/picky/generators/partial_generator.rb +15 -0
  40. data/lib/picky/generators/similarity/default.rb +7 -0
  41. data/lib/picky/generators/similarity/double_metaphone.rb +28 -0
  42. data/lib/picky/generators/similarity/metaphone.rb +28 -0
  43. data/lib/picky/generators/similarity/none.rb +31 -0
  44. data/lib/picky/generators/similarity/phonetic.rb +65 -0
  45. data/lib/picky/generators/similarity/soundex.rb +28 -0
  46. data/lib/picky/generators/similarity/strategy.rb +9 -0
  47. data/lib/picky/generators/similarity_generator.rb +15 -0
  48. data/lib/picky/generators/strategy.rb +14 -0
  49. data/lib/picky/generators/weights/default.rb +7 -0
  50. data/lib/picky/generators/weights/logarithmic.rb +39 -0
  51. data/lib/picky/generators/weights/strategy.rb +9 -0
  52. data/lib/picky/generators/weights_generator.rb +15 -0
  53. data/lib/picky/{internals/helpers → helpers}/measuring.rb +0 -0
  54. data/lib/picky/index/base.rb +119 -104
  55. data/lib/picky/index/base_indexed.rb +27 -0
  56. data/lib/picky/index/base_indexing.rb +119 -0
  57. data/lib/picky/index/memory.rb +6 -18
  58. data/lib/picky/index/redis.rb +6 -18
  59. data/lib/picky/indexed/bundle/base.rb +110 -0
  60. data/lib/picky/indexed/bundle/memory.rb +91 -0
  61. data/lib/picky/indexed/bundle/redis.rb +45 -0
  62. data/lib/picky/indexed/wrappers/bundle/calculation.rb +35 -0
  63. data/lib/picky/indexed/wrappers/bundle/location.rb +42 -0
  64. data/lib/picky/indexed/wrappers/bundle/wrapper.rb +43 -0
  65. data/lib/picky/indexed/wrappers/category/location.rb +25 -0
  66. data/lib/picky/indexed/wrappers/exact_first.rb +55 -0
  67. data/lib/picky/{internals/indexers → indexers}/base.rb +0 -0
  68. data/lib/picky/{internals/indexers → indexers}/parallel.rb +0 -0
  69. data/lib/picky/{internals/indexers → indexers}/serial.rb +0 -0
  70. data/lib/picky/{internals/indexers → indexers}/solr.rb +0 -0
  71. data/lib/picky/indexes.rb +73 -0
  72. data/lib/picky/indexes_indexed.rb +29 -0
  73. data/lib/picky/indexes_indexing.rb +49 -0
  74. data/lib/picky/indexing/bundle/base.rb +212 -0
  75. data/lib/picky/indexing/bundle/memory.rb +25 -0
  76. data/lib/picky/indexing/bundle/redis.rb +24 -0
  77. data/lib/picky/indexing/bundle/super_base.rb +61 -0
  78. data/lib/picky/indexing/wrappers/category/location.rb +25 -0
  79. data/lib/picky/interfaces/live_parameters.rb +8 -8
  80. data/lib/picky/loader.rb +89 -95
  81. data/lib/picky/{internals/performant.rb → performant.rb} +0 -0
  82. data/lib/picky/query/allocation.rb +84 -0
  83. data/lib/picky/query/allocations.rb +114 -0
  84. data/lib/picky/query/combination.rb +76 -0
  85. data/lib/picky/query/combinations/base.rb +70 -0
  86. data/lib/picky/query/combinations/memory.rb +48 -0
  87. data/lib/picky/query/combinations/redis.rb +86 -0
  88. data/lib/picky/query/indexes.rb +195 -0
  89. data/lib/picky/query/qualifiers.rb +76 -0
  90. data/lib/picky/query/token.rb +198 -0
  91. data/lib/picky/query/tokens.rb +103 -0
  92. data/lib/picky/{internals/query → query}/weights.rb +0 -0
  93. data/lib/picky/results.rb +1 -1
  94. data/lib/picky/search.rb +6 -6
  95. data/lib/picky/{internals/solr → solr}/schema_generator.rb +0 -0
  96. data/lib/picky/sources/db.rb +7 -7
  97. data/lib/picky/sources/wrappers/location.rb +2 -2
  98. data/lib/picky/tokenizers/base.rb +224 -0
  99. data/lib/picky/tokenizers/index.rb +30 -0
  100. data/lib/picky/tokenizers/location.rb +49 -0
  101. data/lib/picky/tokenizers/query.rb +55 -0
  102. data/lib/tasks/index.rake +4 -3
  103. data/lib/tasks/try.rake +2 -2
  104. data/spec/lib/{internals/adapters → adapters}/rack/base_spec.rb +1 -1
  105. data/spec/lib/{internals/adapters → adapters}/rack/live_parameters_spec.rb +1 -1
  106. data/spec/lib/{internals/adapters → adapters}/rack/query_spec.rb +1 -1
  107. data/spec/lib/application_spec.rb +3 -3
  108. data/spec/lib/{internals/index → backend}/file/basic_spec.rb +1 -1
  109. data/spec/lib/{internals/index → backend}/file/json_spec.rb +1 -1
  110. data/spec/lib/{internals/index → backend}/file/marshal_spec.rb +1 -1
  111. data/spec/lib/{internals/index → backend}/file/text_spec.rb +1 -1
  112. data/spec/lib/{internals/index → backend}/files_spec.rb +3 -3
  113. data/spec/lib/{internals/index → backend}/redis/basic_spec.rb +1 -1
  114. data/spec/lib/{internals/index → backend}/redis/list_hash_spec.rb +1 -1
  115. data/spec/lib/{internals/index → backend}/redis/string_hash_spec.rb +1 -1
  116. data/spec/lib/{internals/index → backend}/redis_spec.rb +11 -5
  117. data/spec/lib/{internals/calculations → calculations}/location_spec.rb +1 -1
  118. data/spec/lib/{internals/indexed/categories_spec.rb → categories_indexed_spec.rb} +10 -10
  119. data/spec/lib/{internals/indexed/category_spec.rb → category_indexed_spec.rb} +12 -12
  120. data/spec/lib/{internals/indexing/category_spec.rb → category_indexing_spec.rb} +10 -10
  121. data/spec/lib/{internals/cores_spec.rb → cores_spec.rb} +0 -0
  122. data/spec/lib/{internals/extensions → extensions}/array_spec.rb +0 -0
  123. data/spec/lib/{internals/extensions → extensions}/hash_spec.rb +0 -0
  124. data/spec/lib/{internals/extensions → extensions}/module_spec.rb +0 -0
  125. data/spec/lib/{internals/extensions → extensions}/object_spec.rb +0 -0
  126. data/spec/lib/{internals/extensions → extensions}/symbol_spec.rb +0 -0
  127. data/spec/lib/{internals/frontend_adapters → frontend_adapters}/rack_spec.rb +10 -10
  128. data/spec/lib/generators/aliases_spec.rb +3 -3
  129. data/spec/lib/{internals/generators → generators}/cacher_strategy_spec.rb +1 -1
  130. data/spec/lib/{internals/generators → generators}/partial/default_spec.rb +3 -3
  131. data/spec/lib/{internals/generators → generators}/partial/none_spec.rb +2 -2
  132. data/spec/lib/{internals/generators → generators}/partial/substring_spec.rb +1 -1
  133. data/spec/lib/{internals/generators → generators}/partial_generator_spec.rb +3 -3
  134. data/spec/lib/{internals/generators → generators}/similarity/double_metaphone_spec.rb +1 -1
  135. data/spec/lib/{internals/generators → generators}/similarity/metaphone_spec.rb +1 -1
  136. data/spec/lib/{internals/generators → generators}/similarity/none_spec.rb +1 -1
  137. data/spec/lib/{internals/generators → generators}/similarity/phonetic_spec.rb +1 -1
  138. data/spec/lib/{internals/generators → generators}/similarity/soundex_spec.rb +1 -1
  139. data/spec/lib/{internals/generators → generators}/similarity_generator_spec.rb +2 -2
  140. data/spec/lib/{internals/generators → generators}/weights/logarithmic_spec.rb +1 -1
  141. data/spec/lib/{internals/generators → generators}/weights_generator_spec.rb +5 -5
  142. data/spec/lib/{internals/helpers → helpers}/measuring_spec.rb +0 -0
  143. data/spec/lib/{internals/indexed/index_spec.rb → index/base_indexed_spec.rb} +5 -5
  144. data/spec/lib/{internals/indexing/index_spec.rb → index/base_indexing_spec.rb} +6 -19
  145. data/spec/lib/index/base_spec.rb +10 -53
  146. data/spec/lib/{internals/indexed → indexed}/bundle/memory_spec.rb +5 -5
  147. data/spec/lib/{internals/indexed → indexed}/bundle/redis_spec.rb +4 -4
  148. data/spec/lib/{internals/indexed → indexed}/wrappers/bundle/calculation_spec.rb +1 -1
  149. data/spec/lib/{internals/indexed → indexed}/wrappers/bundle/wrapper_spec.rb +1 -1
  150. data/spec/lib/{internals/indexed → indexed}/wrappers/exact_first_spec.rb +7 -7
  151. data/spec/lib/{internals/indexers → indexers}/base_spec.rb +0 -0
  152. data/spec/lib/{internals/indexers → indexers}/parallel_spec.rb +0 -0
  153. data/spec/lib/{internals/indexers → indexers}/serial_spec.rb +0 -0
  154. data/spec/lib/indexes_class_spec.rb +30 -0
  155. data/spec/lib/{indexed/indexes_spec.rb → indexes_indexed_spec.rb} +1 -1
  156. data/spec/lib/{indexing/indexes_spec.rb → indexes_indexing_spec.rb} +8 -8
  157. data/spec/lib/{internals/indexing/indexes_spec.rb → indexes_spec.rb} +15 -12
  158. data/spec/lib/{internals/indexing → indexing}/bundle/memory_partial_generation_speed_spec.rb +4 -4
  159. data/spec/lib/{internals/indexing → indexing}/bundle/memory_spec.rb +3 -3
  160. data/spec/lib/{internals/indexing → indexing}/bundle/redis_spec.rb +3 -3
  161. data/spec/lib/{internals/indexing → indexing}/bundle/super_base_spec.rb +2 -2
  162. data/spec/lib/{internals/interfaces → interfaces}/live_parameters_spec.rb +0 -0
  163. data/spec/lib/query/allocation_spec.rb +1 -1
  164. data/spec/lib/query/allocations_spec.rb +1 -1
  165. data/spec/lib/query/combination_spec.rb +5 -5
  166. data/spec/lib/query/combinations/base_spec.rb +1 -1
  167. data/spec/lib/query/combinations/memory_spec.rb +1 -1
  168. data/spec/lib/query/combinations/redis_spec.rb +1 -1
  169. data/spec/lib/query/indexes_spec.rb +1 -1
  170. data/spec/lib/query/qualifiers_spec.rb +4 -4
  171. data/spec/lib/query/token_spec.rb +3 -3
  172. data/spec/lib/query/tokens_spec.rb +32 -32
  173. data/spec/lib/search_spec.rb +5 -5
  174. data/spec/lib/{internals/solr → solr}/schema_generator_spec.rb +0 -0
  175. data/spec/lib/sources/db_spec.rb +4 -8
  176. data/spec/lib/sources/wrappers/location_spec.rb +1 -1
  177. data/spec/lib/{internals/tokenizers → tokenizers}/base_spec.rb +1 -1
  178. data/spec/lib/{internals/tokenizers → tokenizers}/index_spec.rb +1 -1
  179. data/spec/lib/{internals/tokenizers → tokenizers}/query_spec.rb +1 -1
  180. metadata +214 -215
  181. data/lib/picky/aliases.rb +0 -4
  182. data/lib/picky/index_bundle.rb +0 -48
  183. data/lib/picky/indexed/indexes.rb +0 -59
  184. data/lib/picky/indexing/indexes.rb +0 -87
  185. data/lib/picky/internals/adapters/rack/base.rb +0 -27
  186. data/lib/picky/internals/adapters/rack/live_parameters.rb +0 -37
  187. data/lib/picky/internals/adapters/rack/query.rb +0 -69
  188. data/lib/picky/internals/adapters/rack.rb +0 -34
  189. data/lib/picky/internals/calculations/location.rb +0 -59
  190. data/lib/picky/internals/frontend_adapters/rack.rb +0 -150
  191. data/lib/picky/internals/generators/base.rb +0 -19
  192. data/lib/picky/internals/generators/partial/default.rb +0 -7
  193. data/lib/picky/internals/generators/partial/none.rb +0 -35
  194. data/lib/picky/internals/generators/partial/strategy.rb +0 -29
  195. data/lib/picky/internals/generators/partial/substring.rb +0 -122
  196. data/lib/picky/internals/generators/partial_generator.rb +0 -19
  197. data/lib/picky/internals/generators/similarity/default.rb +0 -9
  198. data/lib/picky/internals/generators/similarity/double_metaphone.rb +0 -32
  199. data/lib/picky/internals/generators/similarity/metaphone.rb +0 -32
  200. data/lib/picky/internals/generators/similarity/none.rb +0 -35
  201. data/lib/picky/internals/generators/similarity/phonetic.rb +0 -69
  202. data/lib/picky/internals/generators/similarity/soundex.rb +0 -32
  203. data/lib/picky/internals/generators/similarity/strategy.rb +0 -11
  204. data/lib/picky/internals/generators/similarity_generator.rb +0 -19
  205. data/lib/picky/internals/generators/strategy.rb +0 -18
  206. data/lib/picky/internals/generators/weights/default.rb +0 -9
  207. data/lib/picky/internals/generators/weights/logarithmic.rb +0 -43
  208. data/lib/picky/internals/generators/weights/strategy.rb +0 -11
  209. data/lib/picky/internals/generators/weights_generator.rb +0 -19
  210. data/lib/picky/internals/index/backend.rb +0 -112
  211. data/lib/picky/internals/index/file/basic.rb +0 -105
  212. data/lib/picky/internals/index/file/json.rb +0 -38
  213. data/lib/picky/internals/index/file/marshal.rb +0 -38
  214. data/lib/picky/internals/index/file/text.rb +0 -60
  215. data/lib/picky/internals/index/files.rb +0 -34
  216. data/lib/picky/internals/index/redis/basic.rb +0 -89
  217. data/lib/picky/internals/index/redis/list_hash.rb +0 -53
  218. data/lib/picky/internals/index/redis/string_hash.rb +0 -44
  219. data/lib/picky/internals/index/redis.rb +0 -44
  220. data/lib/picky/internals/indexed/bundle/base.rb +0 -114
  221. data/lib/picky/internals/indexed/bundle/memory.rb +0 -95
  222. data/lib/picky/internals/indexed/bundle/redis.rb +0 -49
  223. data/lib/picky/internals/indexed/categories.rb +0 -140
  224. data/lib/picky/internals/indexed/category.rb +0 -111
  225. data/lib/picky/internals/indexed/index.rb +0 -63
  226. data/lib/picky/internals/indexed/wrappers/bundle/calculation.rb +0 -37
  227. data/lib/picky/internals/indexed/wrappers/bundle/location.rb +0 -44
  228. data/lib/picky/internals/indexed/wrappers/bundle/wrapper.rb +0 -45
  229. data/lib/picky/internals/indexed/wrappers/category/location.rb +0 -27
  230. data/lib/picky/internals/indexed/wrappers/exact_first.rb +0 -59
  231. data/lib/picky/internals/indexing/bundle/base.rb +0 -216
  232. data/lib/picky/internals/indexing/bundle/memory.rb +0 -29
  233. data/lib/picky/internals/indexing/bundle/redis.rb +0 -28
  234. data/lib/picky/internals/indexing/bundle/super_base.rb +0 -65
  235. data/lib/picky/internals/indexing/category.rb +0 -153
  236. data/lib/picky/internals/indexing/index.rb +0 -142
  237. data/lib/picky/internals/indexing/wrappers/category/location.rb +0 -27
  238. data/lib/picky/internals/query/allocation.rb +0 -88
  239. data/lib/picky/internals/query/allocations.rb +0 -118
  240. data/lib/picky/internals/query/combination.rb +0 -80
  241. data/lib/picky/internals/query/combinations/base.rb +0 -74
  242. data/lib/picky/internals/query/combinations/memory.rb +0 -52
  243. data/lib/picky/internals/query/combinations/redis.rb +0 -90
  244. data/lib/picky/internals/query/indexes.rb +0 -199
  245. data/lib/picky/internals/query/qualifiers.rb +0 -82
  246. data/lib/picky/internals/query/token.rb +0 -202
  247. data/lib/picky/internals/query/tokens.rb +0 -109
  248. data/lib/picky/internals/shared/category.rb +0 -52
  249. data/lib/picky/internals/tokenizers/base.rb +0 -228
  250. data/lib/picky/internals/tokenizers/index.rb +0 -34
  251. data/lib/picky/internals/tokenizers/location.rb +0 -54
  252. data/lib/picky/internals/tokenizers/query.rb +0 -59
  253. data/lib/picky/internals.rb +0 -2
  254. data/spec/lib/aliases_spec.rb +0 -9
  255. data/spec/lib/index_bundle_spec.rb +0 -69
@@ -1,111 +0,0 @@
1
- module Internals
2
-
3
- module Indexed
4
-
5
- # An index category holds a exact and a partial index for a given category.
6
- #
7
- # For example an index category for names holds a exact and
8
- # a partial index bundle for names.
9
- #
10
- class Category
11
-
12
- include Internals::Shared::Category
13
-
14
- attr_accessor :exact
15
- attr_reader :name, :index
16
- attr_writer :partial
17
-
18
- #
19
- #
20
- def initialize name, index, options = {}
21
- @name = name
22
- @index = index
23
-
24
- # TODO Push the defaults out into the index.
25
- #
26
- @partial_strategy = options[:partial] || Internals::Generators::Partial::Default
27
- similarity = options[:similarity] || Internals::Generators::Similarity::Default
28
-
29
- bundle_class = options[:indexed_bundle_class] || Bundle::Memory
30
- @exact = bundle_class.new :exact, self, similarity
31
- @partial = bundle_class.new :partial, self, similarity
32
-
33
- # @exact = exact_lambda.call(@exact, @partial) if exact_lambda = options[:exact_lambda]
34
- # @partial = partial_lambda.call(@exact, @partial) if partial_lambda = options[:partial_lambda]
35
-
36
- # TODO Extract?
37
- #
38
- Query::Qualifiers.add(name, generate_qualifiers_from(options) || [name])
39
- end
40
-
41
- def to_s
42
- <<-CATEGORY
43
- Category(#{name}):
44
- Exact:
45
- #{exact.indented_to_s(4)}
46
- Partial:
47
- #{partial.indented_to_s(4)}
48
- CATEGORY
49
- end
50
-
51
- # TODO Move to Index.
52
- #
53
- def generate_qualifiers_from options
54
- options[:qualifiers] || options[:qualifier] && [options[:qualifier]]
55
- end
56
-
57
- # Loads the index from cache.
58
- #
59
- def load_from_cache
60
- timed_exclaim %Q{"#{identifier}": Loading index from cache.}
61
- exact.load
62
- partial.load
63
- end
64
-
65
- # Loads, analyzes, and clears the index.
66
- #
67
- # Note: The idea is not to run this while the search engine is running.
68
- #
69
- def analyze collector
70
- collector[identifier] = {
71
- :exact => Analyzer.new.analyze(exact),
72
- :partial => Analyzer.new.analyze(partial)
73
- }
74
- collector
75
- end
76
-
77
- # Gets the weight for this token's text.
78
- #
79
- def weight token
80
- bundle_for(token).weight token.text
81
- end
82
-
83
- # Gets the ids for this token's text.
84
- #
85
- def ids token
86
- bundle_for(token).ids token.text
87
- end
88
-
89
- # Returns the right index bundle for this token.
90
- #
91
- def bundle_for token
92
- token.partial?? partial : exact
93
- end
94
-
95
- # The partial strategy defines whether to really use the partial index.
96
- #
97
- def partial
98
- @partial_strategy.use_exact_for_partial?? @exact : @partial
99
- end
100
-
101
- #
102
- #
103
- def combination_for token
104
- weight(token) && Internals::Query::Combination.new(token, self)
105
- end
106
-
107
- end
108
-
109
- end
110
-
111
- end
@@ -1,63 +0,0 @@
1
- module Internals
2
-
3
- module Indexed
4
-
5
- #
6
- #
7
- class Index
8
-
9
- attr_reader :name, :result_identifier, :combinator, :categories
10
-
11
- delegate :load_from_cache,
12
- :analyze,
13
- :to => :categories
14
-
15
- def initialize name, options = {}
16
- @name = name
17
-
18
- @result_identifier = options[:result_identifier] || name
19
- @bundle_class = options[:indexed_bundle_class] # TODO This should actually be a fixed parameter.
20
- ignore_unassigned_tokens = options[:ignore_unassigned_tokens] || false # TODO Move to query, somehow.
21
-
22
- @categories = Categories.new ignore_unassigned_tokens: ignore_unassigned_tokens
23
- end
24
-
25
- def define_category category_name, options = {}
26
- options = default_category_options.merge options
27
-
28
- new_category = Category.new category_name, self, options
29
- categories << new_category
30
- new_category
31
- end
32
-
33
- # By default, the category uses
34
- # * the index's bundle type.
35
- #
36
- def default_category_options
37
- {
38
- :indexed_bundle_class => @bundle_class
39
- }
40
- end
41
-
42
- # Return the possible combinations for this token.
43
- #
44
- # A combination is a tuple <token, index_bundle>.
45
- #
46
- def possible_combinations token
47
- categories.possible_combinations_for token
48
- end
49
-
50
- def to_s
51
- <<-INDEX
52
- Indexed(#{name}):
53
- Result identifier: "#{result_identifier}"
54
- Categories:
55
- #{categories.indented_to_s}
56
- INDEX
57
- end
58
-
59
- end
60
-
61
- end
62
-
63
- end
@@ -1,37 +0,0 @@
1
- module Internals
2
- module Indexed
3
- module Wrappers
4
-
5
- module Bundle
6
-
7
- # A calculation rewrites the symbol into a float.
8
- #
9
- # TODO I really need to allow integers as keys. The code below is just not up to the needed quality.
10
- #
11
- class Calculation < Wrapper
12
-
13
- #
14
- #
15
- def recalculate float
16
- float
17
- end
18
-
19
- #
20
- #
21
- def ids sym
22
- @bundle.ids recalculate(sym.to_s.to_f).to_s.to_sym
23
- end
24
-
25
- #
26
- #
27
- def weight sym
28
- @bundle.weight recalculate(sym.to_s.to_f).to_s.to_sym
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
- end
36
- end
37
- end
@@ -1,44 +0,0 @@
1
- module Internals
2
- module Indexed
3
- module Wrappers
4
-
5
- module Bundle
6
-
7
- # A location calculation recalculates a location to the Picky internal location.
8
- #
9
- class Location < Calculation
10
-
11
- def initialize bundle, options = {}
12
- super bundle
13
-
14
- precision = options[:precision] || 1
15
- user_grid = options[:grid] || raise("Gridsize needs to be given for location #{bundle.identifier}.")
16
-
17
- @calculation = Internals::Calculations::Location.new user_grid, precision
18
- end
19
-
20
- #
21
- #
22
- def recalculate float
23
- @calculation.recalculate float
24
- end
25
-
26
- #
27
- #
28
- def load
29
- # Load first the bundle, then extract the config.
30
- #
31
- bundle.load
32
- # TODO Move the to_f to the backend.
33
- #
34
- minimum = bundle[:location_minimum] && bundle[:location_minimum].to_f || raise("Configuration :location_minimum for #{bundle.identifier} missing. Did you run rake index already?")
35
- @calculation.minimum = minimum
36
- end
37
-
38
- end
39
-
40
- end
41
-
42
- end
43
- end
44
- end
@@ -1,45 +0,0 @@
1
- module Internals
2
- module Indexed
3
- module Wrappers
4
-
5
- # Per Bundle wrappers.
6
- #
7
- module Bundle
8
-
9
- # Base wrapper. Just delegates all methods to the bundle.
10
- #
11
- class Wrapper
12
-
13
- attr_reader :bundle
14
-
15
- def initialize bundle
16
- @bundle = bundle
17
- end
18
-
19
- delegate :load,
20
- :load_index,
21
- :load_weights,
22
- :load_similarity,
23
- :load_configuration,
24
- :clear_index,
25
- :clear_weights,
26
- :clear_similarity,
27
- :clear_configuration,
28
- :ids,
29
- :weight,
30
- :identifier,
31
- :analyze,
32
- :size,
33
- :index,
34
- :weights,
35
- :similarity,
36
- :configuration,
37
- :to => :@bundle
38
-
39
- end
40
-
41
- end
42
-
43
- end
44
- end
45
- end
@@ -1,27 +0,0 @@
1
- module Internals
2
- module Indexed
3
- module Wrappers
4
- module Category
5
-
6
- module Location
7
-
8
- def self.install_on category, grid, precision = 1
9
- wrapped_exact = Internals::Indexed::Wrappers::Bundle::Location.new category.exact, grid: grid, precision: precision
10
-
11
- category.class_eval do
12
- define_method :exact do
13
- wrapped_exact
14
- end
15
- define_method :partial do
16
- wrapped_exact
17
- end
18
- end
19
-
20
- end
21
-
22
- end
23
-
24
- end
25
- end
26
- end
27
- end
@@ -1,59 +0,0 @@
1
- module Internals
2
-
3
- # encoding: utf-8
4
- #
5
- module Indexed
6
-
7
- module Wrappers
8
-
9
- # This index combines an exact and partial index.
10
- # It serves to order the results such that exact hits are found first.
11
- #
12
- class ExactFirst < Indexed::Bundle::Base
13
-
14
- delegate :similar,
15
- :identifier,
16
- :name,
17
- :to => :@exact
18
- delegate :index,
19
- :category,
20
- :weight,
21
- :generate_partial_from,
22
- :generate_caches_from_memory,
23
- :generate_derived,
24
- :dump,
25
- :load,
26
- :to => :@partial
27
-
28
- def initialize category
29
- @exact = category.exact
30
- @partial = category.partial
31
- end
32
-
33
- def self.wrap index_or_category
34
- if index_or_category.respond_to? :categories
35
- wrap_each_of index_or_category.categories
36
- index_or_category
37
- else
38
- new index_or_category
39
- end
40
- end
41
- def self.wrap_each_of categories
42
- categories.categories.collect! { |category| new(category) }
43
- end
44
-
45
- def ids text
46
- @exact.ids(text) + @partial.ids(text)
47
- end
48
-
49
- def weight text
50
- [@exact.weight(text) || 0, @partial.weight(text) || 0].max
51
- end
52
-
53
- end
54
-
55
- end
56
-
57
- end
58
-
59
- end
@@ -1,216 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- module Internals
4
-
5
- module Indexing # :nodoc:all
6
-
7
- module Bundle
8
-
9
- # This is the indexing bundle.
10
- # It does all menial tasks that have nothing to do
11
- # with the actual index running etc.
12
- #
13
- class Base < SuperBase
14
-
15
- attr_accessor :partial_strategy, :weights_strategy
16
-
17
- # Path is in which directory the cache is located.
18
- #
19
- def initialize name, category, similarity_strategy, partial_strategy, weights_strategy
20
- super name, category, similarity_strategy
21
-
22
- @partial_strategy = partial_strategy
23
- @weights_strategy = weights_strategy
24
- end
25
-
26
- # Sets up a piece of the index for the given token.
27
- #
28
- def initialize_index_for token
29
- index[token] ||= []
30
- end
31
-
32
- # Generation
33
- #
34
-
35
- # This method
36
- # * Loads the base index from the "prepared..." file.
37
- # * Generates derived indexes.
38
- # * Dumps all the indexes into files.
39
- #
40
- def generate_caches_from_source
41
- load_from_index_file
42
- generate_caches_from_memory
43
- end
44
- # Generates derived indexes from the index and dumps.
45
- #
46
- # Note: assumes that there is something in the index
47
- #
48
- def generate_caches_from_memory
49
- cache_from_memory_generation_message
50
- generate_derived
51
- end
52
- def cache_from_memory_generation_message
53
- timed_exclaim %Q{"#{identifier}": Caching from intermediate in-memory index.}
54
- end
55
-
56
- # Generates the weights and similarity from the main index.
57
- #
58
- def generate_derived
59
- generate_weights
60
- generate_similarity
61
- end
62
-
63
- # Load the data from the db.
64
- #
65
- def load_from_index_file
66
- load_from_index_generation_message
67
- clear
68
- retrieve
69
- end
70
- def load_from_index_generation_message
71
- timed_exclaim %Q{"#{identifier}": Loading index.}
72
- end
73
- # Retrieves the prepared index data into the index.
74
- #
75
- # This is in preparation for generating
76
- # derived indexes (like weights, similarity)
77
- # and later dumping the optimized index.
78
- #
79
- def retrieve
80
- key_format = self[:key_format] || :to_i
81
- files.retrieve do |id, token|
82
- initialize_index_for token
83
- index[token] << id.send(key_format)
84
- end
85
- end
86
-
87
- # Generates a new index (writes its index) using the
88
- # partial caching strategy of this bundle.
89
- #
90
- def generate_partial
91
- generator = Generators::PartialGenerator.new self.index
92
- self.index = generator.generate self.partial_strategy
93
- end
94
- # Generate a partial index from the given exact index.
95
- #
96
- def generate_partial_from exact_index
97
- timed_exclaim %Q{"#{identifier}": Generating partial index for index.}
98
- self.index = exact_index
99
- self.generate_partial
100
- self
101
- end
102
- # Generates a new similarity index (writes its index) using the
103
- # given similarity caching strategy.
104
- #
105
- def generate_similarity
106
- generator = Generators::SimilarityGenerator.new self.index
107
- self.similarity = generator.generate self.similarity_strategy
108
- end
109
- # Generates a new weights index (writes its index) using the
110
- # given weight caching strategy.
111
- #
112
- def generate_weights
113
- generator = Generators::WeightsGenerator.new self.index
114
- self.weights = generator.generate self.weights_strategy
115
- end
116
-
117
- # Saves the indexes in a dump file.
118
- #
119
- def dump
120
- dump_index
121
- dump_similarity
122
- dump_weights
123
- dump_configuration
124
- end
125
- # Dumps the core index.
126
- #
127
- def dump_index
128
- timed_exclaim %Q{"#{identifier}": Dumping index.}
129
- backend.dump_index index
130
- end
131
- # Dumps the weights index.
132
- #
133
- def dump_weights
134
- timed_exclaim %Q{"#{identifier}": Dumping weights of index.}
135
- backend.dump_weights weights
136
- end
137
- # Dumps the similarity index.
138
- #
139
- def dump_similarity
140
- timed_exclaim %Q{"#{identifier}": Dumping similarity of index.}
141
- backend.dump_similarity similarity
142
- end
143
- # Dumps the similarity index.
144
- #
145
- def dump_configuration
146
- timed_exclaim %Q{"#{identifier}": Dumping configuration for index.}
147
- backend.dump_configuration configuration
148
- end
149
-
150
- # Alerts the user if an index is missing.
151
- #
152
- def raise_unless_cache_exists
153
- raise_unless_index_exists
154
- raise_unless_similarity_exists
155
- end
156
- # Alerts the user if one of the necessary indexes
157
- # (core, weights) is missing.
158
- #
159
- def raise_unless_index_exists
160
- if partial_strategy.saved?
161
- warn_if_index_small
162
- raise_unless_index_ok
163
- end
164
- end
165
- # Alerts the user if the similarity
166
- # index is missing (given that it's used).
167
- #
168
- def raise_unless_similarity_exists
169
- if similarity_strategy.saved?
170
- warn_if_similarity_small
171
- raise_unless_similarity_ok
172
- end
173
- end
174
-
175
- # Outputs a warning for the given cache.
176
- #
177
- def warn_cache_small what
178
- warn "Warning: #{what} cache for #{identifier} smaller than 16 bytes."
179
- end
180
- # Raises an appropriate error message for the given cache.
181
- #
182
- def raise_cache_missing what
183
- raise "Error: The #{what} cache for #{identifier} is missing."
184
- end
185
-
186
- # Warns the user if the similarity index is small.
187
- #
188
- def warn_if_similarity_small
189
- warn_cache_small :similarity if backend.similarity_cache_small?
190
- end
191
- # Alerts the user if the similarity index is not there.
192
- #
193
- def raise_unless_similarity_ok
194
- raise_cache_missing :similarity unless backend.similarity_cache_ok?
195
- end
196
-
197
- # Warns the user if the core or weights indexes are small.
198
- #
199
- def warn_if_index_small
200
- warn_cache_small :index if backend.index_cache_small?
201
- warn_cache_small :weights if backend.weights_cache_small?
202
- end
203
- # Alerts the user if the core or weights indexes are not there.
204
- #
205
- def raise_unless_index_ok
206
- raise_cache_missing :index unless backend.index_cache_ok?
207
- raise_cache_missing :weights unless backend.weights_cache_ok?
208
- end
209
-
210
- end
211
-
212
- end
213
-
214
- end
215
-
216
- end
@@ -1,29 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- module Internals
4
-
5
- module Indexing # :nodoc:all
6
-
7
- module Bundle
8
-
9
- # The memory version dumps its generated indexes to disk
10
- # (mostly JSON) to load them into memory on startup.
11
- #
12
- class Memory < Base
13
-
14
- # We're using files for the memory backend.
15
- # E.g. dump writes files.
16
- #
17
- alias backend files
18
-
19
- def to_s
20
- "Memory\n#{@backend.indented_to_s}"
21
- end
22
-
23
- end
24
-
25
- end
26
-
27
- end
28
-
29
- end
@@ -1,28 +0,0 @@
1
- # encoding: utf-8
2
- #
3
- module Internals
4
-
5
- module Indexing # :nodoc:all
6
-
7
- module Bundle
8
-
9
- # The Redis version dumps its generated indexes to
10
- # the Redis backend.
11
- #
12
- class Redis < Base
13
-
14
- attr_reader :backend
15
-
16
- def initialize name, category, *args
17
- super name, category, *args
18
-
19
- @backend = Internals::Index::Redis.new name, category
20
- end
21
-
22
- end
23
-
24
- end
25
-
26
- end
27
-
28
- end