Newer
Older
/*
* Copyright (c) 2015-2016, Luca Fulchir<luca@fulchir.it>, All rights reserved.
*
* This file is part of "libRaptorQ".
*
* libRaptorQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* libRaptorQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and a copy of the GNU Lesser General Public License
* along with libRaptorQ. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "RaptorQ/v1/common.hpp"
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
#include "RaptorQ/v1/RFC.hpp"
#include <cmath>
#include <future>
#include <memory>
#include <utility>
/////////////////////
//
// These templates are just a wrapper around the
// functionalities offered by the RaptorQ__v1::Impl namespace
// So if you want to see what the algorithm looks like,
// you are in the wrong place
//
/////////////////////
namespace RFC6330__v1 {
////////////////////
//// Free Functions
////////////////////
uint64_t RAPTORQ_API shared_cache_size (const uint64_t shared_cache);
bool RAPTORQ_API local_cache_size (const uint64_t local_cache);
uint64_t RAPTORQ_API get_shared_cache_size();
uint64_t RAPTORQ_API get_local_cache_size();
bool RAPTORQ_API set_thread_pool (const size_t threads,
const uint16_t max_block_concurrency,
const RaptorQ__v1::Work_State exit_type);
////////////////////
//// Encoder
////////////////////
template <typename Rnd_It, typename Fwd_It>
class RAPTORQ_API Encoder
{
public:
~Encoder() {}
Encoder (const Rnd_It data_from, const Rnd_It data_to,
const uint16_t min_subsymbol_size,
const uint16_t symbol_size,
const size_t max_memory)
{
rfc_encoder = std::unique_ptr<Impl::Encoder<Rnd_It, Fwd_It>> (
new Impl::Encoder<Rnd_It, Fwd_It> (
data_from, data_to, min_subsymbol_size,
symbol_size, max_memory));
}
Block_Iterator<Rnd_It, Fwd_It> begin ()
{
if (rfc_encoder != nullptr)
return rfc_encoder->begin();
return end();
}
const Block_Iterator<Rnd_It, Fwd_It> end ()
{
if (rfc_encoder != nullptr)
return rfc_encoder->end();
return Block_Iterator<Rnd_It, Fwd_It> (nullptr, Impl::Partition(), 1);
}
operator bool() const { return rfc_encoder != nullptr && (*rfc_encoder); }
RQ_OTI_Common_Data OTI_Common() const
{
if (rfc_encoder != nullptr)
return rfc_encoder->OTI_Common();
return 0;
}
RQ_OTI_Scheme_Specific_Data OTI_Scheme_Specific() const
{
if (rfc_encoder != nullptr)
return rfc_encoder->OTI_Scheme_Specific();
return 0;
}
// TODO: introduce memory limits on threading ?
std::future<std::pair<Error, uint8_t>> compute (const Compute flags)
{
if (rfc_encoder != nullptr)
return rfc_encoder->compute (flags);
std::promise<std::pair<Error, uint8_t>> p;
p.set_value ({Error::INITIALIZATION, 0});
return p.get_future();
}
size_t precompute_max_memory ()
{
if (rfc_encoder != nullptr)
return rfc_encoder->precompute_max_memory ();
return 0;
}
uint64_t encode (Fwd_It &output, const Fwd_It end, const uint32_t esi,
const uint8_t sbn)
{
if (rfc_encoder != nullptr)
return rfc_encoder->encode (output, end, esi, sbn);
return 0;
}
// id: 8-bit sbn + 24 bit esi
uint64_t encode (Fwd_It &output, const Fwd_It end, const uint32_t &id)
{
if (rfc_encoder != nullptr)
return rfc_encoder->encode (output, end, id);
return 0;
}
void free (const uint8_t sbn)
{
if (rfc_encoder != nullptr)
return rfc_encoder->free (sbn);
}
uint8_t blocks() const
{
if (rfc_encoder != nullptr)
return rfc_encoder->blocks();
return 0;
}
uint32_t block_size (const uint8_t sbn) const
{
if (rfc_encoder != nullptr)
return rfc_encoder->block_size (sbn);
return 0;
}
uint16_t symbol_size() const
{
if (rfc_encoder != nullptr)
return rfc_encoder->symbol_size();
return 0;
}
uint16_t symbols (const uint8_t sbn) const
{
if (rfc_encoder != nullptr)
return rfc_encoder->symbols (sbn);
return 0;
}
uint32_t max_repair (const uint8_t sbn) const
{
if (rfc_encoder != nullptr)
return rfc_encoder->max_repair (sbn);
return 0;
}
private:
std::unique_ptr<Impl::Encoder<Rnd_It, Fwd_It>> rfc_encoder;
};
////////////////////
//// Decoder
////////////////////
template <typename In_It, typename Fwd_It>
class RAPTORQ_API Decoder
{
public:
// rfc 6330, pg 6
// easy explanation for OTI_* comes next.
// we do NOT use bitfields as compilators are not actually forced to put
// them in any particular order. meaning tey're useless.
//
//union OTI_Common_Data {
// uint64_t raw;
// struct {
// uint64_t size:40;
// uint8_t reserved:8;
// uint16_t symbol_size:16;
// };
//};
//union OTI_Scheme_Specific_Data {
// uint32_t raw;
// struct {
// uint8_t source_blocks;
// uint16_t sub_blocks;
// uint8_t alignment;
// };
//};
Decoder (const RQ_OTI_Common_Data common,
const RQ_OTI_Scheme_Specific_Data scheme)
{
rfc_decoder = std::unique_ptr<Impl::Decoder<In_It, Fwd_It>> (
new Impl::Decoder<In_It, Fwd_It> (
common, scheme));
}
Decoder (const uint64_t size, const uint16_t symbol_size,
const uint16_t sub_blocks,
const uint8_t blocks,
const uint8_t alignment)
{
rfc_decoder = std::unique_ptr<Impl::Decoder<In_It, Fwd_It>> (
new Impl::Decoder<In_It, Fwd_It> (
size, symbol_size, sub_blocks,
blocks, alignment));
}
std::future<std::pair<Error, uint8_t>> compute (const Compute flags)
{
if (rfc_decoder != nullptr)
return rfc_decoder->compute (flags);
std::promise<std::pair<Error, uint8_t>> p;
p.set_value ({Error::INITIALIZATION, 0});
return p.get_future();
}
// result in BYTES
uint64_t decode_bytes (Fwd_It &start, const Fwd_It end, const uint8_t skip)
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->decode_bytes (start, end, skip);
}
uint64_t decode_block_bytes (Fwd_It &start, const Fwd_It end,
const uint8_t skip,
const uint8_t sbn)
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->decode_block_bytes (start, end, skip, sbn);
}
// result in ITERATORS
// last *might* be half written depending on data alignments
std::pair<size_t, uint8_t> decode_aligned (Fwd_It &start, const Fwd_It end,
const uint8_t skip)
{
if (rfc_decoder == nullptr)
return {0, 0};
return rfc_decoder->decode_aligned (start, end, skip);
}
std::pair<size_t, uint8_t> decode_block_aligned (Fwd_It &start,
const Fwd_It end,
const uint8_t skip,
const uint8_t sbn)
{
if (rfc_decoder == nullptr)
return {0, 0};
return rfc_decoder->decode_block_aligned (start, end, skip, sbn);
}
// id: 8-bit sbn + 24 bit esi
Error add_symbol (In_It &start, const In_It end, const uint32_t id)
{
if (rfc_decoder == nullptr)
return Error::INITIALIZATION;
return rfc_decoder->add_symbol (start, end, id);
}
Error add_symbol (In_It &start, const In_It end, const uint32_t esi,
const uint8_t sbn)
{
if (rfc_decoder == nullptr)
return Error::INITIALIZATION;
return rfc_decoder->add_symbol (start, end, esi, sbn);
}
void free (const uint8_t sbn)
{
if (rfc_decoder != nullptr)
return rfc_decoder->free (sbn);
}
uint64_t bytes() const
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->bytes();
}
uint8_t blocks() const
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->blocks();
}
uint32_t block_size (const uint8_t sbn) const
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->block_size (sbn);
}
uint16_t symbol_size() const
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->symbol_size();
}
uint16_t symbols (const uint8_t sbn) const
{
if (rfc_decoder == nullptr)
return 0;
return rfc_decoder->symbols (sbn);
}
private:
std::unique_ptr<Impl::Decoder<In_It, Fwd_It>> rfc_decoder;
};
} // namespace RFC6330__v1