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

fix(set): fix random in SRANDMEMBER and SPOP commands #3022

Merged
merged 1 commit into from May 13, 2024

Conversation

BagritsevichStepan
Copy link
Contributor

fixes #3018

  1. For IntSet, the time complexity is O(n) (n - number of elements to return/remove). The space complexity is also O(n)
  2. For StrSet, the time complexity is O(m) (m - number of elements in set). The space complexity is O(n)


class NonUniquePicksGenerator : public PicksGenerator {
public:
NonUniquePicksGenerator(RandomPick max_range);
Copy link

Choose a reason for hiding this comment

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

We can add a comment for max_range is open or closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -284,19 +284,32 @@ void InterStrSet(const DbContext& db_context, const vector<SetType>& vec, String
}
}

StringVec PopStrSet(const DbContext& db_context, unsigned count, const SetType& st) {
StringVec RandMemberStrSet(const DbContext& db_context, const SetType& st,
const std::unique_ptr<PicksGenerator>& generator,
Copy link

Choose a reason for hiding this comment

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

Would a virtual function making it even slower?

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. You can pass just const PicksGenerator&
  2. I don't think the virtual function call adds any mesurable latency 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with @dranikpg, keep the virtual function call as it was

Copy link
Contributor

@dranikpg dranikpg left a comment

Choose a reason for hiding this comment

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

Sorry if this was not ready for review, just strolling by 👣

std::copy(result.begin(), result.end(), mapped.begin() + 1);
RecordJournal(op_args, "SREM"sv, mapped);
}
StringVec rand_members = rand_members_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.

that's a copy 👮🏻

Comment on lines 884 to 946
ArgSlice span{members_to_remove.data(), members_to_remove.size()};

auto rem_members_result = OpRem(op_args, key, span, false);
if (!rem_members_result) {
return rem_members_result.status();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

RETURN_ON_BAD_STATUS(op_args, key, absl::MakeSpan(members), false) 😅

Comment on lines 1064 to 1124
int count = 1;
if (args.size() > 1) {
string_view arg = ArgS(args, 1);
if (!absl::SimpleAtoi(arg, &count)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This changes what overload of SimpleAtoi is selected

Comment on lines +34 to +36
auto ConsistsOf(std::initializer_list<std::string> elements) {
return ConsistsOfMatcher(std::unordered_set<std::string>{elements});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🤩

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please explain what this means 😀

Copy link
Contributor

Choose a reason for hiding this comment

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

This means хороший код

Copy link
Collaborator

Choose a reason for hiding this comment

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

хороший кот

Comment on lines 847 to 850
std::unique_ptr<PicksGenerator> generator =
picks_are_unique ? static_cast<std::unique_ptr<PicksGenerator>>(
std::make_unique<UniquePicksGenerator>(picks_count, size))
: std::make_unique<NonUniquePicksGenerator>(size);
Copy link
Contributor

@dranikpg dranikpg May 7, 2024

Choose a reason for hiding this comment

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

ok mabye the function thing was more readable 😆 You can always also just use a declaration and an if after

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll fix it in zset_family.cc too

@@ -284,19 +284,32 @@ void InterStrSet(const DbContext& db_context, const vector<SetType>& vec, String
}
}

StringVec PopStrSet(const DbContext& db_context, unsigned count, const SetType& st) {
StringVec RandMemberStrSet(const DbContext& db_context, const SetType& st,
const std::unique_ptr<PicksGenerator>& generator,
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. You can pass just const PicksGenerator&
  2. I don't think the virtual function call adds any mesurable latency 🙂

Comment on lines 301 to 310
std::uint32_t ss_entry_index = 0;
for (const sds ptr : *ss) {
std::uint32_t t = times_index_is_picked[ss_entry_index++];
while (t--) {
result.emplace_back(ptr, sdslen(ptr));
}
Copy link
Contributor

@dranikpg dranikpg May 7, 2024

Choose a reason for hiding this comment

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

you use an unordered_map, but during iteration you'll end up creating every cell up to N because of [ss_entry_index], so better use find()

}
}

/* Members in result are sorted by scores. So, it is necessary to shuffle them*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Scores? I think you just mean that equal elements are always succesive and we're limited by the string-set order

Comment on lines 862 to 919
int64_t value;
DCHECK_GT(intsetGet(is, picked_index, &value), std::uint8_t(0));

Copy link
Contributor

Choose a reason for hiding this comment

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

DCHECK it's not called in release builds 🙂

fixes dragonflydb#3018

Signed-off-by: Stepan Bagritsevich <sbagritsevich@quantumbrains.com>
@BagritsevichStepan
Copy link
Contributor Author

BagritsevichStepan commented May 9, 2024

Fix a bug where replicas remove other elements during SPOP. Now all the tests are passed. @dranikpg , please take a look.

Copy link
Contributor

@dranikpg dranikpg left a comment

Choose a reason for hiding this comment

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

👍🏻 (Please push separate commits in the future so it's easier to see changes)

@dranikpg dranikpg merged commit d3a5851 into dragonflydb:main May 13, 2024
7 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.

SRANDMEMBER and SPOP always returns/removes only the lowest scoring element(s)
4 participants