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

Question - Iterator with an UNIQUE result #1129

Open
Loopite opened this issue Jun 24, 2023 · 0 comments
Open

Question - Iterator with an UNIQUE result #1129

Loopite opened this issue Jun 24, 2023 · 0 comments

Comments

@Loopite
Copy link

Loopite commented Jun 24, 2023

Hi, I know there's this implementation of the iterator available in the documentation:

leveldb::Iterator* it { getNewIterator() };

std::string const primaryKey { std::string(bestBlockKey.constData(), bestBlockKey.length()) }; // Get the key PREFIX

for (it->Seek(primaryKey);it->Valid() && it->key().starts_with(primaryKey);it->Next()) // Sort all keys which start with the PREFIX
{
    // Read key
    std::string const key { it->key().ToString() };

    // Read value
    std::string const value { it->value().ToString() };
}

delete it;

However, in my database the PREFIX follows this structure:

KEY_BLOCK (1 Byte) + blockHeight (8 Bytes) + blockHash (32 Bytes)

My primaryKey variable follows this structure but without any blockHash field. Please note, there's ONLY ONE key which starts with this structure because the blockHeight is always different. So I was wondering, if I want this key in order to get the blockHash field, the code would be directly:

leveldb::Iterator* it { getNewIterator() };

std::string const primaryKey { std::string(bestBlockKey.constData(), bestBlockKey.length()) }; // Get the key PREFIX

// Seek the key.
// This will always start with the PREFIX so a it->key().starts_with(primaryKey) is not necessary ?
// Should never fail otherwise, the key is not in the database.
it->Seek(primaryKey);

// Verify if the seek is valid and not invalid.
// If the key doesn't exist, this will return false.
if(it->Valid() && it->key().starts_with(primaryKey))
{
// Parse the key and the value.
}

delete it;

Is it correct ? Do a it->Seek(primaryKey) always will return the key which starts with the PREFIX if it is in the database ?

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

1 participant