forked from Suxyuuu/SWP-of-searchable-encryption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cc
548 lines (489 loc) · 16.5 KB
/
server.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <iostream>
#include <memory>
#include <string>
#include <grpc++/grpc++.h>
#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include "protoc/rpc.grpc.pb.h"
#include "server.h"
#include "submodule/encryption.h"
#include <rocksdb/db.h>
#include <rocksdb/slice.h>
#include <rocksdb/options.h>
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
// 在默认路径创建一个新的数据库
// 输出:执行结果msg
void ServiceImpl::setupDB(std::string &msg)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
options.create_if_missing = true;
options.error_if_exists = true;
r_status = rocksdb::DB::Open(options, DBPATH, &db);
// 不存在数据库则建立新的并添加键值对,存在则不做改变
if (r_status.ok())
{
msg = "Setup Successfully! ";
}
else
{
msg = "DB Exists! If you want to update, you can use 'add' or 'delete'. ";
}
delete db;
}
// 查询数据库 支持用key查询 同时支持用value查询
// 输入:1-是否是key 2-是key则为key的值 不是key则为value
// 输出:1-是key则输出对应的value 2-不是key则返回所有与value相等的k—v对
void ServiceImpl::searchDB(std::vector<unsigned char> &Ki,
std::vector<unsigned char> &Xi,
std::vector<std::string> &hitted_cf,
std::vector<int> &kv_num,
std::vector<int> &key,
std::vector<std::vector<unsigned char>> &value,
std::string &msg)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::ColumnFamilyHandle *cf;
// std::vector<std::string> exist_cf;
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
std::vector<std::string> exist_cf;
rocksdb::DB::ListColumnFamilies(options, DBPATH, &exist_cf);
if (exist_cf.size() == 0)
{
msg = "There's no data in DB.";
}
else
{
for (auto &&columnfamily : exist_cf)
{
column_families.push_back(rocksdb::ColumnFamilyDescriptor(columnfamily, rocksdb::ColumnFamilyOptions()));
}
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
if (!r_status.ok())
{
msg = "Open DB Failed.";
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
}
else
{
std::vector<rocksdb::Iterator *> iterators;
db->NewIterators(rocksdb::ReadOptions(), handles, &iterators);
for (size_t i = 0; i < iterators.size(); i++)
{
for (iterators[i]->SeekToFirst(); iterators[i]->Valid(); iterators[i]->Next())
{
std::vector<unsigned char> Ci;
for (size_t j = 0; j < 24; j++)
{
Ci.push_back(iterators[i]->value().data()[j]);
}
if (search_swp(Ki, Xi, Ci))
{
hitted_cf.push_back(handles[i]->GetName());
break;
}
}
assert(iterators[i]->status().ok());
}
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
// Now already got the hitted cf
if (hitted_cf.size() == 0)
{
msg = "No matches!";
}
else
{
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
// std::cerr << r_status.ToString() << std::endl;
if (!r_status.ok())
{
msg = "Open DB Failed.";
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
}
else
{
std::vector<rocksdb::Iterator *> iterators;
db->NewIterators(rocksdb::ReadOptions(), handles, &iterators);
int kv_count = 0;
for (size_t i = 0; i < iterators.size(); i++)
{
int hit_flag = false;
std::string cfname = handles[i]->GetName();
for (auto &&hitcf : hitted_cf)
{
if (hitcf == cfname)
{
hit_flag = true;
}
}
if (!hit_flag)
{
continue;
}
for (iterators[i]->SeekToFirst(); iterators[i]->Valid(); iterators[i]->Next())
{
std::vector<unsigned char> word;
for (size_t j = 0; j < 24; j++)
{
word.push_back(iterators[i]->value().data()[j]);
}
value.push_back(word);
key.push_back(stoi(iterators[i]->key().ToString()));
kv_count++;
}
assert(iterators[i]->status().ok());
kv_num.push_back(kv_count);
kv_count = 0;
}
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
msg = "search successfully!";
}
}
}
}
}
// 添加k-v对
// 输入:1-key 2-value
// 输出:执行结果msg
bool ServiceImpl::addDB(int add_key, std::vector<unsigned char> &add_value, std::string &add_columnfamily)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::ColumnFamilyHandle *cf;
std::vector<std::string> exist_cf;
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
bool flag = false;
int handle_num = 0;
rocksdb::DB::ListColumnFamilies(options, DBPATH, &exist_cf);
for (size_t i = 0; i < exist_cf.size(); i++)
{
column_families.push_back(rocksdb::ColumnFamilyDescriptor(exist_cf[i], rocksdb::ColumnFamilyOptions()));
if (exist_cf[i] == add_columnfamily)
{
flag = true;
handle_num = i;
}
}
if (!flag)
{
// create a new columnfamily
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
if (r_status.ok())
{
r_status = db->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), add_columnfamily, &cf);
assert(r_status.ok());
column_families.push_back(rocksdb::ColumnFamilyDescriptor(add_columnfamily, rocksdb::ColumnFamilyOptions()));
r_status = db->DestroyColumnFamilyHandle(cf);
assert(r_status.ok());
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
}
else
{
delete db;
return false;
}
}
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
// add
if (r_status.ok())
{
char v[24];
for (int i = 0; i < 24; i++)
{
v[i] = add_value[i];
}
rocksdb::Slice key(std::to_string(add_key));
rocksdb::Slice value(v, 24);
if (flag)
{
r_status = db->Put(rocksdb::WriteOptions(), handles[handle_num], key, value);
}
else
{
r_status = db->Put(rocksdb::WriteOptions(), handles[handles.size() - 1], key, value);
}
if (r_status.ok())
{
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
return true;
}
else
{
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
return false;
}
}
else
{
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
return false;
}
}
// 删除k-v对
// 输入:1-是否是key 2-是key则为key的值 不是key则为value
// 输出:删除结果
void ServiceImpl::deleteDB(const std::string &del_cf, std::string &msg)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::ColumnFamilyHandle *cf;
std::vector<std::string> exist_cf;
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
bool flag = false;
int handle_num = 0;
rocksdb::DB::ListColumnFamilies(options, DBPATH, &exist_cf);
for (size_t i = 0; i < exist_cf.size(); i++)
{
column_families.push_back(rocksdb::ColumnFamilyDescriptor(exist_cf[i], rocksdb::ColumnFamilyOptions()));
if (exist_cf[i] == del_cf)
{
flag = true;
handle_num = i;
}
}
if (!flag)
{
msg = "Delete Failed! No columnfamily equals " + del_cf + ".";
}
else
{
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
assert(r_status.ok());
r_status = db->DropColumnFamily(handles[handle_num]);
assert(r_status.ok());
msg = "Delete Successfully!";
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
}
}
// 展示数据库所有数据 以k-v对的形式展示
bool ServiceImpl::showDB(std::vector<std::string> &exist_cf,
std::vector<int> &kv_num,
std::vector<int> &key,
std::vector<std::vector<unsigned char>> &value)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::ColumnFamilyHandle *cf;
// std::vector<std::string> exist_cf;
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
rocksdb::DB::ListColumnFamilies(options, DBPATH, &exist_cf);
if (exist_cf.size() == 0)
{
return true; // 只是为空 但操作是成功的
}
else
{
for (auto &&columnfamily : exist_cf)
{
// std::cout << columnfamily << std::endl;
column_families.push_back(rocksdb::ColumnFamilyDescriptor(columnfamily, rocksdb::ColumnFamilyOptions()));
}
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
if (!r_status.ok())
{
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
return false;
}
else
{
std::vector<rocksdb::Iterator *> iterators;
db->NewIterators(rocksdb::ReadOptions(), handles, &iterators);
int kv_count = 0;
for (size_t i = 0; i < iterators.size(); i++)
{
for (iterators[i]->SeekToFirst(); iterators[i]->Valid(); iterators[i]->Next())
{
std::vector<unsigned char> word;
for (size_t j = 0; j < 24; j++)
{
word.push_back(iterators[i]->value().data()[j]);
}
value.push_back(word);
key.push_back(stoi(iterators[i]->key().ToString()));
kv_count++;
}
assert(iterators[i]->status().ok());
kv_num.push_back(kv_count);
kv_count = 0;
}
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
return true;
}
}
}
// 根据client产生的随机数据生成数据库(需要数据库已经存在且内无数据)
// 输入:1-多个key值 2-多个value值
// 输出:执行结果
void ServiceImpl::rangenDB(std::vector<std::string> &cf_name,
std::vector<int> &every_cf_kv_num,
std::vector<int> &keyv,
std::vector<std::vector<unsigned char>> &valuev,
std::string &msg)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::ColumnFamilyHandle *cf;
// std::vector<std::string> exist_cf;
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
std::vector<rocksdb::ColumnFamilyHandle *> handles;
r_status = rocksdb::DB::Open(options, DBPATH, &db);
for (auto &&add_cf : cf_name)
{
r_status = db->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), add_cf, &cf);
assert(r_status.ok());
column_families.push_back(rocksdb::ColumnFamilyDescriptor(add_cf, rocksdb::ColumnFamilyOptions()));
r_status = db->DestroyColumnFamilyHandle(cf);
assert(r_status.ok());
}
column_families.push_back(rocksdb::ColumnFamilyDescriptor(rocksdb::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));
delete db;
r_status = rocksdb::DB::Open(rocksdb::DBOptions(), DBPATH, column_families, &handles, &db);
// std::cerr << r_status.ToString() << std::endl;
if (!r_status.ok())
{
msg = "Open DB Failed!";
}
else
{
rocksdb::WriteBatch batch;
int count = 0;
for (size_t i = 0; i < cf_name.size(); i++)
{
for (size_t j = 0; j < every_cf_kv_num[i]; j++)
{
char v[24];
for (int m = 0; m < 24; m++)
{
v[m] = valuev[j + count][m];
}
rocksdb::Slice key(std::to_string(keyv[j + count]));
rocksdb::Slice value(v, 24);
batch.Put(handles[i], key, value);
key.clear();
value.clear();
}
count += every_cf_kv_num[i];
}
r_status = db->Write(rocksdb::WriteOptions(), &batch);
if (!r_status.ok())
{
msg = "GenDB Add Failed!";
}
else
{
msg = "GenDB Successfully!";
}
for (auto handle : handles)
{
r_status = db->DestroyColumnFamilyHandle(handle);
assert(r_status.ok());
}
delete db;
}
}
// 删除数据库本身
// 输出:执行结果
void ServiceImpl::destroyDB(std::string &msg)
{
rocksdb::DB *db;
rocksdb::Options options;
rocksdb::Status r_status;
rocksdb::DestroyDB(DBPATH, options);
// options.error_if_exists = true;
r_status = rocksdb::DB::Open(options, DBPATH, &db);
if (r_status.ok())
{
msg = "Destroy Failed!";
}
else
{
msg = "Destroy Successfully! ";
}
delete db;
}
void RunServer()
{
std::string server_address("0.0.0.0:50051");
ServiceImpl service;
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl;
server->Wait();
}
int main(int argc, char **argv)
{
RunServer();
return 0;
}