use crate::error::Result;
use crate::partition::Partition;
use crate::record::Record;
use bytes::Bytes;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone)]
pub struct Producer {
partition: Arc<Partition>,
}
impl Producer {
pub fn new(partition: Arc<Partition>) -> Self {
Self { partition }
}
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub async fn send(&self, key: Option<Bytes>, value: Bytes) -> Result<u64> {
let timestamp_ms = current_timestamp_ms();
let record = Record::new(timestamp_ms, key, value);
self.partition.append(record).await
}
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub async fn send_record(&self, record: Record) -> Result<u64> {
self.partition.append(record).await
}
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub async fn send_batch(&self, items: Vec<(Option<Bytes>, Bytes)>) -> Result<Vec<u64>> {
if items.is_empty() {
return Ok(Vec::new());
}
let timestamp_ms = current_timestamp_ms();
let records: Vec<Record> = items
.into_iter()
.map(|(key, value)| Record::new(timestamp_ms, key, value))
.collect();
self.partition.append_batch(records).await
}
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub async fn send_batch_records(&self, records: Vec<Record>) -> Result<Vec<u64>> {
self.partition.append_batch(records).await
}
}
fn current_timestamp_ms() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}