karait 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -11,3 +11,4 @@ nbproject
11
11
  *.swp
12
12
  *.pid
13
13
  *.log
14
+ =*
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{karait}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
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 = %q{2011-08-31}
12
+ s.date = %q{2011-09-01}
13
13
  s.description = %q{A ridiculously simple cross-language queuing system, built on top of MongoDB.}
14
14
  s.email = %q{bencoe@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -15,6 +15,7 @@ module Karait
15
15
  @source = raw_message
16
16
  @queue_collection = queue_collection
17
17
  @variables_to_serialize = {}
18
+ set_expired
18
19
  add_accessors raw_message
19
20
  end
20
21
 
@@ -49,6 +50,19 @@ module Karait
49
50
 
50
51
  private
51
52
 
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
+
52
66
  def add_accessors(hash)
53
67
  hash.each do |k, v|
54
68
  if not Message::BLACKLIST.has_key? k
@@ -6,6 +6,7 @@ module Karait
6
6
  include Karait
7
7
 
8
8
  MESSAGES_READ = 10
9
+ NO_OBJECT_FOUND_ERROR = 'No matching object found'
9
10
 
10
11
  def initialize(opts={})
11
12
  set_instance_variables opts
@@ -23,8 +24,7 @@ module Karait
23
24
  :expire => opts.fetch(:expire, -1.0),
24
25
  :timestamp => Time.now().to_f,
25
26
  :expired => false,
26
- :visibility_timeout => -1.0,
27
- :accessed => 0.0
27
+ :visible_after => -1.0
28
28
  }
29
29
 
30
30
  message_dict[:_meta][:routing_key] = opts.fetch(:routing_key) if opts[:routing_key]
@@ -33,28 +33,72 @@ module Karait
33
33
  end
34
34
 
35
35
  def read(opts={})
36
+ opts = {
37
+ :messages_read => 10,
38
+ :visibility_timeout => -1.0,
39
+ :routing_key => nil
40
+ }.update(opts)
41
+
42
+ current_time = Time.new.to_f
36
43
  messages = []
37
44
 
38
- conditions = {
39
- '_meta.expired' => false
45
+ query = {
46
+ '_meta.expired' => false,
47
+ '_meta.visible_after' => {
48
+ '$lt' => current_time
49
+ }
40
50
  }
41
-
42
- if opts[:routing_key]
43
- conditions['_meta.routing_key'] = opts[:routing_key]
51
+ if opts[:routing_key] != nil
52
+ query['_meta.routing_key'] = opts[:routing_key]
44
53
  else
45
- conditions['_meta.routing_key'] = {
54
+ query['_meta.routing_key'] = {
46
55
  '$exists' => false
47
56
  }
48
57
  end
49
58
 
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|
59
+ update = false
60
+ if opts[:visibility_timeout] != -1.0
61
+ update = {
62
+ '$set' => {
63
+ '_meta.visible_after' => current_time + opts[:visibility_timeout]
64
+ }
65
+ }
66
+ end
67
+
68
+ raw_messages = []
69
+
70
+ if update
71
+ (0..opts[:messages_read]).each do
72
+ begin
73
+
74
+ raw_message = @queue_collection.find_and_modify(:query => query, :update => update)
75
+
76
+ if raw_message:
77
+ raw_messages << raw_message
78
+ else
79
+ break
80
+ end
81
+
82
+ rescue Mongo::OperationFailure => operation_failure
83
+ if not operation_failure.to_s.match(Queue::NO_OBJECT_FOUND_ERROR)
84
+ raise operation_failure
85
+ end
86
+ end
87
+
88
+ end
89
+ else
90
+ @queue_collection.find(query).limit(opts[:messages_read]).each do |raw_message|
91
+ raw_messages << raw_message
92
+ end
93
+ end
94
+
95
+ raw_messages.each do |raw_message|
56
96
  message = Karait::Message.new(raw_message=raw_message, queue_collection=@queue_collection)
57
- messages << message
97
+ if message.expired?
98
+ message.delete()
99
+ else
100
+ messages << message
101
+ end
58
102
  end
59
103
 
60
104
  return messages
@@ -111,6 +155,8 @@ module Karait
111
155
  @queue_collection = @database[@queue]
112
156
  @queue_collection.create_index('_id')
113
157
  @queue_collection.create_index('_meta.routing_key')
158
+ @queue_collection.create_index('_meta.expired')
159
+ @queue_collection.create_index('_meta.visible_after')
114
160
  end
115
161
 
116
162
  def create_capped_collection
@@ -121,57 +167,5 @@ module Karait
121
167
  :max => @queue_size
122
168
  )
123
169
  end
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
176
170
  end
177
171
  end
@@ -226,12 +226,12 @@ class TestQueue < Test::Unit::TestCase
226
226
  queue.write Karait::Message.new({'foo' => 1})
227
227
  queue.write Karait::Message.new({:foo => 2})
228
228
  queue.write Karait::Message.new({'foo' => 3})
229
- messages = queue.read(:visibility_timeout => 0.05)
229
+ messages = queue.read(:visibility_timeout => 0.4)
230
230
  assert_equal 3, messages.count
231
- messages = queue.read(:visibility_timeout => 0.05)
231
+ messages = queue.read(:visibility_timeout => 0.4)
232
232
  assert_equal 0, messages.count
233
- sleep(0.1)
234
- messages = queue.read(:visibility_timeout => 0.05)
233
+ sleep(0.5)
234
+ messages = queue.read(:visibility_timeout => 0.4)
235
235
  assert_equal 3, messages.count
236
236
  end
237
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: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
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-08-31 00:00:00 -04:00
18
+ date: 2011-09-01 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21