picky 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/picky/bundle.rb CHANGED
@@ -6,7 +6,7 @@ class Bundle
6
6
  delegate :[], :[]=, :clear, :to => :index
7
7
 
8
8
  def initialize name, category, type, similarity_strategy
9
- @identifier = "#{name}: #{type.name} #{category.name}"
9
+ @identifier = "#{type.name}: #{name} #{category.name}"
10
10
 
11
11
  @index = {}
12
12
  @weights = {}
@@ -11,6 +11,12 @@ module Cacher
11
11
  def generate_from index
12
12
  {}
13
13
  end
14
+
15
+ # Returns if this strategy's generated file is saved.
16
+ #
17
+ def saved?
18
+ false
19
+ end
14
20
 
15
21
  end
16
22
 
@@ -2,6 +2,8 @@ module Cacher
2
2
  module Partial
3
3
  # Superclass for partial strategies.
4
4
  #
5
- class Strategy; end
5
+ class Strategy < Cacher::Strategy
6
+
7
+ end
6
8
  end
7
9
  end
@@ -17,6 +17,12 @@ module Cacher
17
17
  def generate_from index
18
18
  {}
19
19
  end
20
+
21
+ # Returns if this strategy's generated file is saved.
22
+ #
23
+ def saved?
24
+ false
25
+ end
20
26
 
21
27
  end
22
28
 
@@ -2,6 +2,8 @@ module Cacher
2
2
  module Similarity
3
3
  # Base class for all similarity strategies.
4
4
  #
5
- class Strategy; end
5
+ class Strategy < Cacher::Strategy
6
+
7
+ end
6
8
  end
7
9
  end
@@ -0,0 +1,12 @@
1
+ module Cacher
2
+ class Strategy
3
+
4
+ # By default, all caches are saved in a
5
+ # storage (like a file).
6
+ #
7
+ def saved?
8
+ true
9
+ end
10
+
11
+ end
12
+ end
@@ -2,6 +2,8 @@ module Cacher
2
2
  module Weights
3
3
  # Superclass for weighing strategies.
4
4
  #
5
- class Strategy; end
5
+ class Strategy < Cacher::Strategy
6
+
7
+ end
6
8
  end
7
9
  end
@@ -1,10 +1,14 @@
1
- module Kernel
2
-
1
+ class Object
3
2
 
3
+ # Puts a text in the form:
4
+ # 12:34:56: text here
5
+ #
4
6
  def timed_exclaim text
5
7
  exclaim "#{Time.now.strftime("%H:%M:%S")}: #{text}"
6
8
  end
7
9
 
10
+ # Just puts the given text.
11
+ #
8
12
  def exclaim text
9
13
  puts text
10
14
  end
@@ -49,7 +49,7 @@ module Index
49
49
  # Is the cache small?
50
50
  #
51
51
  def cache_small?
52
- size_of(cache_path) < 16
52
+ size_of(cache_path) < 8
53
53
  end
54
54
  # Is the cache ok? I.e. larger than four Bytes in size.
55
55
  #
@@ -14,7 +14,7 @@ module Index
14
14
  hash.dump_json cache_path
15
15
  end
16
16
  def retrieve
17
- raise "Can't retrieve from marshalled file. Use text file."
17
+ raise "Can't retrieve from JSON file. Use text file."
18
18
  end
19
19
 
20
20
  end
@@ -131,16 +131,39 @@ module Indexing
131
131
  # Alerts the user if an index is missing.
132
132
  #
133
133
  def raise_unless_cache_exists
134
- warn_cache_small :index if files.index_cache_small?
134
+ raise_unless_index_exists
135
+ raise_unless_similarity_exists
136
+ end
137
+ def raise_unless_index_exists
138
+ if partial_strategy.saved?
139
+ warn_if_index_small
140
+ raise_unless_index_ok
141
+ end
142
+ end
143
+ def raise_unless_similarity_exists
144
+ if similarity_strategy.saved?
145
+ warn_if_similarity_small
146
+ raise_unless_similarity_ok
147
+ end
148
+ end
149
+ def warn_if_similarity_small
135
150
  warn_cache_small :similarity if files.similarity_cache_small?
