Get a query message, possibly setting the secondaryOk bit.
(
self, read_preference: _ServerMode, conn: _AgnosticConnection, use_cmd: bool = False
)
| 1678 | return self._as_command |
| 1679 | |
| 1680 | def get_message( |
| 1681 | self, read_preference: _ServerMode, conn: _AgnosticConnection, use_cmd: bool = False |
| 1682 | ) -> tuple[int, bytes, int]: |
| 1683 | """Get a query message, possibly setting the secondaryOk bit.""" |
| 1684 | # Use the read_preference decided by _socket_from_server. |
| 1685 | self.read_preference = read_preference |
| 1686 | if read_preference.mode: |
| 1687 | # Set the secondaryOk bit. |
| 1688 | flags = self.flags | 4 |
| 1689 | else: |
| 1690 | flags = self.flags |
| 1691 | |
| 1692 | ns = self.namespace() |
| 1693 | spec = self.spec |
| 1694 | |
| 1695 | if use_cmd: |
| 1696 | spec = self.as_command(conn)[0] |
| 1697 | request_id, msg, size, _ = _op_msg( |
| 1698 | 0, |
| 1699 | spec, |
| 1700 | self.db, |
| 1701 | read_preference, |
| 1702 | self.codec_options, |
| 1703 | ctx=conn.compression_context, |
| 1704 | ) |
| 1705 | return request_id, msg, size |
| 1706 | |
| 1707 | # OP_QUERY treats ntoreturn of -1 and 1 the same, return |
| 1708 | # one document and close the cursor. We have to use 2 for |
| 1709 | # batch size if 1 is specified. |
| 1710 | ntoreturn = self.batch_size == 1 and 2 or self.batch_size |
| 1711 | if self.limit: |
| 1712 | if ntoreturn: |
| 1713 | ntoreturn = min(self.limit, ntoreturn) |
| 1714 | else: |
| 1715 | ntoreturn = self.limit |
| 1716 | |
| 1717 | if conn.is_mongos: |
| 1718 | assert isinstance(spec, MutableMapping) |
| 1719 | spec = _maybe_add_read_preference(spec, read_preference) |
| 1720 | |
| 1721 | return _query( |
| 1722 | flags, |
| 1723 | ns, |
| 1724 | self.ntoskip, |
| 1725 | ntoreturn, |
| 1726 | spec, |
| 1727 | None if use_cmd else self.fields, |
| 1728 | self.codec_options, |
| 1729 | ctx=conn.compression_context, |
| 1730 | ) |
| 1731 | |
| 1732 | |
| 1733 | class _GetMore: |
no test coverage detected