Skip to content

Commit 9c335a2

Browse files
committed
RUBY-946 Use bulk write API for inserting GridFS file chunks
1 parent 92ddf64 commit 9c335a2

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/mongo/grid/fs.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ def find_one(selector = nil)
7575
# @since 2.0.0
7676
def insert_one(file)
7777
files_collection.insert_one(file.metadata)
78-
result = chunks_collection.insert_many(file.chunks)
79-
if write_concern.get_last_error
80-
validate_md5!(file)
81-
else
82-
result
78+
inserts = file.chunks.reduce([]) do |ops, chunk|
79+
ops << { :insert_one => chunk }
8380
end
81+
result = chunks_collection.bulk_write(inserts, ordered: true)
82+
validate_md5!(file) if write_concern.get_last_error
83+
result
8484
end
8585

8686
# Create the GridFS.

spec/mongo/grid/fs_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@
101101
}.to raise_error(Mongo::Error::OperationFailure)
102102
end
103103
end
104+
105+
context 'when the file exceeds the max bson size' do
106+
107+
let(:fs) do
108+
described_class.new(authorized_client.database)
109+
end
110+
111+
let(:file) do
112+
str = 'y' * 16777216
113+
Mongo::Grid::File.new(str, :filename => 'large-file.txt')
114+
end
115+
116+
before do
117+
fs.insert_one(file)
118+
end
119+
120+
after do
121+
fs.files_collection.find.delete_many
122+
fs.chunks_collection.find.delete_many
123+
end
124+
125+
it 'successfully inserts the file' do
126+
expect(
127+
fs.find_one(:filename => 'large-file.txt').chunks
128+
).to eq(file.chunks)
129+
end
130+
end
104131
end
105132

106133
describe '#delete_one' do

0 commit comments

Comments
 (0)