2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 23:37:00 +01: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"
|
2013-12-12 02:46:26 +01:00
|
|
|
#include "rocksdb/slice.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
// Tag numbers for serialized VersionEdit. These numbers are written to
|
|
|
|
// disk and should not be changed.
|
|
|
|
enum Tag {
|
|
|
|
kComparator = 1,
|
|
|
|
kLogNumber = 2,
|
|
|
|
kNextFileNumber = 3,
|
|
|
|
kLastSequence = 4,
|
|
|
|
kCompactPointer = 5,
|
|
|
|
kDeletedFile = 6,
|
|
|
|
kNewFile = 7,
|
2011-04-21 00:48:11 +02:00
|
|
|
// 8 was used for large value refs
|
2013-06-14 07:09:08 +02:00
|
|
|
kPrevLogNumber = 9,
|
|
|
|
|
|
|
|
// these are new formats divergent from open source leveldb
|
2013-12-12 02:46:26 +01:00
|
|
|
kNewFile2 = 100, // store smallest & largest seqno
|
|
|
|
|
2014-01-30 00:26:43 +01:00
|
|
|
kColumnFamily = 200, // specify column family for version edit
|
2013-12-12 02:46:26 +01:00
|
|
|
kColumnFamilyAdd = 201,
|
|
|
|
kColumnFamilyDrop = 202,
|
2014-03-05 21:13:44 +01:00
|
|
|
kMaxColumnFamily = 203,
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void VersionEdit::Clear() {
|
|
|
|
comparator_.clear();
|
2014-01-15 00:27:09 +01:00
|
|
|
max_level_ = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
log_number_ = 0;
|
2011-04-12 21:38:58 +02:00
|
|
|
prev_log_number_ = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
last_sequence_ = 0;
|
|
|
|
next_file_number_ = 0;
|
2014-03-05 21:13:44 +01:00
|
|
|
max_column_family_ = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
has_comparator_ = false;
|
|
|
|
has_log_number_ = false;
|
2011-04-12 21:38:58 +02:00
|
|
|
has_prev_log_number_ = false;
|
2011-03-18 23:37:00 +01:00
|
|
|
has_next_file_number_ = false;
|
|
|
|
has_last_sequence_ = false;
|
2014-03-05 21:13:44 +01:00
|
|
|
has_max_column_family_ = false;
|
2011-03-18 23:37:00 +01:00
|
|
|
deleted_files_.clear();
|
|
|
|
new_files_.clear();
|
2013-12-12 02:46:26 +01:00
|
|
|
column_family_ = 0;
|
|
|
|
is_column_family_add_ = 0;
|
|
|
|
is_column_family_drop_ = 0;
|
|
|
|
column_family_name_.clear();
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12 21:38:58 +02:00
|
|
|
if (has_prev_log_number_) {
|
|
|
|
PutVarint32(dst, kPrevLogNumber);
|
|
|
|
PutVarint64(dst, prev_log_number_);
|
|
|
|
}
|
2011-03-18 23:37:00 +01: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_);
|
|
|
|
}
|
2014-03-05 21:13:44 +01:00
|
|
|
if (has_max_column_family_) {
|
|
|
|
PutVarint32(dst, kMaxColumnFamily);
|
|
|
|
PutVarint32(dst, max_column_family_);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-12-31 03:33:57 +01:00
|
|
|
for (const auto& deleted : deleted_files_) {
|
2011-03-18 23:37:00 +01:00
|
|
|
PutVarint32(dst, kDeletedFile);
|
2013-12-31 03:33:57 +01:00
|
|
|
PutVarint32(dst, deleted.first /* level */);
|
|
|
|
PutVarint64(dst, deleted.second /* file number */);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2011-04-21 00:48:11 +02:00
|
|
|
for (size_t i = 0; i < new_files_.size(); i++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
const FileMetaData& f = new_files_[i].second;
|
2013-06-14 07:09:08 +02:00
|
|
|
PutVarint32(dst, kNewFile2);
|
2011-03-18 23:37:00 +01:00
|
|
|
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());
|
2013-06-14 07:09:08 +02:00
|
|
|
PutVarint64(dst, f.smallest_seqno);
|
|
|
|
PutVarint64(dst, f.largest_seqno);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2013-12-12 02:46:26 +01:00
|
|
|
|
|
|
|
// 0 is default and does not need to be explicitly written
|
|
|
|
if (column_family_ != 0) {
|
|
|
|
PutVarint32(dst, kColumnFamily);
|
|
|
|
PutVarint32(dst, column_family_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_column_family_add_) {
|
|
|
|
PutVarint32(dst, kColumnFamilyAdd);
|
|
|
|
PutLengthPrefixedSlice(dst, Slice(column_family_name_));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_column_family_drop_) {
|
|
|
|
PutVarint32(dst, kColumnFamilyDrop);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetInternalKey(Slice* input, InternalKey* dst) {
|
|
|
|
Slice str;
|
|
|
|
if (GetLengthPrefixedSlice(input, &str)) {
|
|
|
|
dst->DecodeFrom(str);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 19:54:26 +01:00
|
|
|
bool VersionEdit::GetLevel(Slice* input, int* level, const char** msg) {
|
2011-03-18 23:37:00 +01:00
|
|
|
uint32_t v;
|
2014-01-15 00:27:09 +01:00
|
|
|
if (GetVarint32(input, &v)) {
|
2011-03-18 23:37:00 +01:00
|
|
|
*level = v;
|
2014-01-15 00:27:09 +01:00
|
|
|
if (max_level_ < *level) {
|
|
|
|
max_level_ = *level;
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status VersionEdit::DecodeFrom(const Slice& src) {
|
|
|
|
Clear();
|
|
|
|
Slice input = src;
|
2013-03-01 03:04:58 +01:00
|
|
|
const char* msg = nullptr;
|
2011-03-18 23:37:00 +01:00
|
|
|
uint32_t tag;
|
|
|
|
|
|
|
|
// Temporary storage for parsing
|
|
|
|
int level;
|
|
|
|
uint64_t number;
|
|
|
|
FileMetaData f;
|
|
|
|
Slice str;
|
|
|
|
InternalKey key;
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
while (msg == nullptr && GetVarint32(&input, &tag)) {
|
2011-03-18 23:37:00 +01: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-12 21:38:58 +02:00
|
|
|
case kPrevLogNumber:
|
|
|
|
if (GetVarint64(&input, &prev_log_number_)) {
|
|
|
|
has_prev_log_number_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "previous log number";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-03-18 23:37:00 +01: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;
|
|
|
|
|
2014-03-05 21:13:44 +01:00
|
|
|
case kMaxColumnFamily:
|
|
|
|
if (GetVarint32(&input, &max_column_family_)) {
|
|
|
|
has_max_column_family_ = true;
|
|
|
|
} else {
|
|
|
|
msg = "max column family";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
case kCompactPointer:
|
2013-01-24 19:54:26 +01:00
|
|
|
if (GetLevel(&input, &level, &msg) &&
|
2011-03-18 23:37:00 +01:00
|
|
|
GetInternalKey(&input, &key)) {
|
2014-01-16 23:06:53 +01:00
|
|
|
// we don't use compact pointers anymore,
|
|
|
|
// but we should not fail if they are still
|
|
|
|
// in manifest
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
2013-01-24 19:54:26 +01:00
|
|
|
if (!msg) {
|
|
|
|
msg = "compaction pointer";
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kDeletedFile:
|
2013-01-24 19:54:26 +01:00
|
|
|
if (GetLevel(&input, &level, &msg) &&
|
2011-03-18 23:37:00 +01:00
|
|
|
GetVarint64(&input, &number)) {
|
|
|
|
deleted_files_.insert(std::make_pair(level, number));
|
|
|
|
} else {
|
2013-01-24 19:54:26 +01:00
|
|
|
if (!msg) {
|
|
|
|
msg = "deleted file";
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNewFile:
|
2013-01-24 19:54:26 +01:00
|
|
|
if (GetLevel(&input, &level, &msg) &&
|
2011-03-18 23:37:00 +01:00
|
|
|
GetVarint64(&input, &f.number) &&
|
|
|
|
GetVarint64(&input, &f.file_size) &&
|
|
|
|
GetInternalKey(&input, &f.smallest) &&
|
|
|
|
GetInternalKey(&input, &f.largest)) {
|
|
|
|
new_files_.push_back(std::make_pair(level, f));
|
|
|
|
} else {
|
2013-01-24 19:54:26 +01:00
|
|
|
if (!msg) {
|
|
|
|
msg = "new-file entry";
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-06-14 07:09:08 +02:00
|
|
|
case kNewFile2:
|
|
|
|
if (GetLevel(&input, &level, &msg) &&
|
|
|
|
GetVarint64(&input, &f.number) &&
|
|
|
|
GetVarint64(&input, &f.file_size) &&
|
|
|
|
GetInternalKey(&input, &f.smallest) &&
|
|
|
|
GetInternalKey(&input, &f.largest) &&
|
|
|
|
GetVarint64(&input, &f.smallest_seqno) &&
|
|
|
|
GetVarint64(&input, &f.largest_seqno) ) {
|
|
|
|
new_files_.push_back(std::make_pair(level, f));
|
|
|
|
} else {
|
|
|
|
if (!msg) {
|
|
|
|
msg = "new-file2 entry";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-12 02:46:26 +01:00
|
|
|
case kColumnFamily:
|
|
|
|
if (!GetVarint32(&input, &column_family_)) {
|
|
|
|
if (!msg) {
|
|
|
|
msg = "set column family id";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kColumnFamilyAdd:
|
|
|
|
if (GetLengthPrefixedSlice(&input, &str)) {
|
|
|
|
is_column_family_add_ = true;
|
|
|
|
column_family_name_ = str.ToString();
|
|
|
|
} else {
|
|
|
|
if (!msg) {
|
|
|
|
msg = "column family add";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kColumnFamilyDrop:
|
|
|
|
is_column_family_drop_ = true;
|
|
|
|
break;
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
default:
|
|
|
|
msg = "unknown tag";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:04:58 +01:00
|
|
|
if (msg == nullptr && !input.empty()) {
|
2011-03-18 23:37:00 +01:00
|
|
|
msg = "invalid tag";
|
|
|
|
}
|
|
|
|
|
|
|
|
Status result;
|
2013-03-01 03:04:58 +01:00
|
|
|
if (msg != nullptr) {
|
2011-03-18 23:37:00 +01:00
|
|
|
result = Status::Corruption("VersionEdit", msg);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-08-09 00:51:16 +02:00
|
|
|
std::string VersionEdit::DebugString(bool hex_key) const {
|
2011-03-18 23:37:00 +01:00
|
|
|
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-12 21:38:58 +02:00
|
|
|
if (has_prev_log_number_) {
|
|
|
|
r.append("\n PrevLogNumber: ");
|
|
|
|
AppendNumberTo(&r, prev_log_number_);
|
|
|
|
}
|
2011-03-18 23:37:00 +01: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_);
|
|
|
|
}
|
|
|
|
for (DeletedFileSet::const_iterator iter = deleted_files_.begin();
|
|
|
|
iter != deleted_files_.end();
|
|
|
|
++iter) {
|
|
|
|
r.append("\n DeleteFile: ");
|
|
|
|
AppendNumberTo(&r, iter->first);
|
|
|
|
r.append(" ");
|
|
|
|
AppendNumberTo(&r, iter->second);
|
|
|
|
}
|
2011-04-21 00:48:11 +02:00
|
|
|
for (size_t i = 0; i < new_files_.size(); i++) {
|
2011-03-18 23:37:00 +01: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 01:30:28 +02:00
|
|
|
r.append(" ");
|
2013-08-09 00:51:16 +02:00
|
|
|
r.append(f.smallest.DebugString(hex_key));
|
2011-10-06 01:30:28 +02:00
|
|
|
r.append(" .. ");
|
2013-08-09 00:51:16 +02:00
|
|
|
r.append(f.largest.DebugString(hex_key));
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
2013-12-12 02:46:26 +01:00
|
|
|
r.append("\n ColumnFamily: ");
|
|
|
|
AppendNumberTo(&r, column_family_);
|
|
|
|
if (is_column_family_add_) {
|
|
|
|
r.append("\n ColumnFamilyAdd: ");
|
|
|
|
r.append(column_family_name_);
|
|
|
|
}
|
|
|
|
if (is_column_family_drop_) {
|
|
|
|
r.append("\n ColumnFamilyDrop");
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
r.append("\n}\n");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|