]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/duplicate_detector.h
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / duplicate_detector.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #pragma once
7
8 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
10 #endif
11
12 #include <inttypes.h>
13
14 #include "util/set_comparator.h"
15
16 namespace rocksdb {
17 // During recovery if the memtable is flushed we cannot rely on its help on
18 // duplicate key detection and as key insert will not be attempted. This class
19 // will be used as a emulator of memtable to tell if insertion of a key/seq
20 // would have resulted in duplication.
21 class DuplicateDetector {
22 public:
23 explicit DuplicateDetector(DBImpl* db) : db_(db) {}
24 bool IsDuplicateKeySeq(uint32_t cf, const Slice& key, SequenceNumber seq) {
25 assert(seq >= batch_seq_);
26 if (batch_seq_ != seq) { // it is a new batch
27 keys_.clear();
28 }
29 batch_seq_ = seq;
30 CFKeys& cf_keys = keys_[cf];
31 if (cf_keys.size() == 0) { // just inserted
32 InitWithComp(cf);
33 }
34 auto it = cf_keys.insert(key);
35 if (it.second == false) { // second is false if a element already existed.
36 keys_.clear();
37 InitWithComp(cf);
38 keys_[cf].insert(key);
39 return true;
40 }
41 return false;
42 }
43
44 private:
45 SequenceNumber batch_seq_ = 0;
46 DBImpl* db_;
47 using CFKeys = std::set<Slice, SetComparator>;
48 std::map<uint32_t, CFKeys> keys_;
49 void InitWithComp(const uint32_t cf) {
50 auto h = db_->GetColumnFamilyHandle(cf);
51 if (!h) {
52 // TODO(myabandeh): This is not a concern in MyRocks as drop cf is not
53 // implemented yet. When it does, we should return proper error instead
54 // of throwing exception.
55 ROCKS_LOG_FATAL(
56 db_->immutable_db_options().info_log,
57 "Recovering an entry from the dropped column family %" PRIu32
58 ". WAL must must have been emptied before dropping the column "
59 "family", cf);
60 #ifndef ROCKSDB_LITE
61 throw std::runtime_error(
62 "Recovering an entry from a dropped column family. "
63 "WAL must must have been flushed before dropping the column "
64 "family");
65 #endif
66 return;
67 }
68 auto cmp = h->GetComparator();
69 keys_[cf] = CFKeys(SetComparator(cmp));
70 }
71 };
72 } // namespace rocksdb