mongoid-haystack 1.4.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@
2
2
  #
3
3
  module Mongoid
4
4
  module Haystack
5
- const_set(:Version, '1.4.1') unless const_defined?(:Version)
5
+ const_set(:Version, '1.4.2') unless const_defined?(:Version)
6
6
 
7
7
  class << Haystack
8
8
  def version
@@ -11,7 +11,7 @@
11
11
 
12
12
  def dependencies
13
13
  {
14
- 'mongoid' => [ 'mongoid' , '~> 3.0' ] ,
14
+ 'mongoid' => [ 'mongoid' , '~> 3.1' ] ,
15
15
  'moped' => [ 'moped' , '~> 1.3' ] ,
16
16
  'origin' => [ 'origin' , '~> 1.0' ] ,
17
17
  'map' => [ 'map' , '~> 6.2' ] ,
@@ -3,6 +3,8 @@ module Mongoid
3
3
  class Token
4
4
  include Mongoid::Document
5
5
 
6
+ class Error < ::StandardError; end
7
+
6
8
  class << Token
7
9
  def values_for(*args)
8
10
  Haystack.tokens_for(*args)
@@ -15,21 +17,59 @@ module Mongoid
15
17
  values.flatten!
16
18
  values.compact!
17
19
 
18
- # ensure that a token exists for each value seen
20
+ # try very hard to create tokens efficently in bulk
19
21
  #
20
- existing = where(:value.in => values)
21
- missing = values - existing.map(&:value)
22
+ missing = []
23
+
24
+ 42.times do
25
+ existing = where(:value.in => values)
26
+ missing = values - existing.map(&:value)
27
+
28
+ docs = missing.map{|value| {:_id => Token.next_hex_id, :value => value}}
29
+
30
+ unless docs.empty?
31
+ collection = mongo_session.with(:safe => false)[collection_name]
32
+ collection.insert(docs, [:continue_on_error])
33
+ else
34
+ break
35
+ end
36
+ end
22
37
 
23
- docs = missing.map{|value| {:_id => Token.next_hex_id, :value => value}}
24
- unless docs.empty?
25
- collection = mongo_session.with(:safe => false)[collection_name]
26
- collection.insert(docs, [:continue_on_error])
38
+ # fill in gaps individually iff needed... (another process may be racing to create tokens)
39
+ #
40
+ 42.times do
41
+ existing = where(:value.in => values)
42
+ missing = values - existing.map(&:value)
43
+
44
+ unless missing.empty?
45
+ missing.each do |value|
46
+ begin
47
+ Token.new.tap do |t|
48
+ t.id = Token.next_hex_id
49
+ t.value = value
50
+ t.save!
51
+ end
52
+ rescue Object
53
+ next
54
+ end
55
+ end
56
+ else
57
+ break
58
+ end
27
59
  end
28
60
 
29
61
  # new we should have one token per uniq value
30
62
  #
31
63
  tokens = where(:value.in => values).to_a
32
64
 
65
+ # sigh - go boom if we failed to ensure the creation of all required
66
+ # tokens...
67
+ #
68
+ missing = values - tokens.map(&:value)
69
+ unless missing.size == 0
70
+ raise(Error, "missing tokens (#{ missing.inspect })")
71
+ end
72
+
33
73
  # batch update the counts on the tokens by the number of times each
34
74
  # value was seen in the list
35
75
  #
@@ -42,12 +82,6 @@ module Mongoid
42
82
  values.each do |value|
43
83
  token = token_index[value]
44
84
 
45
- unless token
46
- token = Token.create!(:value => value)
47
- value_index[value] ||= []
48
- value_index[value].push(value)
49
- end
50
-
51
85
  count = value_index[value].size
52
86
  counts[count] ||= []
53
87
  counts[count].push(token.id)
@@ -81,7 +115,7 @@ module Mongoid
81
115
 
82
116
  field(:_id, :type => String, :default => proc{ Token.next_hex_id })
83
117
  field(:value, :type => String)
84
- field(:count, :type => Integer, :default => 0)
118
+ field(:count, :type => Integer)
85
119
 
86
120
  index({:value => 1}, {:unique => true})
87
121
  index({:count => 1})
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "mongoid-haystack"
6
- spec.version = "1.4.1"
6
+ spec.version = "1.4.2"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "mongoid-haystack"
9
9
  spec.description = "a mongoid 3 zero-config, zero-integration, POLS pure mongo fulltext solution"
@@ -58,7 +58,7 @@ Gem::Specification::new do |spec|
58
58
  spec.test_files = nil
59
59
 
60
60
 
61
- spec.add_dependency(*["mongoid", "~> 3.0"])
61
+ spec.add_dependency(*["mongoid", "~> 3.1"])
62
62
 
63
63
  spec.add_dependency(*["moped", "~> 1.3"])
64
64
 
@@ -96,6 +96,7 @@ Testing Mongoid::Haystack do
96
96
 
97
97
  assert{ Mongoid::Haystack.index(A) }
98
98
 
99
+
99
100
  assert{ Mongoid::Haystack::Token.count == 3 }
100
101
  assert{ Mongoid::Haystack::Token.all.map(&:value).sort == %w( cat dog dogs ) }
101
102
  assert{ Mongoid::Haystack::Token.total == 4 }
@@ -296,11 +297,15 @@ Testing Mongoid::Haystack do
296
297
  end
297
298
  end
298
299
 
300
+ k.destroy_all
301
+
299
302
  n = 10
300
303
 
301
- n.times do
304
+ n.times do |i|
302
305
  k.create!(:title => 'the cats and dogs', :body => 'now now is is the the time time for for all all good good men women')
306
+ assert{ Mongoid::Haystack.search('cat').count == (i + 1) }
303
307
  end
308
+ assert{ Mongoid::Haystack.search('cat').count == n }
304
309
 
305
310
  n.times do
306
311
  k.create!(:title => 'a b c abc xyz abc xyz b', :body => 'pdq pdq pdq xyz teh ngr am')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-haystack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-05 00:00:00.000000000 Z
12
+ date: 2013-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '3.0'
29
+ version: '3.1'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: moped
32
32
  requirement: !ruby/object:Gem::Requirement