pickledb-rs icon indicating copy to clipboard operation
pickledb-rs copied to clipboard

Add ability to iterate over all Lists in the PickleDB

Open landhb opened this issue 1 year ago • 4 comments

This is definitely a breaking change, and would require a major version bump, since it changes a few of the existing types and methods. But I think this might be a more intuitive naming scheme for the list iterators.

The updated types are:

PickleDbListIterator: Iterates over all lists in the DB PickleDbListItemIterator: Iterates over items in a specific list PickleDbListItem: Item yielded from a list

Updates in this PR to make these changes:

  • The old PickleDb::liter() is now named PickleDb::liter_get() to obtain an iterator over a specific list.
  • The old PickleDbListIterator is now named PickleDbListItemIterator since it iterates over items in a list.
  • Changes PickleDb::liter() to produce an iterator over all lists in the db (the new PickleDbListIterator)
  • Introduces a new PickleDbListIterator that yields an iterator per list PickleDbListItemIterator
let mut db = pickledb::PickleDb::new_bin("1.db", pickledb::PickleDbDumpPolicy::AutoDump);

// Create a new list
db.lcreate("list1").unwrap()
    .lextend(&vec![1,2,3,4]);

// Create another one
db.lcreate("list2").unwrap()
    .lextend(&vec![5,6,7,8]);

// Iterate over all lists
for list in db.liter() {
    println!("Iterating over list: {:?}", list.name());
    for item in list {
        println!("Current item is: {}", item.get_item::<i32>().unwrap());
    }
}

landhb avatar Jan 23 '23 22:01 landhb

@landhb thank you for working on this!

I'm thinking if there's a way to make it backward compatible - maybe preserve the old list iterator (and mark it as deprecated) and introduce new iterators?

This way users can enjoy this new feature and at the same time it won't break anything for existing usage. Let me know what you think

seladb avatar Jan 24 '23 08:01 seladb

Hey @seladb sorry for the late response. I'll see if I can feature gate it and add deprecation warnings.

landhb avatar Feb 25 '23 16:02 landhb

@seladb Finally got some time to come back to this. I've added the deprecation warning, and by default all old code using liter() will continue to compile and work as before. Just with the added warning:

$ cargo run --example lists
   Compiling pickledb v0.5.2 (/mnt/archive/Documents/Projects/pickledb-rs)
warning: use of deprecated associated function `pickledb::PickleDb::liter`: Please switch to `pickledb::PickleDb::liter_get`
   --> examples/lists/src/main.rs:110:25
    |
110 |     for item_iter in db.liter("list2") {
    |                         ^^^^^
    |
    = note: `#[warn(deprecated)]` on by default

warning: `pickledb` (example "lists") generated 1 warning

To use the newer version of liter users will need to opt-in to the list-iterator feature.

landhb avatar Mar 20 '23 19:03 landhb

Thank you @landhb ! I'm a little bit busy right now but I'll take a look soon

seladb avatar Mar 25 '23 06:03 seladb