introduce dedicated return type for PollRange

This commit is contained in:
trinity-1686a 2023-06-24 10:17:20 +02:00 committed by networkException
parent 6a91ab8a40
commit 2d11a4b982
Signed by: networkException
GPG key ID: E3877443AE684391
2 changed files with 22 additions and 13 deletions

View file

@ -311,23 +311,19 @@ impl BatchOutputKind {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
fn display_poll_range_output( fn display_poll_range_output(&self, poll_range: PollRangeResult) -> ! {
&self,
seen_marker: String,
values: BTreeMap<String, CausalValue>,
) -> ! {
if self.json { if self.json {
let json = serde_json::json!({ let json = serde_json::json!({
"values": self.values_json(values), "values": self.values_json(poll_range.items),
"seen_marker": seen_marker, "seen_marker": poll_range.seen_marker,
}); });
let stdout = std::io::stdout(); let stdout = std::io::stdout();
serde_json::to_writer_pretty(stdout, &json).unwrap(); serde_json::to_writer_pretty(stdout, &json).unwrap();
exit(0) exit(0)
} else { } else {
println!("seen marker: {}", seen_marker); println!("seen marker: {}", poll_range.seen_marker);
self.display_human_output(values) self.display_human_output(poll_range.items)
} }
} }
@ -501,8 +497,8 @@ async fn main() -> Result<(), Error> {
) )
.await?; .await?;
match res { match res {
Some((items, seen_marker)) => { Some(poll_range_output) => {
output_kind.display_poll_range_output(seen_marker, items); output_kind.display_poll_range_output(poll_range_output);
} }
None => { None => {
if output_kind.json { if output_kind.json {

View file

@ -182,7 +182,7 @@ impl K2vClient {
filter: Option<PollRangeFilter<'_>>, filter: Option<PollRangeFilter<'_>>,
seen_marker: Option<&str>, seen_marker: Option<&str>,
timeout: Option<Duration>, timeout: Option<Duration>,
) -> Result<Option<(BTreeMap<String, CausalValue>, String)>, Error> { ) -> Result<Option<PollRangeResult>, Error> {
let timeout = timeout.unwrap_or(DEFAULT_POLL_TIMEOUT); let timeout = timeout.unwrap_or(DEFAULT_POLL_TIMEOUT);
let request = PollRangeRequest { let request = PollRangeRequest {
@ -217,7 +217,10 @@ impl K2vClient {
}) })
.collect::<BTreeMap<_, _>>(); .collect::<BTreeMap<_, _>>();
Ok(Some((items, resp.seen_marker))) Ok(Some(PollRangeResult {
items,
seen_marker: resp.seen_marker,
}))
} }
/// Perform an InsertItem request, inserting a value for a single pk+sk. /// Perform an InsertItem request, inserting a value for a single pk+sk.
@ -570,6 +573,7 @@ pub struct Filter<'a> {
pub reverse: bool, pub reverse: bool,
} }
/// Filter for a poll range operations.
#[derive(Debug, Default, Clone, Serialize)] #[derive(Debug, Default, Clone, Serialize)]
pub struct PollRangeFilter<'a> { pub struct PollRangeFilter<'a> {
pub start: Option<&'a str>, pub start: Option<&'a str>,
@ -577,6 +581,15 @@ pub struct PollRangeFilter<'a> {
pub prefix: Option<&'a str>, pub prefix: Option<&'a str>,
} }
/// Response to a poll_range query
#[derive(Debug, Default, Clone, Serialize)]
pub struct PollRangeResult {
/// List of items that have changed since last PollRange call.
pub items: BTreeMap<String, CausalValue>,
/// opaque string representing items already seen for future PollRange calls.
pub seen_marker: String,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct PollRangeRequest<'a> { struct PollRangeRequest<'a> {