Question - Iterator with an UNIQUE result
- Dominant language
- C++
- Stars
- 39.4k
- Forks
- 8.2k
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I know there's this implementation of the iterator available in the documentation:
```cpp
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:
```cpp
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 ?
Contributor guide
Assessment
This issue has not been assessed yet.