jihuayu commented on code in PR #3481:
URL: https://github.com/apache/kvrocks/pull/3481#discussion_r3321661606


##########
src/types/cuckoo_filter_sub_filter.cc:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 "cuckoo_filter_sub_filter.h"
+
+#include "cuckoo_filter.h"
+
+namespace redis {
+
+CuckooSubFilter::CuckooSubFilter(engine::Storage *storage, engine::Context 
&ctx, const Slice &ns_key,
+                                 bool slot_id_encoded, uint64_t version, 
uint8_t bucket_size, uint32_t page_size,
+                                 uint16_t filter_index, uint32_t num_buckets)
+    : bucket_size_(bucket_size),
+      filter_index_(filter_index),
+      num_buckets_(num_buckets),
+      pages_(storage, ctx, ns_key, slot_id_encoded, version, bucket_size, 
page_size) {}
+
+rocksdb::Status CuckooSubFilter::TryInsert(uint64_t hash, uint8_t fingerprint, 
bool *inserted) {
+  *inserted = false;
+  uint32_t bucket1_idx = getPrimaryBucketIndex(hash);
+  uint32_t bucket2_idx = getSecondaryBucketIndex(hash, fingerprint);
+  auto s = pages_.PrefetchBuckets(filter_index_, num_buckets_, bucket1_idx, 
bucket2_idx);
+  if (!s.ok()) return s;
+
+  s = pages_.TryInsertInBucket(filter_index_, num_buckets_, bucket1_idx, 
fingerprint, inserted);
+  if (!s.ok() || *inserted || bucket1_idx == bucket2_idx) return s;
+
+  return pages_.TryInsertInBucket(filter_index_, num_buckets_, bucket2_idx, 
fingerprint, inserted);
+}
+
+rocksdb::Status CuckooSubFilter::TryInsertPrimaryBucket(uint64_t hash, uint8_t 
fingerprint, bool *inserted) {
+  return pages_.TryInsertInBucket(filter_index_, num_buckets_, 
getPrimaryBucketIndex(hash), fingerprint, inserted);
+}
+
+rocksdb::Status CuckooSubFilter::KickOutInsert(uint64_t hash, uint8_t 
fingerprint, uint16_t max_iterations,
+                                               bool *inserted) {
+  *inserted = false;
+
+  uint32_t current_bucket_idx = getPrimaryBucketIndex(hash);
+  uint8_t current_fp = fingerprint;
+  uint32_t victim_slot = 0;
+
+  for (uint16_t iteration = 0; iteration < max_iterations; ++iteration) {

Review Comment:
   Your design is excellent. I think it looks great.
   
   I do have one small suggestion, though: the name **Discard** feels a bit too 
generic. Would it be possible to rename it to **DiscardCachedPages**?
   



-- 
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]

Reply via email to