diff --git a/lib/mongo.rb b/lib/mongo.rb index f20d090..c44fd48 100644 --- a/lib/mongo.rb +++ b/lib/mongo.rb @@ -19,6 +19,7 @@ module Mongo OP_DELETE = 2006 OP_KILL_CURSORS = 2007 + OP_QUERY_TAILABLE = 2 OP_QUERY_SLAVE_OK = 4 OP_QUERY_NO_CURSOR_TIMEOUT = 16 diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 6752fbf..75cb69c 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -47,6 +47,7 @@ module Mongo @timeout = options[:timeout] || false @explain = options[:explain] @socket = options[:socket] + @tailable = options[:tailable] || false @batch_size = options[:batch_size] || Mongo::Constants::DEFAULT_BATCH_SIZE @full_collection_name = "#{@collection.db.name}.#{@collection.name}" @@ -251,7 +252,8 @@ module Mongo def query_opts timeout = @timeout ? 0 : Mongo::Constants::OP_QUERY_NO_CURSOR_TIMEOUT slave_ok = @connection.slave_ok? ? Mongo::Constants::OP_QUERY_SLAVE_OK : 0 - slave_ok + timeout + tailable = @tailable ? Mongo::Constants::OP_QUERY_TAILABLE : 0 + slave_ok + timeout + tailable end # Get the query options for this Cursor. diff --git a/test/collection_test.rb b/test/collection_test.rb index 0e4957d..f3dbfee 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -594,4 +594,42 @@ class TestCollection < Test::Unit::TestCase end end end + + context "Capped collections" do + setup do + @@db.drop_collection('log') + @capped = @@db.create_collection('log', :capped => true, :size => 1024) + + 10.times { |n| @capped.insert({:n => n}) } + end + + should "find using a standard cursor" do + cursor = @capped.find + 10.times do + assert cursor.next_document + end + assert_nil cursor.next_document + @capped.insert({:n => 100}) + assert_nil cursor.next_document + end + + should "" do + col = @@db['regular-collection'] + col.insert({:a => 1000}) + tail = Cursor.new(col, :tailable => true, :order => [['$natural', 1]]) + assert_raise OperationFailure do + tail.next_document + end + end + + should "find using a tailable cursor" do + tail = Cursor.new(@capped, :tailable => true, :order => [['$natural', 1]]) + 10.times do + assert tail.next_document + end + assert_nil tail.next_document + @capped.insert({:n => 100}) + assert tail.next_document + end + end end