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"
55class ObSSTable :
public enable_shared_from_this<ObSSTable>
69 ObLRUCache<uint64_t, shared_ptr<ObBlock>> *block_cache)
71 file_name_(file_name),
72 comparator_(comparator),
73 file_reader_(nullptr),
74 block_cache_(block_cache)
91 uint32_t sst_id()
const {
return sst_id_; }
93 shared_ptr<ObSSTable> get_shared_ptr() {
return shared_from_this(); }
95 ObLsmIterator *new_iterator();
119 shared_ptr<ObBlock>
read_block(uint32_t block_idx)
const;
121 uint32_t block_count()
const {
return block_metas_.size(); }
123 uint32_t size()
const {
return file_reader_->file_size(); }
125 const BlockMeta block_meta(
int i)
const {
return block_metas_[i]; }
127 const ObComparator *comparator()
const {
return comparator_; }
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_; }
136 const ObComparator *comparator_ =
nullptr;
137 unique_ptr<ObFileReader> file_reader_;
138 vector<BlockMeta> block_metas_;
140 ObLRUCache<uint64_t, shared_ptr<ObBlock>> *block_cache_;
146 TableIterator(
const shared_ptr<ObSSTable> &sst) : sst_(sst), block_cnt_(sst->block_count()) {}
149 void seek(
const string_view &
key)
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(); }
158 void read_block_with_cache();
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_;
167using SSTablesPtr = shared_ptr<vector<vector<shared_ptr<ObSSTable>>>>;
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