|
47 | 47 |
|
48 | 48 | namespace redis { |
49 | 49 |
|
| 50 | +namespace { |
| 51 | +template <bool Reverse, typename Container> |
| 52 | +inline decltype(auto) GetCbeginIter(const Container& centroids) { |
| 53 | + if constexpr (Reverse) { |
| 54 | + return centroids.crbegin(); |
| 55 | + } else { |
| 56 | + return centroids.cbegin(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +template <bool Reverse, typename Container> |
| 61 | +inline decltype(auto) GetCendIter(const Container& centroids) { |
| 62 | + if constexpr (Reverse) { |
| 63 | + return centroids.crend(); |
| 64 | + } else { |
| 65 | + return centroids.cend(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +template <bool Reverse> |
| 70 | +rocksdb::Status RankImpl(TDigest* self, engine::Context& ctx, const Slice& digest_name, |
| 71 | + const std::vector<double>& inputs, std::vector<int>& result) { |
| 72 | + auto ns_key = self->AppendNamespacePrefix(digest_name); |
| 73 | + TDigestMetadata metadata; |
| 74 | + { |
| 75 | + LockGuard guard(self->storage_->GetLockManager(), ns_key); |
| 76 | + |
| 77 | + if (auto status = self->getMetaDataByNsKey(ctx, ns_key, &metadata); !status.ok()) { |
| 78 | + return status; |
| 79 | + } |
| 80 | + |
| 81 | + if (metadata.total_observations == 0) { |
| 82 | + result.resize(inputs.size(), -2); |
| 83 | + return rocksdb::Status::OK(); |
| 84 | + } |
| 85 | + |
| 86 | + if (auto status = self->mergeNodes(ctx, ns_key, &metadata); !status.ok()) { |
| 87 | + return status; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + std::vector<Centroid> centroids; |
| 92 | + if (auto status = self->dumpCentroids(ctx, ns_key, metadata, ¢roids); !status.ok()) { |
| 93 | + return status; |
| 94 | + } |
| 95 | + |
| 96 | + auto dump_centroids = DummyCentroids<Reverse>(metadata, centroids); |
| 97 | + if (auto status = TDigestRank<Reverse>(dump_centroids, inputs, result); !status) { |
| 98 | + return rocksdb::Status::InvalidArgument(status.Msg()); |
| 99 | + } |
| 100 | + return rocksdb::Status::OK(); |
| 101 | +} |
| 102 | +} // namespace |
| 103 | + |
| 104 | +// TODO: It should be replaced by a iteration of the rocksdb iterator |
| 105 | +template <bool Reverse> |
| 106 | +class DummyCentroids { |
| 107 | + public: |
| 108 | + DummyCentroids(const TDigestMetadata& meta_data, const std::vector<Centroid>& centroids) |
| 109 | + : meta_data_(meta_data), centroids_(centroids) {} |
| 110 | + class Iterator { |
| 111 | + public: |
| 112 | + using IterType = std::conditional_t<Reverse, std::vector<Centroid>::const_reverse_iterator, |
| 113 | + std::vector<Centroid>::const_iterator>; |
| 114 | + Iterator(IterType iter, const std::vector<Centroid>& centroids) : iter_(iter), centroids_(centroids) {} |
| 115 | + std::unique_ptr<Iterator> Clone() const { |
| 116 | + if (iter_ != GetCendIter<Reverse>(centroids_)) { |
| 117 | + return std::make_unique<Iterator>( |
| 118 | + std::next(GetCbeginIter<Reverse>(centroids_), std::distance(GetCbeginIter<Reverse>(centroids_), iter_)), |
| 119 | + centroids_); |
| 120 | + } |
| 121 | + return std::make_unique<Iterator>(GetCendIter<Reverse>(centroids_), centroids_); |
| 122 | + } |
| 123 | + bool Next() { |
| 124 | + if (Valid()) { |
| 125 | + std::advance(iter_, 1); |
| 126 | + } |
| 127 | + return iter_ != GetCendIter<Reverse>(centroids_); |
| 128 | + } |
| 129 | + |
| 130 | + // The Prev function can only be called for item is not cend, |
| 131 | + // because we must guarantee the iterator to be inside the valid range before iteration. |
| 132 | + bool Prev() { |
| 133 | + if (Valid() && iter_ != GetCendIter<Reverse>(centroids_)) { |
| 134 | + std::advance(iter_, -1); |
| 135 | + } |
| 136 | + return Valid(); |
| 137 | + } |
| 138 | + bool Valid() const { return iter_ != GetCendIter<Reverse>(centroids_); } |
| 139 | + StatusOr<Centroid> GetCentroid() const { |
| 140 | + if (iter_ == GetCendIter<Reverse>(centroids_)) { |
| 141 | + return {::Status::NotOK, "invalid iterator during decoding tdigest centroid"}; |
| 142 | + } |
| 143 | + return *iter_; |
| 144 | + } |
| 145 | + |
| 146 | + private: |
| 147 | + IterType iter_; |
| 148 | + const std::vector<Centroid>& centroids_; |
| 149 | + }; |
| 150 | + |
| 151 | + std::unique_ptr<Iterator> Begin() const { |
| 152 | + return std::make_unique<Iterator>(GetCbeginIter<Reverse>(centroids_), centroids_); |
| 153 | + } |
| 154 | + std::unique_ptr<Iterator> End() const { |
| 155 | + if (centroids_.empty()) { |
| 156 | + return std::make_unique<Iterator>(GetCendIter<Reverse>(centroids_), centroids_); |
| 157 | + } |
| 158 | + return std::make_unique<Iterator>(std::prev(GetCendIter<Reverse>(centroids_)), centroids_); |
| 159 | + } |
| 160 | + double TotalWeight() const { return static_cast<double>(meta_data_.total_weight); } |
| 161 | + double Min() const { return meta_data_.minimum; } |
| 162 | + double Max() const { return meta_data_.maximum; } |
| 163 | + uint64_t Size() const { return meta_data_.merged_nodes; } |
| 164 | + |
| 165 | + private: |
| 166 | + const TDigestMetadata& meta_data_; |
| 167 | + const std::vector<Centroid>& centroids_; |
| 168 | +}; |
| 169 | + |
50 | 170 | uint32_t constexpr kMaxElements = 1 * 1024; // 1k doubles |
51 | 171 |
|
52 | 172 | rocksdb::Status TDigest::Create(engine::Context& ctx, const Slice& digest_name, const TDigestCreateOptions& options, |
@@ -578,4 +698,70 @@ std::string TDigest::internalSegmentGuardPrefixKey(const TDigestMetadata& metada |
578 | 698 | PutFixed8(&prefix_key, static_cast<uint8_t>(seg)); |
579 | 699 | return InternalKey(ns_key, prefix_key, metadata.version, storage_->IsSlotIdEncoded()).Encode(); |
580 | 700 | } |
| 701 | + |
| 702 | +rocksdb::Status TDigest::Rank(engine::Context& ctx, const Slice& digest_name, const std::vector<double>& inputs, |
| 703 | + std::vector<int>& result) { |
| 704 | + auto ns_key = AppendNamespacePrefix(digest_name); |
| 705 | + TDigestMetadata metadata; |
| 706 | + { |
| 707 | + LockGuard guard(storage_->GetLockManager(), ns_key); |
| 708 | + |
| 709 | + if (auto status = getMetaDataByNsKey(ctx, ns_key, &metadata); !status.ok()) { |
| 710 | + return status; |
| 711 | + } |
| 712 | + |
| 713 | + if (metadata.total_observations == 0) { |
| 714 | + result.resize(inputs.size(), -2); |
| 715 | + return rocksdb::Status::OK(); |
| 716 | + } |
| 717 | + |
| 718 | + if (auto status = mergeNodes(ctx, ns_key, &metadata); !status.ok()) { |
| 719 | + return status; |
| 720 | + } |
| 721 | + } |
| 722 | + |
| 723 | + std::vector<Centroid> centroids; |
| 724 | + if (auto status = dumpCentroids(ctx, ns_key, metadata, ¢roids); !status.ok()) { |
| 725 | + return status; |
| 726 | + } |
| 727 | + |
| 728 | + auto dump_centroids = DummyCentroids<false>(metadata, centroids); |
| 729 | + if (auto status = TDigestRank<false>(dump_centroids, inputs, result); !status) { |
| 730 | + return rocksdb::Status::InvalidArgument(status.Msg()); |
| 731 | + } |
| 732 | + return rocksdb::Status::OK(); |
| 733 | +} |
| 734 | + |
| 735 | +rocksdb::Status TDigest::RevRank(engine::Context& ctx, const Slice& digest_name, const std::vector<double>& inputs, |
| 736 | + std::vector<int>& result) { |
| 737 | + auto ns_key = AppendNamespacePrefix(digest_name); |
| 738 | + TDigestMetadata metadata; |
| 739 | + { |
| 740 | + LockGuard guard(storage_->GetLockManager(), ns_key); |
| 741 | + |
| 742 | + if (auto status = getMetaDataByNsKey(ctx, ns_key, &metadata); !status.ok()) { |
| 743 | + return status; |
| 744 | + } |
| 745 | + |
| 746 | + if (metadata.total_observations == 0) { |
| 747 | + result.resize(inputs.size(), -2); |
| 748 | + return rocksdb::Status::OK(); |
| 749 | + } |
| 750 | + |
| 751 | + if (auto status = mergeNodes(ctx, ns_key, &metadata); !status.ok()) { |
| 752 | + return status; |
| 753 | + } |
| 754 | + } |
| 755 | + |
| 756 | + std::vector<Centroid> centroids; |
| 757 | + if (auto status = dumpCentroids(ctx, ns_key, metadata, ¢roids); !status.ok()) { |
| 758 | + return status; |
| 759 | + } |
| 760 | + |
| 761 | + auto dump_centroids = DummyCentroids<true>(metadata, centroids); |
| 762 | + if (auto status = TDigestRank<true>(dump_centroids, inputs, result); !status) { |
| 763 | + return rocksdb::Status::InvalidArgument(status.Msg()); |
| 764 | + } |
| 765 | + return rocksdb::Status::OK(); |
| 766 | +} |
581 | 767 | } // namespace redis |
0 commit comments