136
- warn_cache_small :weights if files.weights_cache_small?
137
-
138
- raise_cache_missing :index unless files.index_cache_ok?
151
+ end
152
+ def raise_unless_similarity_ok
139
153
  raise_cache_missing :similarity unless files.similarity_cache_ok?
140
- raise_cache_missing :weights unless files.weights_cache_ok?
154
+ end
155
+ # TODO Spec on down.
156
+ #
157
+ def warn_if_index_small
158
+ warn_cache_small :index if files.index_cache_small?
159
+ warn_cache_small :weights if files.weights_cache_small?
160
+ end
161
+ def raise_unless_index_ok
162
+ raise_cache_missing :index unless files.index_cache_ok?
163
+ raise_cache_missing :weights unless files.weights_cache_ok?
141
164
  end
142
165
  def warn_cache_small what
143
- puts "#{what} cache for #{identifier} smaller than 16 bytes."
166
+ puts "Warning: #{what} cache for #{identifier} smaller than 16 bytes."
144
167
  end
145
168
  # Raises an appropriate error message.
146
169
  #
data/lib/picky/loader.rb CHANGED
@@ -123,7 +123,11 @@ module Loader
123
123
  load_relative 'indexers/default'
124
124
  #
125
125
  # load_relative 'indexers/solr'
126
-
126
+
127
+ # Cacher.
128
+ #
129
+ load_relative 'cacher/strategy'
130
+
127
131
  # Partial index generation strategies.
128
132
  #
129
133
  load_relative 'cacher/partial/strategy'
@@ -1,26 +1,47 @@
1
1
  module Sources
2
2
 
3
3
  # Sources are where your data comes from.
4
- # Harvest is the most important method as it is used always to get data.
5
4
  #
5
+ # Basically, a source has 1-3 methods.
6
+ # * harvest: Used by the indexer to gather data.
7
+ # Yields an indexed_id (string or integer) and a string value.
8
+ #
9
+ # * connect_backend: Optional, called once for each type/category pair.
10
+ # * take_snapshot: Optional, called once for each type.
6
11
  class Base
7
12
 
8
13
  # Note: Methods listed for illustrative purposes.
9
14
  #
10
15
 
11
- # Yield the data (id, text for id) for the given type and field.
16
+ # Called by the indexer when gathering data.
17
+ #
18
+ # Yields the data (id, text for id) for the given type and field.
19
+ #
20
+ # When implementing or overriding your own,
21
+ # be sure to <tt>yield</tt> (or <tt>block.call</tt>) an id (as string or integer)
22
+ # and a corresponding text for the given type symbol and
23
+ # category symbol.
12
24
  #
13
- def harvest type, field
25
+ def harvest type, category
14
26
  # yields nothing
15
27
  end
16
28
 
17
- # Connects to the backend.
29
+ # Connect to the backend.
30
+ #
31
+ # Note: Called once per index/category combination
32
+ # before harvesting.
33
+ #
34
+ # For example, the db backend connects the db adapter.
18
35
  #
19
36
  def connect_backend
20
37
 
21
38
  end
22
39
 
23
- # Take a snapshot of your data, if it is fast changing.
40
+ # Used to take a snapshot of your data if it is fast changing.
41
+ # e.g. in a database, a table based on the source's select
42
+ # statement is created.
43
+ #
44
+ # Note: Called before harvesting.
24
45
  #
25
46
  def take_snapshot type
26
47
 
@@ -93,8 +93,6 @@ module Sources
93
93
  # Example:
94
94
  # "SELECT indexed_id, value FROM bla_table st WHERE kind = 'bla'"
95
95
  #
