-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_table.cc
100 lines (81 loc) · 2.03 KB
/
test_table.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
#include <iostream>
#include <vector>
#include <string>
#include <cinttypes>
#include <time.h>
#include <sys/time.h>
#include "table/table.h"
#include "table/table_builder.h"
#include "util/slice.h"
#include "util/env.h"
#include "util/status.h"
int num = 1000;
int val_size = 100;
uint64_t file_size;
std::string test_fname = std::string("./test.sst");
void check_status(leveldb::Status s){
if(!s.ok()){
std::cout << s.ToString() << std::endl;
exit(0);
}
}
void test_write(){
leveldb::Status s;
leveldb::Env *env = leveldb::Env::Default();
// new writable file;
leveldb::WritableFile *file;
s = env->NewWritableFile(test_fname, &file);
check_status(s);
// new builder;
leveldb::Options op = leveldb::Options();
leveldb::TableBuilder *builder = new leveldb::TableBuilder(op, file);
srand(0x12345678);
for (int i = 0; i < num; ++i)
{
std::string key = std::to_string(rand());
std::string value = std::string(val_size, key[0]);
builder->Add(key, value);
}
builder->Finish();
file_size = builder->FileSize();
delete builder;
delete file;
}
void test_read(){
leveldb::Status s;
leveldb::Env *env = leveldb::Env::Default();
// new writable file;
leveldb::RandomAccessFile *file;
s = env->NewRandomAccessFile(test_fname, &file);
check_status(s);
// new table reader;
leveldb::Options op = leveldb::Options();
leveldb::Table *table;
s = leveldb::Table::Open(op, file, file_size, &table);
check_status(s);
// read by iterator;
int count = 0;
leveldb::ReadOptions r_op = leveldb::ReadOptions();
leveldb::Iterator *iter = table->NewIterator(r_op);
iter->SeekToFirst();
while(iter->Valid()){
std::string key = iter->key().data();
std::string value = iter->value().data();
if(key[0] != value[0]){
std::cout << "Wrong value; key:[" << key << "]; value: [" << value << "]" << std::endl;
break;
}
++count;
iter->Next();
}
std::cout << "Total valid items: " << count << std::endl;
delete iter;
delete table;
delete file;
}
int main(int argc, char const *argv[])
{
test_write();
test_read();
return 0;
}