karait 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{karait}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["bcoe"]
@@ -15,7 +15,6 @@ module Karait
15
15
  @source = raw_message
16
16
  @queue_collection = queue_collection
17
17
  @variables_to_serialize = {}
18
- set_expired
19
18
  add_accessors raw_message
20
19
  end
21
20
 
@@ -50,19 +49,6 @@ module Karait
50
49
 
51
50
  private
52
51
 
53
- def set_expired
54
- @expired = false
55
-
56
- current_time = Time.now().to_f
57
- meta = @source.fetch('_meta', {})
58
-
59
- return if meta.fetch('expire', -1.0) == -1.0
60
-
61
- if current_time - meta.fetch('timestamp', 0.0) > meta.fetch('expire', -1.0)
62
- @expired = true
63
- end
64
- end
65
-
66
52
  def add_accessors(hash)
67
53
  hash.each do |k, v|
68
54
  if not Message::BLACKLIST.has_key? k
@@ -22,7 +22,9 @@ module Karait
22
22
  message_dict[:_meta] = {
23
23
  :expire => opts.fetch(:expire, -1.0),
24
24
  :timestamp => Time.now().to_f,
25
- :expired => false
25
+ :expired => false,
26
+ :visibility_timeout => -1.0,
27
+ :accessed => 0.0
26
28
  }
27
29
 
28
30
  message_dict[:_meta][:routing_key] = opts.fetch(:routing_key) if opts[:routing_key]
@@ -45,13 +47,14 @@ module Karait
45
47
  }
46
48
  end
47
49
 
48
- @queue_collection.find(conditions).limit(opts.fetch(:messages_read, Queue::MESSAGES_READ)).each do |raw_message|
50
+ @database.eval(generate_find_with_timeouts_code({
51
+ 'conditions' => conditions,
52
+ 'limit' => opts.fetch(:messages_read, Queue::MESSAGES_READ),
53
+ 'collection' => @queue,
54
+ 'visibilityTimeout' => opts.fetch(:visibility_timeout, -1.0)
55
+ })).each do |raw_message|
49
56
  message = Karait::Message.new(raw_message=raw_message, queue_collection=@queue_collection)
50
- if message.expired?
51
- message.delete()
52
- else
53
- messages << message
54
- end
57
+ messages << message
55
58
  end
56
59
 
57
60
  return messages
@@ -119,5 +122,56 @@ module Karait
119
122
  )
120
123
  end
121
124
 
125
+ def generate_find_with_timeouts_code(variable_scope)
126
+ BSON::Code.new("
127
+ function() {
128
+ var results = [];
129
+ var currentTime = parseFloat(new Date().getTime()) / 1000.0;
130
+
131
+ function hiddenByVisibilityTimeout(result) {
132
+ if ( (currentTime - result._meta.accessed) < (result._meta.visibility_timeout) ) {
133
+ return true;
134
+ }
135
+ return false;
136
+ }
137
+
138
+ function expire(result) {
139
+ if (result._meta.expire <= 0.0) {
140
+ return false;
141
+ } else if ( (currentTime - result._meta.timestamp) > result._meta.expire ) {
142
+ db[collection].update({_id: result._id}, {$set: {'_meta.expired': true}})
143
+ return true;
144
+ }
145
+ }
146
+
147
+ (function fetchResults() {
148
+ var cursor = db[collection].find(conditions).limit(limit);
149
+ var accessedIds = [];
150
+ cursor.forEach(function(result) {
151
+ if (!expire(result) && !hiddenByVisibilityTimeout(result)) {
152
+ results.push(result);
153
+ accessedIds.push(result._id);
154
+ }
155
+ });
156
+ if (visibilityTimeout != -1.0) {
157
+ db[collection].update({_id: {$in: accessedIds}},
158
+ {
159
+ $set: {
160
+ '_meta.accessed': currentTime,
161
+ '_meta.visibility_timeout': visibilityTimeout
162
+ }
163
+ },
164
+ false,
165
+ true
166
+ );
167
+ }
168
+ })();
169
+
170
+ return results;
171
+ }
172
+ ",
173
+ variable_scope
174
+ )
175
+ end
122
176
  end
123
177
  end
@@ -78,38 +78,6 @@ class TestMessage < Test::Unit::TestCase
78
78
  message.delete()
79
79
  assert_equal 0, collection.find({'_meta.expired' => false}).count
80
80
  end
81
-
82
- should "set expired to true if current time minus timestamp is greater than expire" do
83
- raw_message = {
84
- 'routing_key' => 'foobar',
85
- 'apple' => 3,
86
- 'banana' => 5,
87
- '_meta' => {
88
- 'timestamp' => 0,
89
- 'expire' => 10,
90
- 'expired' => false
91
- }
92
- }
93
-
94
- message = Karait::Message.new(raw_message=raw_message)
95
- assert_equal true, message.expired?
96
- end
97
-
98
- should "not set expired to true if expire is -1.0" do
99
- raw_message = {
100
- 'routing_key' => 'foobar',
101
- 'apple' => 3,
102
- 'banana' => 5,
103
- '_meta' => {
104
- 'timestamp' => 0,
105
- 'expire' => -1.0,
106
- 'expired' => false
107
- }
108
- }
109
-
110
- message = Karait::Message.new(raw_message=raw_message)
111
- assert_equal false, message.expired?
112
- end
113
81
 
114
82
  should "allow a get method to be called to retrieve keys that conflict with class variables, e.g., send" do
115
83
  message = Karait::Message.new(
@@ -194,6 +194,11 @@ class TestQueue < Test::Unit::TestCase
194
194
  sleep(0.2)
195
195
  messages = queue.read()
196
196
  assert_equal 0, messages.count
197
+
198
+ # Make sure the meta._expired key is actually set.
199
+ collection = Mongo::Connection.new()['karait_test']['queue_test']
200
+ raw_message = collection.find_one
201
+ assert_equal true, raw_message['_meta']['expired']
197
202
  end
198
203
 
199
204
 
@@ -211,4 +216,22 @@ class TestQueue < Test::Unit::TestCase
211
216
  messages = queue.read()
212
217
  assert_equal 0, messages.count
213
218
  end
219
+
220
+ should "not see message in queue again until visibility timeout has passed" do
221
+ queue = Karait::Queue.new(
222
+ :database => 'karait_test',
223
+ :queue => 'queue_test'
224
+ )
225
+
226
+ queue.write Karait::Message.new({'foo' => 1})
227
+ queue.write Karait::Message.new({:foo => 2})
228
+ queue.write Karait::Message.new({'foo' => 3})
229
+ messages = queue.read(:visibility_timeout => 0.05)
230
+ assert_equal 3, messages.count
231
+ messages = queue.read(:visibility_timeout => 0.05)
232
+ assert_equal 0, messages.count
233
+ sleep(0.1)
234
+ messages = queue.read(:visibility_timeout => 0.05)
235
+ assert_equal 3, messages.count
236
+ end
214
237
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - bcoe