-
Notifications
You must be signed in to change notification settings - Fork 955
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: WrapSds from family_utils.h (#3818)
* chore: WrapSds from family_utils.h --------- Signed-off-by: Roman Gershman <romange@gmail.com> Signed-off-by: Kostas Kyrimis <kostaskyrim@gmail.com> Co-authored-by: Roman Gershman <romange@gmail.com> Co-authored-by: Kostas Kyrimis <kostaskyrim@gmail.com>
- Loading branch information
1 parent
fa288c1
commit beb2ec2
Showing
11 changed files
with
176 additions
and
216 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "server/family_utils.h" | ||
|
||
#include "base/logging.h" | ||
|
||
namespace dfly { | ||
|
||
sds WrapSds(std::string_view s) { | ||
static thread_local sds tmp_sds = sdsempty(); | ||
return tmp_sds = sdscpylen(tmp_sds, s.data(), s.length()); | ||
} | ||
|
||
NonUniquePicksGenerator::NonUniquePicksGenerator(RandomPick max_range) : max_range_(max_range) { | ||
CHECK_GT(max_range, RandomPick(0)); | ||
} | ||
|
||
RandomPick NonUniquePicksGenerator::Generate() { | ||
return absl::Uniform(bitgen_, 0u, max_range_); | ||
} | ||
|
||
UniquePicksGenerator::UniquePicksGenerator(std::uint32_t picks_count, RandomPick max_range) | ||
: remaining_picks_count_(picks_count), picked_indexes_(picks_count) { | ||
CHECK_GE(max_range, picks_count); | ||
current_random_limit_ = max_range - picks_count; | ||
} | ||
|
||
RandomPick UniquePicksGenerator::Generate() { | ||
DCHECK_GT(remaining_picks_count_, 0u); | ||
|
||
remaining_picks_count_--; | ||
|
||
const RandomPick max_index = current_random_limit_++; | ||
const RandomPick random_index = absl::Uniform(bitgen_, 0u, max_index + 1u); | ||
|
||
const bool random_index_is_picked = picked_indexes_.emplace(random_index).second; | ||
if (random_index_is_picked) { | ||
return random_index; | ||
} | ||
|
||
picked_indexes_.insert(max_index); | ||
return max_index; | ||
} | ||
|
||
} // namespace dfly |
This file contains 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2024, DragonflyDB authors. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <absl/container/flat_hash_set.h> | ||
#include <absl/random/random.h> | ||
|
||
#include <cstdint> | ||
|
||
extern "C" { | ||
#include "redis/sds.h" | ||
} | ||
namespace dfly { | ||
|
||
// Copy str to thread local sds instance. Valid until next WrapSds call on thread | ||
sds WrapSds(std::string_view str); | ||
|
||
using RandomPick = uint32_t; | ||
|
||
class PicksGenerator { | ||
public: | ||
virtual RandomPick Generate() = 0; | ||
virtual ~PicksGenerator() = default; | ||
}; | ||
|
||
class NonUniquePicksGenerator : public PicksGenerator { | ||
public: | ||
/* The generated value will be within the closed-open interval [0, max_range) */ | ||
NonUniquePicksGenerator(RandomPick max_range); | ||
|
||
RandomPick Generate() override; | ||
|
||
private: | ||
const RandomPick max_range_; | ||
absl::BitGen bitgen_{}; | ||
}; | ||
|
||
/* | ||
* Generates unique index in O(1). | ||
* | ||
* picks_count specifies the number of random indexes to be generated. | ||
* In other words, this is the number of times the Generate() function is called. | ||
* | ||
* The class uses Robert Floyd's sampling algorithm | ||
* https://dl.acm.org/doi/pdf/10.1145/30401.315746 | ||
* */ | ||
class UniquePicksGenerator : public PicksGenerator { | ||
public: | ||
/* The generated value will be within the closed-open interval [0, max_range) */ | ||
UniquePicksGenerator(uint32_t picks_count, RandomPick max_range); | ||
|
||
RandomPick Generate() override; | ||
|
||
private: | ||
RandomPick current_random_limit_; | ||
uint32_t remaining_picks_count_; | ||
absl::flat_hash_set<RandomPick> picked_indexes_; | ||
absl::BitGen bitgen_{}; | ||
}; | ||
|
||
} // namespace dfly |
This file contains 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 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.