Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions crates/ember-core/src/keyspace/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,45 @@ impl Keyspace {
Ok(len)
}

/// Atomically pops a value from `source` and pushes it to `destination`.
///
/// `src_left` controls whether to pop from the head (true) or tail (false)
/// of the source. `dst_left` controls whether to push to the head (true)
/// or tail (false) of the destination. Source and destination may be the
/// same key (rotate). Returns `Ok(None)` if the source list is missing.
pub fn lmove(
&mut self,
source: &str,
destination: &str,
src_left: bool,
dst_left: bool,
) -> Result<Option<Bytes>, WriteError> {
if self.remove_if_expired(source) {
return Ok(None);
}
match self.entries.get(source) {
None => return Ok(None),
Some(e) if !matches!(e.value, Value::List(_)) => return Err(WriteError::WrongType),
_ => {}
}

if source != destination {
self.remove_if_expired(destination);
if let Some(e) = self.entries.get(destination) {
if !matches!(e.value, Value::List(_)) {
return Err(WriteError::WrongType);
}
}
}

let popped = self.list_pop(source, src_left).map_err(WriteError::from)?;
let Some(value) = popped else {
return Ok(None);
};
self.list_push(destination, &[value.clone()], dst_left)?;
Ok(Some(value))
}

/// Internal pop implementation shared by lpop/rpop.
pub(super) fn list_pop(&mut self, key: &str, left: bool) -> Result<Option<Bytes>, WrongType> {
if self.remove_if_expired(key) {
Expand Down Expand Up @@ -1238,4 +1277,44 @@ mod tests {
ks.set("s".into(), Bytes::from("val"), None, false, false);
assert!(ks.lpos("s", b"a", 1, 1, 0).is_err());
}

#[test]
fn lmove_left_to_right() {
let mut ks = Keyspace::new();
ks.rpush("src", &[Bytes::from("a"), Bytes::from("b"), Bytes::from("c")])
.unwrap();
let moved = ks.lmove("src", "dst", true, false).unwrap();
assert_eq!(moved, Some(Bytes::from("a")));
// src should now be [b, c]
assert_eq!(ks.lrange("src", 0, -1).unwrap(), vec![Bytes::from("b"), Bytes::from("c")]);
// dst should be [a]
assert_eq!(ks.lrange("dst", 0, -1).unwrap(), vec![Bytes::from("a")]);
}

#[test]
fn lmove_rotate_same_key() {
let mut ks = Keyspace::new();
ks.rpush("q", &[Bytes::from("1"), Bytes::from("2"), Bytes::from("3")])
.unwrap();
// rotate: pop from left, push to right
let moved = ks.lmove("q", "q", true, false).unwrap();
assert_eq!(moved, Some(Bytes::from("1")));
let items = ks.lrange("q", 0, -1).unwrap();
assert_eq!(items, vec![Bytes::from("2"), Bytes::from("3"), Bytes::from("1")]);
}

#[test]
fn lmove_missing_source() {
let mut ks = Keyspace::new();
let moved = ks.lmove("missing", "dst", true, true).unwrap();
assert_eq!(moved, None);
assert!(!ks.exists("dst"));
}

#[test]
fn lmove_wrong_type_returns_error() {
let mut ks = Keyspace::new();
ks.set("s".into(), Bytes::from("hello"), None, false, false);
assert!(ks.lmove("s", "dst", true, true).is_err());
}
}
136 changes: 136 additions & 0 deletions crates/ember-core/src/keyspace/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,81 @@ impl Keyspace {
}
}

/// Returns the value of a key and deletes it atomically.
///
/// Returns `Ok(None)` if the key does not exist or has expired.
/// Returns `Err(WrongType)` if the key holds a non-string value.
pub fn getdel(&mut self, key: &str) -> Result<Option<Bytes>, WrongType> {
if self.remove_if_expired(key) {
return Ok(None);
}
match self.entries.get(key) {
None => return Ok(None),
Some(e) if !matches!(e.value, Value::String(_)) => return Err(WrongType),
_ => {}
}
let entry = self.entries.remove(key).expect("verified above");
let bytes = match entry.value {
Value::String(ref b) => b.clone(),
_ => unreachable!("type checked above"),
};
self.memory
.remove_with_size(entry.cached_value_size as usize + key.len() + memory::ENTRY_OVERHEAD);
self.decrement_expiry_if_set(&entry);
self.remove_version(key);
Ok(Some(bytes))
}

