common_grpc/flight/
do_put.rs1use arrow_flight::PutResult;
16use common_base::AffectedRows;
17use serde::{Deserialize, Serialize};
18use snafu::ResultExt;
19
20use crate::error::{Error, SerdeJsonSnafu};
21
22#[derive(Serialize, Deserialize)]
28pub struct DoPutMetadata {
29 request_id: i64,
30 #[serde(skip_serializing_if = "Option::is_none")]
32 min_timestamp: Option<i64>,
33 #[serde(skip_serializing_if = "Option::is_none")]
35 max_timestamp: Option<i64>,
36}
37
38impl DoPutMetadata {
39 pub fn new(request_id: i64) -> Self {
40 Self {
41 request_id,
42 min_timestamp: None,
43 max_timestamp: None,
44 }
45 }
46
47 pub fn request_id(&self) -> i64 {
48 self.request_id
49 }
50
51 pub fn min_timestamp(&self) -> Option<i64> {
52 self.min_timestamp
53 }
54
55 pub fn max_timestamp(&self) -> Option<i64> {
56 self.max_timestamp
57 }
58
59 pub fn timestamp_range(&self) -> Option<(i64, i64)> {
60 self.min_timestamp
61 .zip(self.max_timestamp)
62 .filter(|(min, max)| max >= min)
63 }
64}
65
66#[derive(Serialize, Deserialize)]
68pub struct DoPutResponse {
69 request_id: i64,
71 affected_rows: AffectedRows,
73 elapsed_secs: f64,
75}
76
77impl DoPutResponse {
78 pub fn new(request_id: i64, affected_rows: AffectedRows, elapsed_secs: f64) -> Self {
79 Self {
80 request_id,
81 affected_rows,
82 elapsed_secs,
83 }
84 }
85
86 pub fn request_id(&self) -> i64 {
87 self.request_id
88 }
89
90 pub fn affected_rows(&self) -> AffectedRows {
91 self.affected_rows
92 }
93
94 pub fn elapsed_secs(&self) -> f64 {
95 self.elapsed_secs
96 }
97}
98
99impl TryFrom<PutResult> for DoPutResponse {
100 type Error = Error;
101
102 fn try_from(value: PutResult) -> Result<Self, Self::Error> {
103 serde_json::from_slice(&value.app_metadata).context(SerdeJsonSnafu)
104 }
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn test_serde_do_put_metadata() {
113 let serialized = r#"{"request_id":42}"#;
114 let metadata = serde_json::from_str::<DoPutMetadata>(serialized).unwrap();
115 assert_eq!(metadata.request_id(), 42);
116 }
117
118 #[test]
119 fn test_serde_do_put_metadata_with_timestamp_range() {
120 let serialized = r#"{"request_id":42,"min_timestamp":1000,"max_timestamp":2000}"#;
121 let metadata = serde_json::from_str::<DoPutMetadata>(serialized).unwrap();
122 assert_eq!(metadata.request_id(), 42);
123 assert_eq!(metadata.timestamp_range(), Some((1000, 2000)));
124 }
125
126 #[test]
127 fn test_serde_do_put_response() {
128 let x = DoPutResponse::new(42, 88, 0.123);
129 let serialized = serde_json::to_string(&x).unwrap();
130 assert_eq!(
131 serialized,
132 r#"{"request_id":42,"affected_rows":88,"elapsed_secs":0.123}"#
133 );
134 }
135}