|
| 1 | +#[cfg(feature = "in-use-encryption-unstable")] |
| 2 | +use bson::doc; |
| 3 | +use bson::RawDocumentBuf; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + cmap::{conn::PinnedConnectionHandle, Command, RawCommandResponse, StreamDescription}, |
| 7 | + concern::WriteConcern, |
| 8 | + cursor::CursorSpecification, |
| 9 | + error::{Error, Result}, |
| 10 | + operation::{Operation, RunCommand}, |
| 11 | + options::RunCursorCommandOptions, |
| 12 | + selection_criteria::SelectionCriteria, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive(Debug, Clone)] |
| 16 | +pub(crate) struct RunCursorCommand<'conn> { |
| 17 | + run_command: RunCommand<'conn>, |
| 18 | + options: Option<RunCursorCommandOptions>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<'conn> RunCursorCommand<'conn> { |
| 22 | + pub(crate) fn new( |
| 23 | + run_command: RunCommand<'conn>, |
| 24 | + options: Option<RunCursorCommandOptions>, |
| 25 | + ) -> Result<Self> { |
| 26 | + Ok(Self { |
| 27 | + run_command, |
| 28 | + options, |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<'conn> Operation for RunCursorCommand<'conn> { |
| 34 | + type O = CursorSpecification; |
| 35 | + type Command = RawDocumentBuf; |
| 36 | + |
| 37 | + const NAME: &'static str = "run_cursor_command"; |
| 38 | + |
| 39 | + fn build(&mut self, description: &StreamDescription) -> Result<Command<Self::Command>> { |
| 40 | + self.run_command.build(description) |
| 41 | + } |
| 42 | + |
| 43 | + fn serialize_command(&mut self, cmd: Command<Self::Command>) -> Result<Vec<u8>> { |
| 44 | + self.run_command.serialize_command(cmd) |
| 45 | + } |
| 46 | + |
| 47 | + fn extract_at_cluster_time( |
| 48 | + &self, |
| 49 | + response: &bson::RawDocument, |
| 50 | + ) -> Result<Option<bson::Timestamp>> { |
| 51 | + self.run_command.extract_at_cluster_time(response) |
| 52 | + } |
| 53 | + |
| 54 | + fn handle_error(&self, error: Error) -> Result<Self::O> { |
| 55 | + Err(error) |
| 56 | + } |
| 57 | + |
| 58 | + fn selection_criteria(&self) -> Option<&SelectionCriteria> { |
| 59 | + self.run_command.selection_criteria() |
| 60 | + } |
| 61 | + |
| 62 | + fn is_acknowledged(&self) -> bool { |
| 63 | + self.run_command.is_acknowledged() |
| 64 | + } |
| 65 | + |
| 66 | + fn write_concern(&self) -> Option<&WriteConcern> { |
| 67 | + self.run_command.write_concern() |
| 68 | + } |
| 69 | + |
| 70 | + fn supports_read_concern(&self, description: &StreamDescription) -> bool { |
| 71 | + self.run_command.supports_read_concern(description) |
| 72 | + } |
| 73 | + |
| 74 | + fn supports_sessions(&self) -> bool { |
| 75 | + self.run_command.supports_sessions() |
| 76 | + } |
| 77 | + |
| 78 | + fn retryability(&self) -> crate::operation::Retryability { |
| 79 | + self.run_command.retryability() |
| 80 | + } |
| 81 | + |
| 82 | + fn update_for_retry(&mut self) { |
| 83 | + self.run_command.update_for_retry() |
| 84 | + } |
| 85 | + |
| 86 | + fn pinned_connection(&self) -> Option<&PinnedConnectionHandle> { |
| 87 | + self.run_command.pinned_connection() |
| 88 | + } |
| 89 | + |
| 90 | + fn name(&self) -> &str { |
| 91 | + self.run_command.name() |
| 92 | + } |
| 93 | + |
| 94 | + fn handle_response( |
| 95 | + &self, |
| 96 | + response: RawCommandResponse, |
| 97 | + description: &StreamDescription, |
| 98 | + ) -> Result<Self::O> { |
| 99 | + let doc = Operation::handle_response(&self.run_command, response, description)?; |
| 100 | + let cursor_info = bson::from_document(doc)?; |
| 101 | + let batch_size = match &self.options { |
| 102 | + Some(options) => options.batch_size.clone(), |
| 103 | + None => None, |
| 104 | + }; |
| 105 | + let max_time = match &self.options { |
| 106 | + Some(options) => options.max_time.clone(), |
| 107 | + None => None, |
| 108 | + }; |
| 109 | + let comment = match &self.options { |
| 110 | + Some(options) => options.comment.clone(), |
| 111 | + None => None, |
| 112 | + }; |
| 113 | + Ok(CursorSpecification::new( |
| 114 | + cursor_info, |
| 115 | + description.server_address.clone(), |
| 116 | + batch_size, |
| 117 | + max_time, |
| 118 | + comment, |
| 119 | + )) |
| 120 | + } |
| 121 | +} |
0 commit comments