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

Update KEYS and SCAN page with new optimization about pattern with hash tag. #2610

Merged
merged 8 commits into from
Mar 7, 2024

Conversation

CharlesChen888
Copy link
Contributor

As the optimization done in redis/redis#12754, we may doc this in the command page.

Copy link

netlify bot commented Nov 29, 2023

👷 Deploy request for redis-doc pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 80e69f7

commands/keys.md Outdated Show resolved Hide resolved
@oranagra oranagra added waiting-for-upstream waiting for a redis PR to be merged to-be-merged should probably be merged soon labels Nov 29, 2023
@oranagra
Copy link
Member

let's add the same comment about SCAN in this PR as well.

@CharlesChen888 CharlesChen888 changed the title Update KEYS page with new optimization. Update KEYS and SCAN page with new optimization about pattern with hasht tag. Nov 30, 2023
@CharlesChen888 CharlesChen888 changed the title Update KEYS and SCAN page with new optimization about pattern with hasht tag. Update KEYS and SCAN page with new optimization about pattern with hash tag. Nov 30, 2023
@soloestoy
Copy link
Collaborator

soloestoy commented Nov 30, 2023

Currently, there are several limitations when using hashtags in patterns. For instance, there cannot be wildcards or escape character before the hashtag, and the hashtag within curly braces cannot contain wildcards or support escape character. This is related to the implementation of the patternHashSlot function:

/* If it can be inferred that the given glob-style pattern, as implemented in
 * stringmatchlen() in util.c, only can match keys belonging to a single slot,
 * that slot is returned. Otherwise -1 is returned. */
int patternHashSlot(char *pattern, int length) {
    int s = -1; /* index of the first '{' */

    for (int i = 0; i < length; i++) {
        if (pattern[i] == '*' || pattern[i] == '?' || pattern[i] == '[') {
            /* Wildcard or character class found. Keys can be in any slot. */
            return -1;
        } else if (pattern[i] == '\\') {
            /* Escaped character. Computing slot in this case is not
             * implemented. We would need a temp buffer. */
            return -1;
        } else if (s == -1 && pattern[i] == '{') {
            /* Opening brace '{' found. */
            s = i;
        } else if (s >= 0 && pattern[i] == '}' && i == s + 1) {
            /* Empty tag '{}' found. The whole key is hashed. Ignore braces. */
            s = -2;
        } else if (s >= 0 && pattern[i] == '}') {
            /* Non-empty tag '{...}' found. Hash what's between braces. */
            return crc16(pattern + s + 1, i - s - 1) & 0x3FFF;
        }
    }

    /* The pattern matches a single key. Hash the whole pattern. */
    return crc16(pattern, length) & 0x3FFF;
}

For example, "{abc}*" can successfully recognize the hashtag and only scan the slot corresponding to "abc". However, "*{abc}", "{a*c}", or "{a\*bc}" cannot recognize the hashtag.

I believe we need @zuiderkwast 's help to carefully describe the rules for using hashtags in patterns.

commands/keys.md Outdated Show resolved Hide resolved
commands/scan.md Outdated Show resolved Hide resolved
CharlesChen888 and others added 2 commits December 1, 2023 10:59
Co-authored-by: David Dougherty <david.dougherty@redis.com>
@soloestoy
Copy link
Collaborator

@oranagra @zuiderkwast please approve

Copy link
Contributor

@zuiderkwast zuiderkwast left a comment

Choose a reason for hiding this comment

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

Minor suggestions to formulation.

commands/keys.md Outdated Show resolved Hide resolved
commands/scan.md Outdated Show resolved Hide resolved
commands/keys.md Outdated Show resolved Hide resolved
commands/scan.md Outdated Show resolved Hide resolved
Co-authored-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
@zuiderkwast zuiderkwast merged commit a1e94d9 into redis:master Mar 7, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-be-merged should probably be merged soon waiting-for-upstream waiting for a redis PR to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants