mapleFU commented on code in PR #2524: URL: https://github.com/apache/kvrocks/pull/2524#discussion_r1750289859
########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; + } + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + for (size_t i = 2; i < args_.size(); i += 2) { + std::string key = args_[i]; + uint64_t value = 0; + try { + value = std::stoull(args_[i + 1]); Review Comment: we have `ParseInt` in `parse_util.h`? ########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; Review Comment: ? ########## 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)); Review Comment: Should we also try to adding a verify to check the width and depth not too large or too small ? ########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; + } + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + for (size_t i = 2; i < args_.size(); i += 2) { + std::string key = args_[i]; + uint64_t value = 0; + try { + value = std::stoull(args_[i + 1]); + } catch (const std::exception &e) { + return Status::InvalidArgument; + } + elements[key] = value; + } + + s = cms.IncrBy(ctx, args_[1], elements); + if (!s.ok()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::SimpleString("OK"); + return Status::OK(); + } +}; + +/// CMS.INFO key +class CommandCMSInfo final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + std::vector<uint64_t> ret{}; + + s = cms.Info(ctx, args_[1], &ret); + + if (s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + if (!s.ok() && !s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::Array({redis::BulkString("width"), redis::Integer(ret[0]), redis::BulkString("depth"), + redis::Integer(ret[1]), redis::BulkString("count"), redis::Integer(ret[2])}); + + return Status::OK(); + } +}; + +/// CMS.INITBYDIM key width depth +class CommandCMSInitByDim final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + uint64_t width = std::stoull(args_[2]); + uint64_t depth = std::stoull(args_[3]); Review Comment: ParseInt ########## src/types/redis_cms.cc: ########## @@ -0,0 +1,167 @@ +/* + * 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 "redis_cms.h" + +#include <stdint.h> + +#include "cms.h" + +namespace redis { + +rocksdb::Status CMS::GetMetadata(engine::Context &ctx, const Slice &ns_key, + CountMinSketchMetadata *metadata) { + return Database::GetMetadata(ctx, {kRedisCountMinSketch}, ns_key, metadata); +} + +rocksdb::Status CMS::IncrBy(engine::Context &ctx, const Slice &user_key, const std::unordered_map<std::string, uint64_t> &elements) { + std::string ns_key = AppendNamespacePrefix(user_key); + + LockGuard guard(storage_->GetLockManager(), ns_key); + CountMinSketchMetadata metadata{}; Review Comment: If not exists, does this mean getting a `<0, 0, 0>` pair? What does it means? ########## src/storage/redis_metadata.h: ########## @@ -335,3 +336,15 @@ class HyperLogLogMetadata : public Metadata { EncodeType encode_type = EncodeType::DENSE; }; + +class CountMinSketchMetadata : public Metadata { + public: + uint32_t width; + uint32_t depth; + uint64_t counter; Review Comment: Should this have default value? ########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; + } + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + for (size_t i = 2; i < args_.size(); i += 2) { + std::string key = args_[i]; + uint64_t value = 0; + try { + value = std::stoull(args_[i + 1]); + } catch (const std::exception &e) { + return Status::InvalidArgument; + } + elements[key] = value; + } + + s = cms.IncrBy(ctx, args_[1], elements); + if (!s.ok()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::SimpleString("OK"); + return Status::OK(); + } +}; + +/// CMS.INFO key +class CommandCMSInfo final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + std::vector<uint64_t> ret{}; + + s = cms.Info(ctx, args_[1], &ret); + + if (s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + if (!s.ok() && !s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::Array({redis::BulkString("width"), redis::Integer(ret[0]), redis::BulkString("depth"), + redis::Integer(ret[1]), redis::BulkString("count"), redis::Integer(ret[2])}); Review Comment: Can you use a struct for `ret` rather than a `vector`? ########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; + } + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + for (size_t i = 2; i < args_.size(); i += 2) { + std::string key = args_[i]; + uint64_t value = 0; + try { + value = std::stoull(args_[i + 1]); + } catch (const std::exception &e) { + return Status::InvalidArgument; + } + elements[key] = value; + } + + s = cms.IncrBy(ctx, args_[1], elements); + if (!s.ok()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::SimpleString("OK"); + return Status::OK(); + } +}; + +/// CMS.INFO key +class CommandCMSInfo final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; Review Comment: never used? ########## 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, size_t value) { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = HllMurMurHash64A(item.data(), static_cast<int>(item.size()), i); + size_t loc = (hash % width_) + (i * width_); + array_[loc] += value; + if (array_[loc] < value) { + array_[loc] = UINT32_MAX; + } Review Comment: can this changed to check overflow? ########## src/commands/cmd_cms.cc: ########## @@ -0,0 +1,168 @@ +/* + * 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 <types/cms.h> +#include <types/redis_cms.h> + +#include "commander.h" +#include "commands/command_parser.h" +#include "server/redis_reply.h" +#include "server/server.h" + +namespace redis { + +/// CMS.INCRBY key item increment [item increment ...] +class CommandCMSIncrBy final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + if ((args_.size() - 2) % 2 != 0) { + return Status::RedisTryAgain; + } + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + for (size_t i = 2; i < args_.size(); i += 2) { + std::string key = args_[i]; + uint64_t value = 0; + try { + value = std::stoull(args_[i + 1]); + } catch (const std::exception &e) { + return Status::InvalidArgument; + } + elements[key] = value; + } + + s = cms.IncrBy(ctx, args_[1], elements); + if (!s.ok()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::SimpleString("OK"); + return Status::OK(); + } +}; + +/// CMS.INFO key +class CommandCMSInfo final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + std::unordered_map<std::string, uint64_t> elements; + std::vector<uint64_t> ret{}; + + s = cms.Info(ctx, args_[1], &ret); + + if (s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + if (!s.ok() && !s.IsNotFound()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::Array({redis::BulkString("width"), redis::Integer(ret[0]), redis::BulkString("depth"), + redis::Integer(ret[1]), redis::BulkString("count"), redis::Integer(ret[2])}); + + return Status::OK(); + } +}; + +/// CMS.INITBYDIM key width depth +class CommandCMSInitByDim final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + uint64_t width = std::stoull(args_[2]); + uint64_t depth = std::stoull(args_[3]); + + s = cms.InitByDim(ctx, args_[1], width, depth); + if (!s.ok()) { + return {Status::RedisExecErr, s.ToString()}; + } + + *output = redis::SimpleString("OK"); + return Status::OK(); + } +}; + +/// CMS.INITBYPROB key error probability +class CommandCMSInitByProb final : public Commander { + public: + Status Execute(Server *srv, Connection *conn, std::string *output) override { + redis::CMS cms(srv->storage, conn->GetNamespace()); + engine::Context ctx(srv->storage); + rocksdb::Status s; + double error = std::stod(args_[2]); + double delta = std::stod(args_[3]); Review Comment: ParseFloat ########## 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, size_t value) { Review Comment: why value is a size_t 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, size_t value) { + size_t min_count = std::numeric_limits<size_t>::max(); + + for (size_t i = 0; i < depth_; ++i) { + uint64_t hash = HllMurMurHash64A(item.data(), static_cast<int>(item.size()), i); + size_t loc = (hash % width_) + (i * width_); Review Comment: can you extract the loc to a helper function? -- 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]
