mapleFU commented on code in PR #2524: URL: https://github.com/apache/kvrocks/pull/2524#discussion_r1765030678
########## src/types/cms.h: ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "server/redis_reply.h" +#include "xxhash.h" Review Comment: I didn't see xxhash being used in header... ########## src/types/cms.cc: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "cms.h" + +#include <algorithm> +#include <cmath> +#include <cstdint> +#include <vector> + +CMSketch::CMSketchDimensions CMSketch::CMSDimFromProb(double error, double delta) { + CMSketchDimensions dims; + dims.width = std::ceil(2 / error); + dims.depth = std::ceil(std::log10(delta) / std::log10(0.5)); + return dims; +} + +size_t CMSketch::IncrBy(std::string_view item, uint32_t value) { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + size_t loc = GetLocation(hash, i); + if (array_[loc] > UINT32_MAX - value) { + array_[loc] = UINT32_MAX; + } else { + array_[loc] += value; + } + min_count = std::min(min_count, static_cast<size_t>(array_[loc])); + } + counter_ += value; + return min_count; +} + +size_t CMSketch::Query(std::string_view item) const { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + min_count = std::min(min_count, static_cast<size_t>(array_[GetLocation(hash, i)])); + } + return min_count; +} + +int CMSketch::Merge(CMSketch* dest, size_t quantity, const std::vector<const CMSketch*>& src, + const std::vector<long long>& weights) { + if (checkOverflow(dest, quantity, src, weights) != 0) { + return -1; + } + + for (size_t i = 0; i < dest->GetDepth(); ++i) { + for (size_t j = 0; j < dest->GetWidth(); ++j) { + int64_t item_count = 0; + for (size_t k = 0; k < quantity; ++k) { + item_count += static_cast<int64_t>(src[k]->array_[(i * dest->GetWidth()) + j]) * weights[k]; + } + dest->GetArray()[(i * dest->GetWidth()) + j] = item_count; + } + } + + for (size_t i = 0; i < quantity; ++i) { + dest->GetCounter() += src[i]->GetCounter() * weights[i]; + } + + return 0; +} + +int CMSMergeParams(const CMSketch::MergeParams& params) { + return CMSketch::Merge(params.dest, params.num_keys, params.cms_array, params.weights); +} + +int CMSketch::checkOverflow(CMSketch* dest, size_t quantity, const std::vector<const CMSketch*>& src, Review Comment: Also using `StatusOr` here? ########## src/types/cms.h: ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#pragma once + +#include <memory> +#include <vector> + +#include "server/redis_reply.h" +#include "xxhash.h" + +class CMSketch { + public: + explicit CMSketch(uint32_t width, uint32_t depth, uint64_t counter, std::vector<uint32_t> array = {}) Review Comment: Please not use default argument, which might being misused when we program later ########## src/types/cms.cc: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "cms.h" + +#include <algorithm> +#include <cmath> +#include <cstdint> +#include <vector> + +CMSketch::CMSketchDimensions CMSketch::CMSDimFromProb(double error, double delta) { + CMSketchDimensions dims; + dims.width = std::ceil(2 / error); + dims.depth = std::ceil(std::log10(delta) / std::log10(0.5)); + return dims; +} + +size_t CMSketch::IncrBy(std::string_view item, uint32_t value) { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + size_t loc = GetLocation(hash, i); + if (array_[loc] > UINT32_MAX - value) { + array_[loc] = UINT32_MAX; + } else { + array_[loc] += value; + } + min_count = std::min(min_count, static_cast<size_t>(array_[loc])); + } + counter_ += value; + return min_count; +} + +size_t CMSketch::Query(std::string_view item) const { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + min_count = std::min(min_count, static_cast<size_t>(array_[GetLocation(hash, i)])); + } + return min_count; +} + +int CMSketch::Merge(CMSketch* dest, size_t quantity, const std::vector<const CMSketch*>& src, + const std::vector<long long>& weights) { + if (checkOverflow(dest, quantity, src, weights) != 0) { + return -1; + } Review Comment: Should we use `StatusOr<>` rather than `-1` code here? ########## src/types/cms.cc: ########## @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#include "cms.h" + +#include <algorithm> +#include <cmath> +#include <cstdint> +#include <vector> + +CMSketch::CMSketchDimensions CMSketch::CMSDimFromProb(double error, double delta) { + CMSketchDimensions dims; + dims.width = std::ceil(2 / error); + dims.depth = std::ceil(std::log10(delta) / std::log10(0.5)); + return dims; +} + +size_t CMSketch::IncrBy(std::string_view item, uint32_t value) { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + size_t loc = GetLocation(hash, i); + if (array_[loc] > UINT32_MAX - value) { + array_[loc] = UINT32_MAX; + } else { + array_[loc] += value; + } + min_count = std::min(min_count, static_cast<size_t>(array_[loc])); + } + counter_ += value; + return min_count; +} + +size_t CMSketch::Query(std::string_view item) const { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = XXH32(item.data(), static_cast<int>(item.size()), i); + min_count = std::min(min_count, static_cast<size_t>(array_[GetLocation(hash, i)])); + } + return min_count; +} + +int CMSketch::Merge(CMSketch* dest, size_t quantity, const std::vector<const CMSketch*>& src, + const std::vector<long long>& weights) { + if (checkOverflow(dest, quantity, src, weights) != 0) { + return -1; + } + + for (size_t i = 0; i < dest->GetDepth(); ++i) { + for (size_t j = 0; j < dest->GetWidth(); ++j) { + int64_t item_count = 0; + for (size_t k = 0; k < quantity; ++k) { + item_count += static_cast<int64_t>(src[k]->array_[(i * dest->GetWidth()) + j]) * weights[k]; Review Comment: `GetLocation * weights[k]`? ########## src/types/redis_cms.h: ########## @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +#pragma once + +#include "cms.h" +#include "storage/redis_db.h" +#include "storage/redis_metadata.h" + +namespace redis { + +class CMS : public Database { + public: + explicit CMS(engine::Storage *storage, const std::string &ns) : Database(storage, ns) {} + + rocksdb::Status IncrBy(engine::Context &ctx, const Slice &user_key, + const std::unordered_map<std::string, uint64_t> &elements); + rocksdb::Status Info(engine::Context &ctx, const Slice &user_key, CMSketch::CMSInfo *ret); + rocksdb::Status InitByDim(engine::Context &ctx, const Slice &user_key, uint32_t width, uint32_t depth); + rocksdb::Status InitByProb(engine::Context &ctx, const Slice &user_key, double error, double delta); + rocksdb::Status Query(engine::Context &ctx, const Slice &user_key, const std::vector<std::string> &elements, + std::vector<uint32_t> &counters); + + private: + [[nodiscard]] rocksdb::Status GetMetadata(engine::Context &ctx, const Slice &ns_key, + CountMinSketchMetadata *metadata); + + // TODO (jonathanc-n) Review Comment: ```suggestion // TODO (jonathanc-n) // [[nodiscard]] rocksdb::Status mergeUserKeys(engine::Context &ctx, const std::vector<Slice> &user_keys, // std::vector<std::string> *register_segments); ``` Can you do like this? -- 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]