96
- # TODO Perhaps it should be just harvest field.
97
- #
98
96
  def harvest type, field
99
97
  connect_backend
100
98
 
data/lib/picky/types.rb CHANGED
@@ -8,11 +8,12 @@ class Types
8
8
  :load_from_cache,
9
9
  :to => :@indexes
10
10
 
11
- delegate :find,
11
+ delegate :check_caches,
12
+ :find,
13
+ :generate_cache_only,
14
+ :generate_index_only,
12
15
  :index,
13
16
  :index_for_tests,
14
- :generate_index_only,
15
- :generate_cache_only,
16
17
  :to => :@indexings
17
18
 
18
19
  def initialize
data/lib/tasks/index.rake CHANGED
@@ -2,16 +2,6 @@
2
2
  #
3
3
  namespace :index do
4
4
 
5
- # Not necessary anymore.
6
- #
7
- # namespace :directories do
8
- # desc 'Creates the directory structure for the indexes.'
9
- # task :make => :application do
10
- # Indexes.create_directory_structure
11
- # puts "Directory structure generated."
12
- # end
13
- # end
14
-
15
5
  desc "Takes a snapshot, indexes, and caches in random order."
16
6
  task :randomly => :application do
17
7
  Indexes.index true
@@ -26,12 +16,6 @@ namespace :index do
26
16
  Indexes.take_snapshot
27
17
  end
28
18
 
29
- # desc "E.g. Generates a specific index table. Note: intermediate indexes need to be there."
30
- # task :only, [:type, :field] => :application do |_, options|
31
- # type, field = options.type, options.field
32
- # Indexes.generate_index_only type.to_sym, field.to_sym
33
- # end
34
-
35
19
  desc "Generates a specific index from index snapshots."
36
20
  task :specific, [:type, :field] => :application do |_, options|
37
21
  type, field = options.type, options.field
@@ -39,4 +23,10 @@ namespace :index do
39
23
  Indexes.generate_cache_only type.to_sym, field.to_sym
40
24
  end
41
25
 
26
+ desc 'Checks the index files for files that are suspiciously small or missing.'
27
+ task :check => :application do
28
+ Indexes.check_caches
29
+ puts "All indexes look ok."
30
+ end
31
+
42
32
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cacher::Strategy do
4
+
5
+ describe "saved?" do
6
+ it "returns the right answer" do
7
+ Cacher::Strategy.new.saved?.should == true
8
+ end
9
+ end
10
+
11
+ end
@@ -7,7 +7,13 @@ describe Cacher::Similarity::None do
7
7
  before(:each) do
8
8
  @similarity = Cacher::Similarity::None.new
9
9
  end
10
-
10
+
11
+ describe "saved?" do
12
+ it "returns the right answer" do
13
+ @similarity.saved?.should == false
14
+ end
15
+ end
16
+
11
17
  describe 'encode' do
12
18
  it 'should always return nil' do
13
19
  @similarity.encoded(:whatever).should == nil
@@ -12,7 +12,7 @@ describe Index::Bundle do
12
12
 
13
13
  describe 'identifier' do
14
14
  it 'should return a specific identifier' do
15
- @index.identifier.should == 'some_name: some_type some_category'
15
+ @index.identifier.should == 'some_type: some_name some_category'
16
16
  end
17
17
  end
18
18
 
@@ -19,7 +19,7 @@ describe Index::File::JSON do
19
19
  it "raises" do
20
20
  lambda do
21
21
  @file.retrieve
22
- end.should raise_error("Can't retrieve from marshalled file. Use text file.")
22
+ end.should raise_error("Can't retrieve from JSON file. Use text file.")
23
23
  end
24
24
  end
25
25
 
@@ -14,7 +14,7 @@ describe Indexing::Bundle do
14
14
 
15
15
  describe 'identifier' do
16
16
  it 'should return a specific identifier' do
