2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/version_edit.h"
|
|
|
|
|
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
// Tag numbers for serialized VersionEdit. These numbers are written to
|
|
|
|
// disk and should not be changed.
|
|
|
|
enum Tag {
|
2019-05-03 02:01:00 +08:00
|
|
|
kComparator = 1,
|
|
|
|
kLogNumber = 2,
|
|
|
|
kNextFileNumber = 3,
|
|
|
|
kLastSequence = 4,
|
|
|
|
kCompactPointer = 5,
|
|
|
|
kDeletedFile = 6,
|
|
|
|
kNewFile = 7,
|
2011-04-21 06:48:11 +08:00
|
|
|
// 8 was used for large value refs
|
2019-05-03 02:01:00 +08:00
|
|
|
kPrevLogNumber = 9
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void VersionEdit::Clear() {
|
|
|
|
comparator_.clear();
|
|
|
|
log_number_ = 0;
|
2011-04-13 03:38:58 +08:00
|
|
|
prev_log_number_ = 0;
|
2011-03-19 06:37:00 +08:00
|
|
|
last_sequence_ = 0;
|
|
|
|
next_file_number_ = 0;
|
|
|
|
has_comparator_ = false;
|
|
|
|
has_log_number_ = false;
|
2011-04-13 03:38:58 +08:00
|
|
|
has_prev_log_number_ = false;
|
2011-03-19 06:37:00 +08:00
|
|
|
has_next_file_number_ = false;
|
|
|
|
has_last_sequence_ = false;
|
|
|
|
deleted_files_.clear();
|
|
|
|
new_files_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionEdit::EncodeTo(std::string* dst) const {
|
|
|
|
if (has_comparator_) {
|
|
|
|
PutVarint32(dst, kComparator);
|
|
|
|
PutLengthPrefixedSlice(dst, comparator_);
|
|
|
|
}
|
|
|
|
if (has_log_number_) {
|
|
|
|
PutVarint32(dst, kLogNumber);
|
|
|
|
PutVarint64(dst, log_number_);
|
|
|
|
}
|
2011-04-13 03:38:58 +08:00
|
|
|
if (has_prev_log_number_) {
|
|
|
|
PutVarint32(dst, kPrevLogNumber);
|
|
|
|
PutVarint64(dst, prev_log_number_);
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
if (has_next_file_number_) {
|
|
|
|
PutVarint32(dst, kNextFileNumber);
|
|
|
|
PutVarint64(dst, next_file_number_);
|
|
|
|
}
|
|
|
|
if (has_last_sequence_) {
|
|
|
|
PutVarint32(dst, kLastSequence);
|
|
|
|
PutVarint64(dst, last_sequence_);
|
|
|
|
}
|
|
|
|
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < compact_pointers_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
PutVarint32(dst, kCompactPointer);
|
|
|
|
PutVarint32(dst, compact_pointers_[i].first); // level
|
|
|
|
PutLengthPrefixedSlice(dst, compact_pointers_[i].second.Encode());
|
|
|
|
}
|
|
|
|
|
2019-05-05 08:40:21 +08:00
|
|
|
for (const auto& deleted_file_kvp : deleted_files_) {
|
2011-03-19 06:37:00 +08:00
|
|
|
PutVarint32(dst, kDeletedFile);
|
2019-05-05 08:40:21 +08:00
|
|
|
PutVarint32(dst, deleted_file_kvp.first); // level
|
|
|
|
PutVarint64(dst, deleted_file_kvp.second); // file number
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < new_files_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
const FileMetaData& f = new_files_[i].second;
|
|
|
|
PutVarint32(dst, kNewFile);
|
|
|
|
PutVarint32(dst, new_files_[i].first); // level
|
|
|
|
PutVarint64(dst, f.number);
|
|
|
|
PutVarint64(dst, f.file_size);
|
|
|
|
PutLengthPrefixedSlice(dst, f.smallest.Encode());
|
|
|
|
PutLengthPrefixedSlice(dst, f.largest.Encode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetInternalKey(Slice* input, InternalKey* dst) {
|
|
|
|
Slice str;
|
|
|
|
if (GetLengthPrefixedSlice(input, &str)) {
|
2016-10-04 00:40:07 +08:00
|
|
|
return dst->DecodeFrom(str);
|
2011-03-19 06:37:00 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetLevel(Slice* input, int* level) {
|
|
|
|
uint32_t v;
|
2019-05-03 02:01:00 +08:00
|
|
|
if (GetVarint32(input, &v) && v < config::kNumLevels) {
|
2011-03-19 06:37:00 +08:00
|
|
|
*level = v;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status VersionEdit::DecodeFrom(const Slice& src) {
|
|
|
|
Clear();
|
|
|
|
Slice input = src;
|
2018-04-11 07:18:06 +08:00
|
|
|
const char* msg = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
uint32_t tag;
|
|
|
|
|
|
|
|
// Temporary storage for parsing
|
|
|
|
int level;
|
|
|
|
uint64_t number;
|
|
|
|
FileMetaData f;
|
|
|
|
Slice str;
|
|
|
|
InternalKey key;
|
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
while (msg == nullptr && GetVarint32(&input, &tag)) {
|
2011-03-19 06:37:00 +08:00
|
|
|
switch (tag) {
|
|
|
|
case kComparator:
|
|
|
|
if (GetLengthPrefixedSlice(&input, &str)) {
|
|
|
|
comparator_ = str.ToString();
|
|
|
|
has_comparator_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "comparator name";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kLogNumber:
|
|
|
|
if (GetVarint64(&input, &log_number_)) {
|
|
|
|
has_log_number_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "log number";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-04-13 03:38:58 +08:00
|
|
|
case kPrevLogNumber:
|
|
|
|
if (GetVarint64(&input, &prev_log_number_)) {
|
|
|
|
has_prev_log_number_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "previous log number";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
case kNextFileNumber:
|
|
|
|
if (GetVarint64(&input, &next_file_number_)) {
|
|
|
|
has_next_file_number_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "next file number";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kLastSequence:
|
|
|
|
if (GetVarint64(&input, &last_sequence_)) {
|
|
|
|
has_last_sequence_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "last sequence number";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCompactPointer:
|
2019-05-03 02:01:00 +08:00
|
|
|
if (GetLevel(&input, &level) && GetInternalKey(&input, &key)) {
|
2011-03-19 06:37:00 +08:00
|
|
|
compact_pointers_.push_back(std::make_pair(level, key));
|
|
|
|
} else {
|
|
|
|
msg = "compaction pointer";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kDeletedFile:
|
2019-05-03 02:01:00 +08:00
|
|
|
if (GetLevel(&input, &level) && GetVarint64(&input, &number)) {
|
2011-03-19 06:37:00 +08:00
|
|
|
deleted_files_.insert(std::make_pair(level, number));
|
|
|
|
} else {
|
|
|
|
msg = "deleted file";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNewFile:
|
2019-05-03 02:01:00 +08:00
|
|
|
if (GetLevel(&input, &level) && GetVarint64(&input, &f.number) &&
|
2011-03-19 06:37:00 +08:00
|
|
|
GetVarint64(&input, &f.file_size) &&
|
|
|
|
GetInternalKey(&input, &f.smallest) &&
|
|
|
|
GetInternalKey(&input, &f.largest)) {
|
|
|
|
new_files_.push_back(std::make_pair(level, f));
|
|
|
|
} else {
|
|
|
|
msg = "new-file entry";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msg = "unknown tag";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
if (msg == nullptr && !input.empty()) {
|
2011-03-19 06:37:00 +08:00
|
|
|
msg = "invalid tag";
|
|
|
|
}
|
|
|
|
|
|
|
|
Status result;
|
2018-04-11 07:18:06 +08:00
|
|
|
if (msg != nullptr) {
|
2011-03-19 06:37:00 +08:00
|
|
|
result = Status::Corruption("VersionEdit", msg);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VersionEdit::DebugString() const {
|
|
|
|
std::string r;
|
|
|
|
r.append("VersionEdit {");
|
|
|
|
if (has_comparator_) {
|
|
|
|
r.append("\n Comparator: ");
|
|
|
|
r.append(comparator_);
|
|
|
|
}
|
|
|
|
if (has_log_number_) {
|
|
|
|
r.append("\n LogNumber: ");
|
|
|
|
AppendNumberTo(&r, log_number_);
|
|
|
|
}
|
2011-04-13 03:38:58 +08:00
|
|
|
if (has_prev_log_number_) {
|
|
|
|
r.append("\n PrevLogNumber: ");
|
|
|
|
AppendNumberTo(&r, prev_log_number_);
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
if (has_next_file_number_) {
|
|
|
|
r.append("\n NextFile: ");
|
|
|
|
AppendNumberTo(&r, next_file_number_);
|
|
|
|
}
|
|
|
|
if (has_last_sequence_) {
|
|
|
|
r.append("\n LastSeq: ");
|
|
|
|
AppendNumberTo(&r, last_sequence_);
|
|
|
|
}
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < compact_pointers_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
r.append("\n CompactPointer: ");
|
|
|
|
AppendNumberTo(&r, compact_pointers_[i].first);
|
2011-10-06 07:30:28 +08:00
|
|
|
r.append(" ");
|
|
|
|
r.append(compact_pointers_[i].second.DebugString());
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2019-05-05 08:40:21 +08:00
|
|
|
for (const auto& deleted_files_kvp : deleted_files_) {
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
r.append("\n RemoveFile: ");
|
2019-05-05 08:40:21 +08:00
|
|
|
AppendNumberTo(&r, deleted_files_kvp.first);
|
2011-03-19 06:37:00 +08:00
|
|
|
r.append(" ");
|
2019-05-05 08:40:21 +08:00
|
|
|
AppendNumberTo(&r, deleted_files_kvp.second);
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
2011-04-21 06:48:11 +08:00
|
|
|
for (size_t i = 0; i < new_files_.size(); i++) {
|
2011-03-19 06:37:00 +08:00
|
|
|
const FileMetaData& f = new_files_[i].second;
|
|
|
|
r.append("\n AddFile: ");
|
|
|
|
AppendNumberTo(&r, new_files_[i].first);
|
|
|
|
r.append(" ");
|
|
|
|
AppendNumberTo(&r, f.number);
|
|
|
|
r.append(" ");
|
|
|
|
AppendNumberTo(&r, f.file_size);
|
2011-10-06 07:30:28 +08:00
|
|
|
r.append(" ");
|
|
|
|
r.append(f.smallest.DebugString());
|
|
|
|
r.append(" .. ");
|
|
|
|
r.append(f.largest.DebugString());
|
2011-03-19 06:37:00 +08:00
|
|
|
}
|
|
|
|
r.append("\n}\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|