-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.cc
175 lines (144 loc) · 5.11 KB
/
global.cc
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// Created by yichen_X on 2023/4/4.
//
#include "global.h"
/**
* Store memtable to disk, return sstable_cache
* @param memtable: memtable
* @param dir_path: the directory path of sstable
* @param time_stamp: time stamp for this sstable
* @param tag: tag for this sstable
*/
sstable_cache* global::store_memtable(skiplist *memtable,
const std::string &dir_path,
const uint64_t time_stamp,
const uint64_t tag) {
auto* new_sstable_cache = new sstable_cache();
/* set tag */
new_sstable_cache->set_tag(tag);
if(!utils::dirExists(dir_path)){
utils::mkdir(dir_path.c_str());
}
/* File Path & Add time stamp */
string file_path = dir_path + "/" + to_string(time_stamp) + "_" + to_string(tag) + ".sst";
ofstream file;
file.open(file_path, ios::out | ios::binary);
if(!file.is_open()){
return nullptr;
}
file.seekp(0);
/* Set time stamp */
new_sstable_cache->set_timestamp(time_stamp);
file.write((char*)&time_stamp, 8);
/* Set total num */
uint64_t length = memtable->get_length();
new_sstable_cache->set_num(length);
file.write((const char*)&length, 8);
/* Set min & max key */
uint64_t min_key = memtable->get_min_key();
new_sstable_cache->set_minkey(min_key);
file.write((const char*)&min_key, 8);
uint64_t max_key = memtable->get_max_key();
new_sstable_cache->set_maxkey(max_key);
file.write((const char*)&max_key, 8);
/* BloomFilter */
memtable->store_bloomfilter(new_sstable_cache);
char* bloom_buffer = new char[10240];
new_sstable_cache->bloomfilter->saveBloomFilter(bloom_buffer);
file.write(bloom_buffer, 10240);
delete[] bloom_buffer;
/* Index Area */
uint32_t offset = 10272 + 12 * length;
skiplist::node<uint64_t, string>* cur = memtable->head->next[0];
while(cur){
uint64_t cur_key = cur->get_key();
new_sstable_cache->Indexs.emplace_back(cur_key, offset);
file.write((const char*)&cur_key, 8);
file.write((const char*)&offset, 4);
offset += cur->get_value().size();
cur = cur->next[0];
}
/* Value Area */
cur = memtable->head->next[0];
while(cur){
string cur_value = cur->get_value();
auto cur_v_str = cur_value.c_str();
file.write((char*)(cur_v_str), cur_value.size());
cur = cur->next[0];
}
file.close();
return new_sstable_cache;
}
/**
* Read value from disk by given index item
* @param index_i: index item(key & value)
* @param dir_path: the directory path of sstable
* @param time_stamp: time stamp for this sstable
* @param tag: tag for this sstable
* @param value_size: value size to be set
*/
string global::read_disk(index_item* index_i,
const std::string &dir_path,
const uint64_t &time_stamp,
const uint64_t &tag,
uint32_t &value_size) {
if(!utils::dirExists(dir_path)) return "";
string file_path = dir_path + "/" + to_string(time_stamp) + "_" + to_string(tag) + ".sst";
ifstream file;
file.open(file_path, ios_base::in|ios_base::binary);
if(!file.is_open()) return "";
/* read value */
file.seekg(index_i->get_offset());
if(value_size == 0){
string value_str;
file >> value_str;
return value_str;
}
char* value = new char[value_size + 1];
value[value_size] = '\0';
file.read(value, value_size);
string value_str = value;
delete[] value;
return value_str;
}
/**
* Read file from disk and store information to sstable_cache
* @param file_path: file path of the file
*/
sstable_cache* global::read_sstable(const std::string &file_path) {
ifstream file;
file.open(file_path, ios_base::in|ios_base::binary);
if(!file.is_open()) return nullptr;
auto* new_sstable_cache = new sstable_cache();
/* read time stamp */
uint64_t time_stamp;
file.read((char*)&time_stamp, 8);
new_sstable_cache->set_timestamp(time_stamp);
/* read total num */
uint64_t num;
file.read((char*)&num, 8);
new_sstable_cache->set_num(num);
/* read min key */
uint64_t min_key;
file.read((char*)&min_key, 8);
new_sstable_cache->set_minkey(min_key);
/* read max key */
uint64_t max_key;
file.read((char*)&max_key, 8);
new_sstable_cache->set_maxkey(max_key);
/* read bloomfilter */
char* bloom_buffer = new char[10240];
file.read(bloom_buffer, 10240);
new_sstable_cache->bloomfilter = new bloomfilter();
new_sstable_cache->bloomfilter->loadBloomFilter(bloom_buffer);
/* read index area */
for(int i = 0; i < num; i++){
uint64_t key;
uint32_t offset;
file.read((char*)&key, 8);
file.read((char*)&offset, 4);
new_sstable_cache->Indexs.emplace_back(key, offset);
}
file.close();
return new_sstable_cache;
}