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,87 +0,0 @@
1
- module Indexing # :nodoc:all
2
-
3
- # Registers the indexes held at index time, for indexing.
4
- #
5
- class Indexes
6
-
7
- attr_reader :indexes
8
-
9
- each_delegate :take_snapshot,
10
- :generate_caches,
11
- :backup_caches,
12
- :restore_caches,
13
- :check_caches,
14
- :clear_caches,
15
- :create_directory_structure,
16
- :to => :indexes
17
-
18
- def initialize
19
- clear
20
- end
21
-
22
- def to_s
23
- indexes.indented_to_s
24
- end
25
-
26
- # Clears the array of indexes.
27
- #
28
- def clear
29
- @indexes = []
30
- end
31
-
32
- # Registers an index with the indexes.
33
- #
34
- def register index
35
- self.indexes << index
36
- end
37
-
38
- # Runs the indexers in parallel (index + cache).
39
- #
40
- def index randomly = true
41
- take_snapshot
42
-
43
- # Run in parallel.
44
- #
45
- timed_exclaim "Indexing using #{Cores.max_processors} processors, in #{randomly ? 'random' : 'given'} order."
46
-
47
- # Run indexing/caching forked.
48
- #
49
- Cores.forked self.indexes, { randomly: randomly } do |an_index|
50
- an_index.index!
51
- an_index.cache!
52
- end
53
-
54
- timed_exclaim "Indexing finished."
55
- end
56
-
57
- # For integration testing – indexes for the tests
58
- # without forking and shouting ;)
59
- #
60
- def index_for_tests
61
- take_snapshot
62
-
63
- self.indexes.each do |an_index|
64
- an_index.index!
65
- an_index.cache!
66
- end
67
- end
68
-
69
- # Find a given index:category pair.
70
- #
71
- def find index_name, category_name = nil
72
- index_name = index_name.to_sym
73
-
74
- indexes.each do |index|
75
- next unless index.name == index_name
76
-
77
- return index unless category_name
78
-
79
- found = index.find category_name
80
- return found if found
81
- end
82
-
83
- raise %Q{Index "#{index_name}" not found. Possible indexes: "#{indexes.map(&:name).join('", "')}".}
84
- end
85
-
86
- end
87
- end
@@ -1,27 +0,0 @@
1
- module Internals
2
-
3
- module Adapters
4
- # Adapter that is plugged into a Rack outlet.
5
- #
6
- module Rack
7
-
8
- # Subclasses of this class should respond to
9
- # * to_app(options)
10
- #
11
- class Base
12
-
13
- # Puts together an appropriately structured Rack response.
14
- #
15
- # Note: Bytesize is needed to have special characters not trip up Rack.
16
- #
17
- def respond_with response, content_type = 'application/json'
18
- [200, { 'Content-Type' => content_type, 'Content-Length' => response.bytesize.to_s }, [response]]
19
- end
20
-
21
- end
22
-
23
- end
24
-
25
- end
26
-
27
- end
@@ -1,37 +0,0 @@
1
- module Internals
2
-
3
- module Adapters
4
-
5
- #
6
- #
7
- module Rack
8
-
9
- class LiveParameters < Base
10
-
11
- def initialize live_parameters
12
- @live_parameters = live_parameters
13
- end
14
-
15
- #
16
- #
17
- def to_app options = {}
18
- # For capturing by the lambda block.
19
- #
20
- live_parameters = @live_parameters
21
-
22
- lambda do |env|
23
- params = ::Rack::Request.new(env).params
24
-
25
- results = live_parameters.parameters params
26
-
27
- respond_with results.to_json
28
- end
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
- end
36
-
37
- end
@@ -1,69 +0,0 @@
1
- module Internals
2
-
3
- module Adapters
4
- # This is an adapter that is plugged into a Rack outlet.
5
- #
6
- # It looks at what is given to it and generate an appropriate
7
- # adapter for it.
8
- #
9
- # For example, if you give it a query, it will extract the query param etc.
10
- # and call search_with_text on it if it is called by Rack.
11
- #
12
- module Rack
13
-
14
- # TODO Rename to Search.
15
- #
16
- class Query < Base
17
-
18
- @@defaults = {
19
- query_key: 'query'.freeze,
20
- ids_key: 'ids'.freeze,
21
- offset_key: 'offset'.freeze,
22
- content_type: 'application/json'.freeze
23
- }
24
-
25
- def initialize query
26
- @query = query
27
- @defaults = @@defaults.dup
28
- end
29
-
30
- def to_app options = {}
31
- # For capturing in the lambda.
32
- #
33
- query = @query
34
- query_key = options[:query_key] || @defaults[:query_key]
35
- content_type = options[:content_type] || @defaults[:content_type]
36
-
37
- lambda do |env|
38
- params = ::Rack::Request.new(env).params
39
-
40
- results = query.search_with_text *extracted(params)
41
-
42
- PickyLog.log results.to_log(params[query_key])
43
-
44
- respond_with results.to_response, content_type
45
- end
46
- end
47
-
48
- # Helper method to extract the params
49
- #
50
- # Defaults are 20 ids, offset 0.
51
- #
52
- UTF8_STRING = 'UTF-8'.freeze
53
- def extracted params
54
- [
55
- # query is encoded in ASCII
56
- #
57
- params[@defaults[:query_key]] && params[@defaults[:query_key]].force_encoding(UTF8_STRING),
58
- params[@defaults[:ids_key]] && params[@defaults[:ids_key]].to_i || 20,
59
- params[@defaults[:offset_key]] && params[@defaults[:offset_key]].to_i || 0
60
- ]
61
- end
62
-
63
- end
64
-
65
- end
66
-
67
- end
68
-
69
- end
@@ -1,34 +0,0 @@
1
- module Internals
2
-
3
- module Adapters
4
-
5
- # This is an adapter that is plugged into a Rack outlet.
6
- #
7
- # It looks at what is given to it and generate an appropriate
8
- # adapter for it.
9
- #
10
- # For example, if you give it a query, it will extract the query param etc.
11
- # and call search_with_text on it if it is called by Rack.
12
- #
13
- # Usage:
14
- # Adapters::Rack.app_for(thing, options)
15
- #
16
- module Rack
17
-
18
- # Generates the appropriate app for Rack.
19
- #
20
- @@mapping = {
21
- :search_with_text => Query,
22
- :parameters => LiveParameters
23
- }
24
- def self.app_for thing, options = {}
25
- @@mapping.each_pair do |method, adapter|
26
- return adapter.new(thing).to_app(options) if thing.respond_to?(method)
27
- end
28
- end
29
-
30
- end
31
-
32
- end
33
-
34
- end
@@ -1,59 +0,0 @@
1
- module Internals
2
- module Calculations # :nodoc:all
3
-
4
- # A location calculation recalculates a 1-d location
5
- # to the Picky internal 1-d "grid".
6
- #
7
- # For example, if you have a location x == 12.3456,
8
- # it will be recalculated into 3, if the minimum is 9
9
- # and the gridlength is 1.
10
- #
11
- class Location
12
-
13
- attr_reader :minimum, :precision, :grid
14
-
15
- def initialize user_grid, precision = nil
16
- @user_grid = user_grid
17
- @precision = precision || 1
18
- @grid = @user_grid / (@precision + 0.5)
19
- end
20
-
21
- def minimum= minimum
22
- # Add a margin of 1 user grid.
23
- #
24
- minimum -= @user_grid
25
-
26
- # Add plus 1 grid so that the index key never falls on 0.
27
- # Why? to_i maps by default to 0.
28
- #
29
- minimum -= @grid
30
-
31
- @minimum = minimum
32
- end
33
-
34
- #
35
- #
36
- def add_margin length
37
- @minimum -= length
38
- end
39
-
40
- #
41
- #
42
- def recalculated_range location
43
- range recalculate(location)
44
- end
45
- #
46
- #
47
- def range around_location
48
- (around_location - @precision)..(around_location + @precision)
49
- end
50
- #
51
- #
52
- def recalculate location
53
- ((location - @minimum) / @grid).floor
54
- end
55
-
56
- end
57
-
58
- end
59
- end
@@ -1,150 +0,0 @@
1
- require 'rack/mount'
2
-
3
- module Internals
4
-
5
- module FrontendAdapters
6
-
7
- # TODO Rename to Routing again. Push everything back into appropriate Adapters.
8
- #
9
- class Rack # :nodoc:all
10
-
11
- #
12
- #
13
- def reset_routes
14
- @routes = ::Rack::Mount::RouteSet.new
15
- end
16
- def routes
17
- @routes || reset_routes
18
- end
19
- def finalize
20
- routes.freeze
21
- end
22
-
23
- # Routing simply delegates to the route set to handle a request.
24
- #
25
- def call env
26
- routes.call env
27
- end
28
-
29
- # API method.
30
- #
31
- def route options = {}
32
- mappings, route_options = split options
33
- mappings.each do |url, query|
34
- route_one url, query, route_options
35
- end
36
- end
37
- # Splits the route method options
38
- # into real options and route options (/regexp/ => thing or 'some/path' => thing).
39
- #
40
- def split options
41
- mappings = {}
42
- route_options = {}
43
- options.each_pair do |key, value|
44
- if Regexp === key or String === key
45
- mappings[key] = value
46
- else
47
- route_options[key] = value
48
- end
49
- end
50
- [mappings, route_options]
51
- end
52
- def route_one url, query, route_options = {}
53
- raise RouteTargetNilError.new(url) unless query
54
- routes.add_route Internals::Adapters::Rack.app_for(query, route_options), default_options(url, route_options), {}, query.to_s
55
- end
56
- class RouteTargetNilError < StandardError
57
- def initialize url
58
- @url = url
59
- end
60
- def to_s
61
- "Routing for #{@url.inspect} was defined with a nil target object, i.e. #{@url.inspect} => nil."
62
- end
63
- end
64
- #
65
- #
66
- def root status
67
- answer %r{^/$}, STATUSES[status]
68
- end
69
- #
70
- #
71
- def default status
72
- answer nil, STATUSES[status]
73
- end
74
-
75
-
76
-
77
- # TODO Can Rack handle this for me?
78
- #
79
- # Note: Rack-mount already handles the 404.
80
- #
81
- STATUSES = {
82
- 200 => lambda { |_| [200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, ['']] },
83
- 404 => lambda { |_| [404, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, ['']] }
84
- }
85
-
86
- #
87
- #
88
- def default_options url, route_options = {}
89
- url = normalized url
90
-
91
- options = { request_method: 'GET' }.merge route_options
92
-
93
- options[:path_info] = url if url
94
-
95
- options.delete :content_type
96
-
97
- query_params = options.delete :query
98
- options[:query_string] = %r{#{generate_query_string(query_params)}} if query_params
99
-
100
- options
101
- end
102
- #
103
- #
104
- def generate_query_string query_params
105
- raise "At least one query string condition is needed." if query_params.size.zero?
106
- raise "Too many query param conditions (only 1 allowed): #{query_params}" if query_params.size > 1
107
- k, v = query_params.first
108
- "#{k}=#{v}"
109
- end
110
-
111
- # Setup a route that answers using the given app.
112
- #
113
- def answer url = nil, app = nil
114
- routes.add_route (app || STATUSES[200]), default_options(url)
115
- end
116
-
117
- # Returns a regular expression for the url if it is given a String-like object.
118
- #
119
- def normalized url
120
- url.respond_to?(:to_str) ? %r{#{url}} : url
121
- end
122
-
123
- # Returns true if there are no routes defined.
124
- #
125
- def empty?
126
- routes.length.zero?
127
- end
128
-
129
- # TODO Beautify. Rewrite!
130
- #
131
- def to_s
132
- max_length = routes.instance_variable_get(:@routes).reduce(0) do |current_max, route|
133
- route_length = route.conditions[:path_info].source.to_s.size
134
- route_length > current_max ? route_length : current_max
135
- end
136
- "Note: Anchored (\u2713) regexps are faster, e.g. /\\A.*\\Z/ or /^.*$/.\n\n" +
137
- routes.instance_variable_get(:@routes).map do |route|
138
- path_info = route.conditions[:path_info]
139
- anchored = ::Rack::Mount::Utils.regexp_anchored?(path_info)
140
- anchored_ok = anchored ? "\u2713" : " "
141
- source = path_info.source
142
- "#{anchored_ok} #{source.ljust(max_length)} => #{route.name}"
143
- end.join("\n")
144
- end
145
-
146
- end
147
-
148
- end
149
-
150
- end
@@ -1,19 +0,0 @@
1
- module Internals
2
-
3
- module Generators # :nodoc:all
4
-
5
- # A cache generator holds an index.
6
- #
7
- class Base
8
-
9
- attr_reader :index
10
-
11
- def initialize index
12
- @index = index
13
- end
14
-
15
- end
16
-
17
- end
18
-
19
- end
@@ -1,7 +0,0 @@
1
- module Internals
2
- module Generators
3
- module Partial
4
- Default = Substring.new from: -3, to: -1
5
- end
6
- end
7
- end
@@ -1,35 +0,0 @@
1
- module Internals
2
-
3
- module Generators
4
-
5
- module Partial
6
-
7
- # Does not generate a partial index.
8
- #
9
- class None < Strategy
10
-
11
- # Returns an empty index.
12
- #
13
- def generate_from index
14
- {}
15
- end
16
-
17
- # Returns if this strategy's generated file is saved.
18
- #
19
- def saved?
20
- false
21
- end
22
-
23
- # Do not use the partial bundle for getting ids and weights.
24
- #
25
- def use_exact_for_partial?
26
- true
27
- end
28
-
29
- end
30
-
31
- end
32
-
33
- end
34
-
35
- end
@@ -1,29 +0,0 @@
1
- module Internals
2
-
3
- module Generators
4
-
5
- module Partial
6
-
7
- # Superclass for partial strategies.
8
- #
9
- class Strategy < Generators::Strategy
10
-
11
- # Defines whether to use the exact bundle
12
- # instead of the partial one.
13
- #
14
- # Default is @false@.
15
- #
16
- # For example:
17
- # Partial::None.new # Uses the exact index instead of the partial one.
18
- #
19
- def use_exact_for_partial?
20
- false
21
- end
22
-
23
- end
24
-
25
- end
26
-
27
- end
28
-
29
- end
@@ -1,122 +0,0 @@
1
- module Internals
2
-
3
- module Generators
4
-
5
- module Partial
6
-
7
- # Generates the right substrings for use in the substring strategy.
8
- #
9
- class SubstringGenerator
10
-
11
- attr_reader :from, :to
12
-
13
- def initialize from, to
14
- @from, @to = from, to
15
-
16
- if @to.zero?
17
- def each_subtoken token, &block
18
- token.each_subtoken @from, &block
19
- end
20
- else
21
- def each_subtoken token, &block
22
- token[0..@to].intern.each_subtoken @from, &block
23
- end
24
- end
25
-
26
- end
27
-
28
- end
29
-
30
- # The subtoken partial strategy.
31
- #
32
- # If given "florian"
33
- # it will index "floria", "flori", "flor", "flo", "fl", "f"
34
- # (Depending on what the given from value is, the example is with option from: 1)
35
- #
36
- class Substring < Strategy
37
-
38
- # The from option signifies where in the symbol it
39
- # will start in generating the subtokens.
40
- #
41
- # Examples:
42
- #
43
- # With :hello, and to: -1 (default)
44
- # * from: 1 # => [:hello, :hell, :hel, :he, :h]
45
- # * from: 4 # => [:hello, :hell]
46
- #
47
- # With :hello, and to: -2
48
- # * from: 1 # => [:hell, :hel, :he, :h]
49
- # * from: 4 # => [:hell]
50
- #
51
- def initialize options = {}
52
- from = options[:from] || 1
53
- to = options[:to] || -1
54
- @generator = SubstringGenerator.new from, to
55
- end
56
-
57
- # Delegator to generator#from.
58
- #
59
- def from
60
- @generator.from
61
- end
62
-
63
- # Delegator to generator#to.
64
- #
65
- def to
66
- @generator.to
67
- end
68
-
69
- # Generates a partial index from the given index.
70
- #
71
- def generate_from index
72
- result = {}
73
-
74
- # Generate for each key token the subtokens.
75
- #
76
- i = 0
77
- index.each_key do |token|
78
- i += 1
79
- if i == 5000
80
- timed_exclaim "Generating partial tokens for token #{token}. This appears every 5000 tokens."
81
- i = 0
82
- end
83
- generate_for token, index, result
84
- end
85
-
86
- # Remove duplicate ids.
87
- #
88
- # THINK If it is unique for a subtoken, it is
89
- # unique for all derived longer tokens.
90
- #
91
- result.each_value &:uniq!
92
-
93
- result
94
- end
95
-
96
- private
97
-
98
- # To each shortened token of :test
99
- # :test, :tes, :te, :t
100
- # add all ids of :test
101
- #
102
- # "token" here means just text.
103
- #
104
- # THINK Could be improved by appending the aforegoing ids?
105
- #
106
- def generate_for token, index, result
107
- @generator.each_subtoken(token) do |subtoken|
108
- if result[subtoken]
109
- result[subtoken] += index[token] # unique
110
- else
111
- result[subtoken] = index[token].dup
112
- end
113
- end
114
- end
115
-
116
- end
117
-
118
- end
119
-
120
- end
121
-
122
- end
@@ -1,19 +0,0 @@
1
- module Internals
2
-
3
- module Generators
4
-
5
- # The partial generator uses a subtoken(downto:1) generator as default.
6
- #
7
- class PartialGenerator < Base
8
-
9
- # Generate a partial index based on the given index.
10
- #
11
- def generate strategy = Partial::Substring.new(from: 1)
12
- strategy.generate_from self.index
13
- end
14
-
15
- end
16
-
17
- end
18
-
19
- end