mongodb/sync/client/
session.rs

1use super::Client;
2use crate::{bson::Document, client::session::ClusterTime, ClientSession as AsyncClientSession};
3
4/// A MongoDB client session. This struct represents a logical session used for ordering sequential
5/// operations. To create a `ClientSession`, call `start_session` on a
6/// [`Client`](../struct.Client.html).
7///
8/// `ClientSession` instances are not thread safe or fork safe. They can only be used by one thread
9/// or process at a time.
10pub struct ClientSession {
11    pub(crate) async_client_session: AsyncClientSession,
12}
13
14impl From<AsyncClientSession> for ClientSession {
15    fn from(async_client_session: AsyncClientSession) -> Self {
16        Self {
17            async_client_session,
18        }
19    }
20}
21
22impl<'a> From<&'a mut ClientSession> for &'a mut AsyncClientSession {
23    fn from(value: &'a mut ClientSession) -> &'a mut AsyncClientSession {
24        &mut value.async_client_session
25    }
26}
27
28impl ClientSession {
29    pub(crate) fn new(async_client_session: AsyncClientSession) -> Self {
30        Self {
31            async_client_session,
32        }
33    }
34
35    /// The client used to create this session.
36    pub fn client(&self) -> Client {
37        self.async_client_session.client().into()
38    }
39
40    /// The id of this session.
41    pub fn id(&self) -> &Document {
42        self.async_client_session.id()
43    }
44
45    /// The highest seen cluster time this session has seen so far.
46    /// This will be `None` if this session has not been used in an operation yet.
47    pub fn cluster_time(&self) -> Option<&ClusterTime> {
48        self.async_client_session.cluster_time()
49    }
50
51    /// Set the cluster time to the provided one if it is greater than this session's highest seen
52    /// cluster time or if this session's cluster time is `None`.
53    pub fn advance_cluster_time(&mut self, to: &ClusterTime) {
54        self.async_client_session.advance_cluster_time(to)
55    }
56}