diff options
author | Alex Auvolat <alex@adnab.me> | 2022-07-13 12:30:35 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-07-13 12:30:35 +0200 |
commit | 9fa2e958b3b37538b80b7f26107b7df2238f335b (patch) | |
tree | da34cfe10a8c5d6f9590e7e477635008f509efd4 /src/k2v_util.rs | |
parent | 33fa51021cce58e170ae0c943ab8c8b9fa94d6a9 (diff) | |
download | aerogramme-9fa2e958b3b37538b80b7f26107b7df2238f335b.tar.gz aerogramme-9fa2e958b3b37538b80b7f26107b7df2238f335b.zip |
Begin add watch mechanism to Bayou
Diffstat (limited to 'src/k2v_util.rs')
-rw-r--r-- | src/k2v_util.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/k2v_util.rs b/src/k2v_util.rs new file mode 100644 index 0000000..9dadab4 --- /dev/null +++ b/src/k2v_util.rs @@ -0,0 +1,29 @@ +use anyhow::Result; + +use k2v_client::{CausalValue, CausalityToken, K2vClient}; + +// ---- UTIL: function to wait for a value to have changed in K2V ---- + +pub async fn k2v_wait_value_changed( + k2v: &K2vClient, + pk: &str, + sk: &str, + prev_ct: &Option<CausalityToken>, +) -> Result<CausalValue> { + loop { + if let Some(ct) = prev_ct { + match k2v.poll_item(pk, sk, ct.clone(), None).await? { + None => continue, + Some(cv) => return Ok(cv), + } + } else { + match k2v.read_item(pk, sk).await { + Err(k2v_client::Error::NotFound) => { + k2v.insert_item(pk, sk, vec![0u8], None).await?; + } + Err(e) => return Err(e.into()), + Ok(cv) => return Ok(cv), + } + } + } +} |