Skip to content

Commit b09a95c

Browse files
author
Mike Dirolf
committed
fix for chunk number calculation when unaligned - thanks to Daniel Lundin for the patch
1 parent ebd0e53 commit b09a95c

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ The following is a list of people who have contributed to
1818
- Joshua Roesslein (joshthecoder)
1919
- Gregg Lind (gregglind)
2020
- Michael Schurter (schmichael)
21+
- Daniel Lundin

gridfs/grid_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def read(self, size=-1):
306306
size = remainder
307307

308308
bytes = self.__buffer
309-
chunk_number = math.floor(self.__position / self.__chunk_size)
309+
chunk_number = (len(bytes) + self.__position) / self.__chunk_size
310310

311311
while len(bytes) < size:
312312
chunk = self.__collection.chunks.find_one({"files_id": self.__id, "n": chunk_number})

test/test_grid_file.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,26 @@ def test_spec_with_id(self):
406406
file = GridFile({"_id": "foobar", "filename": "foobar"}, self.db, "w")
407407
file.close()
408408

409+
def test_read_chunks_unaligned_buffer_size(self):
410+
self.db.fs.files.remove({})
411+
self.db.fs.chunks.remove({})
412+
413+
in_data = "This is a text that doesn't quite fit in a single 16-byte chunk."
414+
f = GridFile({"filename":"test", "chunkSize":16}, self.db, "w")
415+
f.write(in_data)
416+
f.close()
417+
418+
f = GridFile({"filename":"test"}, self.db)
419+
out_data = ''
420+
while 1:
421+
s = f.read(13)
422+
if not s:
423+
break
424+
out_data += s
425+
f.close()
426+
427+
self.assertEqual(in_data, out_data)
428+
429+
409430
if __name__ == "__main__":
410431
unittest.main()

0 commit comments

Comments
 (0)