-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhbt.cpp
199 lines (174 loc) · 4.31 KB
/
mhbt.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include "disk.h"
#include "lsm.h"
#include "crypt.h"
#include "fs.h"
#include "cache.h"
#define MAX_LAYER 4
using namespace std;
int xxx = 0;
int transform(lsm_kv *lv, int size, lsm_kv **mhbtv) {
vector<lsm_kv> q[MAX_LAYER];
vector<lsm_kv> m;
for (int i = 0; i < MAX_LAYER; i++)
q[i].clear();
m.clear();
int pos = 0;
int len = BLOCK_SIZE / 16;
for (int i = 0; i < size; i++) {
q[0].push_back(lv[i]);
int j = 0;
while (q[j].size() == len){
int key = -1, mac = -1;
for (int k = 0; k < len; k++)
m.push_back(q[j][k]);
q[j + 1].push_back(lsm_kv(q[j][0].lba, key, mac, pos));
pos++;
q[j].clear();
j++;
}
}
for (int j = 0; j < MAX_LAYER; j++)
if (q[j].size() != 0 ){
int key = -1, mac = -1;
int padlen = len - q[j].size();
for (int i = 0; i < padlen; i++)
q[j].push_back(lsm_kv(-1, -1, -1, -1));
if (j != MAX_LAYER - 1){
q[j + 1].push_back(lsm_kv(q[j][0].lba, key, mac, pos));
}
for (int k = 0; k < len; k++)
m.push_back(q[j][k]);
pos++;
q[j].clear();
}
int mhbt_size = sizeof(lsm_kv) * m.size();
*mhbtv = new lsm_kv[m.size()];
memcpy(*mhbtv, &m[0], sizeof(lsm_kv) * m.size());
return mhbt_size;
}
void mhbt_write(lsm_kv *lv, int size, vector<char> &output) {
vector<lsm_kv> q[MAX_LAYER];
for (int i = 0; i < MAX_LAYER; i++)
q[i].clear();
output.clear();
int pos = 0;
int len = BLOCK_SIZE / 16;
char *cipher_text = new char[BLOCK_SIZE];
for (int i = 0; i < size; i++) {
q[0].push_back(lv[i]);
int j = 0;
while (q[j].size() == len){
int _key = -1, _mac = -1;
char plain_text[BLOCK_SIZE];
memcpy(plain_text, &q[j][0], BLOCK_SIZE);
char key[16];
char mac[16];
encrypt(plain_text, BLOCK_SIZE, &cipher_text, key, mac);
for (int i = 0; i < BLOCK_SIZE; i++)
output.push_back(cipher_text[i]);
memcpy(&_key, key, 4);
q[j + 1].push_back(lsm_kv(q[j][0].lba, _key, _mac, pos));
pos++;
q[j].clear();
j++;
}
}
for (int j = 0; j < MAX_LAYER; j++) {
int _key = -1, _mac = -1;
int padlen = len - q[j].size();
for (int i = 0; i < padlen; i++)
q[j].push_back(lsm_kv(-1, -1, -1, -1));
char plain_text[BLOCK_SIZE];
memcpy(plain_text, &q[j][0], BLOCK_SIZE);
char key[16];
char mac[16];
if (j != MAX_LAYER - 1)
encrypt(plain_text, BLOCK_SIZE, &cipher_text, key, mac);
else {
encrypt0(plain_text, BLOCK_SIZE, &cipher_text, mac);
}
for (int i = 0; i < BLOCK_SIZE; i++)
output.push_back(cipher_text[i]);
memcpy(&_key, key, 4);
if (j != MAX_LAYER - 1){
q[j + 1].push_back(lsm_kv(q[j][0].lba, _key, _mac, pos));
}
pos++;
q[j].clear();
}
delete[] cipher_text;
}
int read_mhbt(int num, int pos, int lba, lsm_kv *value){
int j;
lsm_kv *kv;
int len = BLOCK_SIZE / 16;
pos = pos / len - 1;
int _key = 0;
for (int layer = MAX_LAYER - 1; layer >= 0; layer--) {
kv = (lsm_kv*)find_in_cache(pos * 100 + num, mhbt_cache);
//printf("%d %d\n", pos*100+ num, kv == nullptr);
if (kv == nullptr){
//printf("sst block not find in cache! %d\n", xxx);
xxx++;
char *plain_text;
char buf[BLOCK_SIZE];
sst_read_pos(num, pos, buf);
char key[16];
char mac[16];
memcpy(key, &_key, 4);
for (int i = 4; i < 16; i++)
key[i] = 0;
decrypt(&plain_text, BLOCK_SIZE, buf, key, mac);
kv = (lsm_kv*)plain_text;
put_to_cache(pos * 100 + num, (char *)kv, mhbt_cache);
for (j = 0; j < len; j++)
if (kv[j].lba > lba || kv[j].lba == -1)
break;
if (j == 0) {
//printf("Not found in mhbt\n");
return 0;
}
pos = kv[j - 1].pba;
_key = kv[j - 1].key;
if (layer == 0){
if (kv[j - 1].lba == lba) {
*value = kv[j - 1];
//printf("Found in mhbt\n");
return 1;
}
else {
//printf("Not found in mhbt\n");
return 0;
}
}
free(plain_text);
}
else{
// printf("sst block find in cache!\n");
for (j = 0; j < len; j++)
if (kv[j].lba > lba || kv[j].lba == -1)
break;
if (j == 0) {
//printf("Not found in mhbt\n");
return 0;
}
pos = kv[j - 1].pba;
_key = kv[j - 1].key;
if (layer == 0){
if (kv[j - 1].lba == lba) {
*value = kv[j - 1];
//printf("Found in mhbt\n");
return 1;
}
else {
//printf("Not found in mhbt\n");
return 0;
}
}
}
}
}