MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_memtable.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 "common/sys/rc.h"
14#include "common/lang/string.h"
15#include "common/lang/string_view.h"
16#include "common/lang/memory.h"
17#include "oblsm/memtable/ob_skiplist.h"
18#include "oblsm/util/ob_comparator.h"
19#include "oblsm/util/ob_arena.h"
20#include "oblsm/include/ob_lsm_iterator.h"
21
22namespace oceanbase {
23
33class ObMemTable : public enable_shared_from_this<ObMemTable>
34{
35public:
37
38 ~ObMemTable() = default;
39
49 shared_ptr<ObMemTable> get_shared_ptr() { return shared_from_this(); }
50
62 void put(uint64_t seq, const string_view &key, const string_view &value);
63
72 size_t appro_memory_usage() const { return arena_.memory_usage(); }
73
84
85private:
86 friend class ObMemTableIterator;
101 {
102 const ObInternalKeyComparator comparator;
103 explicit KeyComparator() {}
104 int operator()(const char *a, const char *b) const;
105 };
106
107 // TODO: currently the memtable use skiplist as the underlying data structure,
108 // it is possible to use other data structure, for example, hash table.
110
118
126
134};
135
141{
142public:
143 explicit ObMemTableIterator(shared_ptr<ObMemTable> mem, ObMemTable::Table *table) : mem_(mem), iter_(table) {}
144
145 ObMemTableIterator(const ObMemTableIterator &) = delete;
146 ObMemTableIterator &operator=(const ObMemTableIterator &) = delete;
147
148 ~ObMemTableIterator() override = default;
149
150 void seek(const string_view &k) override;
151 void seek_to_first() override { iter_.seek_to_first(); }
152 void seek_to_last() override { iter_.seek_to_last(); }
153
154 bool valid() const override { return iter_.valid(); }
155 void next() override { iter_.next(); }
156 string_view key() const override;
157 string_view value() const override;
158
159private:
160 shared_ptr<ObMemTable> mem_;
161 ObMemTable::Table::Iterator iter_;
162 string tmp_; // For seek key
163};
164
165} // namespace oceanbase
a simple memory allocator.
Definition: ob_arena.h:26
internal key comparator
Definition: ob_comparator.h:49
Abstract class for iterating over key-value pairs in an LSM-Tree.
Definition: ob_lsm_iterator.h:41
An iterator for traversing the contents of an ObMemTable.
Definition: ob_memtable.h:141
void next() override
Moves the iterator to the next key-value pair in the source.
Definition: ob_memtable.h:155
void seek_to_first() override
Positions the iterator at the first key-value pair in the source.
Definition: ob_memtable.h:151
void seek(const string_view &k) override
Positions the iterator at the first entry with a key greater than or equal to the specified key.
Definition: ob_memtable.cpp:65
bool valid() const override
Checks if the iterator is currently positioned at a valid key-value pair.
Definition: ob_memtable.h:154
string_view key() const override
Returns the key of the current entry the iterator is positioned at.
Definition: ob_memtable.cpp:57
void seek_to_last() override
Positions the iterator at the last key-value pair in the source.
Definition: ob_memtable.h:152
string_view value() const override
Returns the value of the current entry the iterator is positioned at.
Definition: ob_memtable.cpp:59
MemTable implementation for LSM-Tree.
Definition: ob_memtable.h:34
Table table_
The underlying data structure used for key-value storage.
Definition: ob_memtable.h:125
shared_ptr< ObMemTable > get_shared_ptr()
Retrieves a shared pointer to the current ObMemTable instance.
Definition: ob_memtable.h:49
ObArena arena_
Memory arena used for memory management in the memtable.
Definition: ob_memtable.h:133
ObLsmIterator * new_iterator()
Creates a new iterator for traversing the contents of the memtable.
Definition: ob_memtable.cpp:55
size_t appro_memory_usage() const
Estimates the memory usage of the memtable.
Definition: ob_memtable.h:72
void put(uint64_t seq, const string_view &key, const string_view &value)
Inserts a key-value pair into the memtable.
Definition: ob_memtable.cpp:19
KeyComparator comparator_
Comparator used for ordering keys in the memtable.
Definition: ob_memtable.h:117
Definition: ob_skiplist.h:48
Compares two keys.
Definition: ob_memtable.h:101