karait 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/karait.gemspec +2 -2
  3. data/lib/queue.rb +14 -2
  4. data/test/test_queue.rb +15 -0
  5. metadata +4 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 1.0.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "karait"
8
- s.version = "0.0.8"
8
+ s.version = "1.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["bcoe"]
12
- s.date = "2011-11-12"
12
+ s.date = "2011-12-04"
13
13
  s.description = "A ridiculously simple cross-language queuing system, built on top of MongoDB."
14
14
  s.email = "bencoe@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -27,9 +27,14 @@ module Karait
27
27
  :visible_after => -1.0
28
28
  }
29
29
 
30
- message_dict[:_meta][:routing_key] = opts.fetch(:routing_key) if opts[:routing_key]
30
+ message_dict[:_meta][:routing_key] = opts[:routing_key] if opts[:routing_key]
31
+
32
+ if opts[:unique_key]
33
+ unique_insert(message_dict, opts[:unique_key])
34
+ else
35
+ @queue_collection.insert(message_dict, :safe => true)
36
+ end
31
37
 
32
- @queue_collection.insert(message_dict, :safe => true)
33
38
  end
34
39
 
35
40
  def read(opts={})
@@ -130,6 +135,13 @@ module Karait
130
135
  end
131
136
 
132
137
  private
138
+
139
+ def unique_insert(message_dict, unique_key)
140
+ return @database.eval(
141
+ "function(obj) { if ( db.#{@queue}.count({#{unique_key}: obj.#{unique_key}}) ) { return false; } db.#{@queue}.insert(obj); return true;}",
142
+ message_dict
143
+ )
144
+ end
133
145
 
134
146
  def block_until_message_available(query, polling_interval=1.0, polling_timeout=None)
135
147
  current_time = Time.new.to_f
@@ -251,4 +251,19 @@ class TestQueue < Test::Unit::TestCase
251
251
  stop_time = Time.new.to_f
252
252
  assert_equal true, stop_time - start_time > 0.1
253
253
  end
254
+
255
+ should "should not insert two documents with the same value for a unique key" do
256
+ queue = Karait::Queue.new(
257
+ :database => 'karait_test',
258
+ :queue => 'queue_test'
259
+ )
260
+
261
+ queue.write Karait::Message.new({'foo' => 'value1'}), :unique_key => 'foo'
262
+ queue.write Karait::Message.new({'foo' => 'value1'}), :unique_key => 'foo'
263
+ queue.write Karait::Message.new({'foo' => 'value2'}), :unique_key => 'foo'
264
+
265
+ messages = queue.read()
266
+
267
+ assert_equal 2, messages.count
268
+ end
254
269
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: karait
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
+ - 1
7
8
  - 0
8
9
  - 0
9
- - 8
10
- version: 0.0.8
10
+ version: 1.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - bcoe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-12 00:00:00 Z
18
+ date: 2011-12-04 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: A ridiculously simple cross-language queuing system, built on top of MongoDB.