agatedb icon indicating copy to clipboard operation
agatedb copied to clipboard

Proposal for new AgateIterator interface

Open skyzh opened this issue 5 years ago • 2 comments

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

skyzh avatar Nov 25 '20 09:11 skyzh

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

Fullstop000 avatar Nov 30 '20 10:11 Fullstop000

Returning KeyValuePair in seek is to avoid another condition check for current. "lazy loading" is for value, key is loaded any way.

BusyJay avatar Nov 30 '20 11:11 BusyJay