-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtree_index.hpp
More file actions
118 lines (91 loc) · 2.93 KB
/
Copy pathbtree_index.hpp
File metadata and controls
118 lines (91 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @file btree_index.hpp
* @brief C++ wrapper for B-tree index
*/
#ifndef CLOUDSQL_STORAGE_BTREE_INDEX_HPP
#define CLOUDSQL_STORAGE_BTREE_INDEX_HPP
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "common/value.hpp"
#include "storage/heap_table.hpp"
#include "storage/buffer_pool_manager.hpp"
namespace cloudsql::storage {
/**
* @brief B+ Tree index for fast lookups
*/
class BTreeIndex {
public:
/**
* @brief Node types in the B+ Tree
*/
enum class NodeType : uint8_t { Leaf = 0, Internal = 1 };
/**
* @brief Page header for B-tree nodes
*/
struct NodeHeader {
NodeType type;
uint16_t num_keys;
uint32_t parent_page;
uint32_t next_leaf; // For leaf nodes
};
/**
* @brief Index entry (Key + TupleId)
*/
struct Entry {
common::Value key;
HeapTable::TupleId tuple_id;
Entry() = default;
Entry(common::Value k, HeapTable::TupleId tid) : key(std::move(k)), tuple_id(tid) {}
};
/**
* @brief Scan iterator for index
*/
class Iterator {
private:
BTreeIndex& index_;
uint32_t current_page_;
uint16_t current_slot_;
bool eof_ = false;
public:
Iterator(BTreeIndex& index, uint32_t page, uint16_t slot);
bool next(Entry& out_entry);
[[nodiscard]] bool is_done() const { return eof_; }
};
private:
std::string index_name_;
std::string filename_;
BufferPoolManager& bpm_;
common::ValueType key_type_;
uint32_t root_page_ = 0;
public:
BTreeIndex(std::string index_name, BufferPoolManager& bpm, common::ValueType key_type);
~BTreeIndex() = default;
/* Non-copyable */
BTreeIndex(const BTreeIndex&) = delete;
BTreeIndex& operator=(const BTreeIndex&) = delete;
/* Movable (assignment deleted due to reference member) */
BTreeIndex(BTreeIndex&&) noexcept = default;
BTreeIndex& operator=(BTreeIndex&&) noexcept = delete;
[[nodiscard]] const std::string& index_name() const { return index_name_; }
[[nodiscard]] common::ValueType key_type() const { return key_type_; }
bool create();
bool open();
void close();
bool drop();
bool insert(const common::Value& key, HeapTable::TupleId tuple_id);
bool remove(const common::Value& key, HeapTable::TupleId tuple_id);
[[nodiscard]] std::vector<HeapTable::TupleId> search(const common::Value& key);
[[nodiscard]] Iterator scan();
private:
/* Internal B-tree logic */
[[nodiscard]] uint32_t find_leaf(const common::Value& key) const;
void split_leaf(uint32_t page_num, char* buffer);
// void split_internal(...) // TODO phase 2
bool read_page(uint32_t page_num, char* buffer) const;
bool write_page(uint32_t page_num, const char* buffer);
[[nodiscard]] uint32_t allocate_page();
};
} // namespace cloudsql::storage
#endif // CLOUDSQL_STORAGE_BTREE_INDEX_HPP