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).
8 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
14 #include "util/set_comparator.h"
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
{
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
30 CFKeys
& cf_keys
= keys_
[cf
];
31 if (cf_keys
.size() == 0) { // just inserted
34 auto it
= cf_keys
.insert(key
);
35 if (it
.second
== false) { // second is false if a element already existed.
38 keys_
[cf
].insert(key
);
45 SequenceNumber batch_seq_
= 0;
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
);
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.
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 "
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 "
68 auto cmp
= h
->GetComparator();
69 keys_
[cf
] = CFKeys(SetComparator(cmp
));
72 } // namespace rocksdb