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

Implement an unify key-value iterator for Kvrocks #2004

Merged
merged 6 commits into from
Jan 12, 2024

Conversation

git-hulk
Copy link
Member

@git-hulk git-hulk commented Jan 11, 2024

Currently, we need to iterate all keys in the database in different places like the cluster migration and kvrocks2redis, but don't have an iterator for this purpose. It's very error-prone to implement this in different places since Kvrocks may add a new column family in the future, and we must be careful to iterate all keys in all column families. This would be a burden for maintenance, So we want to implement an iterator for iterating keys.

DBIter iter(storage, read_option);
for (iter.Seek(); iter.Valid(); iter.Next()) {
    if (iter.Type() == kRedisString || iter.Type() == kRedisJSON) {
        // the string/json type didn't have subkeys
        continue;
    }

    auto subkey_iter = iter.GetSubKeyIterator();
    for (subkey_iter.Seek(); subkey_iter.Valid(); subkey_iter.Next()) {
        // handle its subkey and value here
    }
}

When using this iterator, it will iterate the metadata column family first and check its type, if it's not a string or JSON, then it will iterate the corresponding column family to get subkeys. That said, if we have a key foo with type hash, then the iterator will iterate foo and foo:field1, foo:field2, and so on.

This solution can bring those benefits:

  • The codes look more intuitive
  • Can reuse this iterator if we want to iterate keys only

This closes #1989

Currently, we need to iterate all keys in the database in different places like
the cluster migration and kvrocks2redis, but don't have an iterator for this purpose.
It's very error-prone to implement this in different places since Kvrocks may add
a new column family in the future, and we must be careful to iterate all keys in all column families.
This would be a burden for maintenance, So we want to implement an iterator for iterating keys.

```C++

DBIter iter(storage, read_option);
for (iter.Seek(); iter.Valid(); iter.Next()) {
    if (iter.Type() == kRedisString || iter.Type() == kRedisJSON) {
        // the string/json type didn't have subkeys
        continue;
    }

    auto subkey_iter = iter.GetSubKeyIterator();
    for (subkey_iter.Seek(); subkey_iter.Valid(); subkey_iter.Next()) {
        // handle its subkey and value here
    }
}

```

When using this iterator, it will iterate the metadata column family first and check its type,
if it's not a string or json, then it will iterate the corresponding column family to get subkeys.
That said, if we have a key foo with type hash, then the iterator will iterate foo and foo:field1, foo:field2, and so on.

This solution can bring those benefits:

- The codes look more intutive
- Can reuse this iterator if we want to iterate keys only
caipengbo
caipengbo previously approved these changes Jan 11, 2024
Copy link
Contributor

@caipengbo caipengbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent code, LGTM!

caipengbo
caipengbo previously approved these changes Jan 11, 2024
@jihuayu
Copy link
Member

jihuayu commented Jan 11, 2024

I have 2 question:

  1. I found the test doesn't have Bloom, JSON and SortedInt type. Is that OK?
  2. When I iter the zset subkey, it only returns the subkeys key|version|member=>score, not return subkeys key|version|score|member=>NULL?

@git-hulk
Copy link
Member Author

@jihuayu Thanks for your review.

  1. I found the test doesn't have Bloom, JSON and SortedInt type. Is that OK?

I'm not quite sure about how to add test cases for bloom. Welcome to send a PR after this if anyone knows that. I will also add a test case for JSON and SortedInt.

When I iter the zset subkey, it only returns the subkeys key|version|member=>score, not return subkeys key|version|score|member=>NULL?

Yes, it's not necessary to scan the score column family since it has enough information from the subkey column family.

src/storage/iterator.cc Outdated Show resolved Hide resolved
src/storage/iterator.cc Show resolved Hide resolved
src/storage/iterator.cc Outdated Show resolved Hide resolved
src/storage/iterator.cc Outdated Show resolved Hide resolved
src/storage/iterator.cc Show resolved Hide resolved
src/storage/iterator.cc Show resolved Hide resolved
@git-hulk git-hulk requested a review from mapleFU January 12, 2024 05:50
Copy link

sonarcloud bot commented Jan 12, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

6 New issues
0 Security Hotspots
No data about Coverage
1.7% Duplication on New Code

See analysis details on SonarCloud

@git-hulk git-hulk merged commit 4059b43 into apache:unstable Jan 12, 2024
29 checks passed
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

Successfully merging this pull request may close these issues.

Implement the migrate iterator for iterating key values
4 participants