PokIsemaine commented on code in PR #2332: URL: https://github.com/apache/kvrocks/pull/2332#discussion_r1713885758
########## 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, Review Comment: I might need to rethink this, as I may have been disrupted by an interim test. **Previous thoughts:** We can perceive operations on the database from two places: the database that specifies the `snapshot and` the current `WriteBatchWithIndex`. Assuming the following operations now occur: ``` OP1: snapshot1(ctx) => snapshot2 PUT(k1,a), PUT(k2,b), PUT(k3,c), PUT(k4,d) OP2: snapshot1(ctx) DeleteRange(k2, k3) ``` `snapshot1` cannot perceive the existence of keys k1-k4 in the `snapshot2` database (after OP1 is completed), and thus it cannot iterate over these keys via an iterator to convert `DeleteRange` into individual `Delete`. On the other hand, the `WriteBatchWithIndex` is still incomplete; `batch_indexer` is there to construct the `WriteBatchWithIndex`, but now it depends on it as well. **Current thoughts:** We don't need a complete `WriteBatchWithIndex`, the `dest_batch_` should already have the `PUT` operations that can be detected. I'll give it a try later. -- 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]
