diff --git a/utilities/write_batch_with_index/write_batch_with_index.cc b/utilities/write_batch_with_index/write_batch_with_index.cc index 6f26c1328..8f3163ab3 100644 --- a/utilities/write_batch_with_index/write_batch_with_index.cc +++ b/utilities/write_batch_with_index/write_batch_with_index.cc @@ -486,108 +486,108 @@ void WriteBatchWithIndex::Rep::AddNewEntry(uint32_t column_family_id) { ReadKeyFromWriteBatchEntry(&entry_ptr, &key, column_family_id != 0); assert(success); - auto* mem = arena.Allocate(sizeof(WriteBatchIndexEntry)); - auto* index_entry = - new (mem) WriteBatchIndexEntry(last_entry_offset, column_family_id, - key.data() - wb_data.data(), key.size()); - skip_list.Insert(index_entry); - } + auto* mem = arena.Allocate(sizeof(WriteBatchIndexEntry)); + auto* index_entry = + new (mem) WriteBatchIndexEntry(last_entry_offset, column_family_id, + key.data() - wb_data.data(), key.size()); + skip_list.Insert(index_entry); +} - void WriteBatchWithIndex::Rep::Clear() { - write_batch.Clear(); - ClearIndex(); - } +void WriteBatchWithIndex::Rep::Clear() { + write_batch.Clear(); + ClearIndex(); +} - void WriteBatchWithIndex::Rep::ClearIndex() { - skip_list.~WriteBatchEntrySkipList(); - arena.~Arena(); - new (&arena) Arena(); - new (&skip_list) WriteBatchEntrySkipList(comparator, &arena); - last_entry_offset = 0; - } +void WriteBatchWithIndex::Rep::ClearIndex() { + skip_list.~WriteBatchEntrySkipList(); + arena.~Arena(); + new (&arena) Arena(); + new (&skip_list) WriteBatchEntrySkipList(comparator, &arena); + last_entry_offset = 0; +} - Status WriteBatchWithIndex::Rep::ReBuildIndex() { - Status s; +Status WriteBatchWithIndex::Rep::ReBuildIndex() { + Status s; - ClearIndex(); - - if (write_batch.Count() == 0) { - // Nothing to re-index - return s; - } - - size_t offset = WriteBatchInternal::GetFirstOffset(&write_batch); - - Slice input(write_batch.Data()); - input.remove_prefix(offset); - - // Loop through all entries in Rep and add each one to the index - int found = 0; - while (s.ok() && !input.empty()) { - Slice key, value, blob, xid; - uint32_t column_family_id = 0; // default - char tag = 0; - - // set offset of current entry for call to AddNewEntry() - last_entry_offset = input.data() - write_batch.Data().data(); - - s = ReadRecordFromWriteBatch(&input, &tag, &column_family_id, &key, - &value, &blob, &xid); - if (!s.ok()) { - break; - } - - switch (tag) { - case kTypeColumnFamilyValue: - case kTypeValue: - case kTypeColumnFamilyDeletion: - case kTypeDeletion: - case kTypeColumnFamilySingleDeletion: - case kTypeSingleDeletion: - case kTypeColumnFamilyMerge: - case kTypeMerge: - found++; - if (!UpdateExistingEntryWithCfId(column_family_id, key)) { - AddNewEntry(column_family_id); - } - break; - case kTypeLogData: - case kTypeBeginPrepareXID: - case kTypeBeginPersistedPrepareXID: - case kTypeEndPrepareXID: - case kTypeCommitXID: - case kTypeRollbackXID: - case kTypeNoop: - break; - default: - return Status::Corruption("unknown WriteBatch tag in ReBuildIndex", - ToString(static_cast(tag))); - } - } - - if (s.ok() && found != write_batch.Count()) { - s = Status::Corruption("WriteBatch has wrong count"); - } + ClearIndex(); + if (write_batch.Count() == 0) { + // Nothing to re-index return s; } - WriteBatchWithIndex::WriteBatchWithIndex( - const Comparator* default_index_comparator, size_t reserved_bytes, - bool overwrite_key, size_t max_bytes) - : rep(new Rep(default_index_comparator, reserved_bytes, max_bytes, - overwrite_key)) {} + size_t offset = WriteBatchInternal::GetFirstOffset(&write_batch); - WriteBatchWithIndex::~WriteBatchWithIndex() {} + Slice input(write_batch.Data()); + input.remove_prefix(offset); - WriteBatch* WriteBatchWithIndex::GetWriteBatch() { return &rep->write_batch; } + // Loop through all entries in Rep and add each one to the index + int found = 0; + while (s.ok() && !input.empty()) { + Slice key, value, blob, xid; + uint32_t column_family_id = 0; // default + char tag = 0; - bool WriteBatchWithIndex::HasDuplicateKeys() { - return rep->obsolete_offsets.size() > 0; + // set offset of current entry for call to AddNewEntry() + last_entry_offset = input.data() - write_batch.Data().data(); + + s = ReadRecordFromWriteBatch(&input, &tag, &column_family_id, &key, + &value, &blob, &xid); + if (!s.ok()) { + break; + } + + switch (tag) { + case kTypeColumnFamilyValue: + case kTypeValue: + case kTypeColumnFamilyDeletion: + case kTypeDeletion: + case kTypeColumnFamilySingleDeletion: + case kTypeSingleDeletion: + case kTypeColumnFamilyMerge: + case kTypeMerge: + found++; + if (!UpdateExistingEntryWithCfId(column_family_id, key)) { + AddNewEntry(column_family_id); + } + break; + case kTypeLogData: + case kTypeBeginPrepareXID: + case kTypeBeginPersistedPrepareXID: + case kTypeEndPrepareXID: + case kTypeCommitXID: + case kTypeRollbackXID: + case kTypeNoop: + break; + default: + return Status::Corruption("unknown WriteBatch tag in ReBuildIndex", + ToString(static_cast(tag))); + } } - WBWIIterator* WriteBatchWithIndex::NewIterator() { - return new WBWIIteratorImpl(0, &(rep->skip_list), &rep->write_batch); + if (s.ok() && found != write_batch.Count()) { + s = Status::Corruption("WriteBatch has wrong count"); + } + + return s; +} + +WriteBatchWithIndex::WriteBatchWithIndex( + const Comparator* default_index_comparator, size_t reserved_bytes, + bool overwrite_key, size_t max_bytes) + : rep(new Rep(default_index_comparator, reserved_bytes, max_bytes, + overwrite_key)) {} + +WriteBatchWithIndex::~WriteBatchWithIndex() {} + +WriteBatch* WriteBatchWithIndex::GetWriteBatch() { return &rep->write_batch; } + +bool WriteBatchWithIndex::HasDuplicateKeys() { + return rep->obsolete_offsets.size() > 0; +} + +WBWIIterator* WriteBatchWithIndex::NewIterator() { + return new WBWIIteratorImpl(0, &(rep->skip_list), &rep->write_batch); } WBWIIterator* WriteBatchWithIndex::NewIterator(