17
- @index.identifier.should == 'some_name: some_type some_category'
17
+ @index.identifier.should == 'some_type: some_name some_category'
18
18
  end
19
19
  end
20
20
 
@@ -99,47 +99,114 @@ describe Indexing::Bundle do
99
99
  @index.dump
100
100
  end
101
101
  end
102
-
102
+
103
103
  describe 'raise_unless_cache_exists' do
104
- before(:each) do
105
- @files = stub :files
106
- @files.stub! :index_cache_ok? => true
107
- @files.stub! :similarity_cache_ok? => true
108
- @files.stub! :weights_cache_ok? => true
109
- @files.stub! :index_cache_small? => false
110
- @files.stub! :similarity_cache_small? => false
111
- @files.stub! :weights_cache_small? => false
104
+ it "calls methods in order" do
105
+ @index.should_receive(:raise_unless_index_exists).once.ordered
106
+ @index.should_receive(:raise_unless_similarity_exists).once.ordered
112
107
 
113
- @index.stub! :files => @files
108
+ @index.raise_unless_cache_exists
114
109
  end
115
- context 'weights cache missing' do
110
+ end
111
+ describe 'raise_unless_index_exists' do
112
+ context 'partial strategy saved' do
116
113
  before(:each) do
117
- @files.stub! :weights_cache_ok? => false
114
+ strategy = stub :strategy, :saved? => true
115
+ @index.stub! :partial_strategy => strategy
118
116
  end
119
- it 'should raise' do
120
- lambda do
121
- @index.raise_unless_cache_exists
122
- end.should raise_error("weights cache for some_name: some_type some_category missing.")
117
+ it "calls the methods in order" do
118
+ @index.should_receive(:warn_if_index_small).once.ordered
119
+ @index.should_receive(:raise_unless_index_ok).once.ordered
120
+
121
+ @index.raise_unless_index_exists
123
122
  end
124
123
  end
125
- context 'similarity cache missing' do
124
+ context 'partial strategy not saved' do
126
125
  before(:each) do
127
- @files.stub! :similarity_cache_ok? => false
126
+ strategy = stub :strategy, :saved? => false
127
+ @index.stub! :partial_strategy => strategy
128
128
  end
129
- it 'should raise' do
130
- lambda do
131
- @index.raise_unless_cache_exists
132
- end.should raise_error("similarity cache for some_name: some_type some_category missing.")
129
+ it "calls nothing" do
130
+ @index.should_receive(:warn_if_index_small).never
131
+ @index.should_receive(:raise_unless_index_ok).never
132
+
133
+ @index.raise_unless_index_exists
134
+ end
135
+ end
136
+ end
137
+ describe 'raise_unless_similarity_exists' do
138
+ context 'similarity strategy saved' do
139
+ before(:each) do
140
+ strategy = stub :strategy, :saved? => true
141
+ @index.stub! :similarity_strategy => strategy
133
142
  end
143
+ it "calls the methods in order" do
144
+ @index.should_receive(:warn_if_similarity_small).once.ordered
145
+ @index.should_receive(:raise_unless_similarity_ok).once.ordered
146
+
147
+ @index.raise_unless_similarity_exists
148
+ end
149
+ end
150
+ context 'similarity strategy not saved' do
151
+ before(:each) do
152
+ strategy = stub :strategy, :saved? => false
153
+ @index.stub! :similarity_strategy => strategy
154
+ end
155
+ it "calls nothing" do
156
+ @index.should_receive(:warn_if_similarity_small).never
157
+ @index.should_receive(:raise_unless_similarity_ok).never
158
+
159
+ @index.raise_unless_similarity_exists
160
+ end
161
+ end
162
+ end
163
+ describe 'warn_if_similarity_small' do
164
+ before(:each) do
165
+ @files = @index.files
134
166
  end
135
- context 'index cache missing' do
167
+ context "files similarity cache small" do
136
168
  before(:each) do
