LindaSummer commented on code in PR #3130:
URL: https://github.com/apache/kvrocks/pull/3130#discussion_r2463889339
##########
src/types/redis_tdigest.cc:
##########
@@ -67,18 +67,18 @@ class DummyCentroids {
if (Valid()) {
std::advance(iter_, 1);
}
- return iter_ != centroids_.cend();
+ return Valid();
}
// The Prev function can only be called for item is not cend,
// because we must guarantee the iterator to be inside the valid range
before iteration.
bool Prev() {
- if (Valid() && iter_ != centroids_.cbegin()) {
+ if (Valid()) {
std::advance(iter_, -1);
}
return Valid();
}
- bool Valid() const { return iter_ != centroids_.cend(); }
+ bool Valid() const { return iter_ < centroids_.cend() && iter_ >=
centroids_.cbegin(); }
Review Comment:
It seems that `iter_` should always greater or equal to the `cbegin()`.
##########
src/commands/cmd_tdigest.cc:
##########
@@ -176,6 +176,49 @@ class CommandTDigestAdd : public Commander {
std::vector<double> values_;
};
+class CommandTDigestRevRank : public Commander {
+ public:
+ Status Parse(const std::vector<std::string> &args) override {
+ key_name_ = args[1];
+ inputs_.reserve(args.size() - 2);
+ for (size_t i = 2; i < args.size(); i++) {
+ auto value = ParseFloat(args[i]);
+ if (!value) {
+ return {Status::RedisParseErr, errValueIsNotFloat};
+ }
+ inputs_.push_back(*value);
+ }
+ return Status::OK();
+ }
+ Status Execute(engine::Context &ctx, Server *srv, Connection *conn,
std::string *output) override {
+ TDigest tdigest(srv->storage, conn->GetNamespace());
+ std::vector<int> result;
+ result.reserve(inputs_.size());
+ if (const auto s = tdigest.RevRank(ctx, key_name_, inputs_, result);
!s.ok()) {
+ if (s.IsNotFound()) {
+ return {Status::RedisExecErr, errKeyNotFound};
+ }
+ return {Status::RedisExecErr, s.ToString()};
+ }
+
+ if (!result.empty()) {
+ std::vector<std::string> rev_ranks;
+ rev_ranks.reserve(result.size());
+ for (const auto v : result) {
+ rev_ranks.push_back(redis::Integer(v));
+ }
+ *output = redis::Array(rev_ranks);
+ } else {
+ *output = redis::BulkString("nan");
Review Comment:
refer to
[tdigest.revrank](https://redis.io/docs/latest/commands/tdigest.revrank/).
> All values are -2 if the sketch is empty.
##########
src/types/tdigest.h:
##########
@@ -150,3 +152,70 @@ inline StatusOr<double> TDigestQuantile(TD&& td, double q)
{
diff /= (lc.weight / 2 + rc.weight / 2);
return Lerp(lc.mean, rc.mean, diff);
}
+
+inline void AssignRankForEqualInputs(const std::vector<size_t>& indices,
double cumulative_weight,
+ std::vector<int>& result) {
+ for (auto index : indices) {
+ result[index] = static_cast<int>(cumulative_weight);
+ }
+}
+
+template <typename TD>
+inline Status TDigestRevRank(TD&& td, const std::vector<double>& inputs,
std::vector<int>& result) {
+ std::map<double, std::vector<size_t>> value_to_indices;
+ for (size_t i = 0; i < inputs.size(); ++i) {
+ value_to_indices[inputs[i]].push_back(i);
Review Comment:
it is hard to compare two double number for a map. we need a stable way for
the compare operator.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]