diff --git a/db/kv_checksum.h b/db/kv_checksum.h index 4687972c3..196c6f7c9 100644 --- a/db/kv_checksum.h +++ b/db/kv_checksum.h @@ -11,13 +11,12 @@ // K = key // V = value // O = optype aka value type -// T = timestamp // S = seqno // C = CF ID // // Then, for example, a class that protects an entry consisting of key, value, -// optype, timestamp, and CF ID (i.e., a `WriteBatch` entry) would be named -// `ProtectionInfoKVOTC`. +// optype, and CF ID (i.e., a `WriteBatch` entry) would be named +// `ProtectionInfoKVOC`. // // The `ProtectionInfo.*` classes are templated on the integer type used to hold // the XOR of hashes for each field. Only unsigned integer types are supported, @@ -42,17 +41,17 @@ namespace ROCKSDB_NAMESPACE { template class ProtectionInfo; template -class ProtectionInfoKVOT; +class ProtectionInfoKVO; template -class ProtectionInfoKVOTC; +class ProtectionInfoKVOC; template -class ProtectionInfoKVOTS; +class ProtectionInfoKVOS; // Aliases for 64-bit protection infos. using ProtectionInfo64 = ProtectionInfo; -using ProtectionInfoKVOT64 = ProtectionInfoKVOT; -using ProtectionInfoKVOTC64 = ProtectionInfoKVOTC; -using ProtectionInfoKVOTS64 = ProtectionInfoKVOTS; +using ProtectionInfoKVO64 = ProtectionInfoKVO; +using ProtectionInfoKVOC64 = ProtectionInfoKVOC; +using ProtectionInfoKVOS64 = ProtectionInfoKVOS; template class ProtectionInfo { @@ -60,16 +59,16 @@ class ProtectionInfo { ProtectionInfo() = default; Status GetStatus() const; - ProtectionInfoKVOT ProtectKVOT(const Slice& key, const Slice& value, - ValueType op_type) const; - ProtectionInfoKVOT ProtectKVOT(const SliceParts& key, - const SliceParts& value, - ValueType op_type) const; + ProtectionInfoKVO ProtectKVO(const Slice& key, const Slice& value, + ValueType op_type) const; + ProtectionInfoKVO ProtectKVO(const SliceParts& key, + const SliceParts& value, + ValueType op_type) const; private: - friend class ProtectionInfoKVOT; - friend class ProtectionInfoKVOTS; - friend class ProtectionInfoKVOTC; + friend class ProtectionInfoKVO; + friend class ProtectionInfoKVOS; + friend class ProtectionInfoKVOC; // Each field is hashed with an independent value so we can catch fields being // swapped. Per the `NPHash64()` docs, using consecutive seeds is a pitfall, @@ -81,9 +80,8 @@ class ProtectionInfo { static const uint64_t kSeedK = 0; static const uint64_t kSeedV = 0xD28AAD72F49BD50B; static const uint64_t kSeedO = 0xA5155AE5E937AA16; - static const uint64_t kSeedT = 0x77A00858DDD37F21; - static const uint64_t kSeedS = 0x4A2AB5CBD26F542C; - static const uint64_t kSeedC = 0x1CB5633EC70B2937; + static const uint64_t kSeedS = 0x77A00858DDD37F21; + static const uint64_t kSeedC = 0x4A2AB5CBD26F542C; ProtectionInfo(T val) : val_(val) { static_assert(sizeof(ProtectionInfo) == sizeof(T), ""); @@ -96,32 +94,31 @@ class ProtectionInfo { }; template -class ProtectionInfoKVOT { +class ProtectionInfoKVO { public: - ProtectionInfoKVOT() = default; + ProtectionInfoKVO() = default; - ProtectionInfo StripKVOT(const Slice& key, const Slice& value, - ValueType op_type, const Slice& timestamp) const; - ProtectionInfo StripKVOT(const SliceParts& key, const SliceParts& value, - ValueType op_type, const Slice& timestamp) const; + ProtectionInfo StripKVO(const Slice& key, const Slice& value, + ValueType op_type) const; + ProtectionInfo StripKVO(const SliceParts& key, const SliceParts& value, + ValueType op_type) const; - ProtectionInfoKVOTC ProtectC(ColumnFamilyId column_family_id) const; - ProtectionInfoKVOTS ProtectS(SequenceNumber sequence_number) const; + ProtectionInfoKVOC ProtectC(ColumnFamilyId column_family_id) const; + ProtectionInfoKVOS ProtectS(SequenceNumber sequence_number) const; void UpdateK(const Slice& old_key, const Slice& new_key); void UpdateK(const SliceParts& old_key, const SliceParts& new_key); void UpdateV(const Slice& old_value, const Slice& new_value); void UpdateV(const SliceParts& old_value, const SliceParts& new_value); void UpdateO(ValueType old_op_type, ValueType new_op_type); - void UpdateT(const Slice& old_timestamp, const Slice& new_timestamp); private: friend class ProtectionInfo; - friend class ProtectionInfoKVOTS; - friend class ProtectionInfoKVOTC; + friend class ProtectionInfoKVOS; + friend class ProtectionInfoKVOC; - ProtectionInfoKVOT(T val) : info_(val) { - static_assert(sizeof(ProtectionInfoKVOT) == sizeof(T), ""); + ProtectionInfoKVO(T val) : info_(val) { + static_assert(sizeof(ProtectionInfoKVO) == sizeof(T), ""); } T GetVal() const { return info_.GetVal(); } @@ -131,85 +128,79 @@ class ProtectionInfoKVOT { }; template -class ProtectionInfoKVOTC { +class ProtectionInfoKVOC { public: - ProtectionInfoKVOTC() = default; + ProtectionInfoKVOC() = default; - ProtectionInfoKVOT StripC(ColumnFamilyId column_family_id) const; + ProtectionInfoKVO StripC(ColumnFamilyId column_family_id) const; void UpdateK(const Slice& old_key, const Slice& new_key) { - kvot_.UpdateK(old_key, new_key); + kvo_.UpdateK(old_key, new_key); } void UpdateK(const SliceParts& old_key, const SliceParts& new_key) { - kvot_.UpdateK(old_key, new_key); + kvo_.UpdateK(old_key, new_key); } void UpdateV(const Slice& old_value, const Slice& new_value) { - kvot_.UpdateV(old_value, new_value); + kvo_.UpdateV(old_value, new_value); } void UpdateV(const SliceParts& old_value, const SliceParts& new_value) { - kvot_.UpdateV(old_value, new_value); + kvo_.UpdateV(old_value, new_value); } void UpdateO(ValueType old_op_type, ValueType new_op_type) { - kvot_.UpdateO(old_op_type, new_op_type); - } - void UpdateT(const Slice& old_timestamp, const Slice& new_timestamp) { - kvot_.UpdateT(old_timestamp, new_timestamp); + kvo_.UpdateO(old_op_type, new_op_type); } void UpdateC(ColumnFamilyId old_column_family_id, ColumnFamilyId new_column_family_id); private: - friend class ProtectionInfoKVOT; + friend class ProtectionInfoKVO; - ProtectionInfoKVOTC(T val) : kvot_(val) { - static_assert(sizeof(ProtectionInfoKVOTC) == sizeof(T), ""); + ProtectionInfoKVOC(T val) : kvo_(val) { + static_assert(sizeof(ProtectionInfoKVOC) == sizeof(T), ""); } - T GetVal() const { return kvot_.GetVal(); } - void SetVal(T val) { kvot_.SetVal(val); } + T GetVal() const { return kvo_.GetVal(); } + void SetVal(T val) { kvo_.SetVal(val); } - ProtectionInfoKVOT kvot_; + ProtectionInfoKVO kvo_; }; template -class ProtectionInfoKVOTS { +class ProtectionInfoKVOS { public: - ProtectionInfoKVOTS() = default; + ProtectionInfoKVOS() = default; - ProtectionInfoKVOT StripS(SequenceNumber sequence_number) const; + ProtectionInfoKVO StripS(SequenceNumber sequence_number) const; void UpdateK(const Slice& old_key, const Slice& new_key) { - kvot_.UpdateK(old_key, new_key); + kvo_.UpdateK(old_key, new_key); } void UpdateK(const SliceParts& old_key, const SliceParts& new_key) { - kvot_.UpdateK(old_key, new_key); + kvo_.UpdateK(old_key, new_key); } void UpdateV(const Slice& old_value, const Slice& new_value) { - kvot_.UpdateV(old_value, new_value); + kvo_.UpdateV(old_value, new_value); } void UpdateV(const SliceParts& old_value, const SliceParts& new_value) { - kvot_.UpdateV(old_value, new_value); + kvo_.UpdateV(old_value, new_value); } void UpdateO(ValueType old_op_type, ValueType new_op_type) { - kvot_.UpdateO(old_op_type, new_op_type); - } - void UpdateT(const Slice& old_timestamp, const Slice& new_timestamp) { - kvot_.UpdateT(old_timestamp, new_timestamp); + kvo_.UpdateO(old_op_type, new_op_type); } void UpdateS(SequenceNumber old_sequence_number, SequenceNumber new_sequence_number); private: - friend class ProtectionInfoKVOT; + friend class ProtectionInfoKVO; - ProtectionInfoKVOTS(T val) : kvot_(val) { - static_assert(sizeof(ProtectionInfoKVOTS) == sizeof(T), ""); + ProtectionInfoKVOS(T val) : kvo_(val) { + static_assert(sizeof(ProtectionInfoKVOS) == sizeof(T), ""); } - T GetVal() const { return kvot_.GetVal(); } - void SetVal(T val) { kvot_.SetVal(val); } + T GetVal() const { return kvo_.GetVal(); } + void SetVal(T val) { kvo_.SetVal(val); } - ProtectionInfoKVOT kvot_; + ProtectionInfoKVO kvo_; }; template @@ -221,9 +212,9 @@ Status ProtectionInfo::GetStatus() const { } template -ProtectionInfoKVOT ProtectionInfo::ProtectKVOT(const Slice& key, - const Slice& value, - ValueType op_type) const { +ProtectionInfoKVO ProtectionInfo::ProtectKVO(const Slice& key, + const Slice& value, + ValueType op_type) const { T val = GetVal(); val = val ^ static_cast(GetSliceNPHash64(key, ProtectionInfo::kSeedK)); val = @@ -231,13 +222,13 @@ ProtectionInfoKVOT ProtectionInfo::ProtectKVOT(const Slice& key, val = val ^ static_cast(NPHash64(reinterpret_cast(&op_type), sizeof(op_type), ProtectionInfo::kSeedO)); - return ProtectionInfoKVOT(val); + return ProtectionInfoKVO(val); } template -ProtectionInfoKVOT ProtectionInfo::ProtectKVOT(const SliceParts& key, - const SliceParts& value, - ValueType op_type) const { +ProtectionInfoKVO ProtectionInfo::ProtectKVO(const SliceParts& key, + const SliceParts& value, + ValueType op_type) const { T val = GetVal(); val = val ^ static_cast(GetSlicePartsNPHash64(key, ProtectionInfo::kSeedK)); @@ -246,12 +237,11 @@ ProtectionInfoKVOT ProtectionInfo::ProtectKVOT(const SliceParts& key, val = val ^ static_cast(NPHash64(reinterpret_cast(&op_type), sizeof(op_type), ProtectionInfo::kSeedO)); - return ProtectionInfoKVOT(val); + return ProtectionInfoKVO(val); } template -void ProtectionInfoKVOT::UpdateK(const Slice& old_key, - const Slice& new_key) { +void ProtectionInfoKVO::UpdateK(const Slice& old_key, const Slice& new_key) { T val = GetVal(); val = val ^ static_cast(GetSliceNPHash64(old_key, ProtectionInfo::kSeedK)); @@ -261,8 +251,8 @@ void ProtectionInfoKVOT::UpdateK(const Slice& old_key, } template -void ProtectionInfoKVOT::UpdateK(const SliceParts& old_key, - const SliceParts& new_key) { +void ProtectionInfoKVO::UpdateK(const SliceParts& old_key, + const SliceParts& new_key) { T val = GetVal(); val = val ^ static_cast( GetSlicePartsNPHash64(old_key, ProtectionInfo::kSeedK)); @@ -272,8 +262,8 @@ void ProtectionInfoKVOT::UpdateK(const SliceParts& old_key, } template -void ProtectionInfoKVOT::UpdateV(const Slice& old_value, - const Slice& new_value) { +void ProtectionInfoKVO::UpdateV(const Slice& old_value, + const Slice& new_value) { T val = GetVal(); val = val ^ static_cast(GetSliceNPHash64(old_value, ProtectionInfo::kSeedV)); @@ -283,8 +273,8 @@ void ProtectionInfoKVOT::UpdateV(const Slice& old_value, } template -void ProtectionInfoKVOT::UpdateV(const SliceParts& old_value, - const SliceParts& new_value) { +void ProtectionInfoKVO::UpdateV(const SliceParts& old_value, + const SliceParts& new_value) { T val = GetVal(); val = val ^ static_cast( GetSlicePartsNPHash64(old_value, ProtectionInfo::kSeedV)); @@ -294,8 +284,8 @@ void ProtectionInfoKVOT::UpdateV(const SliceParts& old_value, } template -void ProtectionInfoKVOT::UpdateO(ValueType old_op_type, - ValueType new_op_type) { +void ProtectionInfoKVO::UpdateO(ValueType old_op_type, + ValueType new_op_type) { T val = GetVal(); val = val ^ static_cast(NPHash64(reinterpret_cast(&old_op_type), sizeof(old_op_type), @@ -307,20 +297,9 @@ void ProtectionInfoKVOT::UpdateO(ValueType old_op_type, } template -void ProtectionInfoKVOT::UpdateT(const Slice& old_timestamp, - const Slice& new_timestamp) { - T val = GetVal(); - val = val ^ static_cast( - GetSliceNPHash64(old_timestamp, ProtectionInfo::kSeedT)); - val = val ^ static_cast( - GetSliceNPHash64(new_timestamp, ProtectionInfo::kSeedT)); - SetVal(val); -} - -template -ProtectionInfo ProtectionInfoKVOT::StripKVOT( - const Slice& key, const Slice& value, ValueType op_type, - const Slice& timestamp) const { +ProtectionInfo ProtectionInfoKVO::StripKVO(const Slice& key, + const Slice& value, + ValueType op_type) const { T val = GetVal(); val = val ^ static_cast(GetSliceNPHash64(key, ProtectionInfo::kSeedK)); val = @@ -328,15 +307,13 @@ ProtectionInfo ProtectionInfoKVOT::StripKVOT( val = val ^ static_cast(NPHash64(reinterpret_cast(&op_type), sizeof(op_type), ProtectionInfo::kSeedO)); - val = val ^ - static_cast(GetSliceNPHash64(timestamp, ProtectionInfo::kSeedT)); return ProtectionInfo(val); } template -ProtectionInfo ProtectionInfoKVOT::StripKVOT( - const SliceParts& key, const SliceParts& value, ValueType op_type, - const Slice& timestamp) const { +ProtectionInfo ProtectionInfoKVO::StripKVO(const SliceParts& key, + const SliceParts& value, + ValueType op_type) const { T val = GetVal(); val = val ^ static_cast(GetSlicePartsNPHash64(key, ProtectionInfo::kSeedK)); @@ -345,34 +322,32 @@ ProtectionInfo ProtectionInfoKVOT::StripKVOT( val = val ^ static_cast(NPHash64(reinterpret_cast(&op_type), sizeof(op_type), ProtectionInfo::kSeedO)); - val = val ^ - static_cast(GetSliceNPHash64(timestamp, ProtectionInfo::kSeedT)); return ProtectionInfo(val); } template -ProtectionInfoKVOTC ProtectionInfoKVOT::ProtectC( +ProtectionInfoKVOC ProtectionInfoKVO::ProtectC( ColumnFamilyId column_family_id) const { T val = GetVal(); val = val ^ static_cast(NPHash64( reinterpret_cast(&column_family_id), sizeof(column_family_id), ProtectionInfo::kSeedC)); - return ProtectionInfoKVOTC(val); + return ProtectionInfoKVOC(val); } template -ProtectionInfoKVOT ProtectionInfoKVOTC::StripC( +ProtectionInfoKVO ProtectionInfoKVOC::StripC( ColumnFamilyId column_family_id) const { T val = GetVal(); val = val ^ static_cast(NPHash64( reinterpret_cast(&column_family_id), sizeof(column_family_id), ProtectionInfo::kSeedC)); - return ProtectionInfoKVOT(val); + return ProtectionInfoKVO(val); } template -void ProtectionInfoKVOTC::UpdateC(ColumnFamilyId old_column_family_id, - ColumnFamilyId new_column_family_id) { +void ProtectionInfoKVOC::UpdateC(ColumnFamilyId old_column_family_id, + ColumnFamilyId new_column_family_id) { T val = GetVal(); val = val ^ static_cast(NPHash64( reinterpret_cast(&old_column_family_id), @@ -384,28 +359,28 @@ void ProtectionInfoKVOTC::UpdateC(ColumnFamilyId old_column_family_id, } template -ProtectionInfoKVOTS ProtectionInfoKVOT::ProtectS( +ProtectionInfoKVOS ProtectionInfoKVO::ProtectS( SequenceNumber sequence_number) const { T val = GetVal(); val = val ^ static_cast(NPHash64(reinterpret_cast(&sequence_number), sizeof(sequence_number), ProtectionInfo::kSeedS)); - return ProtectionInfoKVOTS(val); + return ProtectionInfoKVOS(val); } template -ProtectionInfoKVOT ProtectionInfoKVOTS::StripS( +ProtectionInfoKVO ProtectionInfoKVOS::StripS( SequenceNumber sequence_number) const { T val = GetVal(); val = val ^ static_cast(NPHash64(reinterpret_cast(&sequence_number), sizeof(sequence_number), ProtectionInfo::kSeedS)); - return ProtectionInfoKVOT(val); + return ProtectionInfoKVO(val); } template -void ProtectionInfoKVOTS::UpdateS(SequenceNumber old_sequence_number, - SequenceNumber new_sequence_number) { +void ProtectionInfoKVOS::UpdateS(SequenceNumber old_sequence_number, + SequenceNumber new_sequence_number) { T val = GetVal(); val = val ^ static_cast(NPHash64( reinterpret_cast(&old_sequence_number), diff --git a/db/memtable.cc b/db/memtable.cc index 2b2598658..936883e24 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -487,7 +487,7 @@ MemTable::MemTableStats MemTable::ApproximateStats(const Slice& start_ikey, } Status MemTable::VerifyEncodedEntry(Slice encoded, - const ProtectionInfoKVOTS64& kv_prot_info) { + const ProtectionInfoKVOS64& kv_prot_info) { uint32_t ikey_len = 0; if (!GetVarint32(&encoded, &ikey_len)) { return Status::Corruption("Unable to parse internal key length"); @@ -500,12 +500,9 @@ Status MemTable::VerifyEncodedEntry(Slice encoded, return Status::Corruption("Internal key length too long"); } uint32_t value_len = 0; - const size_t key_without_ts_len = ikey_len - ts_sz - 8; - Slice key(encoded.data(), key_without_ts_len); - encoded.remove_prefix(key_without_ts_len); - - Slice timestamp(encoded.data(), ts_sz); - encoded.remove_prefix(ts_sz); + const size_t user_key_len = ikey_len - 8; + Slice key(encoded.data(), user_key_len); + encoded.remove_prefix(user_key_len); uint64_t packed = DecodeFixed64(encoded.data()); ValueType value_type = kMaxValue; @@ -525,14 +522,14 @@ Status MemTable::VerifyEncodedEntry(Slice encoded, Slice value(encoded.data(), value_len); return kv_prot_info.StripS(sequence_number) - .StripKVOT(key, value, value_type, timestamp) + .StripKVO(key, value, value_type) .GetStatus(); } Status MemTable::Add(SequenceNumber s, ValueType type, const Slice& key, /* user key */ const Slice& value, - const ProtectionInfoKVOTS64* kv_prot_info, + const ProtectionInfoKVOS64* kv_prot_info, bool allow_concurrent, MemTablePostProcessInfo* post_process_info, void** hint) { // Format of an entry is concatenation of: @@ -1036,7 +1033,7 @@ void MemTable::MultiGet(const ReadOptions& read_options, MultiGetRange* range, Status MemTable::Update(SequenceNumber seq, const Slice& key, const Slice& value, - const ProtectionInfoKVOTS64* kv_prot_info) { + const ProtectionInfoKVOS64* kv_prot_info) { LookupKey lkey(key, seq); Slice mem_key = lkey.memtable_key(); @@ -1081,7 +1078,7 @@ Status MemTable::Update(SequenceNumber seq, const Slice& key, VarintLength(value.size()) + value.size())); RecordTick(moptions_.statistics, NUMBER_KEYS_UPDATED); if (kv_prot_info != nullptr) { - ProtectionInfoKVOTS64 updated_kv_prot_info(*kv_prot_info); + ProtectionInfoKVOS64 updated_kv_prot_info(*kv_prot_info); // `seq` is swallowed and `existing_seq` prevails. updated_kv_prot_info.UpdateS(seq, existing_seq); Slice encoded(entry, p + value.size() - entry); @@ -1099,7 +1096,7 @@ Status MemTable::Update(SequenceNumber seq, const Slice& key, Status MemTable::UpdateCallback(SequenceNumber seq, const Slice& key, const Slice& delta, - const ProtectionInfoKVOTS64* kv_prot_info) { + const ProtectionInfoKVOS64* kv_prot_info) { LookupKey lkey(key, seq); Slice memkey = lkey.memtable_key(); @@ -1154,7 +1151,7 @@ Status MemTable::UpdateCallback(SequenceNumber seq, const Slice& key, RecordTick(moptions_.statistics, NUMBER_KEYS_UPDATED); UpdateFlushState(); if (kv_prot_info != nullptr) { - ProtectionInfoKVOTS64 updated_kv_prot_info(*kv_prot_info); + ProtectionInfoKVOS64 updated_kv_prot_info(*kv_prot_info); // `seq` is swallowed and `existing_seq` prevails. updated_kv_prot_info.UpdateS(seq, existing_seq); updated_kv_prot_info.UpdateV(delta, @@ -1166,7 +1163,7 @@ Status MemTable::UpdateCallback(SequenceNumber seq, const Slice& key, } else if (status == UpdateStatus::UPDATED) { Status s; if (kv_prot_info != nullptr) { - ProtectionInfoKVOTS64 updated_kv_prot_info(*kv_prot_info); + ProtectionInfoKVOS64 updated_kv_prot_info(*kv_prot_info); updated_kv_prot_info.UpdateV(delta, str_value); s = Add(seq, kTypeValue, key, Slice(str_value), &updated_kv_prot_info); diff --git a/db/memtable.h b/db/memtable.h index e6580379f..d93058066 100644 --- a/db/memtable.h +++ b/db/memtable.h @@ -199,7 +199,7 @@ class MemTable { const ReadOptions& read_options, SequenceNumber read_seq); Status VerifyEncodedEntry(Slice encoded, - const ProtectionInfoKVOTS64& kv_prot_info); + const ProtectionInfoKVOS64& kv_prot_info); // Add an entry into memtable that maps key to value at the // specified sequence number and with the specified type. @@ -212,7 +212,7 @@ class MemTable { // in the memtable and `MemTableRepFactory::CanHandleDuplicatedKey()` is true. // The next attempt should try a larger value for `seq`. Status Add(SequenceNumber seq, ValueType type, const Slice& key, - const Slice& value, const ProtectionInfoKVOTS64* kv_prot_info, + const Slice& value, const ProtectionInfoKVOS64* kv_prot_info, bool allow_concurrent = false, MemTablePostProcessInfo* post_process_info = nullptr, void** hint = nullptr); @@ -278,7 +278,7 @@ class MemTable { // REQUIRES: external synchronization to prevent simultaneous // operations on the same MemTable. Status Update(SequenceNumber seq, const Slice& key, const Slice& value, - const ProtectionInfoKVOTS64* kv_prot_info); + const ProtectionInfoKVOS64* kv_prot_info); // If `key` exists in current memtable with type `kTypeValue` and the existing // value is at least as large as the new value, updates it in-place. Otherwise @@ -296,7 +296,7 @@ class MemTable { // operations on the same MemTable. Status UpdateCallback(SequenceNumber seq, const Slice& key, const Slice& delta, - const ProtectionInfoKVOTS64* kv_prot_info); + const ProtectionInfoKVOS64* kv_prot_info); // Returns the number of successive merge entries starting from the newest // entry for the key up to the last non-merge entry or last entry for the diff --git a/db/write_batch.cc b/db/write_batch.cc index 43275cab9..3343cca06 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -217,11 +217,14 @@ class TimestampAssigner : public WriteBatch::Handler { // This key does not have timestamp, so skip. return; } - char* ptr = const_cast(key.data() + key.size() - ts_sz); if (prot_info_ != nullptr) { - Slice old_ts(ptr, ts_sz), new_ts(ts.data(), ts_sz); - prot_info_->entries_[idx_].UpdateT(old_ts, new_ts); + SliceParts old_key(&key, 1); + Slice key_no_ts(key.data(), key.size() - ts_sz); + std::array new_key_cmpts{{key_no_ts, ts}}; + SliceParts new_key(new_key_cmpts.data(), 2); + prot_info_->entries_[idx_].UpdateK(old_key, new_key); } + char* ptr = const_cast(key.data() + key.size() - ts_sz); memcpy(ptr, ts.data(), ts_sz); } @@ -808,10 +811,9 @@ Status WriteBatchInternal::Put(WriteBatch* b, uint32_t column_family_id, // (a missing/extra encoded CF ID would corrupt another field). It is // convenient to consolidate on `kTypeValue` here as that is what will be // inserted into memtable. - b->prot_info_->entries_.emplace_back( - ProtectionInfo64() - .ProtectKVOT(key, value, kTypeValue) - .ProtectC(column_family_id)); + b->prot_info_->entries_.emplace_back(ProtectionInfo64() + .ProtectKVO(key, value, kTypeValue) + .ProtectC(column_family_id)); } return save.commit(); } @@ -864,11 +866,10 @@ Status WriteBatchInternal::Put(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. - b->prot_info_->entries_.emplace_back( - ProtectionInfo64() - .ProtectKVOT(key, value, kTypeValue) - .ProtectC(column_family_id)); + // `ValueType` argument passed to `ProtectKVO()`. + b->prot_info_->entries_.emplace_back(ProtectionInfo64() + .ProtectKVO(key, value, kTypeValue) + .ProtectC(column_family_id)); } return save.commit(); } @@ -950,10 +951,10 @@ Status WriteBatchInternal::Delete(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(key, "" /* value */, kTypeDeletion) + .ProtectKVO(key, "" /* value */, kTypeDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -980,12 +981,12 @@ Status WriteBatchInternal::Delete(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(key, - SliceParts(nullptr /* _parts */, 0 /* _num_parts */), - kTypeDeletion) + .ProtectKVO(key, + SliceParts(nullptr /* _parts */, 0 /* _num_parts */), + kTypeDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -1014,10 +1015,10 @@ Status WriteBatchInternal::SingleDelete(WriteBatch* b, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(key, "" /* value */, kTypeSingleDeletion) + .ProtectKVO(key, "" /* value */, kTypeSingleDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -1046,13 +1047,13 @@ Status WriteBatchInternal::SingleDelete(WriteBatch* b, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(key, - SliceParts(nullptr /* _parts */, - 0 /* _num_parts */) /* value */, - kTypeSingleDeletion) + .ProtectKVO(key, + SliceParts(nullptr /* _parts */, + 0 /* _num_parts */) /* value */, + kTypeSingleDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -1082,11 +1083,11 @@ Status WriteBatchInternal::DeleteRange(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. // In `DeleteRange()`, the end key is treated as the value. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(begin_key, end_key, kTypeRangeDeletion) + .ProtectKVO(begin_key, end_key, kTypeRangeDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -1116,11 +1117,11 @@ Status WriteBatchInternal::DeleteRange(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. // In `DeleteRange()`, the end key is treated as the value. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(begin_key, end_key, kTypeRangeDeletion) + .ProtectKVO(begin_key, end_key, kTypeRangeDeletion) .ProtectC(column_family_id)); } return save.commit(); @@ -1157,11 +1158,10 @@ Status WriteBatchInternal::Merge(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. - b->prot_info_->entries_.emplace_back( - ProtectionInfo64() - .ProtectKVOT(key, value, kTypeMerge) - .ProtectC(column_family_id)); + // `ValueType` argument passed to `ProtectKVO()`. + b->prot_info_->entries_.emplace_back(ProtectionInfo64() + .ProtectKVO(key, value, kTypeMerge) + .ProtectC(column_family_id)); } return save.commit(); } @@ -1195,11 +1195,10 @@ Status WriteBatchInternal::Merge(WriteBatch* b, uint32_t column_family_id, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. - b->prot_info_->entries_.emplace_back( - ProtectionInfo64() - .ProtectKVOT(key, value, kTypeMerge) - .ProtectC(column_family_id)); + // `ValueType` argument passed to `ProtectKVO()`. + b->prot_info_->entries_.emplace_back(ProtectionInfo64() + .ProtectKVO(key, value, kTypeMerge) + .ProtectC(column_family_id)); } return save.commit(); } @@ -1228,10 +1227,10 @@ Status WriteBatchInternal::PutBlobIndex(WriteBatch* b, std::memory_order_relaxed); if (b->prot_info_ != nullptr) { // See comment in first `WriteBatchInternal::Put()` overload concerning the - // `ValueType` argument passed to `ProtectKVOT()`. + // `ValueType` argument passed to `ProtectKVO()`. b->prot_info_->entries_.emplace_back( ProtectionInfo64() - .ProtectKVOT(key, value, kTypeBlobIndex) + .ProtectKVO(key, value, kTypeBlobIndex) .ProtectC(column_family_id)); } return save.commit(); @@ -1380,8 +1379,8 @@ class MemTableInserter : public WriteBatch::Handler { (&duplicate_detector_)->IsDuplicateKeySeq(column_family_id, key, sequence_); } - const ProtectionInfoKVOTC64* NextProtectionInfo() { - const ProtectionInfoKVOTC64* res = nullptr; + const ProtectionInfoKVOC64* NextProtectionInfo() { + const ProtectionInfoKVOC64* res = nullptr; if (prot_info_ != nullptr) { assert(prot_info_idx_ < prot_info_->entries_.size()); res = &prot_info_->entries_[prot_info_idx_]; @@ -1534,10 +1533,10 @@ class MemTableInserter : public WriteBatch::Handler { Status PutCFImpl(uint32_t column_family_id, const Slice& key, const Slice& value, ValueType value_type, - const ProtectionInfoKVOTS64* kv_prot_info) { + const ProtectionInfoKVOS64* kv_prot_info) { // optimize for non-recovery mode if (UNLIKELY(write_after_commit_ && rebuilding_trx_ != nullptr)) { - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. return WriteBatchInternal::Put(rebuilding_trx_, column_family_id, key, value); // else insert the values to the memtable right away @@ -1549,7 +1548,7 @@ class MemTableInserter : public WriteBatch::Handler { assert(!write_after_commit_); // The CF is probably flushed and hence no need for insert but we still // need to keep track of the keys for upcoming rollback/commit. - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Put(rebuilding_trx_, column_family_id, key, value); if (ret_status.ok()) { @@ -1620,7 +1619,7 @@ class MemTableInserter : public WriteBatch::Handler { if (update_status == UpdateStatus::UPDATED_INPLACE) { assert(get_status.ok()); if (kv_prot_info != nullptr) { - ProtectionInfoKVOTS64 updated_kv_prot_info(*kv_prot_info); + ProtectionInfoKVOS64 updated_kv_prot_info(*kv_prot_info); updated_kv_prot_info.UpdateV(value, Slice(prev_buffer, prev_size)); // prev_value is updated in-place with final value. @@ -1637,7 +1636,7 @@ class MemTableInserter : public WriteBatch::Handler { } } else if (update_status == UpdateStatus::UPDATED) { if (kv_prot_info != nullptr) { - ProtectionInfoKVOTS64 updated_kv_prot_info(*kv_prot_info); + ProtectionInfoKVOS64 updated_kv_prot_info(*kv_prot_info); updated_kv_prot_info.UpdateV(value, merged_value); // merged_value contains the final value. ret_status = mem->Add(sequence_, value_type, key, @@ -1670,7 +1669,7 @@ class MemTableInserter : public WriteBatch::Handler { // away. So we only need to add to it when `ret_status.ok()`. if (UNLIKELY(ret_status.ok() && rebuilding_trx_ != nullptr)) { assert(!write_after_commit_); - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Put(rebuilding_trx_, column_family_id, key, value); } @@ -1693,7 +1692,7 @@ class MemTableInserter : public WriteBatch::Handler { Status DeleteImpl(uint32_t /*column_family_id*/, const Slice& key, const Slice& value, ValueType delete_type, - const ProtectionInfoKVOTS64* kv_prot_info) { + const ProtectionInfoKVOS64* kv_prot_info) { Status ret_status; MemTable* mem = cf_mems_->GetMemTable(); ret_status = @@ -1715,7 +1714,7 @@ class MemTableInserter : public WriteBatch::Handler { const auto* kv_prot_info = NextProtectionInfo(); // optimize for non-recovery mode if (UNLIKELY(write_after_commit_ && rebuilding_trx_ != nullptr)) { - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. return WriteBatchInternal::Delete(rebuilding_trx_, column_family_id, key); // else insert the values to the memtable right away } @@ -1726,7 +1725,7 @@ class MemTableInserter : public WriteBatch::Handler { assert(!write_after_commit_); // The CF is probably flushed and hence no need for insert but we still // need to keep track of the keys for upcoming rollback/commit. - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Delete(rebuilding_trx_, column_family_id, key); if (ret_status.ok()) { @@ -1762,7 +1761,7 @@ class MemTableInserter : public WriteBatch::Handler { // away. So we only need to add to it when `ret_status.ok()`. if (UNLIKELY(ret_status.ok() && rebuilding_trx_ != nullptr)) { assert(!write_after_commit_); - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Delete(rebuilding_trx_, column_family_id, key); } @@ -1773,7 +1772,7 @@ class MemTableInserter : public WriteBatch::Handler { const auto* kv_prot_info = NextProtectionInfo(); // optimize for non-recovery mode if (UNLIKELY(write_after_commit_ && rebuilding_trx_ != nullptr)) { - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. return WriteBatchInternal::SingleDelete(rebuilding_trx_, column_family_id, key); // else insert the values to the memtable right away @@ -1785,7 +1784,7 @@ class MemTableInserter : public WriteBatch::Handler { assert(!write_after_commit_); // The CF is probably flushed and hence no need for insert but we still // need to keep track of the keys for upcoming rollback/commit. - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::SingleDelete(rebuilding_trx_, column_family_id, key); if (ret_status.ok()) { @@ -1814,7 +1813,7 @@ class MemTableInserter : public WriteBatch::Handler { // away. So we only need to add to it when `ret_status.ok()`. if (UNLIKELY(ret_status.ok() && rebuilding_trx_ != nullptr)) { assert(!write_after_commit_); - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::SingleDelete(rebuilding_trx_, column_family_id, key); } @@ -1826,7 +1825,7 @@ class MemTableInserter : public WriteBatch::Handler { const auto* kv_prot_info = NextProtectionInfo(); // optimize for non-recovery mode if (UNLIKELY(write_after_commit_ && rebuilding_trx_ != nullptr)) { - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. return WriteBatchInternal::DeleteRange(rebuilding_trx_, column_family_id, begin_key, end_key); // else insert the values to the memtable right away @@ -1838,7 +1837,7 @@ class MemTableInserter : public WriteBatch::Handler { assert(!write_after_commit_); // The CF is probably flushed and hence no need for insert but we still // need to keep track of the keys for upcoming rollback/commit. - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::DeleteRange( rebuilding_trx_, column_family_id, begin_key, end_key); if (ret_status.ok()) { @@ -1897,7 +1896,7 @@ class MemTableInserter : public WriteBatch::Handler { // away. So we only need to add to it when `ret_status.ok()`. if (UNLIKELY(!ret_status.IsTryAgain() && rebuilding_trx_ != nullptr)) { assert(!write_after_commit_); - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::DeleteRange( rebuilding_trx_, column_family_id, begin_key, end_key); } @@ -1909,7 +1908,7 @@ class MemTableInserter : public WriteBatch::Handler { const auto* kv_prot_info = NextProtectionInfo(); // optimize for non-recovery mode if (UNLIKELY(write_after_commit_ && rebuilding_trx_ != nullptr)) { - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. return WriteBatchInternal::Merge(rebuilding_trx_, column_family_id, key, value); // else insert the values to the memtable right away @@ -1921,7 +1920,7 @@ class MemTableInserter : public WriteBatch::Handler { assert(!write_after_commit_); // The CF is probably flushed and hence no need for insert but we still // need to keep track of the keys for upcoming rollback/commit. - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Merge(rebuilding_trx_, column_family_id, key, value); if (ret_status.ok()) { @@ -2047,7 +2046,7 @@ class MemTableInserter : public WriteBatch::Handler { // away. So we only need to add to it when `ret_status.ok()`. if (UNLIKELY(ret_status.ok() && rebuilding_trx_ != nullptr)) { assert(!write_after_commit_); - // TODO(ajkr): propagate `ProtectionInfoKVOTS64`. + // TODO(ajkr): propagate `ProtectionInfoKVOS64`. ret_status = WriteBatchInternal::Merge(rebuilding_trx_, column_family_id, key, value); } diff --git a/db/write_batch_internal.h b/db/write_batch_internal.h index abb882585..8e1d0e3e2 100644 --- a/db/write_batch_internal.h +++ b/db/write_batch_internal.h @@ -66,7 +66,7 @@ class ColumnFamilyMemTablesDefault : public ColumnFamilyMemTables { struct WriteBatch::ProtectionInfo { // `WriteBatch` usually doesn't contain a huge number of keys so protecting // with a fixed, non-configurable eight bytes per key may work well enough. - autovector entries_; + autovector entries_; size_t GetBytesPerKey() const { return 8; } };