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_DB_IMPL_H_
|
|
|
|
#define STORAGE_LEVELDB_DB_DB_IMPL_H_
|
|
|
|
|
2019-03-12 04:04:53 +08:00
|
|
|
#include <atomic>
|
2012-03-09 08:23:21 +08:00
|
|
|
#include <deque>
|
2011-03-19 06:37:00 +08:00
|
|
|
#include <set>
|
2019-03-12 04:04:53 +08:00
|
|
|
#include <string>
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/log_writer.h"
|
|
|
|
#include "db/snapshot.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/db.h"
|
|
|
|
#include "leveldb/env.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "port/port.h"
|
2012-10-13 02:53:12 +08:00
|
|
|
#include "port/thread_annotations.h"
|
2023-04-18 05:29:42 +08:00
|
|
|
#include "leveldb/filesystem.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2023-04-18 05:29:42 +08:00
|
|
|
using namespace filesystem;
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
class MemTable;
|
|
|
|
class TableCache;
|
|
|
|
class Version;
|
|
|
|
class VersionEdit;
|
|
|
|
class VersionSet;
|
|
|
|
|
|
|
|
class DBImpl : public DB {
|
|
|
|
public:
|
|
|
|
DBImpl(const Options& options, const std::string& dbname);
|
2019-05-04 00:31:18 +08:00
|
|
|
|
|
|
|
DBImpl(const DBImpl&) = delete;
|
|
|
|
DBImpl& operator=(const DBImpl&) = delete;
|
|
|
|
|
2019-05-10 05:00:07 +08:00
|
|
|
~DBImpl() override;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Implementations of the DB interface
|
2019-05-10 05:00:07 +08:00
|
|
|
Status Put(const WriteOptions&, const Slice& key,
|
|
|
|
const Slice& value) override;
|
|
|
|
Status Delete(const WriteOptions&, const Slice& key) override;
|
|
|
|
Status Write(const WriteOptions& options, WriteBatch* updates) override;
|
|
|
|
Status Get(const ReadOptions& options, const Slice& key,
|
|
|
|
std::string* value) override;
|
|
|
|
Iterator* NewIterator(const ReadOptions&) override;
|
|
|
|
const Snapshot* GetSnapshot() override;
|
|
|
|
void ReleaseSnapshot(const Snapshot* snapshot) override;
|
|
|
|
bool GetProperty(const Slice& property, std::string* value) override;
|
|
|
|
void GetApproximateSizes(const Range* range, int n, uint64_t* sizes) override;
|
|
|
|
void CompactRange(const Slice* begin, const Slice* end) override;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Extra methods (for testing) that are not in the public DB interface
|
|
|
|
|
2011-10-06 07:30:28 +08:00
|
|
|
// Compact any files in the named level that overlap [*begin,*end]
|
|
|
|
void TEST_CompactRange(int level, const Slice* begin, const Slice* end);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Force current memtable contents to be compacted.
|
|
|
|
Status TEST_CompactMemTable();
|
|
|
|
|
|
|
|
// Return an internal iterator over the current state of the database.
|
|
|
|
// The keys of this iterator are internal keys (see format.h).
|
|
|
|
// The returned iterator should be deleted when no longer needed.
|
|
|
|
Iterator* TEST_NewInternalIterator();
|
|
|
|
|
2011-03-23 02:32:49 +08:00
|
|
|
// Return the maximum overlapping data (in bytes) at next level for any
|
|
|
|
// file at a level >= 1.
|
2011-03-23 07:24:02 +08:00
|
|
|
int64_t TEST_MaxNextLevelOverlappingBytes();
|
2011-03-23 02:32:49 +08:00
|
|
|
|
2013-08-22 02:12:47 +08:00
|
|
|
// Record a sample of bytes read at the specified internal key.
|
|
|
|
// Samples are taken approximately once every config::kReadBytesPeriod
|
|
|
|
// bytes.
|
|
|
|
void RecordReadSample(Slice key);
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
private:
|
|
|
|
friend class DB;
|
2012-03-09 08:23:21 +08:00
|
|
|
struct CompactionState;
|
|
|
|
struct Writer;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-04 00:31:18 +08:00
|
|
|
// Information for a manual compaction
|
|
|
|
struct ManualCompaction {
|
|
|
|
int level;
|
|
|
|
bool done;
|
|
|
|
const InternalKey* begin; // null means beginning of key range
|
|
|
|
const InternalKey* end; // null means end of key range
|
|
|
|
InternalKey tmp_storage; // Used to keep track of compaction progress
|
|
|
|
};
|
|
|
|
|
|
|
|
// Per level compaction stats. stats_[level] stores the stats for
|
|
|
|
// compactions that produced data for the specified "level".
|
|
|
|
struct CompactionStats {
|
|
|
|
CompactionStats() : micros(0), bytes_read(0), bytes_written(0) {}
|
|
|
|
|
|
|
|
void Add(const CompactionStats& c) {
|
|
|
|
this->micros += c.micros;
|
|
|
|
this->bytes_read += c.bytes_read;
|
|
|
|
this->bytes_written += c.bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t micros;
|
|
|
|
int64_t bytes_read;
|
|
|
|
int64_t bytes_written;
|
|
|
|
};
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
Iterator* NewInternalIterator(const ReadOptions&,
|
2013-08-22 02:12:47 +08:00
|
|
|
SequenceNumber* latest_snapshot,
|
|
|
|
uint32_t* seed);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
Status NewDB();
|
|
|
|
|
|
|
|
// Recover the descriptor from persistent storage. May do a significant
|
|
|
|
// amount of work to recover recently logged updates. Any changes to
|
|
|
|
// be made to the descriptor are added to *edit.
|
2014-12-12 00:13:18 +08:00
|
|
|
Status Recover(VersionEdit* edit, bool* save_manifest)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
void MaybeIgnoreError(Status* s) const;
|
|
|
|
|
|
|
|
// Delete any unneeded files and stale in-memory entries.
|
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 RemoveObsoleteFiles() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Compact the in-memory write buffer to disk. Switches to a new
|
|
|
|
// log-file/memtable and writes a new descriptor iff successful.
|
2013-12-11 02:36:31 +08:00
|
|
|
// Errors are recorded in bg_error_.
|
|
|
|
void CompactMemTable() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2014-12-12 00:13:18 +08:00
|
|
|
Status RecoverLogFile(uint64_t log_number, bool last_log, bool* save_manifest,
|
|
|
|
VersionEdit* edit, SequenceNumber* max_sequence)
|
2012-10-13 02:53:12 +08:00
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2012-10-13 02:53:12 +08:00
|
|
|
Status WriteLevel0Table(MemTable* mem, VersionEdit* edit, Version* base)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2012-10-13 02:53:12 +08:00
|
|
|
Status MakeRoomForWrite(bool force /* compact even if there is room? */)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2018-03-17 01:06:35 +08:00
|
|
|
WriteBatch* BuildBatchGroup(Writer** last_writer)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2013-12-11 02:36:31 +08:00
|
|
|
void RecordBackgroundError(const Status& s);
|
|
|
|
|
2012-10-13 02:53:12 +08:00
|
|
|
void MaybeScheduleCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
static void BGWork(void* db);
|
|
|
|
void BackgroundCall();
|
2018-03-17 01:06:35 +08:00
|
|
|
void BackgroundCompaction() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2012-10-13 02:53:12 +08:00
|
|
|
void CleanupCompaction(CompactionState* compact)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
|
|
|
Status DoCompactionWork(CompactionState* compact)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
Status OpenCompactionOutputFile(CompactionState* compact);
|
|
|
|
Status FinishCompactionOutputFile(CompactionState* compact, Iterator* input);
|
2012-10-13 02:53:12 +08:00
|
|
|
Status InstallCompactionResults(CompactionState* compact)
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-04 00:31:18 +08:00
|
|
|
const Comparator* user_comparator() const {
|
|
|
|
return internal_comparator_.user_comparator();
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// Constant after construction
|
|
|
|
Env* const env_;
|
|
|
|
const InternalKeyComparator internal_comparator_;
|
2012-04-17 23:36:46 +08:00
|
|
|
const InternalFilterPolicy internal_filter_policy_;
|
2011-03-19 06:37:00 +08:00
|
|
|
const Options options_; // options_.comparator == &internal_comparator_
|
2018-03-17 01:06:35 +08:00
|
|
|
const bool owns_info_log_;
|
|
|
|
const bool owns_cache_;
|
2023-04-18 05:29:42 +08:00
|
|
|
// TODO: replace with Path;
|
2011-03-19 06:37:00 +08:00
|
|
|
const std::string dbname_;
|
2023-04-18 05:29:42 +08:00
|
|
|
Path* const path_;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// table_cache_ provides its own synchronization
|
2018-03-17 01:06:35 +08:00
|
|
|
TableCache* const table_cache_;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2018-04-11 07:18:06 +08:00
|
|
|
// Lock over the persistent DB state. Non-null iff successfully acquired.
|
2011-03-19 06:37:00 +08:00
|
|
|
FileLock* db_lock_;
|
|
|
|
|
|
|
|
// State below is protected by mutex_
|
|
|
|
port::Mutex mutex_;
|
2019-03-12 04:04:53 +08:00
|
|
|
std::atomic<bool> shutting_down_;
|
2018-03-17 01:06:35 +08:00
|
|
|
port::CondVar background_work_finished_signal_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
MemTable* mem_;
|
2018-03-17 01:06:35 +08:00
|
|
|
MemTable* imm_ GUARDED_BY(mutex_); // Memtable being compacted
|
2019-03-12 04:04:53 +08:00
|
|
|
std::atomic<bool> has_imm_; // So bg thread can detect non-null imm_
|
2011-03-19 06:37:00 +08:00
|
|
|
WritableFile* logfile_;
|
2018-03-17 01:06:35 +08:00
|
|
|
uint64_t logfile_number_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
log::Writer* log_;
|
2018-03-17 01:06:35 +08:00
|
|
|
uint32_t seed_ GUARDED_BY(mutex_); // For sampling.
|
2012-03-09 08:23:21 +08:00
|
|
|
|
|
|
|
// Queue of writers.
|
2018-03-17 01:06:35 +08:00
|
|
|
std::deque<Writer*> writers_ GUARDED_BY(mutex_);
|
|
|
|
WriteBatch* tmp_batch_ GUARDED_BY(mutex_);
|
2012-03-09 08:23:21 +08:00
|
|
|
|
2018-03-17 01:06:35 +08:00
|
|
|
SnapshotList snapshots_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Set of table files to protect from deletion because they are
|
|
|
|
// part of ongoing compactions.
|
2018-03-17 01:06:35 +08:00
|
|
|
std::set<uint64_t> pending_outputs_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Has a background compaction been scheduled or is running?
|
2018-03-17 01:06:35 +08:00
|
|
|
bool background_compaction_scheduled_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2018-03-17 01:06:35 +08:00
|
|
|
ManualCompaction* manual_compaction_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-05-16 04:13:13 +08:00
|
|
|
VersionSet* const versions_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Have we encountered a background error in paranoid mode?
|
2018-03-17 01:06:35 +08:00
|
|
|
Status bg_error_ GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2018-03-17 01:06:35 +08:00
|
|
|
CompactionStats stats_[config::kNumLevels] GUARDED_BY(mutex_);
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Sanitize db options. The caller should delete result.info_log if
|
|
|
|
// it is not equal to src.info_log.
|
2018-03-13 00:14:44 +08:00
|
|
|
Options SanitizeOptions(const std::string& db,
|
|
|
|
const InternalKeyComparator* icmp,
|
|
|
|
const InternalFilterPolicy* ipolicy,
|
|
|
|
const Options& src);
|
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_DB_IMPL_H_
|