137
- @files.stub! :index_cache_ok? => false
169
+ @files.stub! :similarity_cache_small? => true
170
+ end
171
+ it "warns" do
172
+ @index.should_receive(:warn_cache_small).once.with :similarity
173
+
174
+ @index.warn_if_similarity_small
175
+ end
176
+ end
177
+ context "files similarity cache not small" do
178
+ before(:each) do
179
+ @files.stub! :similarity_cache_small? => false
180
+ end
181
+ it "does not warn" do
182
+ @index.should_receive(:warn_cache_small).never
183
+
184
+ @index.warn_if_similarity_small
185
+ end
186
+ end
187
+ end
188
+ describe 'raise_unless_similarity_ok' do
189
+ before(:each) do
190
+ @files = @index.files
191
+ end
192
+ context "files similarity cache ok" do
193
+ before(:each) do
194
+ @files.stub! :similarity_cache_ok? => true
195
+ end
196
+ it "warns" do
197
+ @index.should_receive(:raise_cache_missing).never
198
+
199
+ @index.raise_unless_similarity_ok
200
+ end
201
+ end
202
+ context "files similarity cache not ok" do
203
+ before(:each) do
204
+ @files.stub! :similarity_cache_ok? => false
138
205
  end
139
- it 'should raise' do
140
- lambda do
141
- @index.raise_unless_cache_exists
142
- end.should raise_error("index cache for some_name: some_type some_category missing.")
206
+ it "does not warn" do
207
+ @index.should_receive(:raise_cache_missing).once.with :similarity
208
+
209
+ @index.raise_unless_similarity_ok
143
210
  end
144
211
  end
145
212
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 11
8
- - 1
9
- version: 0.11.1
8
+ - 2
9
+ version: 0.11.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-09 00:00:00 +01:00
17
+ date: 2010-11-10 00:00:00 +01:00
18
18
  default_executable: picky
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  version: "0"
31
31
  type: :development
32
32
  version_requirements: *id001
33
- description: Fast Combinatorial Ruby Search Engine
33
+ description: Fast Ruby small-text search engine with comfortable single field interface.
34
34
  email: florian.hanke+picky@gmail.com
35
35
  executables:
36
36
  - picky
@@ -57,6 +57,7 @@ files:
57
57
  - lib/picky/cacher/similarity/none.rb
58
58
  - lib/picky/cacher/similarity/strategy.rb
59
59
  - lib/picky/cacher/similarity_generator.rb
60
+ - lib/picky/cacher/strategy.rb
60
61
  - lib/picky/cacher/weights/default.rb
61
62
  - lib/picky/cacher/weights/logarithmic.rb
62
63
  - lib/picky/cacher/weights/strategy.rb
@@ -159,6 +160,7 @@ files:
159
160
  - project_prototype/unicorn.ru
160
161
  - spec/ext/performant_spec.rb
161
162
  - spec/lib/application_spec.rb
163
+ - spec/lib/cacher/cacher_strategy_spec.rb
162
164
  - spec/lib/cacher/partial/default_spec.rb
163
165
  - spec/lib/cacher/partial/none_spec.rb
164
166
  - spec/lib/cacher/partial/subtoken_spec.rb
@@ -256,10 +258,11 @@ rubyforge_project: http://rubyforge.org/projects/picky
256
258
  rubygems_version: 1.3.7
257
259
  signing_key:
258
260
  specification_version: 3
259
- summary: Picky the Search Engine
261
+ summary: "Picky: Helps you find your data."
260
262
  test_files:
261
263
  - spec/ext/performant_spec.rb
262
264
  - spec/lib/application_spec.rb
265
+ - spec/lib/cacher/cacher_strategy_spec.rb
263
266
  - spec/lib/cacher/partial/default_spec.rb
264
267
  - spec/lib/cacher/partial/none_spec.rb
265
268
  - spec/lib/cacher/partial/subtoken_spec.rb