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

feat(server): Implement BITPOS. #907

Merged
merged 5 commits into from
Mar 11, 2023
Merged

feat(server): Implement BITPOS. #907

merged 5 commits into from
Mar 11, 2023

Conversation

chakaz
Copy link
Collaborator

@chakaz chakaz commented Mar 4, 2023

Fixes #835.

Fixes dragonflydb#835.

Signed-off-by: chakaz <chakaz@chakaz>
@romange romange requested a review from ashotland March 4, 2023 19:58
Copy link
Contributor

@ashotland ashotland left a comment

Choose a reason for hiding this comment

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

Thanks! - just some naming related comments


if (position == -1 && !bit_value && static_cast<size_t>(start) < result_value.size() &&
end == std::numeric_limits<int64_t>::max()) {
// Not-found return value is compatible with Redis, but is subtle.
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment is a bit enigmatic to me :)

If returning -1 is compatible with Redis I am not sure I agree with the logic, or with the cognitive burden on the reader this edge-case yields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this specific case, Redis returns (and documents that it returns) the size, in bits, of the value.
I agree, this edge case is super weird and has a potentially significant burden on users. But that's the Redis API, and Dragonfly aims to be compatible, so we must mimic this weird API :(
I slightly modified the comment, please lmk if that's not good enough or if you have other suggestions.


OpResult<int64_t> FindFirstBitWithValue(const OpArgs& op_args, std::string_view key, bool bit_value,
int64_t start, int64_t end, bool as_bit) {
OpResult<std::string> result = ReadValue(op_args.db_cntx, key, op_args.shard);
Copy link
Contributor

Choose a reason for hiding this comment

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

rename result to value?

Result is a bit confusing with the result of the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted to distinguish bit_value from value, but you're right that it's more confusing as is. Renamed to value per your suggestion.

int64_t start, int64_t end, bool as_bit) {
OpResult<std::string> result = ReadValue(op_args.db_cntx, key, op_args.shard);

std::string_view result_value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, could be input_value or similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I went with value_str to reflect that it's the value's string value. lmk if you have a better idea.

EXPECT_EQ(35, CheckedInt({"bitpos", "a", "1", "-5", "-1", "BIT"}));
EXPECT_EQ(34, CheckedInt({"bitpos", "a", "1", "-6", "-1", "BIT"}));

// Test weird return value spec, specifically when looking for clear bits in
Copy link
Contributor

Choose a reason for hiding this comment

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

// Edge cases for for clear bits in an all-set string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's actually not the edge case that I'm testing, but rather that we behave like Redis in terms of the weird spec.
I updated the comment.

chakaz added 3 commits March 10, 2023 09:31
Signed-off-by: chakaz <chakaz@chakaz>
Signed-off-by: chakaz <chakaz@chakaz>
ashotland
ashotland previously approved these changes Mar 10, 2023
Copy link
Contributor

@ashotland ashotland left a comment

Choose a reason for hiding this comment

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

Thanks!

EXPECT_EQ(-1, CheckedInt({"bitpos", "b", "0", "0", "3"}));
EXPECT_EQ(-1, CheckedInt({"bitpos", "b", "0", "0", "3", "BYTE"}));

ASSERT_EQ(Run({"set", "c", ""_b}), "OK");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I'd call this key "empty"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done :)

Signed-off-by: chakaz <chakaz@chakaz>
@ashotland ashotland merged commit 011c086 into dragonflydb:main Mar 11, 2023
@chakaz chakaz deleted the bitpos branch March 18, 2023 18:55
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.

BITPOS does not work
2 participants