2013-10-16 21:59:46 +00: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 22:37:00 +00: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.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <set>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-01-29 23:26:43 +00:00
|
|
|
#include <string>
|
2014-01-07 04:29:17 +00:00
|
|
|
#include "rocksdb/cache.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class VersionSet;
|
|
|
|
|
2014-07-02 16:54:20 +00:00
|
|
|
const uint64_t kFileNumberMask = 0x3FFFFFFFFFFFFFFF;
|
|
|
|
|
|
|
|
extern uint64_t PackFileNumberAndPathId(uint64_t number, uint64_t path_id);
|
|
|
|
|
2014-06-13 22:54:19 +00:00
|
|
|
// A copyable structure contains information needed to read data from an SST
|
|
|
|
// file. It can contains a pointer to a table reader opened for the file, or
|
|
|
|
// file number and size, which can be used to create a new table reader for it.
|
|
|
|
// The behavior is undefined when a copied of the structure is used when the
|
|
|
|
// file is not in any live version any more.
|
|
|
|
struct FileDescriptor {
|
|
|
|
// Table reader in table_reader_handle
|
|
|
|
TableReader* table_reader;
|
2014-07-02 16:54:20 +00:00
|
|
|
uint64_t packed_number_and_path_id;
|
|
|
|
uint64_t file_size; // File size in bytes
|
|
|
|
|
|
|
|
FileDescriptor() : FileDescriptor(0, 0, 0) {}
|
2014-06-13 22:54:19 +00:00
|
|
|
|
2014-07-02 16:54:20 +00:00
|
|
|
FileDescriptor(uint64_t number, uint32_t path_id, uint64_t file_size)
|
|
|
|
: table_reader(nullptr),
|
|
|
|
packed_number_and_path_id(PackFileNumberAndPathId(number, path_id)),
|
|
|
|
file_size(file_size) {}
|
2014-06-13 22:54:19 +00:00
|
|
|
|
2014-07-02 16:54:20 +00:00
|
|
|
FileDescriptor& operator=(const FileDescriptor& fd) {
|
|
|
|
table_reader = fd.table_reader;
|
|
|
|
packed_number_and_path_id = fd.packed_number_and_path_id;
|
|
|
|
file_size = fd.file_size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetNumber() const {
|
|
|
|
return packed_number_and_path_id & kFileNumberMask;
|
|
|
|
}
|
|
|
|
uint32_t GetPathId() const {
|
|
|
|
return packed_number_and_path_id / (kFileNumberMask + 1);
|
|
|
|
}
|
2014-06-13 22:54:19 +00:00
|
|
|
uint64_t GetFileSize() const { return file_size; }
|
|
|
|
};
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
struct FileMetaData {
|
|
|
|
int refs;
|
2014-06-13 22:54:19 +00:00
|
|
|
FileDescriptor fd;
|
2014-06-25 01:22:11 +00:00
|
|
|
InternalKey smallest; // Smallest internal key served by table
|
|
|
|
InternalKey largest; // Largest internal key served by table
|
|
|
|
bool being_compacted; // Is this file undergoing compaction?
|
|
|
|
SequenceNumber smallest_seqno; // The smallest seqno in this file
|
|
|
|
SequenceNumber largest_seqno; // The largest seqno in this file
|
|
|
|
|
|
|
|
// Needs to be disposed when refs becomes 0.
|
|
|
|
Cache::Handle* table_reader_handle;
|
|
|
|
|
|
|
|
// stats for compensating deletion entries during compaction
|
2014-06-24 22:37:06 +00:00
|
|
|
uint64_t compensated_file_size; // File size compensated by deletion entry.
|
|
|
|
uint64_t num_entries; // the number of entries.
|
|
|
|
uint64_t num_deletions; // the number of deletion entries.
|
|
|
|
uint64_t raw_key_size; // total uncompressed key size.
|
|
|
|
uint64_t raw_value_size; // total uncompressed value size.
|
2014-01-07 04:29:17 +00:00
|
|
|
|
2014-06-13 22:54:19 +00:00
|
|
|
FileMetaData()
|
2014-02-06 23:42:16 +00:00
|
|
|
: refs(0),
|
2014-06-25 01:22:11 +00:00
|
|
|
being_compacted(false),
|
|
|
|
table_reader_handle(nullptr),
|
2014-06-24 22:37:06 +00:00
|
|
|
compensated_file_size(0),
|
|
|
|
num_entries(0),
|
|
|
|
num_deletions(0),
|
|
|
|
raw_key_size(0),
|
2014-06-25 01:22:11 +00:00
|
|
|
raw_value_size(0) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class VersionEdit {
|
|
|
|
public:
|
2014-01-14 23:27:09 +00:00
|
|
|
VersionEdit() { Clear(); }
|
2011-03-18 22:37:00 +00:00
|
|
|
~VersionEdit() { }
|
|
|
|
|
|
|
|
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-12 19:38:58 +00:00
|
|
|
void SetPrevLogNumber(uint64_t num) {
|
|
|
|
has_prev_log_number_ = true;
|
|
|
|
prev_log_number_ = num;
|
|
|
|
}
|
2011-03-18 22:37:00 +00: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;
|
|
|
|
}
|
2014-03-05 20:13:44 +00:00
|
|
|
void SetMaxColumnFamily(uint32_t max_column_family) {
|
|
|
|
has_max_column_family_ = true;
|
|
|
|
max_column_family_ = max_column_family;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// 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
|
2014-07-02 16:54:20 +00:00
|
|
|
void AddFile(int level, uint64_t file, uint64_t file_size,
|
|
|
|
uint64_t file_path_id, const InternalKey& smallest,
|
|
|
|
const InternalKey& largest, const SequenceNumber& smallest_seqno,
|
2013-06-14 05:09:08 +00:00
|
|
|
const SequenceNumber& largest_seqno) {
|
2013-12-31 02:33:57 +00:00
|
|
|
assert(smallest_seqno <= largest_seqno);
|
2011-03-18 22:37:00 +00:00
|
|
|
FileMetaData f;
|
2014-07-02 16:54:20 +00:00
|
|
|
f.fd = FileDescriptor(file, file_size, file_path_id);
|
2011-03-18 22:37:00 +00:00
|
|
|
f.smallest = smallest;
|
|
|
|
f.largest = largest;
|
2013-06-14 05:09:08 +00:00
|
|
|
f.smallest_seqno = smallest_seqno;
|
|
|
|
f.largest_seqno = largest_seqno;
|
2011-03-18 22:37:00 +00:00
|
|
|
new_files_.push_back(std::make_pair(level, f));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the specified "file" from the specified "level".
|
|
|
|
void DeleteFile(int level, uint64_t file) {
|
2013-12-31 02:33:57 +00:00
|
|
|
deleted_files_.insert({level, file});
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 21:23:58 +00:00
|
|
|
// Number of edits
|
|
|
|
int NumEntries() {
|
|
|
|
return new_files_.size() + deleted_files_.size();
|
|
|
|
}
|
|
|
|
|
2014-02-28 22:05:11 +00:00
|
|
|
bool IsColumnFamilyManipulation() {
|
|
|
|
return is_column_family_add_ || is_column_family_drop_;
|
|
|
|
}
|
|
|
|
|
2013-12-12 01:46:26 +00:00
|
|
|
void SetColumnFamily(uint32_t column_family_id) {
|
|
|
|
column_family_ = column_family_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set column family ID by calling SetColumnFamily()
|
|
|
|
void AddColumnFamily(const std::string& name) {
|
|
|
|
assert(!is_column_family_drop_);
|
|
|
|
assert(!is_column_family_add_);
|
|
|
|
assert(NumEntries() == 0);
|
|
|
|
is_column_family_add_ = true;
|
|
|
|
column_family_name_ = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set column family ID by calling SetColumnFamily()
|
|
|
|
void DropColumnFamily() {
|
|
|
|
assert(!is_column_family_drop_);
|
|
|
|
assert(!is_column_family_add_);
|
|
|
|
assert(NumEntries() == 0);
|
|
|
|
is_column_family_drop_ = true;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
void EncodeTo(std::string* dst) const;
|
|
|
|
Status DecodeFrom(const Slice& src);
|
|
|
|
|
2013-08-08 22:51:16 +00:00
|
|
|
std::string DebugString(bool hex_key = false) const;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend class VersionSet;
|
2014-06-24 22:37:06 +00:00
|
|
|
friend class Version;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-12-31 02:33:57 +00:00
|
|
|
typedef std::set< std::pair<int, uint64_t>> DeletedFileSet;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-24 18:54:26 +00:00
|
|
|
bool GetLevel(Slice* input, int* level, const char** msg);
|
2012-06-23 02:30:03 +00:00
|
|
|
|
2014-01-14 23:27:09 +00:00
|
|
|
int max_level_;
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string comparator_;
|
|
|
|
uint64_t log_number_;
|
2011-04-12 19:38:58 +00:00
|
|
|
uint64_t prev_log_number_;
|
2011-03-18 22:37:00 +00:00
|
|
|
uint64_t next_file_number_;
|
2014-03-05 20:13:44 +00:00
|
|
|
uint32_t max_column_family_;
|
2011-03-18 22:37:00 +00:00
|
|
|
SequenceNumber last_sequence_;
|
|
|
|
bool has_comparator_;
|
|
|
|
bool has_log_number_;
|
2011-04-12 19:38:58 +00:00
|
|
|
bool has_prev_log_number_;
|
2011-03-18 22:37:00 +00:00
|
|
|
bool has_next_file_number_;
|
|
|
|
bool has_last_sequence_;
|
2014-03-05 20:13:44 +00:00
|
|
|
bool has_max_column_family_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
DeletedFileSet deleted_files_;
|
2014-03-05 20:13:44 +00:00
|
|
|
std::vector<std::pair<int, FileMetaData>> new_files_;
|
2013-12-12 01:46:26 +00:00
|
|
|
|
|
|
|
// Each version edit record should have column_family_id set
|
|
|
|
// If it's not set, it is default (0)
|
|
|
|
uint32_t column_family_;
|
|
|
|
// a version edit can be either column_family add or
|
|
|
|
// column_family drop. If it's column family add,
|
|
|
|
// it also includes column family name.
|
|
|
|
bool is_column_family_drop_;
|
|
|
|
bool is_column_family_add_;
|
|
|
|
std::string column_family_name_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|