mapleFU commented on code in PR #2332:
URL: https://github.com/apache/kvrocks/pull/2332#discussion_r1713908944


##########
src/storage/batch_indexer.h:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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 <rocksdb/db.h>
+#include <rocksdb/slice.h>
+#include <rocksdb/utilities/write_batch_with_index.h>
+
+#include <string>
+#include <vector>
+
+#include "storage.h"
+
+// WriteBatchIndexer traverses the operations in WriteBatch and append to the 
specified WriteBatchWithIndex
+class WriteBatchIndexer : public rocksdb::WriteBatch::Handler {
+ public:
+  explicit WriteBatchIndexer(engine::Storage* storage, 
rocksdb::WriteBatchWithIndex* dest_batch)
+      : storage_(storage), dest_batch_(dest_batch) {}
+  rocksdb::Status PutCF(uint32_t column_family_id, const rocksdb::Slice& key, 
const rocksdb::Slice& value) override {
+    return 
dest_batch_->Put(storage_->GetCFHandle(static_cast<ColumnFamilyID>(column_family_id)),
 key, value);
+  }
+
+  void Put(const rocksdb::Slice& key, const rocksdb::Slice& value) override { 
dest_batch_->Put(key, value); }
+
+  rocksdb::Status DeleteCF(uint32_t column_family_id, const rocksdb::Slice& 
key) override {
+    return 
dest_batch_->Delete(storage_->GetCFHandle(static_cast<ColumnFamilyID>(column_family_id)),
 key);
+  }
+
+  void Delete(const rocksdb::Slice& key) override { dest_batch_->Delete(key); }
+
+  rocksdb::Status SingleDeleteCF(uint32_t column_family_id, const 
rocksdb::Slice& key) override {
+    return 
dest_batch_->SingleDelete(storage_->GetCFHandle(static_cast<ColumnFamilyID>(column_family_id)),
 key);
+  }
+
+  void SingleDelete(const rocksdb::Slice& key) override { 
dest_batch_->SingleDelete(key); }
+
+  rocksdb::Status DeleteRangeCF(uint32_t column_family_id, const 
rocksdb::Slice& begin_key,
+                                const rocksdb::Slice& end_key) override {
+    // The latest snapshot (default ReadOptions) needs to be used here.
+    // If an old snapshot is used here and writebatchwithindex is still being 
built,
+    // the newly added keys after the snapshot may not be obtained.
+    rocksdb::ReadOptions read_options;
+    auto cf_handle = 
storage_->GetCFHandle(static_cast<ColumnFamilyID>(column_family_id));
+    std::unique_ptr<rocksdb::Iterator> 
it(storage_->GetDB()->NewIterator(read_options, cf_handle));

Review Comment:
   Both ok to me 🤔
   
   I need correct myself that "skipping iterating some key-values" is not 
right, the right wording might be "skipping lock some sst". Adding in 
`DefaultScanOptions(begin, end)` is also ok to me



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