Skip to content

Commit 25e313b

Browse files
committed
Merge pull request #633 from estolfo/RUBY-946-gridfs
RUBY-946 Account for max bson size when inserting GridFS file chunks
2 parents 60871a3 + 587980e commit 25e313b

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

lib/mongo/bulk_write/bulk_writable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def execute
8585

8686
private
8787

88+
def valid_doc?(doc)
89+
doc.respond_to?(:keys) ||
90+
doc.respond_to?(:document)
91+
end
92+
8893
def write_concern
8994
@write_concern ||= WriteConcern.get(@options[:write_concern]) ||
9095
@collection.write_concern

lib/mongo/bulk_write/deletable.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ module Deletable
2222

2323
private
2424

25-
def valid_doc?(doc)
26-
doc.respond_to?(:keys)
27-
end
28-
2925
def validate_delete_op!(type, d)
3026
raise Error::InvalidBulkOperation.new(type, d) unless valid_doc?(d)
3127
end

lib/mongo/bulk_write/insertable.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ module Insertable
2222

2323
private
2424

25-
def valid_doc?(doc)
26-
doc.respond_to?(:keys)
27-
end
28-
2925
def validate_insert_ops!(type, inserts)
3026
if inserts.empty?
3127
raise Error::InvalidBulkOperation.new(type, inserts)

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+
file.id
8484
end
8585

8686
# Create the GridFS.

spec/mongo/grid/fs_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565

6666
context 'when inserting the file once' do
6767

68-
before do
68+
let!(:result) do
6969
fs.insert_one(file)
7070
end
7171

@@ -85,6 +85,10 @@
8585
it 'includes the chunks and data with the file' do
8686
expect(from_db.data).to eq('Hello!')
8787
end
88+
89+
it 'returns the file id' do
90+
expect(result).to eq(file.id)
91+
end
8892
end
8993

9094
context 'when inserting the file more than once' do
@@ -101,6 +105,33 @@
101105
}.to raise_error(Mongo::Error::OperationFailure)
102106
end
103107
end
108+
109+
context 'when the file exceeds the max bson size' do
110+
111+
let(:fs) do
112+
described_class.new(authorized_client.database)
113+
end
114+
115+
let(:file) do
116+
str = 'y' * 16777216
117+
Mongo::Grid::File.new(str, :filename => 'large-file.txt')
118+
end
119+
120+
before do
121+
fs.insert_one(file)
122+
end
123+
124+
after do
125+
fs.files_collection.find.delete_many
126+
fs.chunks_collection.find.delete_many
127+
end
128+
129+
it 'successfully inserts the file' do
130+
expect(
131+
fs.find_one(:filename => 'large-file.txt').chunks
132+
).to eq(file.chunks)
133+
end
134+
end
104135
end
105136

106137
describe '#delete_one' do

0 commit comments

Comments
 (0)