picky 4.0.0pre5 → 4.0.0pre6

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,6 +40,7 @@ class Analyzer # :nodoc:all
40
40
  end
41
41
  def cardinality identifier, index
42
42
  return if index.size.zero?
43
+ return unless index.respond_to?(:each_pair)
43
44
 
44
45
  key_length_average = 0
45
46
  ids_length_average = 0
@@ -49,7 +50,7 @@ class Analyzer # :nodoc:all
49
50
  min_key_length = 1.0/0 # Infinity
50
51
  max_key_length = 0
51
52
 
52
- key_size, ids_size =
53
+ key_size, ids_size = 0, 0
53
54
  index.each_pair do |key, ids|
54
55
  key_size = key.size
55
56
  if key_size < min_key_length
@@ -91,7 +92,8 @@ class Analyzer # :nodoc:all
91
92
  end
92
93
  end
93
94
  def weights index
94
- return if index.size.zero?
95
+ return if !index.respond_to?(:size) || index.size.zero?
96
+ return unless index.respond_to?(:each_pair)
95
97
 
96
98
  min_weight = 1.0/0 # Infinity
97
99
  max_weight = 0.0
@@ -131,11 +133,11 @@ class Analyzer # :nodoc:all
131
133
  end
132
134
  def index_to_s
133
135
  return if analysis[:__keys].zero?
134
- [
135
- "index key cardinality: #{"%10d" % analysis[:__keys]}",
136
- "index key length range (avg): #{"%10s" % analysis[:index][:key_length]} (#{analysis[:index][:key_length_average].round(2)})",
137
- "index ids per key length range (avg): #{"%10s" % analysis[:index][:ids_length]} (#{analysis[:index][:ids_length_average].round(2)})"
138
- ].join("\n")
136
+ ary = ["index key cardinality: #{"%10d" % analysis[:__keys]}"]
137
+ return ary.join "\n" unless analysis[:index]
138
+ ary << "index key length range (avg): #{"%10s" % analysis[:index][:key_length]} (#{analysis[:index][:key_length_average].round(2)})"
139
+ ary << "index ids per key length range (avg): #{"%10s" % analysis[:index][:ids_length]} (#{analysis[:index][:ids_length_average].round(2)})"
140
+ ary.join "\n"
139
141
  end
140
142
  def weights_to_s
141
143
  return unless analysis[:weights]
@@ -40,6 +40,12 @@ module Picky
40
40
  self.mapping.clear
41
41
  end
42
42
 
43
+ # Size of the index is equal to the mapping size.
44
+ #
45
+ def size
46
+ self.mapping.size
47
+ end
48
+
43
49
  # Loads the mapping hash from json format.
44
50
  #
45
51
  def load
@@ -18,6 +18,15 @@ module Picky
18
18
  end
19
19
  end
20
20
 
21
+ # Size of the list(s).
22
+ #
23
+ def size
24
+ redis_key = "#{namespace}:*"
25
+ client.keys(redis_key).inject(0) do |total, key|
26
+ total + client.zcard(key)
27
+ end
28
+ end
29
+
21
30
  # Deletes the list for the key.
22
31
  #
23
32
  def delete key
@@ -12,6 +12,12 @@ module Picky
12
12
  client.del namespace
13
13
  end
14
14
 
15
+ # Returns the size of the hash.
16
+ #
17
+ def size
18
+ client.hlen namespace
19
+ end
20
+
15
21
  # Deletes the single value.
16
22
  #
17
23
  def delete key
@@ -10,6 +10,11 @@ module Picky
10
10
  db.execute 'create table key_value (key varchar(255), value text);'
11
11
  end
12
12
 
13
+ def size
14
+ result = db.execute 'SELECT COUNT(*) FROM key_value'
15
+ result.first.first.to_i
16
+ end
17
+
13
18
  def []= key, array
14
19
  unless array.empty?
15
20
  db.execute 'INSERT OR REPLACE INTO key_value (key,value) VALUES (?,?)',
data/lib/picky/bundle.rb CHANGED
@@ -91,11 +91,8 @@ module Picky
91
91
  # the strategy itself pretends to be an index.
92
92
  #
93
93
  def initialize_backends
94
- # @inverted, @weights, @similarity, @configuration, @realtime = backend.initial weight_strategy
95
94
  @inverted = @backend_inverted.initial
96
- # TODO @weights = @weight_strategy.initial || @backend_weights.initial
97
- #
98
- @weights = @weight_strategy.saved?? @backend_weights.initial : @weight_strategy
95
+ @weights = @weight_strategy.saved? ? @backend_weights.initial : @weight_strategy
99
96
  @similarity = @backend_similarity.initial
