torwig commented on code in PR #2545:
URL: https://github.com/apache/kvrocks/pull/2545#discussion_r1766507453


##########
src/types/redis_cuckoo.cc:
##########
@@ -0,0 +1,487 @@
+/*
+ * 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_cuckoo.h"
+
+#include "rocksdb/status.h"
+#include "storage/redis_metadata.h"
+#include "types/cuckoo.h"
+
+namespace redis {
+
+rocksdb::Status CFilter::GetMetadata(engine::Context &ctx, const Slice 
&ns_key, CuckooFilterMetadata *metadata) {
+  return Database::GetMetadata(ctx, {kRedisCuckooFilter}, ns_key, metadata);
+}
+
+void CFilter::updateMetadata(CuckooFilter &cf, CuckooFilterMetadata *metadata) 
{
+  metadata->capacity = cf.GetCapacity();
+  metadata->num_buckets = cf.GetNumBuckets();
+  metadata->num_filters = cf.GetNumFilters();
+  metadata->num_items = cf.GetNumItems();
+  metadata->num_deletes = cf.GetNumDeletes();
+  cf.GetFilter(metadata->filters);
+}
+
+rocksdb::Status CFilter::Add(engine::Context &ctx, const Slice &user_key, 
const std::string &element, int *ret) {
+  std::string ns_key = AppendNamespacePrefix(user_key);
+
+  LockGuard guard(storage_->GetLockManager(), ns_key);
+  CuckooFilterMetadata metadata{};
+  rocksdb::Status s = GetMetadata(ctx, ns_key, &metadata);
+  if (!s.ok() && !s.IsNotFound()) {
+    return s;
+  }
+
+  if (s.IsNotFound()) {
+    metadata.capacity = 1024;
+    metadata.bucket_size = 4;
+    metadata.max_iterations = 500;
+    metadata.expansion = 2;
+    metadata.num_buckets = metadata.capacity / metadata.bucket_size;
+    metadata.num_filters = 1;
+    metadata.num_items = 0;
+    metadata.num_deletes = 0;
+    metadata.filters.clear();
+
+    SubCF initial_filter;
+    initial_filter.bucket_size = metadata.bucket_size;
+    initial_filter.num_buckets = metadata.num_buckets;
+    initial_filter.data.resize(metadata.num_buckets * metadata.bucket_size, 
CUCKOO_NULLFP);
+    metadata.filters.push_back(initial_filter);
+  }
+
+  auto batch = storage_->GetWriteBatchBase();
+  WriteBatchLogData log_data(kRedisCuckooFilter);
+  batch->PutLogData(log_data.Encode());
+
+  CuckooFilter cf(metadata.capacity, metadata.bucket_size, 
metadata.max_iterations, metadata.expansion,

Review Comment:
   Maybe you can pass just `metadata` instead of 9 fields as parameters?



##########
src/types/redis_cuckoo.cc:
##########
@@ -0,0 +1,487 @@
+/*
+ * 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_cuckoo.h"
+
+#include "rocksdb/status.h"
+#include "storage/redis_metadata.h"
+#include "types/cuckoo.h"
+
+namespace redis {
+
+rocksdb::Status CFilter::GetMetadata(engine::Context &ctx, const Slice 
&ns_key, CuckooFilterMetadata *metadata) {
+  return Database::GetMetadata(ctx, {kRedisCuckooFilter}, ns_key, metadata);
+}
+
+void CFilter::updateMetadata(CuckooFilter &cf, CuckooFilterMetadata *metadata) 
{
+  metadata->capacity = cf.GetCapacity();
+  metadata->num_buckets = cf.GetNumBuckets();
+  metadata->num_filters = cf.GetNumFilters();
+  metadata->num_items = cf.GetNumItems();
+  metadata->num_deletes = cf.GetNumDeletes();
+  cf.GetFilter(metadata->filters);
+}
+
+rocksdb::Status CFilter::Add(engine::Context &ctx, const Slice &user_key, 
const std::string &element, int *ret) {
+  std::string ns_key = AppendNamespacePrefix(user_key);
+
+  LockGuard guard(storage_->GetLockManager(), ns_key);
+  CuckooFilterMetadata metadata{};
+  rocksdb::Status s = GetMetadata(ctx, ns_key, &metadata);
+  if (!s.ok() && !s.IsNotFound()) {
+    return s;
+  }
+
+  if (s.IsNotFound()) {
+    metadata.capacity = 1024;

Review Comment:
   Perhaps, you can extract this logic into a separate function with a 
descriptive name (e.g. `createDefaultCuckooFilterMetadata` or any other you 
find appropriate). I see you repeat this piece of code so it's an ideal 
candidate for a separate 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: issues-unsubscr...@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to