Skip to content

Commit

Permalink
Merge pull request #72 from amazingdatamachine/carson/list-offset-fix
Browse files Browse the repository at this point in the history
Fix issue with offset + prefix + delim off by one error
  • Loading branch information
sanderpick authored Jun 7, 2024
2 parents e4836f3 + 16ea3db commit 417fec0
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions fendermint/actors/objectstore/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl State {
}
}
count += 1;
if count < offset {
if count <= offset {
continue;
}
let item = match v {
Expand Down Expand Up @@ -550,7 +550,7 @@ mod tests {
let store = MemoryBlockstore::default();
let mut state = State::new(&store, Address::new_id(100), WriteAccess::OnlyOwner).unwrap();

let (_, _, baz_key) = create_and_put_objects(&mut state, &store).unwrap();
let (_, bar_key, _) = create_and_put_objects(&mut state, &store).unwrap();

let default_item = ObjectListItem::External((Cid::default(), false));

Expand All @@ -559,6 +559,44 @@ mod tests {
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.objects.len(), 1);
assert_eq!(result.objects.first(), Some(&(baz_key.0, default_item)));
// Note that baz is listed first in order, so an offset of 1 will return bar
assert_eq!(result.objects.first(), Some(&(bar_key.0, default_item)));
}

#[test]
fn test_list_with_prefix_delimiter_and_offset_and_limit() {
let store = MemoryBlockstore::default();
let mut state = State::new(&store, Address::new_id(100), WriteAccess::OnlyOwner).unwrap();

let one = BytesKey("hello/world".as_bytes().to_vec());
state
.put(
&store,
one.clone(),
ObjectKind::Internal(ByteBuf(vec![4, 5, 6])),
false,
)
.unwrap();
let two = BytesKey("hello/again".as_bytes().to_vec());
state
.put(
&store,
two.clone(),
ObjectKind::External(Cid::default()),
false,
)
.unwrap();

// List all keys with a limit and offset
let result = state.list(
&store,
"hello/".as_bytes().to_vec(),
"/".as_bytes().to_vec(),
2,
0,
);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(result.objects.len(), 0);
}
}

0 comments on commit 417fec0

Please sign in to comment.