100
97
  @configuration = @backend_configuration.initial
101
98
  @realtime = @backend_realtime.initial
@@ -106,8 +103,6 @@ module Picky
106
103
  #
107
104
  def empty
108
105
  @inverted = @backend_inverted.empty
109
- # THINK about this. Perhaps the strategies should implement the backend methods?
110
- #
111
106
  @weights = @weight_strategy.saved? ? @backend_weights.empty : @weight_strategy
112
107
  @similarity = @backend_similarity.empty
113
108
  @configuration = @backend_configuration.empty
@@ -25,7 +25,7 @@ module Picky
25
25
  #
26
26
  def ids sym_or_string
27
27
  @inverted[sym_or_string] || []
28
- # TODO ?
28
+ # THINK Place the key_format conversion here – or move into the backend?
29
29
  #
30
30
  # if @key_format
31
31
  # class << self
@@ -79,8 +79,6 @@ module Picky
79
79
  # Loads the weights index.
80
80
  #
81
81
  def load_weights
82
- # TODO @weights = @weight_strategy.load || @backend_weights.load
83
- #
84
82
  self.weights = @backend_weights.load if @weight_strategy.saved?
85
83
  end
86
84
  # Loads the similarity index.
@@ -74,6 +74,8 @@ module Picky
74
74
  #
75
75
  similars.delete str_or_sym if similars.include? str_or_sym
76
76
  similars << str_or_sym
77
+
78
+ self.similarity_strategy.prioritize similars, str_or_sym
77
79
  end
78
80
  end
79
81
 
@@ -16,13 +16,16 @@ module Picky
16
16
  # * partial: Partial::None.new, Partial::Substring.new(from:start_char, to:up_to_char) (defaults from:-3, to:-1)
17
17
  # * similarity: Similarity::None.new (default), Similarity::DoubleMetaphone.new(amount_of_similarly_linked_words)
18
18
  # * from: The source category identifier to take the data from.
19
+ # * key_format: What this category's keys are formatted with (default is :to_i)
20
+ # * backend: The backend to use. Default is Backends::Memory.new.
21
+ # Other options are: Backends::Redis.new, Backends::SQLite.new, Backends::File.new.
22
+ # * qualifiers: Which qualifiers can be used to predefine the category. E.g. "title:bla".
19
23
  #
20
24
  # Advanced Options:
21
- # * source: Use if the category should use a different source.
22
- # * weights: Query::Weights.new( [:category1, :category2] => +2, ... )
25
+ # * weight: Query::Weights.new( [:category1, :category2] => +2, ... )
23
26
  # * tokenizer: Use a subclass of Tokenizers::Base that implements #tokens_for and #empty_tokens.
24
- # * key_format: What this category's keys are formatted with (default is :to_i)
25
27
  # * use_symbols: Whether to use symbols internally instead of strings.
28
+ # * source: Use if the category should use a different source.
26
29
  #
27
30
  def initialize name, index, options = {}
28
31
  @name = name
@@ -36,11 +36,9 @@ module Picky
36
36
 
37
37
  # Sorts the index values in place.
38
38
  #
39
- # Not used currently.
40
- #
41
- def prioritize! ary, code
39
+ def prioritize ary, code
42
40
  ary.sort_by_levenshtein! code
43
- ary.slice! amount, ary.size # THINK size is not perfectly correct, but anyway
41
+ ary.slice! amount, ary.size # Note: The ary.size is not perfectly correct.
44
42
  end
45
43
 
46
44
  end
@@ -130,7 +130,8 @@ module Picky
130
130
  }
131
131
  end
132
132
 
133
- # TODO Move to Interface object.
133
+ # THINK What to do about this? Standardize the tokenizer interface,
134
+ # then access each individual tokenizer.
134
135
  #
135
136
  def querying_removes_characters
136
137
  regexp = Tokenizer.searching.instance_variable_get :@removes_characters_regexp
@@ -25,7 +25,7 @@ describe Picky::Bundle do
25
25
  bundle.similar(:dragon).should == [:dargon]
26
26
  end
27
27
  it 'returns the right similars' do
28
- bundle.similar(:trkn).should == [:dragon, :dargon]
28
+ bundle.similar(:trkn).should == [:dargon, :dragon]
29
29
  end
30
30
  it 'performs' do
31
31
  performance_of { bundle.similar(:dragon) }.should < 0.000075
@@ -21,13 +21,13 @@ describe Picky::Generators::Similarity::Phonetic do
21
21
  instance.send :initialize
22
22
  end
23
23
 
