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.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_DB_VERSION_EDIT_H_
|
|
|
|
#define STORAGE_LEVELDB_DB_VERSION_EDIT_H_
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-05-03 02:01:00 +08:00
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class VersionSet;
|
|
|
|
|
|
|
|
struct FileMetaData {
|
2019-05-04 00:31:18 +08:00
|
|
|
FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0) {}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
int refs;
|
2019-05-03 02:01:00 +08:00
|
|
|
int allowed_seeks; // Seeks allowed until compaction
|
2011-03-19 06:37:00 +08:00
|
|
|
uint64_t number;
|
2019-05-03 02:01:00 +08:00
|
|
|
uint64_t file_size; // File size in bytes
|
|
|
|
InternalKey smallest; // Smallest internal key served by table
|
|
|
|
InternalKey largest; // Largest internal key served by table
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class VersionEdit {
|
|
|
|
public:
|
|
|
|
VersionEdit() { Clear(); }
|
2019-05-05 08:40:21 +08:00
|
|
|
~VersionEdit() = default;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
void SetComparatorName(const Slice& name) {
|
|
|
|
has_comparator_ = true;
|
|
|
|
comparator_ = name.ToString();
|
|
|
|
}
|
|
|
|
void SetLogNumber(uint64_t num) {
|
|
|
|
has_log_number_ = true;
|
|
|
|
log_number_ = num;
|
|
|
|
}
|
2011-04-13 03:38:58 +08:00
|
|
|
void SetPrevLogNumber(uint64_t num) {
|
|
|
|
has_prev_log_number_ = true;
|
|
|
|
prev_log_number_ = num;
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
void SetNextFile(uint64_t num) {
|
|
|
|
has_next_file_number_ = true;
|
|
|
|
next_file_number_ = num;
|
|
|
|
}
|
|
|
|
void SetLastSequence(SequenceNumber seq) {
|
|
|
|
has_last_sequence_ = true;
|
|
|
|
last_sequence_ = seq;
|
|
|
|
}
|
|
|
|
void SetCompactPointer(int level, const InternalKey& key) {
|
|
|
|
compact_pointers_.push_back(std::make_pair(level, key));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the specified file at the specified number.
|
|
|
|
// REQUIRES: This version has not been saved (see VersionSet::SaveTo)
|
|
|
|
// REQUIRES: "smallest" and "largest" are smallest and largest keys in file
|
2019-05-03 02:01:00 +08:00
|
|
|
void AddFile(int level, uint64_t file, uint64_t file_size,
|
|
|
|
const InternalKey& smallest, const InternalKey& largest) {
|
2011-03-19 06:37:00 +08:00
|
|
|
FileMetaData f;
|
|
|
|
f.number = file;
|
|
|
|
f.file_size = file_size;
|
|
|
|
f.smallest = smallest;
|
|
|
|
f.largest = largest;
|
|
|
|
new_files_.push_back(std::make_pair(level, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the specified "file" from the specified "level".
|
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
|
|
|
void RemoveFile(int level, uint64_t file) {
|
2011-03-19 06:37:00 +08:00
|
|
|
deleted_files_.insert(std::make_pair(level, file));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EncodeTo(std::string* dst) const;
|
|
|
|
Status DecodeFrom(const Slice& src);
|
|
|
|
|
|
|
|
std::string DebugString() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class VersionSet;
|
|
|
|
|
2019-05-29 07:17:44 +08:00
|
|
|
typedef std::set<std::pair<int, uint64_t>> DeletedFileSet;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
std::string comparator_;
|
|
|
|
uint64_t log_number_;
|
2011-04-13 03:38:58 +08:00
|
|
|
uint64_t prev_log_number_;
|
2011-03-19 06:37:00 +08:00
|
|
|
uint64_t next_file_number_;
|
|
|
|
SequenceNumber last_sequence_;
|
|
|
|
bool has_comparator_;
|
|
|
|
bool has_log_number_;
|
2011-04-13 03:38:58 +08:00
|
|
|
bool has_prev_log_number_;
|
2011-03-19 06:37:00 +08:00
|
|
|
bool has_next_file_number_;
|
|
|
|
bool has_last_sequence_;
|
|
|
|
|
2019-05-29 07:17:44 +08:00
|
|
|
std::vector<std::pair<int, InternalKey>> compact_pointers_;
|
2011-03-19 06:37:00 +08:00
|
|
|
DeletedFileSet deleted_files_;
|
2019-05-29 07:17:44 +08:00
|
|
|
std::vector<std::pair<int, FileMetaData>> new_files_;
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_DB_VERSION_EDIT_H_
|