MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_lsm_impl.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include "oblsm/include/ob_lsm.h"
14
15#include "common/lang/mutex.h"
16#include "common/lang/atomic.h"
17#include "common/lang/memory.h"
18#include "common/lang/condition_variable.h"
19#include "common/lang/utility.h"
20#include "common/thread/thread_pool_executor.h"
21#include "oblsm/include/ob_lsm_transaction.h"
22#include "oblsm/memtable/ob_memtable.h"
23#include "oblsm/table/ob_sstable.h"
24#include "oblsm/util/ob_lru_cache.h"
25#include "oblsm/compaction/ob_compaction.h"
26#include "oblsm/ob_manifest.h"
27#include "oblsm/wal/ob_lsm_wal.h"
28
29namespace oceanbase {
30
32{
33 ObLsmBgCompactCtx() = default;
34 ObLsmBgCompactCtx(uint64_t id) : new_memtable_id(id) {}
35 uint64_t new_memtable_id;
36};
37
38class ObLsmImpl : public ObLsm
39{
40public:
41 ObLsmImpl(const ObLsmOptions &options, const string &path);
42 ~ObLsmImpl() override
43 {
44 if (!options_.force_sync_new_log) {
45 wal_->sync();
46 }
47 executor_.shutdown();
48 executor_.await_termination();
49 }
50
51 RC put(const string_view &key, const string_view &value) override;
52
53 RC get(const string_view &key, string *value) override;
54
55 RC remove(const string_view &key) override;
56
57 ObLsmTransaction *begin_transaction() override;
58
60
61 SSTablesPtr get_sstables() { return sstables_; }
62
63 RC recover();
64 RC batch_put(const std::vector<pair<string, string>> &kvs) override;
65
66 // used for debug
67 void dump_sstables() override;
68
69private:
70 RC recover_from_wal();
71 RC recover_from_manifest_records(const std::vector<ObManifestCompaction> &records);
72 RC load_manifest_snapshot(const ObManifestSnapshot &snapshot);
73 RC load_manifest_sstable(const std::vector<std::vector<uint64_t>> &sstables);
74 RC write_manifest_snapshot();
75
76private:
87
112 vector<shared_ptr<ObSSTable>> do_compaction(ObCompaction *compaction);
113
125
131 void background_compaction(std::shared_ptr<ObLsmBgCompactCtx> ctx);
132
143 void build_sstable(shared_ptr<ObMemTable> imem);
144
151 string get_sstable_path(uint64_t sstable_id);
152
159 string get_wal_path(uint64_t memtable_id);
160
161 ObLsmOptions options_;
162 string path_;
163 mutex mu_;
164 std::shared_ptr<WAL> wal_;
165 std::vector<std::shared_ptr<WAL>> frozen_wals_;
166 shared_ptr<ObMemTable> mem_table_;
167 vector<shared_ptr<ObMemTable>> imem_tables_;
168 SSTablesPtr sstables_;
170 ObManifest manifest_;
171 atomic<uint64_t> seq_{0};
172 atomic<uint64_t> sstable_id_{0};
173 atomic<uint64_t> memtable_id_{0};
174 condition_variable cv_;
175 // TODO: use global variable?
176 const ObDefaultComparator default_comparator_;
177 const ObInternalKeyComparator internal_key_comparator_;
178 atomic<bool> compacting_ = false;
179 std::unique_ptr<ObLRUCache<uint64_t, shared_ptr<ObBlock>>> block_cache_;
180};
181
182} // namespace oceanbase
Definition: thread_pool_executor.h:44
int shutdown()
关闭线程池
Definition: thread_pool_executor.cpp:73
int await_termination()
等待线程池处理完所有任务并退出
Definition: thread_pool_executor.cpp:104
Represents a compaction task in the LSM-Tree.
Definition: ob_compaction.h:28
comparator with lexicographical order
Definition: ob_comparator.h:38
internal key comparator
Definition: ob_comparator.h:49
Definition: ob_lsm_impl.h:39
ObLsmIterator * new_iterator(ObLsmReadOptions options) override
Creates a new iterator for traversing the LSM-Tree database.
Definition: ob_lsm_impl.cpp:333
RC try_freeze_memtable()
Attempts to freeze the current active MemTable.
Definition: ob_lsm_impl.cpp:155
void build_sstable(shared_ptr< ObMemTable > imem)
Builds an SSTable from the given MemTable.
Definition: ob_lsm_impl.cpp:280
RC put(const string_view &key, const string_view &value) override
Inserts or updates a key-value entry in the LSM-Tree.
Definition: ob_lsm_impl.cpp:103
RC get(const string_view &key, string *value) override
Retrieves the value associated with a specified key.
Definition: ob_lsm_impl.cpp:315
vector< shared_ptr< ObSSTable > > do_compaction(ObCompaction *compaction)
Performs compaction on the SSTables selected by the compaction strategy.
Definition: ob_lsm_impl.cpp:278
void dump_sstables() override
Dumps all SSTables for debugging purposes.
Definition: ob_lsm_impl.cpp:362
string get_wal_path(uint64_t memtable_id)
Retrieves the file path for a given Memtable id.
Definition: ob_lsm_impl.cpp:310
void try_major_compaction()
Initiates a major compaction process.
Definition: ob_lsm_impl.cpp:213
void background_compaction(std::shared_ptr< ObLsmBgCompactCtx > ctx)
Handles background compaction tasks.
Definition: ob_lsm_impl.cpp:183
string get_sstable_path(uint64_t sstable_id)
Retrieves the file path for a given SSTable.
Definition: ob_lsm_impl.cpp:305
RC remove(const string_view &key) override
Delete a key-value entry in the LSM-Tree.
Definition: ob_lsm_impl.cpp:153
Abstract class for iterating over key-value pairs in an LSM-Tree.
Definition: ob_lsm_iterator.h:41
A class representing a transaction in oblsm.
Definition: ob_lsm_transaction.h:36
ObLsm is a key-value storage engine for educational purpose. ObLsm learned a lot about design from le...
Definition: ob_lsm.h:29
Represents a snapshot of the manifest.
Definition: ob_manifest.h:202
A class that manages the manifest file, including reading and writing records and snapshots.
Definition: ob_manifest.h:271
Definition: ob_lsm_impl.h:32
Configuration options for the LSM-Tree implementation.
Definition: ob_lsm_options.h:22
Definition: ob_lsm_options.h:48