-
Notifications
You must be signed in to change notification settings - Fork 60
/
compress.cc
167 lines (152 loc) · 4.86 KB
/
compress.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
#include <iostream>
#include <unistd.h>
#include "succinct_shard.h"
#include "succinct_file.h"
#include "npa/npa.h"
/**
* Example program that takes an input file and compresses it using Succinct.
*/
/**
* Prints usage
*/
void print_usage(char *exec) {
fprintf(
stderr,
"Usage: %s [-s sa_sampling_rate] [-i isa_sampling_rate] [-x sampling_scheme] [-n npa_sampling_rate] [-r npa_encoding_scheme] [-t input_type] [file]\n",
exec);
}
/**
* Converts integer option to SamplingScheme
*/
SamplingScheme SamplingSchemeFromOption(int opt) {
switch (opt) {
case 0: {
fprintf(stderr, "Sampling Scheme = Flat Sample by Index\n");
return SamplingScheme::FLAT_SAMPLE_BY_INDEX;
}
case 1: {
fprintf(stderr, "Sampling Scheme = Flat Sample by Value\n");
return SamplingScheme::FLAT_SAMPLE_BY_VALUE;
}
case 2: {
fprintf(stderr, "Sampling Scheme = Layered Sample by Index\n");
return SamplingScheme::LAYERED_SAMPLE_BY_INDEX;
}
case 3: {
fprintf(stderr,
"Sampling Scheme = Opportunistic Layered Sample by Index\n");
return SamplingScheme::OPPORTUNISTIC_LAYERED_SAMPLE_BY_INDEX;
}
default: {
fprintf(stderr, "Sampling Scheme = Flat Sample by Index\n");
return SamplingScheme::FLAT_SAMPLE_BY_INDEX;
}
}
}
/**
* Converts integer option to NPAEncodingScheme
*/
NPA::NPAEncodingScheme EncodingSchemeFromOption(int opt) {
switch (opt) {
case 0: {
fprintf(stderr, "NPA Encoding Scheme = Elias Delta\n");
return NPA::NPAEncodingScheme::ELIAS_DELTA_ENCODED;
}
case 1: {
fprintf(stderr, "NPA Encoding Scheme = Elias Gamma\n");
return NPA::NPAEncodingScheme::ELIAS_GAMMA_ENCODED;
}
case 2: {
fprintf(stderr, "NPA Encoding Scheme = Wavelet Tree\n");
return NPA::NPAEncodingScheme::WAVELET_TREE_ENCODED;
}
default: {
fprintf(stderr, "NPA Encoding Scheme = Elias Gamma\n");
return NPA::NPAEncodingScheme::ELIAS_GAMMA_ENCODED;
}
}
}
int main(int argc, char **argv) {
// Logic to parse command line arguments
// starts here ==>
if (argc < 2 || argc > 12) {
print_usage(argv[0]);
return -1;
}
int c;
uint32_t sa_sampling_rate = 32;
uint32_t isa_sampling_rate = 32;
uint32_t npa_sampling_rate = 128;
SamplingScheme sampling_scheme = SamplingScheme::FLAT_SAMPLE_BY_INDEX;
NPA::NPAEncodingScheme npa_encoding_scheme =
NPA::NPAEncodingScheme::ELIAS_GAMMA_ENCODED;
std::string type = "file";
while ((c = getopt(argc, argv, "s:i:x:n:r:t:")) != -1) {
switch (c) {
case 's': {
sa_sampling_rate = atoi(optarg);
break;
}
case 'i': {
isa_sampling_rate = atoi(optarg);
break;
}
case 'x': {
sampling_scheme = SamplingSchemeFromOption(atoi(optarg));
break;
}
case 'n': {
npa_sampling_rate = atoi(optarg);
break;
}
case 'r': {
npa_encoding_scheme = EncodingSchemeFromOption(atoi(optarg));
break;
}
case 't': {
type = optarg;
break;
}
default: {
fprintf(stderr, "Error parsing options\n");
exit(-1);
}
}
}
if (optind == argc) {
print_usage(argv[0]);
return -1;
}
std::string inputpath = std::string(argv[optind]);
// <== ends here
if (type == "file") {
// The following compresses an input file at "inputpath" in memory
// as a flat file (no structure) using the compression parameters
// passed in (sampling rates, etc.).
// Leave the arguments unspecified to use default values.
auto *fd = new SuccinctFile(inputpath,
SuccinctMode::CONSTRUCT_IN_MEMORY,
sa_sampling_rate, isa_sampling_rate,
npa_sampling_rate, sampling_scheme,
sampling_scheme, npa_encoding_scheme);
// Serialize the compressed representation to disk at the location <inputpath>.succinct
fd->Serialize(inputpath + ".succinct");
delete fd;
} else if (type == "kv") {
// The following compresses an input file at "inputpath" in memory
// as a buffer containing key-value pairs. It uses newline '\n' to
// differentiate between successive values, and assigns the line number
// as the key for the corresponding value.
auto *fd = new SuccinctShard(0, inputpath,
SuccinctMode::CONSTRUCT_IN_MEMORY,
sa_sampling_rate, isa_sampling_rate,
npa_sampling_rate, sampling_scheme,
sampling_scheme, npa_encoding_scheme);
// Serialize the compressed representation to disk at the location <inputpath>.succinct
fd->Serialize(inputpath + ".succinct");
delete fd;
} else {
fprintf(stderr, "Invalid type: %s\n", type.c_str());
}
return 0;
}