MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_block.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/lang/string.h"
14#include "common/lang/vector.h"
15#include "oblsm/include/ob_lsm_iterator.h"
16#include "oblsm/util/ob_comparator.h"
17
18namespace oceanbase {
19
20// TODO: block align to 4KB
21// ┌─────────────────┐
22// │ entry 1 │◄───┐
23// ├─────────────────┤ │
24// │ entry 2 │ │
25// ├─────────────────┤ │
26// │ .. │ │
27// ├─────────────────┤ │
28// │ entry n │◄─┐ │
29// ├─────────────────┤ │ │
30// ┌───►│ offset size(n) │ │ │
31// │ ├─────────────────┤ │ │
32// │ │ offset 1 ├──┼─┘
33// │ ├─────────────────┤ │
34// │ │ .. │ │
35// │ ├─────────────────┤ │
36// │ │ offset n ├──┘
37// │ ├─────────────────┤
38// └────┤ offset start │
39// └─────────────────┘
50{
51
52public:
53 ObBlock(const ObComparator *comparator) : comparator_(comparator) {}
54
55 void add_offset(uint32_t offset) { offsets_.push_back(offset); }
56
57 uint32_t get_offset(int index) const { return offsets_[index]; }
58
59 string_view get_entry(uint32_t offset) const;
60
61 int size() const { return offsets_.size(); }
62
72 RC decode(const string &data);
73
74 ObLsmIterator *new_iterator() const;
75
76private:
77 string data_;
78 vector<uint32_t> offsets_;
79 // TODO: remove
80 const ObComparator *comparator_;
81};
82
84{
85public:
86 BlockIterator(const ObComparator *comparator, const ObBlock *data, uint32_t count)
87 : comparator_(comparator), data_(data), count_(count)
88 {}
89 BlockIterator(const BlockIterator &) = delete;
90 BlockIterator &operator=(const BlockIterator &) = delete;
91
92 ~BlockIterator() override = default;
93
94 void seek(const string_view &lookup_key) override;
95 void seek_to_first() override
96 {
97 index_ = 0;
98 parse_entry();
99 }
100 void seek_to_last() override
101 {
102 index_ = count_ - 1;
103 parse_entry();
104 }
105
106 bool valid() const override { return index_ < count_; }
107 void next() override
108 {
109 index_++;
110 if (valid()) {
111 parse_entry();
112 }
113 }
114 string_view key() const override { return key_; };
115 string_view value() const override { return value_; }
116
117private:
118 void parse_entry();
119
120private:
121 const ObComparator *comparator_;
122 const ObBlock *const data_;
123 string_view curr_entry_;
124 string_view key_;
125 string_view value_;
126 uint32_t count_ = 0;
127 uint32_t index_ = 0;
128};
129
131{
132public:
133 BlockMeta() {}
134 BlockMeta(const string &first_key, const string &last_key, uint32_t offset, uint32_t size)
135 : first_key_(first_key), last_key_(last_key), offset_(offset), size_(size)
136 {}
137 string encode() const;
138 RC decode(const string &data);
139
140 string first_key_;
141 string last_key_;
142
143 // Offset of ObBlock in SSTable
144 uint32_t offset_;
145 uint32_t size_;
146};
147} // namespace oceanbase
Definition: ob_block.h:84
void seek_to_first() override
Positions the iterator at the first key-value pair in the source.
Definition: ob_block.h:95
void seek_to_last() override
Positions the iterator at the last key-value pair in the source.
Definition: ob_block.h:100
string_view value() const override
Returns the value of the current entry the iterator is positioned at.
Definition: ob_block.h:115
void next() override
Moves the iterator to the next key-value pair in the source.
Definition: ob_block.h:107
bool valid() const override
Checks if the iterator is currently positioned at a valid key-value pair.
Definition: ob_block.h:106
string_view key() const override
Returns the key of the current entry the iterator is positioned at.
Definition: ob_block.h:114
void seek(const string_view &lookup_key) override
Positions the iterator at the first entry with a key greater than or equal to the specified key.
Definition: ob_block.cpp:71
Definition: ob_block.h:131
Represents a data block in the LSM-Tree.
Definition: ob_block.h:50
RC decode(const string &data)
Decodes serialized block data.
Definition: ob_block.cpp:17
base class of all comparators
Definition: ob_comparator.h:21
Abstract class for iterating over key-value pairs in an LSM-Tree.
Definition: ob_lsm_iterator.h:41