Merge pull request #41 from rmm5t/refactor_gets

Fix and refactoring for GridIO#gets.
This commit is contained in:
Kyle Banker 2011-05-09 08:12:40 -07:00
commit 74faed3f38
2 changed files with 62 additions and 36 deletions

View File

@ -208,43 +208,9 @@ module Mongo
elsif separator.is_a?(Integer) elsif separator.is_a?(Integer)
read_length(separator) read_length(separator)
elsif separator.length > 1 elsif separator.length > 1
result = '' read_to_string(separator, length)
len = 0
match_idx = 0
match_num = separator.length - 1
to_match = separator[match_idx].chr
if length
matcher = lambda {|idx, num| idx < num && len < length }
else
matcher = lambda {|idx, num| idx < num}
end
while matcher.call(match_idx, match_num) && char = getc
result << char
len += 1
if char == to_match
while match_idx < match_num do
match_idx += 1
to_match = separator[match_idx].chr
char = getc
result << char
if char != to_match
match_idx = 0
to_match = separator[match_idx].chr
break
end
end
end
end
result
else else
result = '' read_to_character(separator, length)
len = 0
while char = getc
result << char
len += 1
break if char == separator || (length ? len >= length : false)
end
result
end end
end end
@ -359,6 +325,48 @@ module Mongo
buf buf
end end
def read_to_character(character="\n", length=nil)
result = ''
len = 0
while char = getc
result << char
len += 1
break if char == character || (length ? len >= length : false)
end
result.length > 0 ? result : nil
end
def read_to_string(string="\n", length=nil)
result = ''
len = 0
match_idx = 0
match_num = string.length - 1
to_match = string[match_idx].chr
if length
matcher = lambda {|idx, num| idx < num && len < length }
else
matcher = lambda {|idx, num| idx < num}
end
while matcher.call(match_idx, match_num) && char = getc
result << char
len += 1
if char == to_match
while match_idx < match_num do
match_idx += 1
to_match = string[match_idx].chr
char = getc
result << char
if char != to_match
match_idx = 0
to_match = string[match_idx].chr
break
end
end
end
end
result.length > 0 ? result : nil
end
def cache_chunk_data def cache_chunk_data
@current_chunk_data = @current_chunk['data'].to_s @current_chunk_data = @current_chunk['data'].to_s
if @current_chunk_data.respond_to?(:force_encoding) if @current_chunk_data.respond_to?(:force_encoding)

View File

@ -69,6 +69,24 @@ class GridIOTest < Test::Unit::TestCase
assert_equal 10, string.length assert_equal 10, string.length
end end
should "read to the end of the file one line at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to the end of the file one multi-character separator at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets("45")
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to a given separator" do should "read to a given separator" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id}) file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets("5") string = file.gets("5")