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

improve BITPOS command #3893

Closed
Niennienzz opened this issue Oct 7, 2024 · 6 comments · Fixed by #3910
Closed

improve BITPOS command #3893

Niennienzz opened this issue Oct 7, 2024 · 6 comments · Fixed by #3910
Assignees
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest Participates in Hacktoberfest help wanted Extra attention is needed

Comments

@Niennienzz
Copy link
Contributor

Niennienzz commented Oct 7, 2024

Currently, the BITPOS command works fine for normal cases. However, for some edge cases, it's not behaving the same as Redis/Valkey. It's better to improve this command for edge cases.

Specifically:

  • The bit parameter should accept only 0 or 1:

    dragonfly$> SET mykey "\xff\xf0\x00"
    OK
    
    dragonfly$> BITPOS mykey 0
    (integer) 12
    
    dragonfly$> BITPOS mykey 1
    (integer) 0
    
    dragonfly$> BITPOS mykey 2 # Should throw an error here.
    (integer) 0
  • For non-existent or empty strings, Dragonfly always returns -1, which makes sense to me. But for compatibility purpose, this can be improved as well so that client SDKs can deal with it better. See example and details below.

    dragonfly$> BITPOS non_existent_key 0 # Should return 0 instead of -1 when searching clear bit in empty string.
    (integer) -1
    
    dragonfly$> BITPOS non_existent_key 1
    (integer) -1

    Taking from the Redis doc:

    Integer reply: the position of the first bit set to 1 or 0 according to the request
    Integer reply: -1. In case the bit argument is 1 and the string is empty or composed of just zero bytes

    If we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is returned.

    If we look for clear bits (the bit argument is 0) and the string only contains bits set to 1, the function returns the first bit not part of the string on the right. So if the string is three bytes set to the value 0xff the command BITPOS key 0 will return 24, since up to bit 23 all the bits are 1.

    The function considers the right of the string as padded with zeros if you look for clear bits and specify no range or the start argument only.

    However, this behavior changes if you are looking for clear bits and specify a range with both start and end. If a clear bit isn't found in the specified range, the function returns -1 as the user specified a clear range and there are no 0 bits in that range.

@Niennienzz
Copy link
Contributor Author

I think this can be a hacktoberfest and a good first issue. @romange

@romange
Copy link
Collaborator

romange commented Oct 7, 2024

Sure

@Niennienzz Niennienzz added good first issue Good for newcomers hacktoberfest Participates in Hacktoberfest labels Oct 7, 2024
@romange romange added help wanted Extra attention is needed bug Something isn't working labels Oct 8, 2024
@Diskein
Copy link
Contributor

Diskein commented Oct 8, 2024

Hey, do you mind if i take it?

@kostasrim
Copy link
Contributor

@Diskein sure!

Diskein added a commit to Diskein/dragonfly that referenced this issue Oct 11, 2024
BITPOS returns 0 for non-existent keys according to Redis's
implmentation.

BITPOS allows only 0 and 1 as the bit mode argument.
Diskein added a commit to Diskein/dragonfly that referenced this issue Oct 11, 2024
BITPOS returns 0 for non-existent keys according to Redis's
implmentation.

BITPOS allows only 0 and 1 as the bit mode argument.

Signed-off-by: Denis K <[email protected]>
romange pushed a commit that referenced this issue Oct 12, 2024
BITPOS returns 0 for non-existent keys according to Redis's
implmentation.

BITPOS allows only 0 and 1 as the bit mode argument.

Signed-off-by: Denis K <[email protected]>
@Diskein
Copy link
Contributor

Diskein commented Oct 12, 2024

@Niennienzz you can close this issue since the problems were resolved in #3910

@romange
Copy link
Collaborator

romange commented Oct 12, 2024

Fixed by #3910

@romange romange closed this as completed Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers hacktoberfest Participates in Hacktoberfest help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants