Skip to content

Commit

Permalink
fix: correct the range behavior in Memory KvBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Oct 18, 2023
1 parent 3217b56 commit a8ca859
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/common/meta/src/kv_backend/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ impl<T: ErrorExt + Send + Sync + 'static> KvBackend for MemoryKvBackend<T> {

let iter: Box<dyn Iterator<Item = (&Vec<u8>, &Vec<u8>)>> = if range_end.is_empty() {
Box::new(kvs.get_key_value(&key).into_iter())
} else if range_end.len() == 1 && range_end[0] == 0 {
// If both key and range_end are ‘\0’, then range represents all keys.
if key.len() == 1 && key[0] == 0 {
Box::new(kvs.iter())
} else {
// If range_end is ‘\0’, the range is all keys greater than or equal to the key argument.
Box::new(kvs.range(key..))
}
} else {
Box::new(kvs.range(key..range_end))
};
Expand Down Expand Up @@ -504,6 +512,43 @@ mod tests {
assert_eq!(b"val1", resp.kvs[0].value());
}

#[tokio::test]
async fn test_range_2() {
let kv = MemoryKvBackend::<Error>::new();

kv.put(PutRequest::new().with_key("atest").with_value("value"))
.await
.unwrap();

kv.put(PutRequest::new().with_key("test").with_value("value"))
.await
.unwrap();

// If both key and range_end are ‘\0’, then range represents all keys.
let result = kv
.range(RangeRequest::new().with_range(b"\0".to_vec(), b"\0".to_vec()))
.await
.unwrap();

assert_eq!(result.kvs.len(), 2);

// If range_end is ‘\0’, the range is all keys greater than or equal to the key argument.
let result = kv
.range(RangeRequest::new().with_range(b"a".to_vec(), b"\0".to_vec()))
.await
.unwrap();

assert_eq!(result.kvs.len(), 2);

let result = kv
.range(RangeRequest::new().with_range(b"b".to_vec(), b"\0".to_vec()))
.await
.unwrap();

assert_eq!(result.kvs.len(), 1);
assert_eq!(result.kvs[0].key, b"test");
}

#[tokio::test]
async fn test_batch_get() {
let kv_store = mock_mem_store_with_data().await;
Expand Down

0 comments on commit a8ca859

Please sign in to comment.