ReadConcern :param level: (string) The read concern level specifies the level of isolation for read operations. For example, a read operation using a read concern level of ``majority`` will only return data that has been written to a majority of nodes. If the leve
| 22 | |
| 23 | |
| 24 | class ReadConcern: |
| 25 | """ReadConcern |
| 26 | |
| 27 | :param level: (string) The read concern level specifies the level of |
| 28 | isolation for read operations. For example, a read operation using a |
| 29 | read concern level of ``majority`` will only return data that has been |
| 30 | written to a majority of nodes. If the level is left unspecified, the |
| 31 | server default will be used. |
| 32 | |
| 33 | .. versionadded:: 3.2 |
| 34 | |
| 35 | """ |
| 36 | |
| 37 | def __init__(self, level: Optional[str] = None) -> None: |
| 38 | if level is None or isinstance(level, str): |
| 39 | self.__level = level |
| 40 | else: |
| 41 | raise TypeError(f"level must be a string or None, not {type(level)}") |
| 42 | |
| 43 | @property |
| 44 | def level(self) -> Optional[str]: |
| 45 | """The read concern level.""" |
| 46 | return self.__level |
| 47 | |
| 48 | @property |
| 49 | def ok_for_legacy(self) -> bool: |
| 50 | """Return ``True`` if this read concern is compatible with |
| 51 | old wire protocol versions. |
| 52 | """ |
| 53 | return self.level is None or self.level == "local" |
| 54 | |
| 55 | @property |
| 56 | def document(self) -> dict[str, Any]: |
| 57 | """The document representation of this read concern. |
| 58 | |
| 59 | .. note:: |
| 60 | :class:`ReadConcern` is immutable. Mutating the value of |
| 61 | :attr:`document` does not mutate this :class:`ReadConcern`. |
| 62 | """ |
| 63 | doc = {} |
| 64 | if self.__level: |
| 65 | doc["level"] = self.level |
| 66 | return doc |
| 67 | |
| 68 | def __eq__(self, other: Any) -> bool: |
| 69 | if isinstance(other, ReadConcern): |
| 70 | return self.document == other.document |
| 71 | return NotImplemented |
| 72 | |
| 73 | def __repr__(self) -> str: |
| 74 | if self.level: |
| 75 | return "ReadConcern(%s)" % self.level |
| 76 | return "ReadConcern()" |
| 77 | |
| 78 | |
| 79 | DEFAULT_READ_CONCERN = ReadConcern() |
no outgoing calls