-
Notifications
You must be signed in to change notification settings - Fork 592
Add BIT support to BITPOS #2170
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
815d11f
Add bit support to BITPOS
sheharyaar f0f7dbb
Fix bitmap logic and add test cases
sheharyaar 5b86fff
Add lamda for bit range query
sheharyaar 980c174
Merge branch 'unstable' into feat-bitpos-bit
jihuayu d64ec7b
Fix clang format
sheharyaar 7ca036e
Fix CI and fix RawBitpos
sheharyaar a93d2af
Fix golangci-lint
sheharyaar d1a0285
Merge branch 'unstable' into feat-bitpos-bit
jihuayu 039e5b9
Improve lamda function
sheharyaar 59662aa
Merge branch 'unstable' into feat-bitpos-bit
jihuayu 1f036d6
Fix review suggestions
sheharyaar 4cd8aba
Merge branch 'unstable' into feat-bitpos-bit
sheharyaar 80cef97
Merge branch 'unstable' into feat-bitpos-bit
mapleFU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,31 +100,80 @@ rocksdb::Status BitmapString::BitCount(const std::string &raw_value, int64_t sta | |
| } | ||
|
|
||
| rocksdb::Status BitmapString::BitPos(const std::string &raw_value, bool bit, int64_t start, int64_t stop, | ||
| bool stop_given, int64_t *pos) { | ||
| bool stop_given, int64_t *pos, bool is_bit_index) { | ||
| std::string_view string_value = std::string_view{raw_value}.substr(Metadata::GetOffsetAfterExpire(raw_value[0])); | ||
| auto strlen = static_cast<int64_t>(string_value.size()); | ||
| /* Convert negative and out-of-bound indexes */ | ||
| std::tie(start, stop) = NormalizeRange(start, stop, strlen); | ||
|
|
||
| int64_t length = is_bit_index ? strlen * 8 : strlen; | ||
| std::tie(start, stop) = NormalizeRange(start, stop, length); | ||
|
|
||
| if (start > stop) { | ||
| *pos = -1; | ||
| } else { | ||
| int64_t bytes = stop - start + 1; | ||
| *pos = util::msb::RawBitpos(reinterpret_cast<const uint8_t *>(string_value.data()) + start, bytes, bit); | ||
|
|
||
| /* If we are looking for clear bits, and the user specified an exact | ||
| * range with start-end, we can't consider the right of the range as | ||
| * zero padded (as we do when no explicit end is given). | ||
| * | ||
| * So if redisBitpos() returns the first bit outside the range, | ||
| * we return -1 to the caller, to mean, in the specified range there | ||
| * is not a single "0" bit. */ | ||
| if (stop_given && bit == 0 && *pos == bytes * 8) { | ||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
| int64_t byte_start = is_bit_index ? start / 8 : start; | ||
| int64_t byte_stop = is_bit_index ? stop / 8 : stop; | ||
| int64_t bit_in_start_byte = is_bit_index ? start % 8 : 0; | ||
| int64_t bit_in_stop_byte = is_bit_index ? stop % 8 : 7; | ||
| int64_t bytes_cnt = byte_stop - byte_start + 1; | ||
|
|
||
| auto bit_pos_in_byte_startstop = [](char byte, bool bit, uint32_t start, uint32_t stop) -> int { | ||
mapleFU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (uint32_t i = start; i <= stop; i++) { | ||
| if (util::msb::GetBitFromByte(byte, i) == bit) { | ||
| return (int)i; | ||
| } | ||
| } | ||
| return -1; | ||
| }; | ||
|
|
||
| // if the bit start and bit end are in the same byte, we can process it manually | ||
| if (is_bit_index && byte_start == byte_stop) { | ||
| int res = bit_pos_in_byte_startstop(string_value[byte_start], bit, bit_in_start_byte, bit_in_stop_byte); | ||
| if (res != -1) { | ||
| *pos = res + byte_start * 8; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
| *pos = -1; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
| if (is_bit_index && bit_in_start_byte != 0) { | ||
| // process first byte | ||
| int res = bit_pos_in_byte_startstop(string_value[byte_start], bit, bit_in_start_byte, 7); | ||
| if (res != -1) { | ||
| *pos = res + byte_start * 8; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
| byte_start++; | ||
| bytes_cnt--; | ||
| } | ||
|
|
||
| *pos = util::msb::RawBitpos(reinterpret_cast<const uint8_t *>(string_value.data()) + byte_start, bytes_cnt, bit); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check byte_stop here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, my bad, it handle the case in |
||
| if (is_bit_index && *pos != -1 && *pos != bytes_cnt * 8) { | ||
| // if the pos is more than stop bit, then it is not in the range | ||
| if (*pos > stop) { | ||
| *pos = -1; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
| if (*pos != -1) *pos += start * 8; /* Adjust for the bytes we skipped. */ | ||
| } | ||
|
|
||
| /* If we are looking for clear bits, and the user specified an exact | ||
| * range with start-end, we tcan' consider the right of the range as | ||
| * zero padded (as we do when no explicit end is given). | ||
| * | ||
| * So if redisBitpos() returns the first bit outside the range, | ||
| * we return -1 to the caller, to mean, in the specified range there | ||
| * is not a single "0" bit. */ | ||
| if (stop_given && bit == 0 && *pos == bytes_cnt * 8) { | ||
| *pos = -1; | ||
| return rocksdb::Status::OK(); | ||
| } | ||
| if (*pos != -1) *pos += byte_start * 8; /* Adjust for the bytes we skipped. */ | ||
|
|
||
| return rocksdb::Status::OK(); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.