-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_struct.h
129 lines (114 loc) · 3.02 KB
/
file_struct.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
126
127
128
129
//
// Created by xmh on 23-11-18.
//
#ifndef MMKV_FILE_STRUCT_H
#define MMKV_FILE_STRUCT_H
#include <string>
#include <map>
#include <list>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <unistd.h>
#include <sys/mman.h>
#include <boost/serialization/map.hpp>
#include <boost/serialization/list.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/array.hpp>
//key固定是string
//value使用boost_serialization来序列化
//一个MMKV对象对应3个文件:filename(存储序列化的数据)
//filename.used(存储used_map)
//filename.list(存储used区域key的链表)
using offset = unsigned long long;
using mmkv_size = unsigned long long;
using mmkv_key = std::string;
struct Area{
offset of1;
offset of2;
Area()=default;
Area(offset o1, offset o2):of1(o1), of2(o2){};
};
//左闭右开
struct UsedContentArea{
offset of1;
offset of2;
std::string valueType;
UsedContentArea()=default;
UsedContentArea(offset o1, offset o2):of1(o1), of2(o2){}
UsedContentArea(offset o1, offset o2, std::string type):of1(o1), of2(o2), valueType(type){}
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & of1;
ar & of2;
ar & valueType;
}
};
//左闭右开
struct FreeContentArea{
offset of1;
offset of2;
FreeContentArea()=default;
FreeContentArea(offset o1, offset o2):of1(o1), of2(o2){}
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & of1;
ar & of2;
}
};
struct UsedTable{
std::map<std::string, UsedContentArea> used_table;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & used_table;
}
};
struct FreeTable{
std::multimap<mmkv_size, FreeContentArea> free_table;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & free_table;
}
};
struct UsedAreaList{
std::list<std::string> usedList;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & usedList;
}
};
struct StringData{
std::string value;
StringData(std::string&& d):value(std::move(d)){}
StringData()= default;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & value;
}
};
template<typename T>
struct Data{
T value;
Data(T d):value(d){};
Data()= default;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & value;
}
};
struct Tag{
std::string tag;
template<typename Archive>
void serialize(Archive & ar, const unsigned int version){
ar & tag;
}
};
template<typename T>
std::string serializeData(T data);
#endif //MMKV_FILE_STRUCT_H