-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableHeader.h
More file actions
80 lines (67 loc) · 2.97 KB
/
Copy pathTableHeader.h
File metadata and controls
80 lines (67 loc) · 2.97 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
/*
constant member function
reinterpret_cast<const char*>(&version): This is a C++ type cast.
It treats the memory location of the version variable as if it were a pointer to a constant character (const char*).
This is necessary because the out.write() function expects a pointer to a character buffer (const char*) as its first argument when writing raw bytes.
We are essentially telling the compiler to treat the byte representing the version as a character for the purpose of writing it.
we completed filling 17 bytes
Padding to complete 4096 bytes
4096 - 17 = 4079
*/
#ifndef TABLEHEADER_H
#define TABLEHEADER_H
#include <cstdint>
#include <iostream>
#include <fstream>
#include <cstring>
class TableHeader {
private:
char magic[4] = {'M', 'Y', 'T', 'B'}; // 4 Bytes
uint8_t version = 1; // 1 Byte
uint32_t pageSize = 4096; // 4 Bytes
uint32_t numRecords = 0; // 4 Bytes
uint32_t schemaOffset = 4096; // 4 Bytes
public:
TableHeader() = default;
// --- Serialization ---
void serialize(std::ofstream& out) const {
out.write(magic, sizeof(magic));
out.write(reinterpret_cast<const char*>(&version), sizeof(version));
out.write(reinterpret_cast<const char*>(&pageSize), sizeof(pageSize));
out.write(reinterpret_cast<const char*>(&numRecords), sizeof(numRecords));
out.write(reinterpret_cast<const char*>(&schemaOffset), sizeof(schemaOffset));
// Fill the rest of 4096 bytes with padding
char padding[4079] = {0};
out.write(padding, sizeof(padding));
}
// --- Deserialization ---
bool deserialize(std::ifstream& in) {
in.read(magic, sizeof(magic));
in.read(reinterpret_cast<char*>(&version), sizeof(version));
in.read(reinterpret_cast<char*>(&pageSize), sizeof(pageSize));
in.read(reinterpret_cast<char*>(&numRecords), sizeof(numRecords));
in.read(reinterpret_cast<char*>(&schemaOffset), sizeof(schemaOffset));
if (std::strncmp(magic, "MYTB", 4) != 0) {
std::cerr << "Invalid table file (magic mismatch)\n";
return false;
}
return true;
}
// --- Print header ---
void print() const {
std::cout << "Magic: " << std::string(magic, 4) << "\n";
std::cout << "Version: " << static_cast<int>(version) << "\n";
std::cout << "Page Size: " << pageSize << "\n";
std::cout << "Number of Records: " << numRecords << "\n";
std::cout << "Schema Offset: " << schemaOffset << "\n";
}
// --- Getters ---
uint32_t getPageSize() const { return pageSize; }
uint32_t getNumRecords() const { return numRecords; }
uint32_t getSchemaOffset() const { return schemaOffset; }
uint8_t getVersion() const { return version; }
// --- Setters ---
void setNumRecords(uint32_t num) { numRecords = num; }
void setSchemaOffset(uint32_t offset) { schemaOffset = offset; }
};
#endif // TABLEHEADER_H