24
- describe 'prioritize!' do
24
+ describe 'prioritize' do
25
25
  let(:phonetic) { described_class.allocate }
26
26
  it 'sorts correctly' do
27
27
  phonetic.instance_variable_set :@amount, 2
28
28
 
29
29
  ary = [:a, :b, :c]
30
- phonetic.prioritize! ary, :b
30
+ phonetic.prioritize ary, :b
31
31
 
32
32
  ary.should == [:b, :a]
33
33
  end
@@ -35,7 +35,7 @@ describe Picky::Generators::Similarity::Phonetic do
35
35
  phonetic.instance_variable_set :@amount, 2
36
36
 
37
37
  ary = [:aaa, :aa, :aaaa]
38
- phonetic.prioritize! ary, :aaa
38
+ phonetic.prioritize ary, :aaa
39
39
 
40
40
  ary.should == [:aaa, :aa]
41
41
  end
@@ -43,7 +43,7 @@ describe Picky::Generators::Similarity::Phonetic do
43
43
  phonetic.instance_variable_set :@amount, 3
44
44
 
45
45
  ary = [:aaa, :aa, :aaaa]
46
- phonetic.prioritize! ary, :aaa
46
+ phonetic.prioritize ary, :aaa
47
47
 
48
48
  ary.should == [:aaa, :aa, :aaaa]
49
49
  end
@@ -51,7 +51,7 @@ describe Picky::Generators::Similarity::Phonetic do
51
51
  phonetic.instance_variable_set :@amount, 3
52
52
 
53
53
  ary = [:aaaaa, :aa, :aaaa]
54
- phonetic.prioritize! ary, :aaa
54
+ phonetic.prioritize ary, :aaa
55
55
 
56
56
  ary.should == [:aaaa, :aa, :aaaaa]
57
57
  end
@@ -59,7 +59,7 @@ describe Picky::Generators::Similarity::Phonetic do
59
59
  phonetic.instance_variable_set :@amount, 3
60
60
 
61
61
  ary = [:aaaaa, :aa]
62
- phonetic.prioritize! ary, :aaa
62
+ phonetic.prioritize ary, :aaa
63
63
 
64
64
  ary.should == [:aa, :aaaaa]
65
65
  end
@@ -67,7 +67,15 @@ describe Picky::Generators::Similarity::Phonetic do
67
67
  phonetic.instance_variable_set :@amount, 3
68
68
 
69
69
  ary = [:aaa]
70
- phonetic.prioritize! ary, :aaa
70
+ phonetic.prioritize ary, :aaa
71
+
72
+ ary.should == [:aaa]
73
+ end
74
+ it 'sorts correctly' do
75
+ phonetic.instance_variable_set :@amount, 1
76
+
77
+ ary = [:a, :aa, :aaa]
78
+ phonetic.prioritize ary, :aaa
71
79
 
72
80
  ary.should == [:aaa]
73
81
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0pre5
4
+ version: 4.0.0pre6
5
5
  prerelease: 5
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-15 00:00:00.000000000 Z
12
+ date: 2011-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70289791461980 !ruby/object:Gem::Requirement
16
+ requirement: &70149986589980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70289791461980
24
+ version_requirements: *70149986589980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: picky-client
27
- requirement: &70289791461180 !ruby/object:Gem::Requirement
27
+ requirement: &70149986589080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 4.0.0pre5
32
+ version: 4.0.0pre6
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70289791461180
35
+ version_requirements: *70149986589080
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: text
38
- requirement: &70289791460540 !ruby/object:Gem::Requirement
38
+ requirement: &70149986588600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70289791460540
46
+ version_requirements: *70149986588600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: yajl-ruby
49
- requirement: &70289791459880 !ruby/object:Gem::Requirement
49
+ requirement: &70149986588080 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70289791459880
57
+ version_requirements: *70149986588080
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: activesupport
60
- requirement: &70289791459240 !ruby/object:Gem::Requirement
60
+ requirement: &70149986587440 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '3.0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70289791459240
68
+ version_requirements: *70149986587440
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: procrastinate
71
- requirement: &70289791458320 !ruby/object:Gem::Requirement
71
+ requirement: &70149986602940 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0.4'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70289791458320
79
+ version_requirements: *70149986602940
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rack_fast_escape
82
- requirement: &70289791457120 !ruby/object:Gem::Requirement
82
+ requirement: &70149986602420 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70289791457120
90
+ version_requirements: *70149986602420
91
91
  description: Fast Ruby semantic text search engine with comfortable single field interface.
92
92
  email: florian.hanke+picky@gmail.com
93
93
  executables: