LindaSummer commented on code in PR #3268:
URL: https://github.com/apache/kvrocks/pull/3268#discussion_r2579845952
##########
src/types/redis_tdigest.h:
##########
@@ -33,6 +33,101 @@
#include "tdigest.h"
namespace redis {
+
+// TODO: It should be replaced by a iteration of the rocksdb iterator
+template <bool Reverse>
+class DummyCentroids {
+ public:
+ DummyCentroids(const TDigestMetadata& meta_data, const
std::vector<Centroid>& centroids)
+ : meta_data_(meta_data), centroids_(centroids) {}
+ class Iterator {
+ public:
+ using IterType = std::conditional_t<Reverse,
std::vector<Centroid>::const_reverse_iterator,
+ std::vector<Centroid>::const_iterator>;
+ Iterator(IterType iter, const std::vector<Centroid>& centroids) :
iter_(iter), centroids_(centroids) {}
+ std::unique_ptr<Iterator> Clone() const {
+ if (iter_ != getCendIter(centroids_)) {
+ return std::make_unique<Iterator>(
+ std::next(getCbeginIter(centroids_),
std::distance(getCbeginIter(centroids_), iter_)), centroids_);
+ }
+ return std::make_unique<Iterator>(getCendIter(centroids_), centroids_);
+ }
+ bool Next() {
+ if (Valid()) {
+ std::advance(iter_, 1);
+ }
+ return iter_ != getCendIter(centroids_);
+ }
+
+ // 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_ != getCendIter(centroids_)) {
+ std::advance(iter_, -1);
+ }
+ return Valid();
+ }
+ bool Valid() const { return iter_ != getCendIter(centroids_); }
+ StatusOr<Centroid> GetCentroid() const {
+ if (iter_ == getCendIter(centroids_)) {
+ return {::Status::NotOK, "invalid iterator during decoding tdigest
centroid"};
+ }
+ return *iter_;
+ }
+
+ private:
+ IterType iter_;
+ const std::vector<Centroid>& centroids_;
+ template <typename Container>
+ decltype(auto) getCbeginIter(const Container& centroids) const {
+ if constexpr (Reverse) {
+ return centroids.crbegin();
+ } else {
+ return centroids.cbegin();
+ }
+ }
+
+ template <typename Container>
+ decltype(auto) getCendIter(const Container& centroids) const {
+ if constexpr (Reverse) {
+ return centroids.crend();
+ } else {
+ return centroids.cend();
+ }
+ }
+ };
+
+ std::unique_ptr<Iterator> Begin() const {
+ if constexpr (Reverse) {
Review Comment:
I think the getbegin and getend are both stateless, static is ok.
maybe they could be a free inline function protected by a `detail` private
namespace if you prefer this way.
--
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]