MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
ob_manifest.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2 miniob is licensed under Mulan PSL v2.
3 You can use this software according to the terms and conditions of the Mulan PSL v2.
4 You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7 EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8 MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9 See the Mulan PSL v2 for more details. */
10//
11// Created by Ping Xu(haibarapink@gmail.com) on 2025/1/24.
12//
13#pragma once
14
15#include "json/json.h"
16#include "common/lang/filesystem.h"
17#include "common/lang/string_view.h"
18#include "common/log/log.h"
19#include "common/sys/rc.h"
20#include "oblsm/ob_lsm_define.h"
21#include "oblsm/util/ob_file_writer.h"
22#include "oblsm/util/ob_file_reader.h"
23
24namespace oceanbase {
25
33{
34public:
44 template <typename T>
45 static Json::Value to_json(const T &t)
46 {
47 return t.to_json();
48 }
49
60 template <typename T>
61 static RC from_json(const Json::Value &v, T &t)
62 {
63 RC rc = t.from_json(v);
64 return rc;
65 }
66};
67
76template <>
77inline Json::Value JsonConverter::to_json<CompactionType>(const CompactionType &type)
78{
79 int type_as_int = static_cast<int>(type);
80 Json::Value res{type_as_int};
81 return res;
82}
83
93template <>
94inline RC JsonConverter::from_json<CompactionType>(const Json::Value &v, CompactionType &type)
95{
96 int type_as_int = -1;
97 if (v.isInt()) {
98 type_as_int = v.asInt();
99 } else {
100 return RC::INVALID_ARGUMENT;
101 }
102
103 type = static_cast<CompactionType>(type_as_int);
104 return RC::SUCCESS;
105}
106
113{
115 int level;
116
123 ObManifestSSTableInfo(int sstable_id_, int level_) : sstable_id(sstable_id_), level(level_) {}
124
127
134 bool operator==(const ObManifestSSTableInfo &rhs) const { return sstable_id == rhs.sstable_id && level == rhs.level; }
135
141 Json::Value to_json() const
142 {
143 Json::Value v;
144 v["sstable_id"] = sstable_id;
145 v["level"] = level;
146 return v;
147 }
148
155 RC from_json(const Json::Value &v);
156};
157
164{
165public:
166 CompactionType compaction_type;
167 std::vector<ObManifestSSTableInfo> deleted_tables;
168 std::vector<ObManifestSSTableInfo> added_tables;
170 uint64_t seq_id;
171
172 static string record_type() { return "ObManifestCompaction"; }
173
174 bool operator==(const ObManifestCompaction &rhs) const
175 {
178 }
179
185 Json::Value to_json() const;
186
193 RC from_json(const Json::Value &v);
194};
195
202{
203public:
204 std::vector<std::vector<uint64_t>> sstables;
205 uint64_t seq;
206 uint64_t sstable_id;
207 CompactionType compaction_type;
208
209 static string record_type() { return "ObManifestSnapshot"; }
210
211 bool operator==(const ObManifestSnapshot &rhs) const
212 {
213 return sstables == rhs.sstables && seq == rhs.seq && sstable_id == rhs.sstable_id &&
215 }
216
222 Json::Value to_json() const;
223
230 RC from_json(const Json::Value &v);
231};
232
239{
240public:
241 uint64_t memtable_id;
243
244 static string record_type() { return "ObManifestNewMemtable"; }
245
246 bool operator==(const ObManifestNewMemtable &rhs) const { return memtable_id == rhs.memtable_id; }
247
253 Json::Value to_json() const;
254
261 RC from_json(const Json::Value &v);
262};
263
271{
272public:
277 ObManifest(const std::string &path) : path_(filesystem::path(path)) { current_path_ = path_ / "CURRENT"; }
278
283 RC open();
284
295 template <typename T>
296 RC push(const T &data)
297 {
298 static_assert(std::is_same<T, ObManifestCompaction>::value || std::is_same<T, ObManifestSnapshot>::value ||
299 std::is_same<T, ObManifestNewMemtable>::value,
300 "push() only supports ObManifestCompaction ,ObManifestNewMemtable and ObManifestSnapshot types.");
301 Json::Value json = JsonConverter::to_json(data);
302 string str = json.toStyledString();
303 size_t len = str.size();
304 RC rc = writer_->write(string_view{reinterpret_cast<char *>(&len), sizeof(len)});
305 if (OB_FAIL(rc)) {
306 LOG_WARN("Failed to push record into manifest file %s, rc %s", writer_->file_name().c_str(), strrc(rc));
307 return rc;
308 }
309 rc = writer_->write(str);
310 if (OB_FAIL(rc)) {
311 LOG_WARN("Failed to push record into manifest file %s, rc %s", writer_->file_name().c_str(), strrc(rc));
312 return rc;
313 }
314 rc = writer_->flush();
315 if (OB_FAIL(rc)) {
316 LOG_WARN("Failed to flush data of manifest file %s, rc %s", writer_->file_name().c_str(), strrc(rc));
317 return rc;
318 }
319 return rc;
320 }
321
329 RC redirect(const ObManifestSnapshot &snapshot, const ObManifestNewMemtable &memtable);
330
339 RC recover(std::unique_ptr<ObManifestSnapshot> &snapshot_record,
340 std::unique_ptr<ObManifestNewMemtable> &memtbale_record, std::vector<ObManifestCompaction> &compactions);
341
342 uint64_t latest_seq{0};
343
344 friend class ObManifestTester;
345
346private:
354 string get_manifest_file_path(string path, uint64_t mf_seq)
355 {
356 return filesystem::path(path) / (std::to_string(mf_seq) + MANIFEST_SUFFIX);
357 }
358
359private:
360 filesystem::path path_;
361 filesystem::path current_path_;
362 uint64_t mf_seq_;
363
364 std::unique_ptr<ObFileWriter> writer_;
365 std::unique_ptr<ObFileReader> reader_;
366};
367
368} // namespace oceanbase
A utility class to convert between JSON and user-defined types.
Definition: ob_manifest.h:33
static Json::Value to_json(const T &t)
Converts an object of type T to a JSON value.
Definition: ob_manifest.h:45
static RC from_json(const Json::Value &v, T &t)
Converts a JSON value to an object of type T.
Definition: ob_manifest.h:61
Represents a record in the manifest, used for compaction operations.
Definition: ob_manifest.h:164
uint64_t seq_id
Sequence ID for the record.
Definition: ob_manifest.h:170
Json::Value to_json() const
Converts the ObManifestCompaction to a JSON value.
Definition: ob_manifest.cpp:36
CompactionType compaction_type
The type of compaction (e.g., level)
Definition: ob_manifest.h:166
uint64_t sstable_sequence_id
Sequence ID for SSTables.
Definition: ob_manifest.h:169
std::vector< ObManifestSSTableInfo > added_tables
List of added SSTables.
Definition: ob_manifest.h:168
std::vector< ObManifestSSTableInfo > deleted_tables
List of deleted SSTables.
Definition: ob_manifest.h:167
RC from_json(const Json::Value &v)
Populates the ObManifestCompaction object from a JSON value.
Definition: ob_manifest.cpp:59
Represents a new memtable of ObLsm.
Definition: ob_manifest.h:239
Json::Value to_json() const
Converts the ObManifestNewMemtable to a JSON value.
Definition: ob_manifest.cpp:158
uint64_t memtable_id
The id of memtable and it is used to find the WAL file of memtable.(e.g., memtable=1,...
Definition: ob_manifest.h:241
RC from_json(const Json::Value &v)
Populates the ObManifestNewMemtable object from a JSON value.
Definition: ob_manifest.cpp:166
Represents a snapshot of the manifest.
Definition: ob_manifest.h:202
Json::Value to_json() const
Converts the ObManifestSnapshot to a JSON value.
Definition: ob_manifest.cpp:118
RC from_json(const Json::Value &v)
Populates the ObManifestSnapshot object from a JSON value.
Definition: ob_manifest.cpp:139
uint64_t sstable_id
The ID of the SSTable.
Definition: ob_manifest.h:206
CompactionType compaction_type
The type of compaction.
Definition: ob_manifest.h:207
std::vector< std::vector< uint64_t > > sstables
A list of SSTable IDs grouped by levels.
Definition: ob_manifest.h:204
uint64_t seq
The sequence ID for the snapshot.
Definition: ob_manifest.h:205
A class that manages the manifest file, including reading and writing records and snapshots.
Definition: ob_manifest.h:271
RC push(const T &data)
Pushes a record or snapshot to the writer.
Definition: ob_manifest.h:296
std::unique_ptr< ObFileWriter > writer_
The file writer for manifest data.
Definition: ob_manifest.h:364
RC redirect(const ObManifestSnapshot &snapshot, const ObManifestNewMemtable &memtable)
Redirects to a new manifest file.
Definition: ob_manifest.cpp:303
uint64_t mf_seq_
The sequence ID of the current manifest file.
Definition: ob_manifest.h:362
ObManifest(const std::string &path)
Constructs an ObManifest object with the specified path.
Definition: ob_manifest.h:277
RC open()
Opens the manifest file and initializes the reader and writer.
Definition: ob_manifest.cpp:175
RC recover(std::unique_ptr< ObManifestSnapshot > &snapshot_record, std::unique_ptr< ObManifestNewMemtable > &memtbale_record, std::vector< ObManifestCompaction > &compactions)
Redirects to a new manifest file.
Definition: ob_manifest.cpp:239
filesystem::path path_
The directory path where manifest files are stored.
Definition: ob_manifest.h:360
uint64_t latest_seq
The latest sequence number persisted in the LSM.
Definition: ob_manifest.h:342
std::unique_ptr< ObFileReader > reader_
The file reader for manifest data.
Definition: ob_manifest.h:365
string get_manifest_file_path(string path, uint64_t mf_seq)
Constructs the path to the manifest file.
Definition: ob_manifest.h:354
filesystem::path current_path_
The path to the CURRENT file that tracks the latest manifest file.
Definition: ob_manifest.h:361
Struct representing SSTable information in a manifest.
Definition: ob_manifest.h:113
RC from_json(const Json::Value &v)
Populates the ObManifestSSTableInfo object from a JSON value.
Definition: ob_manifest.cpp:20
int sstable_id
The ID of the SSTable.
Definition: ob_manifest.h:114
Json::Value to_json() const
Converts the ObManifestSSTableInfo object to a JSON value.
Definition: ob_manifest.h:141
ObManifestSSTableInfo()
Definition: ob_manifest.h:126
ObManifestSSTableInfo(int sstable_id_, int level_)
Constructor to initialize with given SSTable ID and level.
Definition: ob_manifest.h:123
bool operator==(const ObManifestSSTableInfo &rhs) const
Equality operator for comparing two ObManifestSSTableInfo objects.
Definition: ob_manifest.h:134
int level
The level of the SSTable.
Definition: ob_manifest.h:115