MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_sstable.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/util/ob_file_reader.h"
14#include "common/lang/memory.h"
15#include "common/sys/rc.h"
16#include "oblsm/table/ob_block.h"
17#include "oblsm/util/ob_comparator.h"
18#include "oblsm/util/ob_lru_cache.h"
19
20namespace oceanbase {
21// TODO: add a dumptool to dump sst files(example for usage: ./dumptool sst_file)
22// ┌─────────────────┐
23// │ block 1 │◄──┐
24// ├─────────────────┤ │
25// │ block 2 │ │
26// ├─────────────────┤ │
27// │ .. │ │
28// ├─────────────────┤ │
29// │ block n │◄┐ │
30// ├─────────────────┤ │ │
31// ┌─►│ meta size(n) │ │ │
32// │ ├─────────────────┤ │ │
33// │ │block meta 1 size│ │ │
34// │ ├─────────────────┤ │ │
35// │ │ block meta 1 ┼─┼─┘
36// │ ├─────────────────┤ │
37// │ │ .. │ │
38// │ ├─────────────────┤ │
39// │ │block meta n size│ │
40// │ ├─────────────────┤ │
41// │ │ block meta n ┼─┘
42// │ ├─────────────────┤
43// └──┼ │
44// └─────────────────┘
45
55class ObSSTable : public enable_shared_from_this<ObSSTable>
56{
57public:
68 ObSSTable(uint32_t sst_id, const string &file_name, const ObComparator *comparator,
69 ObLRUCache<uint64_t, shared_ptr<ObBlock>> *block_cache)
70 : sst_id_(sst_id),
71 file_name_(file_name),
72 comparator_(comparator),
73 file_reader_(nullptr),
74 block_cache_(block_cache)
75 {
76 (void)block_cache_;
77 }
78
79 ~ObSSTable() = default;
80
89 void init();
90
91 uint32_t sst_id() const { return sst_id_; }
92
93 shared_ptr<ObSSTable> get_shared_ptr() { return shared_from_this(); }
94
95 ObLsmIterator *new_iterator();
96
107 shared_ptr<ObBlock> read_block_with_cache(uint32_t block_idx) const;
108
119 shared_ptr<ObBlock> read_block(uint32_t block_idx) const;
120
121 uint32_t block_count() const { return block_metas_.size(); }
122
123 uint32_t size() const { return file_reader_->file_size(); }
124
125 const BlockMeta block_meta(int i) const { return block_metas_[i]; }
126
127 const ObComparator *comparator() const { return comparator_; }
128
129 void remove();
130 string first_key() const { return block_metas_.empty() ? "" : block_metas_[0].first_key_; }
131 string last_key() const { return block_metas_.empty() ? "" : block_metas_.back().last_key_; }
132
133private:
134 uint32_t sst_id_;
135 string file_name_;
136 const ObComparator *comparator_ = nullptr;
137 unique_ptr<ObFileReader> file_reader_;
138 vector<BlockMeta> block_metas_;
139
140 ObLRUCache<uint64_t, shared_ptr<ObBlock>> *block_cache_;
141};
142
144{
145public:
146 TableIterator(const shared_ptr<ObSSTable> &sst) : sst_(sst), block_cnt_(sst->block_count()) {}
147 ~TableIterator() override = default;
148
149 void seek(const string_view &key) override;
150 void seek_to_first() override;
151 void seek_to_last() override;
152 void next() override;
153 bool valid() const override { return block_iterator_ != nullptr && block_iterator_->valid(); }
154 string_view key() const override { return block_iterator_->key(); }
155 string_view value() const override { return block_iterator_->value(); }
156
157private:
158 void read_block_with_cache();
159
160 const shared_ptr<ObSSTable> sst_;
161 uint32_t block_cnt_ = 0;
162 uint32_t curr_block_idx_ = 0;
163 shared_ptr<ObBlock> block_;
164 unique_ptr<ObLsmIterator> block_iterator_;
165};
166
167using SSTablesPtr = shared_ptr<vector<vector<shared_ptr<ObSSTable>>>>;
168
169} // namespace oceanbase
base class of all comparators
Definition: ob_comparator.h:21
A thread-safe implementation of an LRU (Least Recently Used) cache.
Definition: ob_lru_cache.h:31
Abstract class for iterating over key-value pairs in an LSM-Tree.
Definition: ob_lsm_iterator.h:41
Represents an SSTable (Sorted String Table) in the LSM-Tree.
Definition: ob_sstable.h:56
shared_ptr< ObBlock > read_block_with_cache(uint32_t block_idx) const
Reads a block from the SSTable using the block cache.
Definition: ob_sstable.cpp:22
shared_ptr< ObBlock > read_block(uint32_t block_idx) const
Reads a block directly from the SSTable file.
Definition: ob_sstable.cpp:28
void init()
Initializes the SSTable instance.
Definition: ob_sstable.cpp:17
ObSSTable(uint32_t sst_id, const string &file_name, const ObComparator *comparator, ObLRUCache< uint64_t, shared_ptr< ObBlock > > *block_cache)
Constructor for ObSSTable.
Definition: ob_sstable.h:68
Definition: ob_sstable.h:144
void next() override
Moves the iterator to the next key-value pair in the source.
Definition: ob_sstable.cpp:58
void seek(const string_view &key) override
Positions the iterator at the first entry with a key greater than or equal to the specified key.
Definition: ob_sstable.cpp:69
bool valid() const override
Checks if the iterator is currently positioned at a valid key-value pair.
Definition: ob_sstable.h:153
string_view value() const override
Returns the value of the current entry the iterator is positioned at.
Definition: ob_sstable.h:155
string_view key() const override
Returns the key of the current entry the iterator is positioned at.
Definition: ob_sstable.h:154
void seek_to_first() override
Positions the iterator at the first key-value pair in the source.
Definition: ob_sstable.cpp:44
void seek_to_last() override
Positions the iterator at the last key-value pair in the source.
Definition: ob_sstable.cpp:51