Simplify GridIO#read_all

This commit is contained in:
Kyle Banker 2011-04-26 07:24:23 -04:00
parent e9195c83ad
commit 9cc6bad613
2 changed files with 26 additions and 9 deletions

View File

@ -316,21 +316,18 @@ module Mongo
chunk
end
def last_chunk_number
(@file_length / @chunk_size).to_i
end
# Read a file in its entirety.
def read_all
buf = ''
if @current_chunk
buf << @current_chunk['data'].to_s
while chunk = get_chunk(@current_chunk['n'] + 1)
buf << chunk['data'].to_s
@current_chunk = chunk
while buf.size < @file_length
@current_chunk = get_chunk(@current_chunk['n'] + 1)
break if @current_chunk.nil?
buf << @current_chunk['data'].to_s
end
@file_position = @file_length
end
@file_position = @file_length
buf
end

View File

@ -30,6 +30,21 @@ class GridTest < Test::Unit::TestCase
@chunks.remove
end
context "A one-chunk grid-stored file" do
setup do
@data = "GRIDDATA" * 5
@grid = Grid.new(@db, 'test-fs')
@id = @grid.put(@data, :filename => 'sample',
:metadata => {'app' => 'photos'})
end
should "retrieve the file" do
data = @grid.get(@id).data
assert_equal @data, data
end
end
context "A basic grid-stored file" do
setup do
@data = "GRIDDATA" * 50000
@ -55,7 +70,7 @@ class GridTest < Test::Unit::TestCase
should "retrieve the stored data" do
data = @grid.get(@id).data
assert_equal @data, data
assert_equal @data.length, data.length
end
should "have a unique index on chunks" do
@ -161,6 +176,11 @@ class GridTest < Test::Unit::TestCase
end
end
should "be equal in length" do
@io.rewind
assert_equal @io.read.length, @file.read.length
end
should "read the file" do
read_data = ""
@file.each do |chunk|