-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.h
101 lines (87 loc) · 1.7 KB
/
main.h
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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#define COLUMN_USERNAME_SIZE 32
#define COLUMN_EMAIL_SIZE 255
#define size_of_attribute(Struct, Attribute) sizeof( ((Struct*)0)->Attribute )
#define PAGE_SIZE 4096
#define TABLE_MAX_PAGES 100
// 行
typedef struct
{
uint32_t id;
char username[COLUMN_USERNAME_SIZE + 1];
char email[COLUMN_EMAIL_SIZE + 1];
} Row;
// 输入缓存
typedef struct
{
char* buffer;
size_t buffer_length;
ssize_t input_length;
} InputBuffer;
// 元命令结果
typedef enum
{
META_COMMAND_SUCCESS,
META_COMMAND_UNRECOGNIZED_COMMAND
} MetaCommandResult;
// 准备结果
typedef enum
{
PREPARE_SUCCESS,
PREPARE_NEGATIVE_ID,
PREPARE_SYNTAX_ERROR,
PREPARE_STRING_TOO_LONG,
PREPARE_UNRECOGNIZED_STATEMENT
} PrepareResult;
// 语句类型
typedef enum
{
STATEMENT_INSERT,
STATEMENT_SELECT
} StatementType;
// 语句
typedef struct
{
StatementType type; // 语句类型
Row row_to_insert; // 插入行的结构
} Statement;
// 分页器
typedef struct
{
int file_descriptor;
uint32_t file_length;
uint32_t num_pages;
void* pages[TABLE_MAX_PAGES];
} Pager;
// 表
typedef struct
{
Pager *pager;
uint32_t root_page_num;
} Table;
// 命令执行结果
typedef enum
{
EXECUTE_SUCCESS,
EXECUTE_ERROR,
EXECUTE_DUPLICATE_KEY
} ExecuteResult;
// 游标
typedef struct
{
Table *table; // 表的指针
uint32_t page_num; // 第几页
uint32_t cell_num; // 第几个单元
bool end_of_table; // 是否到表尾
} Cursor;
// 节点类型
typedef enum {
NODE_INTERNAL,
NODE_LEAF
} NodeType;