Skip to content

Commit

Permalink
Higher-performance solution to PYTHON-417
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Oct 18, 2012
1 parent 37c1509 commit c503f1d
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions gridfs/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,7 @@ def __flush_data(self, data):
"n": self._chunk_number,
"data": Binary(data)}

# See PYTHON-417, "Sharded GridFS fails with exception: chunks out of
# order": inserts via mongos, even though they're using a single
# connection, can succeed out-of-order due to the writebackListener.
# We mustn't call "filemd5" until all inserts are complete, which we
# ensure by calling getLastError.
self._chunks.insert(chunk, safe=True)
self._chunks.insert(chunk)
self._chunk_number += 1
self._position += len(data)

Expand All @@ -217,8 +212,17 @@ def __flush(self):
try:
self.__flush_buffer()

md5 = self._coll.database.command("filemd5", self._id,
root=self._coll.name)["md5"]
db = self._coll.database

# See PYTHON-417, "Sharded GridFS fails with exception: chunks out
# of order." Inserts via mongos, even if they use a single
# connection, can succeed out-of-order due to the writebackListener.
# We mustn't call "filemd5" until all inserts are complete, which
# we ensure by calling getLastError (and ignoring the result).
db.error()

md5 = db.command(
"filemd5", self._id, root=self._coll.name)["md5"]

self._file["md5"] = md5
self._file["length"] = self._position
Expand Down

0 comments on commit c503f1d

Please sign in to comment.