-
Notifications
You must be signed in to change notification settings - Fork 60
/
query_kv.cc
133 lines (117 loc) · 3.57 KB
/
query_kv.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
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <ctime>
#include <sys/time.h>
#include <sstream>
#include "succinct_shard.h"
/**
* Prints usage.
*/
void print_usage(char *exec) {
fprintf(stderr, "Usage: %s [-m mode] [file]\n", exec);
}
void print_valid_cmds() {
std::cerr
<< "Command must be one of: search [query], count [query], get [key]\n";
}
typedef unsigned long long int timestamp_t;
static timestamp_t get_timestamp() {
struct timeval now{};
gettimeofday(&now, nullptr);
return (now.tv_usec + (time_t) now.tv_sec * 1000000);
}
int main(int argc, char **argv) {
if (argc < 2 || argc > 5) {
print_usage(argv[0]);
return -1;
}
int c;
uint32_t mode = 0;
while ((c = getopt(argc, argv, "m:")) != -1) {
switch (c) {
case 'm':
mode = atoi(optarg);
break;
default:
fprintf(stderr, "Invalid option %c\n", c);
exit(0);
}
}
if (optind == argc) {
print_usage(argv[0]);
return -1;
}
std::string filename = std::string(argv[optind]);
SuccinctShard *s_file = nullptr;
if (mode == 0) {
// If mode is set to 0, compress the input file.
// Use default parameters.
std::cout << "Constructing Succinct data structures...\n";
s_file = new SuccinctShard(0, filename);
std::cout << "Serializing Succinct data structures...\n";
s_file->Serialize(filename + ".succinct");
} else {
// If mode is set to 1, read the serialized data structures from disk.
// The serialized data structures must exist at <filename>.succinct.
std::cout << "De-serializing Succinct data structures...\n";
s_file = new SuccinctShard(0, filename, SuccinctMode::LOAD_IN_MEMORY);
}
std::cout << "Done. Starting Succinct Shell...\n";
print_valid_cmds();
while (true) {
char cmd_line[500];
std::cout << "succinct> ";
std::cin.getline(cmd_line, sizeof(cmd_line));
std::istringstream iss(cmd_line);
std::string cmd, arg;
int64_t key;
if (!(iss >> cmd)) {
std::cerr << "Could not parse command: " << cmd_line << "\n";
continue;
}
if (cmd == "search") {
if (!(iss >> arg)) {
std::cerr << "Could not parse argument: " << cmd_line << "\n";
continue;
}
std::set<int64_t> results;
timestamp_t start = get_timestamp();
s_file->Search(results, arg);
timestamp_t tot_time = get_timestamp() - start;
std::cout << "Found " << results.size() << " records in " << tot_time
<< "us; Matching keys:\n";
for (auto res : results) {
std::cout << res << ", ";
}
std::cout << std::endl;
} else if (cmd == "count") {
if (!(iss >> arg)) {
std::cerr << "Could not parse argument: " << cmd_line << "\n";
continue;
}
timestamp_t start = get_timestamp();
int64_t count = s_file->Count(arg);
timestamp_t tot_time = get_timestamp() - start;
std::cout << "Number of matching records = " << count << "; Time taken: "
<< tot_time << "us\n";
} else if (cmd == "get") {
if (!(iss >> key)) {
std::cerr << "Could not parse argument: " << cmd_line << "\n";
continue;
}
timestamp_t start = get_timestamp();
std::string result;
s_file->Get(result, key);
timestamp_t tot_time = get_timestamp() - start;
std::cout << "Value = " << result << "; Time taken: "
<< tot_time << "us\n";
} else if (cmd == "exit") {
break;
} else {
std::cerr << "Unsupported command: " << cmd << std::endl;
print_valid_cmds();
}
}
return 0;
}