Skip to content

RUBY-946 Account for max bson size when inserting GridFS file chunks #633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/mongo/bulk_write/bulk_writable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def execute

private

def valid_doc?(doc)
doc.respond_to?(:keys) ||
doc.respond_to?(:document)
end

def write_concern
@write_concern ||= WriteConcern.get(@options[:write_concern]) ||
@collection.write_concern
Expand Down
4 changes: 0 additions & 4 deletions lib/mongo/bulk_write/deletable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ module Deletable

private

def valid_doc?(doc)
doc.respond_to?(:keys)
end

def validate_delete_op!(type, d)
raise Error::InvalidBulkOperation.new(type, d) unless valid_doc?(d)
end
Expand Down
4 changes: 0 additions & 4 deletions lib/mongo/bulk_write/insertable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ module Insertable

private

def valid_doc?(doc)
doc.respond_to?(:keys)
end

def validate_insert_ops!(type, inserts)
if inserts.empty?
raise Error::InvalidBulkOperation.new(type, inserts)
Expand Down
10 changes: 5 additions & 5 deletions lib/mongo/grid/fs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ def find_one(selector = nil)
# @since 2.0.0
def insert_one(file)
files_collection.insert_one(file.metadata)
result = chunks_collection.insert_many(file.chunks)
if write_concern.get_last_error
validate_md5!(file)
else
result
inserts = file.chunks.reduce([]) do |ops, chunk|
ops << { :insert_one => chunk }
end
result = chunks_collection.bulk_write(inserts, ordered: true)
validate_md5!(file) if write_concern.get_last_error
file.id
end

# Create the GridFS.
Expand Down
33 changes: 32 additions & 1 deletion spec/mongo/grid/fs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

context 'when inserting the file once' do

before do
let!(:result) do
fs.insert_one(file)
end

Expand All @@ -85,6 +85,10 @@
it 'includes the chunks and data with the file' do
expect(from_db.data).to eq('Hello!')
end

it 'returns the file id' do
expect(result).to eq(file.id)
end
end

context 'when inserting the file more than once' do
Expand All @@ -101,6 +105,33 @@
}.to raise_error(Mongo::Error::OperationFailure)
end
end

context 'when the file exceeds the max bson size' do

let(:fs) do
described_class.new(authorized_client.database)
end

let(:file) do
str = 'y' * 16777216
Mongo::Grid::File.new(str, :filename => 'large-file.txt')
end

before do
fs.insert_one(file)
end

after do
fs.files_collection.find.delete_many
fs.chunks_collection.find.delete_many
end

it 'successfully inserts the file' do
expect(
fs.find_one(:filename => 'large-file.txt').chunks
).to eq(file.chunks)
end
end
end

describe '#delete_one' do
Expand Down