-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.h
125 lines (95 loc) · 3.16 KB
/
Data.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Created by JakoError on 2022/11/10.
//
#ifndef DBMS_DATA_H
#define DBMS_DATA_H
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/shared_ptr.hpp>
#include <utility>
#include "DBMSExceptions.hpp"
#ifndef STR_LENGTH
#define STR_LENGTH 256
#endif //STR_LENGTH
#ifndef DATA_SIZE_TYPE
#define DATA_SIZE_TYPE int
#endif //DATA_SIZE_TYPE
using std::string;
using boost::filesystem::fstream;
using boost::filesystem::path;
namespace cppDBMS {
class Data {
public:
typedef DATA_SIZE_TYPE size_type;
typedef int type_num_type;
path dataPath;
static const int STR_LEN = STR_LENGTH;
static const size_t STR_SIZE = sizeof(char) * STR_LEN;
static const size_t INT_SIZE = sizeof(int);
enum type_num : type_num_type {
int_type_num = 0, str_type_num = 1
};
template<typename T>
type_num_type get_type_num() {
if (std::is_same<T, int>::value)
return int_type_num;
if (std::is_same<T, std::string>::value)
return str_type_num;
return -1;
}
virtual path get_data_path() {
return dataPath;
}
virtual void create() = 0;
virtual void drop() = 0;
virtual void load_data() = 0;
virtual void save_data() = 0;
virtual void release_data() = 0;
explicit Data(path dataPath) : dataPath(std::move(dataPath)) {}
};
static void read_value(fstream &f, int &value) {
f.read(reinterpret_cast<char *>(&value), sizeof(int));
}
static void read_value(fstream &f, string &value) {
char buff[STR_LENGTH];
f.read(buff, STR_LENGTH);
string take(buff, STR_LENGTH);
boost::trim(take);
value = take;
}
static void write_value(fstream &f, int value) {
f.write(reinterpret_cast<char *>(&value), sizeof(int));
}
static void write_value(fstream &f, string &value) {
if (value.length() > Data::STR_LEN)
BOOST_THROW_EXCEPTION(std::runtime_error("string max length " + std::to_string(Data::STR_LEN)));
f.write(value.c_str(), static_cast<std::streamsize>(value.length()));
string space(Data::STR_LEN - value.length(), ' ');
f.write(space.c_str(), static_cast<std::streamsize>(space.length()));
}
static string read(fstream &f, string::size_type len) {
boost::shared_ptr<char> buffer(new char[len]);
f.read(buffer.get(), static_cast<std::streamsize>(len));
return {buffer.get(), len};
}
template<typename T>
static T read(fstream &f) {
T v;
f.read(reinterpret_cast<char *>(&v), sizeof(T));
return v;
}
static void write(fstream &f, string &value) {
f.write(value.c_str(), static_cast<std::streamsize>(value.length()));
}
template<typename T>
static void write(fstream &f, T value) {
f.write(reinterpret_cast<char *>(&value), sizeof(T));
}
template<typename T>
static size_t hash(T value) {
std::hash<T> T_hash;
return T_hash(value);
}
}
#endif //DBMS_DATA_H