-
Notifications
You must be signed in to change notification settings - Fork 125
/
rbzip2.cpp
243 lines (218 loc) · 7.49 KB
/
rbzip2.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
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
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <bzlib.h>
#include <cassert>
#include <iostream>
#include <raft>
#include <raftio>
#include <cmd>
#include <type_traits>
/**
* used interface found here:
* http://www.bzip.org/1.0.3/html/hl-interface.html
*/
template < class chunktype > class filewrite : public raft::parallel_k
{
public:
filewrite( const std::string filename,
const std::size_t n_output_ports = 1 ) : raft::parallel_k()
{
out_fp = std::fopen( filename.c_str(), "wb" );
if( out_fp == nullptr )
{
std::cerr << "failed to open output file, " << filename << "\n";
exit( EXIT_FAILURE );
}
using index_type = std::remove_const_t<decltype(n_output_ports)>;
for( index_type i( 0 ); i < n_output_ports; i++ )
{
addPortTo< chunktype >( input );
}
}
virtual ~filewrite()
{
std::fflush( out_fp );
std::fclose( out_fp );
}
virtual raft::kstatus run()
{
for( auto &port : input )
{
if( port.size() > 0 )
{
auto &ele( port.template peek< chunktype >() );
if( ele.index == curr_position )
{
std::fwrite( ele.buffer,
1,
ele.length,
out_fp );
curr_position++;
port.recycle( 1 );
}
}
}
return( raft::proceed );
}
private:
FILE *out_fp = nullptr;
std::size_t curr_position = 0;
};
template < class chunktype > class compress : public raft::kernel
{
public:
compress( const int blocksize,
const int verbosity,
const int workfactor ) : raft::kernel(),
blocksize( blocksize ),
verbosity( verbosity ),
workfactor( workfactor )
{
input.addPort< chunktype >( "in" );
output.addPort< chunktype >( "out" );
}
/** need a copy constructor for cloning **/
compress( const compress &other ) : raft::kernel(),
blocksize( other.blocksize ),
verbosity( other.verbosity ),
workfactor( other.workfactor )
{
input.addPort< chunktype >( "in" );
output.addPort< chunktype >( "out" );
}
virtual ~compress() = default;
virtual raft::kstatus run()
{
auto &in_ele( input[ "in" ].template peek< chunktype >() );
auto &out_ele( output[ "out" ].template allocate< chunktype >() );
unsigned int length_out( chunktype::getChunkSize() );
const auto ret_val( BZ2_bzBuffToBuffCompress( out_ele.buffer,
&length_out,
in_ele.buffer,
in_ele.length,
blocksize,
verbosity,
workfactor ) );
if( ret_val == BZ_OK )
{
out_ele.length = length_out;
out_ele.index = in_ele.index;
output[ "out" ].send();
input[ "in" ].recycle( 1 );
}
else
{
switch( ret_val )
{
case BZ_CONFIG_ERROR:
{
std::cerr << "if the library has been mis-compiled\n";
}
break;
case BZ_PARAM_ERROR:
{
std::cerr << "if dest is NULL or destLen is NULL\n";
std::cerr << "or blockSize100k < 1 or blockSize100k > 9\n";
std::cerr << "or verbosity < 0 or verbosity > 4\n";
std::cerr << "or workFactor < 0 or workFactor > 250\n";
}
break;
case BZ_MEM_ERROR:
{
std::cerr << "if insufficient memory is available\n";
}
break;
case BZ_OUTBUFF_FULL:
{
std::cerr << "if the size of the compressed data exceeds *destLen\n";
}
break;
default:
{
std::cerr << "undefined error\n";
}
}
/** we need to fail here **/
output[ "out" ].deallocate();
input[ "in" ].unpeek();
exit( EXIT_FAILURE );
}
return( raft::proceed );
}
/** enable cloning **/
CLONE();
private:
const int blocksize;
const int verbosity;
const int workfactor;
};
int
main( int argc, char **argv )
{
const static auto chunksize( 65536 );
using chunk_t = raft::filechunk< chunksize >;
using fr_t = raft::filereader< chunk_t, false >;
using fw_t = filewrite< chunk_t >;
using comp = compress< chunk_t >;
/** variables to set below **/
bool help( false );
int blocksize ( 9 );
int verbosity ( 0 );
int workfactor( 250 );
std::string inputfile( "" );
std::string outputfile( "" );
std::int64_t num_threads( 1 );
CmdArgs cmdargs( argv[ 0 ] /** prog name **/,
std::cout /** std stream **/,
std::cerr /** err stream **/ );
/** set options **/
cmdargs.addOption( new Option< bool >( help,
"-h",
"print this message" ) );
cmdargs.addOption( new Option< int >( blocksize,
"-b",
"set block size to 100k .. 900k" ) );
cmdargs.addOption( new Option< int >( workfactor,
"-e",
"effort" ) );
cmdargs.addOption( new Option< std::string >( inputfile,
"-i",
"input file",
true /** required **/ ) );
cmdargs.addOption( new Option< std::string >( outputfile,
"-o",
"output file",
true /** required **/ ) );
cmdargs.addOption( new Option< std::int64_t >( num_threads,
"-th",
"number of worker threads" ) );
/** process args **/
cmdargs.processArgs( argc, argv );
if( help || ! cmdargs.allMandatorySet() )
{
cmdargs.printArgs();
exit( EXIT_SUCCESS );
}
/** declare kernels **/
fr_t reader( inputfile,
0, /** no offset needed **/
num_threads /** manually set threads for b-marking **/ );
fw_t writer( outputfile,
num_threads /** manually set threads for b-marking **/ );
comp c( blocksize,
verbosity,
workfactor );
/** set-up map **/
raft::map m;
/**
* detect # output ports from reader,
* duplicate c that #, assign each
* output port to the input ports in
* writer
*/
m += reader <= c >= writer;
m.exe();
return( EXIT_SUCCESS );
}