Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for new AgateIterator interface #63

Open
skyzh opened this issue Nov 25, 2020 · 2 comments
Open

Proposal for new AgateIterator interface #63

skyzh opened this issue Nov 25, 2020 · 2 comments

Comments

@skyzh
Copy link
Member

skyzh commented Nov 25, 2020

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 #30 (comment)

@Fullstop000
Copy link
Member

Fullstop000 commented Nov 30, 2020

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

@BusyJay
Copy link
Member

BusyJay commented Nov 30, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants