-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchema.h
More file actions
91 lines (75 loc) · 2.31 KB
/
Copy pathSchema.h
File metadata and controls
91 lines (75 loc) · 2.31 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
#ifndef SCHEMA_H
#define SCHEMA_H
#include <cstdint>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
enum class ColumnType : uint8_t {
INT = 1,
TEXT = 2,
FLOAT = 3,
};
class Column {
private:
std::string name;
ColumnType type;
public:
Column() = default;
Column(const std::string& name, ColumnType type) : name(name), type(type) {}
// Serialize Column
void serialize(std::ofstream& out) const {
uint8_t nameLength = name.size();
out.write(reinterpret_cast<const char*>(&nameLength), sizeof(nameLength));
out.write(name.c_str(), name.size());
out.write(reinterpret_cast<const char*>(&type), sizeof(type));
}
// Deserialize Column
bool deserialize(std::ifstream& in) {
uint8_t nameLength;
if (!(in.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength)))) return false;
name.resize(nameLength);
if (!(in.read(&name[0], nameLength))) return false;
if (!(in.read(reinterpret_cast<char*>(&type), sizeof(type)))) return false;
return true;
}
const std::string& getName() const { return name; }
ColumnType getType() const { return type; }
void print() const {
std::cout << "Column Name: " << name << ", Type: " << static_cast<int>(type) << "\n";
}
};
class Schema {
private:
std::vector<Column> columns;
public:
Schema() = default;
void addColumn(const std::string& name, ColumnType type) {
columns.emplace_back(name, type);
}
void serialize(std::ofstream& out) const {
uint32_t numColumns = columns.size();
out.write(reinterpret_cast<const char*>(&numColumns), sizeof(numColumns));
for (const auto& column : columns) {
column.serialize(out);
}
}
bool deserialize(std::ifstream& in) {
uint32_t numColumns;
if (!(in.read(reinterpret_cast<char*>(&numColumns), sizeof(numColumns)))) return false;
columns.resize(numColumns);
for (auto& column : columns) {
if (!column.deserialize(in)) return false;
}
return true;
}
void print() const {
for (const auto& column : columns) {
column.print();
}
}
const std::vector<Column>& getColumns() const {
return columns;
}
};
#endif