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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
* Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/DeprecatedString.h>
#include <AK/HashMap.h>
#include <LibCore/File.h>
#include <LibCore/Object.h>
namespace SQL {
/**
* A Block represents a single discrete chunk of 1024 bytes inside the Heap, and
* acts as the container format for the actual data we are storing. This structure
* is used for everything except block 0, the zero / super block.
*
* If data needs to be stored that is larger than 1016 bytes, Blocks are chained
* together by setting the next block index and the data is reconstructed by
* repeatedly reading blocks until the next block index is 0.
*/
class Block {
public:
typedef u32 Index;
static constexpr u32 SIZE = 1024;
static constexpr u32 HEADER_SIZE = sizeof(u32) + sizeof(Index);
static constexpr u32 DATA_SIZE = SIZE - HEADER_SIZE;
Block(Index index, u32 size_in_bytes, Index next_block, ByteBuffer data)
: m_index(index)
, m_size_in_bytes(size_in_bytes)
, m_next_block(next_block)
, m_data(move(data))
{
}
Index index() const { return m_index; }
u32 size_in_bytes() const { return m_size_in_bytes; }
Index next_block() const { return m_next_block; }
ByteBuffer const& data() const { return m_data; }
private:
Index m_index;
u32 m_size_in_bytes;
Index m_next_block;
ByteBuffer m_data;
};
/**
* A Heap is a logical container for database (SQL) data. Conceptually a
* Heap can be a database file, or a memory block, or another storage medium.
* It contains datastructures, like B-Trees, hash_index tables, or tuple stores
* (basically a list of data tuples).
*
* A Heap can be thought of the backing storage of a single database. It's
* assumed that a single SQL database is backed by a single Heap.
*/
class Heap : public Core::Object {
C_OBJECT(Heap);
public:
static constexpr u32 VERSION = 4;
virtual ~Heap() override;
ErrorOr<void> open();
bool has_block(Block::Index) const;
[[nodiscard]] Block::Index request_new_block_index() { return m_next_block++; }
Block::Index schemas_root() const { return m_schemas_root; }
void set_schemas_root(Block::Index root)
{
m_schemas_root = root;
update_zero_block().release_value_but_fixme_should_propagate_errors();
}
Block::Index tables_root() const { return m_tables_root; }
void set_tables_root(Block::Index root)
{
m_tables_root = root;
update_zero_block().release_value_but_fixme_should_propagate_errors();
}
Block::Index table_columns_root() const { return m_table_columns_root; }
void set_table_columns_root(Block::Index root)
{
m_table_columns_root = root;
update_zero_block().release_value_but_fixme_should_propagate_errors();
}
u32 version() const { return m_version; }
u32 user_value(size_t index) const
{
return m_user_values[index];
}
void set_user_value(size_t index, u32 value)
{
m_user_values[index] = value;
update_zero_block().release_value_but_fixme_should_propagate_errors();
}
ErrorOr<ByteBuffer> read_storage(Block::Index);
ErrorOr<void> write_storage(Block::Index, ReadonlyBytes);
ErrorOr<void> flush();
private:
explicit Heap(DeprecatedString);
ErrorOr<ByteBuffer> read_raw_block(Block::Index);
ErrorOr<void> write_raw_block(Block::Index, ReadonlyBytes);
ErrorOr<void> write_raw_block_to_wal(Block::Index, ByteBuffer&&);
ErrorOr<Block> read_block(Block::Index);
ErrorOr<void> write_block(Block const&);
ErrorOr<void> read_zero_block();
ErrorOr<void> initialize_zero_block();
ErrorOr<void> update_zero_block();
OwnPtr<Core::InputBufferedFile> m_file;
Block::Index m_highest_block_written { 0 };
Block::Index m_next_block { 1 };
Block::Index m_schemas_root { 0 };
Block::Index m_tables_root { 0 };
Block::Index m_table_columns_root { 0 };
u32 m_version { VERSION };
Array<u32, 16> m_user_values { 0 };
HashMap<Block::Index, ByteBuffer> m_write_ahead_log;
};
}
|