Skip to main content

sierradb_server/request/
epseq.rs

1use combine::Parser;
2use redis_protocol::resp3::types::BytesFrame;
3use sierradb_cluster::read::GetPartitionSequence;
4
5use crate::error::MapRedisError;
6use crate::parser::{FrameStream, partition_selector};
7use crate::request::{HandleRequest, PartitionSelector, number};
8use crate::server::Conn;
9
10/// Get the current sequence number for a partition.
11///
12/// # Syntax
13/// ```text
14/// EPSEQ <partition>
15/// ```
16///
17/// # Parameters
18/// - `partition`: Partition selector (partition ID 0-65535 or UUID key)
19///
20/// # Examples
21/// ```text
22/// EPSEQ 42
23/// EPSEQ 550e8400-e29b-41d4-a716-446655440000
24/// ```
25pub struct EPSeq {
26    pub partition: PartitionSelector,
27}
28
29impl EPSeq {
30    pub fn parser<'a>() -> impl Parser<FrameStream<'a>, Output = EPSeq> + 'a {
31        partition_selector().map(|partition| EPSeq { partition })
32    }
33}
34
35impl HandleRequest for EPSeq {
36    type Error = String;
37    type Ok = EPSeqResp;
38
39    async fn handle_request(self, conn: &mut Conn) -> Result<Option<Self::Ok>, Self::Error> {
40        let partition_id = self.partition.into_partition_id(conn.num_partitions);
41
42        let sequence = conn
43            .cluster_ref
44            .ask(GetPartitionSequence { partition_id })
45            .await
46            .map_redis_err()?;
47
48        Ok(Some(EPSeqResp { sequence }))
49    }
50}
51
52pub struct EPSeqResp {
53    sequence: Option<u64>,
54}
55
56impl From<EPSeqResp> for BytesFrame {
57    fn from(resp: EPSeqResp) -> Self {
58        match resp.sequence {
59            Some(n) => number(n as i64),
60            None => BytesFrame::Null,
61        }
62    }
63}