MCPcopy Create free account
hub / github.com/mongodb/mongo-python-driver / readchunk

Method readchunk

gridfs/synchronous/grid_file.py:1593–1620  ·  view source on GitHub ↗

Reads a chunk at a time. If the current position is within a chunk the remainder of the chunk is returned.

(self)

Source from the content-addressed store, hash-verified

1591 return True
1592
1593 def readchunk(self) -> bytes:
1594 """Reads a chunk at a time. If the current position is within a
1595 chunk the remainder of the chunk is returned.
1596 """
1597 self.open()
1598 received = len(self._buffer) - self._buffer_pos
1599 chunk_data = EMPTY
1600 chunk_size = int(self.chunk_size)
1601
1602 if received > 0:
1603 chunk_data = self._buffer[self._buffer_pos :]
1604 elif self._position < int(self.length):
1605 chunk_number = int((received + self._position) / chunk_size)
1606 if self._chunk_iter is None:
1607 self._chunk_iter = GridOutChunkIterator(
1608 self, self._chunks, self._session, chunk_number
1609 )
1610
1611 chunk = self._chunk_iter.next()
1612 chunk_data = chunk["data"][self._position % chunk_size :]
1613
1614 if not chunk_data:
1615 raise CorruptGridFile("truncated chunk")
1616
1617 self._position += len(chunk_data)
1618 self._buffer = EMPTY
1619 self._buffer_pos = 0
1620 return chunk_data
1621
1622 def _read_size_or_line(self, size: int = -1, line: bool = False) -> bytes:
1623 """Internal read() and readline() helper."""

Callers 7

_read_size_or_lineMethod · 0.95
test_readchunkMethod · 0.95
download_to_streamMethod · 0.45

Calls 4

openMethod · 0.95
CorruptGridFileClass · 0.90
nextMethod · 0.45

Tested by 4

test_readchunkMethod · 0.76