agatedb
agatedb copied to clipboard
Proposal for new AgateIterator interface
The iterator trait can panic due to incorrect usage. I propose a safer design:
trait KeyValuePair {
fn key(&self) -> &[u8];
fn value(&self) -> Value;
}
trait Iterator {
type KeyValuePair;
fn next(&mut self) -> Result<KeyValuePair>;
fn seek(&mut self) -> Result<KeyValuePair>;
fn rewind(&mut self) -> Result<KeyValuePair>;
fn current(&self) -> Option<KeyValuePair>;
}
KeyValuePair trait allows customization of key and value, so it's still lazily loaded. And key and value methods will not have to check for valid any more. Iterator doesn't have to store the error either.
This interface is for future refactor, doesn't have to be implemented in this PR.
Originally posted by @BusyJay in https://github.com/tikv/agatedb/pull/30#discussion_r530193538
Maybe the seek should be fn seek(&mut self, key: &[u8]) -> Result<()> ?
And it seems strange to return KeyValuePair in every next, seek and rewind. IMO, only current need to yield out the entry, which meets the "lazy loading" requirements.
@BusyJay @skyzh
Returning KeyValuePair in seek is to avoid another condition check for current. "lazy loading" is for value, key is loaded any way.