1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
use std::time::Duration as StdDuration; use chrono::{DateTime, Duration, Utc}; use protobuf::SingularPtrField; use crate::{ command::{Command, PayloadCommand, PayloadCommandPayload}, protos::protobuf::pulsar_api::{ BaseCommand, BaseCommand_Type as Type, CommandSend, MessageMetadata, SingleMessageMetadata, }, types::{CompressionType, MessageProperties, ProducerId, ProducerName, SequenceId}, }; #[derive(Clone, Debug)] pub struct SendCommand { #[cfg(feature = "with-hacking-commands")] pub inner_command: CommandSend, #[cfg(not(feature = "with-hacking-commands"))] pub(crate) inner_command: CommandSend, #[cfg(feature = "with-hacking-commands")] pub message_metadata: MessageMetadata, #[cfg(not(feature = "with-hacking-commands"))] pub(crate) message_metadata: MessageMetadata, #[cfg(feature = "with-hacking-commands")] pub payload: PayloadCommandPayload, #[cfg(not(feature = "with-hacking-commands"))] pub(crate) payload: PayloadCommandPayload, } impl SendCommand { pub fn single( sequence_id: SequenceId, properties: impl Into<Option<MessageProperties>>, msg: impl AsRef<[u8]>, compression: impl Into<Option<CompressionType>>, ) -> Self { let mut inner_command = CommandSend::new(); inner_command.set_sequence_id(sequence_id.to_owned().into()); let mut message_metadata = MessageMetadata::new(); message_metadata.set_sequence_id(sequence_id.into()); message_metadata.set_publish_time(Utc::now().timestamp_millis() as u64); if let Some(properties) = properties.into() { for kv in properties.inner { message_metadata.properties.push(kv); } } if let Some(compression) = compression.into() { message_metadata.set_compression(compression.into()); } let payload = PayloadCommandPayload::Single(msg.as_ref().to_owned()); Self { inner_command, message_metadata, payload, } } pub fn batch( sequence_id: SequenceId, msgs: Vec<(impl Into<MessageProperties>, impl AsRef<[u8]>)>, compression: impl Into<Option<CompressionType>>, ) -> Self { let mut inner_command = CommandSend::new(); inner_command.set_sequence_id(sequence_id.to_owned().into()); inner_command.set_num_messages(msgs.len() as i32); let mut message_metadata = MessageMetadata::new(); message_metadata.set_sequence_id(sequence_id.into()); message_metadata.set_publish_time(Utc::now().timestamp_millis() as u64); message_metadata.set_num_messages_in_batch(msgs.len() as i32); if let Some(compression) = compression.into() { message_metadata.set_compression(compression.into()); } let mut payloads = vec![]; for (properties, msg) in msgs.into_iter() { let mut single_message_metadata = SingleMessageMetadata::new(); for kv in properties.into().inner { single_message_metadata.properties.push(kv); } single_message_metadata.set_payload_size(msg.as_ref().len() as i32); payloads.push((single_message_metadata, msg.as_ref().to_owned())) } let payload = PayloadCommandPayload::Batch(payloads); Self { inner_command, message_metadata, payload, } } pub fn set_producer_id(&mut self, producer_id: ProducerId) -> &mut Self { self.inner_command.set_producer_id(producer_id.into()); self } pub fn set_producer_name(&mut self, producer_name: ProducerName) -> &mut Self { self.message_metadata .set_producer_name(producer_name.into()); self } pub fn get_sequence_id(&self) -> SequenceId { SequenceId::new(self.inner_command.get_sequence_id()) } pub fn set_deliver_at_time(&mut self, dt: DateTime<Utc>) -> &mut Self { self.message_metadata .set_deliver_at_time(dt.timestamp_millis() as i64); self } pub fn set_deliver_after(&mut self, dur: StdDuration) -> Result<&mut Self, String> { let dur = Duration::from_std(dur).map_err(|err| err.to_string())?; self.message_metadata .set_deliver_at_time((Utc::now() + dur).timestamp_millis() as i64); Ok(self) } } impl From<&SendCommand> for Command { fn from(c: &SendCommand) -> Self { let mut base_command = BaseCommand::new(); base_command.set_field_type(Type::SEND); base_command.send = SingularPtrField::some(c.inner_command.to_owned()); Command::Payload( PayloadCommand { message: base_command, metadata: c.message_metadata.to_owned(), payload: c.payload.to_owned(), } .into(), ) } } #[cfg(test)] mod tests { use super::*; use crate::types::SequenceIdBuilder; #[test] fn single() { SendCommand::single( SequenceIdBuilder::default().next(), None, b"foo".to_vec(), None, ); SendCommand::single(SequenceIdBuilder::default().next(), None, b"foo", None); SendCommand::single( SequenceIdBuilder::default().next(), None, "foo".to_string(), None, ); let c = SendCommand::single( SequenceIdBuilder::default().next(), MessageProperties::from(&[("a", "1")]), "foo", None, ); match c.payload { PayloadCommandPayload::Single(bytes) => assert_eq!(bytes, b"foo"), _ => assert!(false), } } }