use std::io;
use std::time::Duration;
use kevy_store::{HExpireCode, HExpireCond, now_unix_ms};
#[cfg(not(target_arch = "wasm32"))]
use crate::replica_glue::ensure_writable;
use crate::store::{Store, commit_write, store_err};
#[cfg(target_arch = "wasm32")]
fn ensure_writable(_s: &Store) -> io::Result<()> {
Ok(())
}
impl Store {
pub fn hexpire(
&self,
key: &[u8],
fields: &[&[u8]],
ttl: Duration,
cond: HExpireCond,
) -> io::Result<Vec<HExpireCode>> {
let deadline = now_unix_ms().saturating_add(ttl.as_millis() as u64);
self.hpexpire_at(key, fields, deadline, cond)
}
pub fn hpexpire_at(
&self,
key: &[u8],
fields: &[&[u8]],
deadline_ms: u64,
cond: HExpireCond,
) -> io::Result<Vec<HExpireCode>> {
ensure_writable(self)?;
let mut g = self.wshard(key);
let codes = g.store.hexpire_at(key, fields, deadline_ms, cond).map_err(store_err)?;
if codes.iter().any(|&c| c == 1 || c == 2) {
let ms = deadline_ms.to_string();
let n = fields.len().to_string();
let mut argv: Vec<&[u8]> = Vec::with_capacity(5 + fields.len());
argv.push(b"HPEXPIREAT");
argv.push(key);
argv.push(ms.as_bytes());
argv.push(b"FIELDS");
argv.push(n.as_bytes());
argv.extend(fields.iter().copied());
commit_write(&mut g, &argv)?;
}
Ok(codes)
}
pub fn httl(&self, key: &[u8], fields: &[&[u8]]) -> io::Result<Vec<i64>> {
let mut g = self.wshard(key);
g.store.httl(key, fields).map_err(store_err)
}
pub fn hpersist(&self, key: &[u8], fields: &[&[u8]]) -> io::Result<Vec<HExpireCode>> {
ensure_writable(self)?;
let mut g = self.wshard(key);
let codes = g.store.hpersist(key, fields).map_err(store_err)?;
if codes.contains(&1) {
let n = fields.len().to_string();
let mut argv: Vec<&[u8]> = Vec::with_capacity(4 + fields.len());
argv.push(b"HPERSIST");
argv.push(key);
argv.push(b"FIELDS");
argv.push(n.as_bytes());
argv.extend(fields.iter().copied());
commit_write(&mut g, &argv)?;
}
Ok(codes)
}
}