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 chunk
end end
def last_chunk_number
(@file_length / @chunk_size).to_i
end
# Read a file in its entirety. # Read a file in its entirety.
def read_all def read_all
buf = '' buf = ''
if @current_chunk if @current_chunk
buf << @current_chunk['data'].to_s buf << @current_chunk['data'].to_s
while chunk = get_chunk(@current_chunk['n'] + 1) while buf.size < @file_length
buf << chunk['data'].to_s @current_chunk = get_chunk(@current_chunk['n'] + 1)
@current_chunk = chunk break if @current_chunk.nil?
buf << @current_chunk['data'].to_s
end end
@file_position = @file_length
end end
@file_position = @file_length
buf buf
end end

View File

@ -30,6 +30,21 @@ class GridTest < Test::Unit::TestCase
@chunks.remove @chunks.remove
end 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 context "A basic grid-stored file" do
setup do setup do
@data = "GRIDDATA" * 50000 @data = "GRIDDATA" * 50000
@ -55,7 +70,7 @@ class GridTest < Test::Unit::TestCase
should "retrieve the stored data" do should "retrieve the stored data" do
data = @grid.get(@id).data data = @grid.get(@id).data
assert_equal @data, data assert_equal @data.length, data.length
end end
should "have a unique index on chunks" do should "have a unique index on chunks" do
@ -161,6 +176,11 @@ class GridTest < Test::Unit::TestCase
end end
end end
should "be equal in length" do
@io.rewind
assert_equal @io.read.length, @file.read.length
end
should "read the file" do should "read the file" do
read_data = "" read_data = ""
@file.each do |chunk| @file.each do |chunk|