/// Returns the value of a key and optionally updates its expiry.
///
/// `expire` controls what happens to the TTL:
/// - `None` — leave the TTL unchanged (plain GET semantics)
/// - `Some(None)` — remove the TTL (PERSIST semantics)
/// - `Some(Some(duration))` — set a new TTL from now
///
/// Returns `Ok(None)` if the key does not exist or has expired.
/// Returns `Err(WrongType)` if the key holds a non-string value.
pub fn getex(
&mut self,
key: &str,
expire: Option<Option<Duration>>,
) -> Result<Option<Bytes>, WrongType> {
if self.remove_if_expired(key) {
return Ok(None);
}

let (bytes, had_expiry) = match self.entries.get(key) {
None => return Ok(None),
Some(e) => match &e.value {
Value::String(b) => (b.clone(), e.expires_at_ms != 0),
_ => return Err(WrongType),
},
};

if let Some(new_expire) = expire {
let entry = self.entries.get_mut(key).expect("verified above");
match new_expire {
Some(duration) => {
entry.expires_at_ms =
time::now_ms().saturating_add(duration.as_millis() as u64);
if !had_expiry {
self.expiry_count += 1;
}
}
None => {
entry.expires_at_ms = 0;
if had_expiry {
self.expiry_count = self.expiry_count.saturating_sub(1);
}
}
}
entry.touch(self.track_access);
self.bump_version(key);
}

Ok(Some(bytes))
}

/// Appends a value to an existing string key, or creates a new key if
/// it doesn't exist. Returns the new string length.
pub fn append(&mut self, key: &str, value: &[u8]) -> Result<usize, WriteError> {
Expand Down Expand Up @@ -893,4 +968,65 @@ mod tests {
assert_eq!(super::format_float(2.72), "2.72");
assert_eq!(super::format_float(10.5), "10.5");
}

#[test]
fn getdel_returns_value_and_removes_key() {
let mut ks = Keyspace::new();
ks.set("k".into(), Bytes::from("hello"), None, false, false);
let val = ks.getdel("k").unwrap();
assert_eq!(val, Some(Bytes::from("hello")));
assert!(!ks.exists("k"));
assert_eq!(ks.stats().used_bytes, 0);
}

#[test]
fn getdel_missing_key_returns_none() {
let mut ks = Keyspace::new();
assert_eq!(ks.getdel("nope").unwrap(), None);
}

#[test]
fn getdel_wrong_type_returns_error() {
let mut ks = Keyspace::new();
ks.zadd("z", &[(1.0, "a".into())], &ZAddFlags::default())
.unwrap();
assert!(ks.getdel("z").is_err());
}

#[test]
fn getex_no_option_leaves_ttl_unchanged() {
let mut ks = Keyspace::new();
ks.set("k".into(), Bytes::from("v"), Some(Duration::from_secs(60)), false, false);
let ttl_before = ks.ttl("k");
let val = ks.getex("k", None).unwrap();
assert_eq!(val, Some(Bytes::from("v")));
let ttl_after = ks.ttl("k");
// ttl should still be set (both should be positive)
assert!(matches!(ttl_before, TtlResult::Seconds(_)));
assert!(matches!(ttl_after, TtlResult::Seconds(_)));
}

#[test]
fn getex_persist_clears_ttl() {
let mut ks = Keyspace::new();
ks.set("k".into(), Bytes::from("v"), Some(Duration::from_secs(60)), false, false);
let val = ks.getex("k", Some(None)).unwrap();
assert_eq!(val, Some(Bytes::from("v")));
assert!(matches!(ks.ttl("k"), TtlResult::NoExpiry));
}

#[test]
fn getex_set_new_ttl() {
let mut ks = Keyspace::new();
ks.set("k".into(), Bytes::from("v"), None, false, false);
let val = ks.getex("k", Some(Some(Duration::from_secs(30)))).unwrap();
assert_eq!(val, Some(Bytes::from("v")));
assert!(matches!(ks.ttl("k"), TtlResult::Seconds(_)));
}

#[test]
fn getex_missing_key_returns_none() {
let mut ks = Keyspace::new();
assert_eq!(ks.getex("nope", None).unwrap(), None);
}
}
Loading
Loading