/
ChannelSerializer.d
440 lines (335 loc) · 13.5 KB
/
ChannelSerializer.d
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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*******************************************************************************
DHT channel serializer helper.
Helper class template for dumping a cached or mirrored channel from a map
(of some kind) in memory to disk, then loading it again at startup. The
dumping can be done in either a blocking way (e.g. at application shutdown)
or asynchronously via `fork` (e.g. periodically while the application is
running).
Notes:
* In-memory data structure: Is assumed to be some kind of map, from
`hash_t` keys to `Contiguous!(S)` values (where `S` is the type of
the deserialized struct).
* Blocking dump: Dumps the map without forking. If an asynchronous dump
is already in progress, the call will block until the dump is
finished, then start it again.
* Asynchronous dump: When an asynchronous dump is in progress, further
calls to `dump` will not start a second, clashing dump; the dump
method will simply return.
Copyright:
Copyright (c) 2018 dunnhumby Germany GmbH. All rights reserved.
License:
Boost Software License Version 1.0. See LICENSE.txt for details.
*******************************************************************************/
module dhtproto.client.helper.ChannelSerializer;
import ocean.transition;
import ocean.io.device.File;
import ocean.io.serialize.SimpleStreamSerializer;
import ocean.io.stream.Buffered;
import ocean.sys.SafeFork;
import ocean.util.container.map.Map;
import ocean.util.serialize.contiguous.Contiguous;
import ocean.util.serialize.contiguous.Deserializer;
import ocean.util.serialize.contiguous.MultiVersionDecorator;
import ocean.util.serialize.contiguous.Serializer;
import ocean.util.serialize.Version;
/*******************************************************************************
DHT channel serializer helper.
Params:
S = type of record value stored in cached / mirrored channel
*******************************************************************************/
public class ChannelSerializer ( S )
{
/// Path of the dump file.
private istring path;
/// Buffer for formatting temporary paths (see forkEntryPoint()).
private mstring temp_path;
/// Buffered output instance.
private BufferedOutput buffered_output;
/// Buffered input instance.
private BufferedInput buffered_input;
/// Fork helper for asynchronous dumping.
private SafeFork fork;
/// Delegate type required by `dump`.
private alias void delegate ( void delegate ( hash_t, ref Contiguous!(S) ) )
DumpRecordDg;
/// Delegate type required by `load`.
private alias void delegate ( hash_t, ref Contiguous!(S) ) LoadRecordDg;
/// Delegate used by the fork for providing the records to be dumped. (Note
/// that the fork copy-on-write behaviour means it's safe for this value to
/// be changed while a fork is running.)
private DumpRecordDg record_dump_dg;
/// Buffer used for de/serialization of record values.
private void[] serialize_buf;
/// Version decorator, for record value structs that have version info.
static if ( Version.Info!(S).exists )
private VersionDecorator version_decorator;
/***************************************************************************
Constructor.
Params:
path = path of the dump file
***************************************************************************/
public this ( istring path )
{
static immutable buffer_size = 64 * 1024;
this.path = path;
this.buffered_output = new BufferedOutput(null, buffer_size);
this.buffered_input = new BufferedInput(null, buffer_size);
this.fork = new SafeFork(&this.forkEntryPoint);
static if ( Version.Info!(S).exists )
this.version_decorator = new VersionDecorator;
}
/***************************************************************************
Dumps the provided associative array to disk.
Params:
aa = associative array to dump
block = if true, performs a blocking dump; otherwise performs an
asynchronous dump (see module header)
Returns:
true if a dump occurred; false if it was skipped
***************************************************************************/
public bool dump ( Contiguous!(S)[hash_t] aa, bool block = false )
{
void dumpAA ( scope void delegate ( hash_t, ref Contiguous!(S) ) dump_record )
{
foreach ( k, v; aa)
dump_record(k, v);
}
return this.dump(&dumpAA, block);
}
/***************************************************************************
Dumps the provided map to disk.
Params:
map = map to dump
block = if true, performs a blocking dump; otherwise performs an
asynchronous dump (see module header)
Returns:
true if a dump occurred; false if it was skipped
***************************************************************************/
public bool dump ( Map!(Contiguous!(S), hash_t) map, bool block = false )
{
void dumpMap ( scope void delegate ( hash_t, ref Contiguous!(S) ) dump_record )
{
foreach ( k, v; map)
dump_record(k, v);
}
return this.dump(&dumpMap, block);
}
/***************************************************************************
Dumps an arbitrary key-value data structure to disk.
Params:
record_dump_dg = delegate to receive a delegate to be called once
per record to dump
block = if true, performs a blocking dump; otherwise performs an
asynchronous dump (see module header)
Returns:
true if a dump occurred; false if it was skipped
***************************************************************************/
public bool dump ( scope DumpRecordDg record_dump_dg, bool block = false )
{
this.record_dump_dg = record_dump_dg;
return this.fork.call(block);
}
/***************************************************************************
Loads an associative array from disk.
Params:
aa = associative array to load into
***************************************************************************/
public void load ( ref Contiguous!(S)[hash_t] aa )
{
void loadRecord ( hash_t k, ref Contiguous!(S) v )
{
aa[k] = v;
}
this.load(&loadRecord);
}
/***************************************************************************
Loads a map from disk.
Params:
map = map to load into
***************************************************************************/
public void load ( Map!(Contiguous!(S), hash_t) map )
{
void loadRecord ( hash_t k, ref Contiguous!(S) v )
{
(*map.put(k)) = v;
}
this.load(&loadRecord);
}
/***************************************************************************
Loads an arbitrary key-value data structure from disk.
Params:
record_load_dg = delegate to be called once per loaded record
***************************************************************************/
public void load ( scope LoadRecordDg record_load_dg )
{
size_t num_records;
scope file = new File(this.path, File.ReadExisting);
this.buffered_input.input(file);
this.buffered_input.clear();
SimpleStreamSerializer.read(this.buffered_input, num_records);
for ( size_t i = 0; i < num_records; i++ )
{
hash_t k;
Contiguous!(S) v;
SimpleStreamSerializer.read(this.buffered_input, k);
SimpleStreamSerializer.read(this.buffered_input, this.serialize_buf);
static if ( Version.Info!(S).exists )
v = this.version_decorator.loadCopy!(T)(this.serialize_buf, v);
else
Deserializer.deserialize(this.serialize_buf, v);
record_load_dg(k, v);
}
}
/***************************************************************************
SafeFork entry point. Calls `this.new_record_dump_dg` to dump the
provided data structure to disk.
***************************************************************************/
private void forkEntryPoint ( )
{
size_t num_records;
// Open file (with a temp name) and initialise buffer.
scope file = new TempFile(this.path, this.temp_path);
this.buffered_output.output(file);
this.buffered_output.clear();
// Once the file is written successfully, swap it to the real path.
scope ( success )
{
this.buffered_output.flush();
file.swap();
}
scope dumpRecord =
( hash_t k, ref Contiguous!(S) v )
{
// Serialize record.
static if ( Version.Info!(S).exists )
VersionDecorator.store!(S)(*(v.ptr), this.serialize_buf);
else
Serializer.serialize(*(v.ptr), this.serialize_buf);
// Write record key and serialized value.
SimpleStreamSerializer.write(this.buffered_output, k);
SimpleStreamSerializer.write(this.buffered_output,
this.serialize_buf);
num_records++;
};
// Write dummy file length.
SimpleStreamSerializer.write(this.buffered_output, size_t.init);
// Write all records sent by the dump delegate.
this.record_dump_dg(dumpRecord);
this.buffered_output.flush();
// Write real file length.
this.buffered_output.seek(0);
SimpleStreamSerializer.write(this.buffered_output, num_records);
}
}
version ( UnitTest )
{
import ocean.util.container.map.HashMap;
/// Record value struct used in all exampels and unittests.
struct S
{
int i;
bool b;
cstring str;
}
}
// Instantiate template to check compilation.
unittest
{
ChannelSerializer!(S) ser;
}
/// Example of dumping a channel stored in the form of a HashMap of
/// Contiguous!(S) records.
unittest
{
void dumpChannelFromMap ( HashMap!(Contiguous!(S)) map )
{
auto ser = new ChannelSerializer!(S)("test_channel");
ser.dump(map, true);
}
}
/// Example of loading a channel into a HashMap of Contiguous!(S) records.
unittest
{
void loadChannelIntoMap ( HashMap!(Contiguous!(S)) map )
{
auto ser = new ChannelSerializer!(S)("test_channel");
ser.load(map);
}
}
/// Example of dumping a channel stored in an arbitrary container that can be
/// iterated over.
unittest
{
void dumpChannelFromContainer ( )
{
void containerIterator (
scope void delegate ( hash_t, ref Contiguous!(S) ) dump_record )
{
Contiguous!(S) v;
// Iterate over your container and call `dump_record` once per
// record. (In this example, we just iterate over an imaginary
// container with a for loop.)
for ( hash_t k = 0; k < 10; k++ )
dump_record(k, v);
}
auto ser = new ChannelSerializer!(S)("test_channel");
ser.dump(&containerIterator, true);
}
}
/// Example of loading a channel into an arbitrary container.
unittest
{
void loadChannelIntoContainer ( )
{
void containerInsert ( hash_t k, ref Contiguous!(S) v )
{
// Insert the provided record into your container.
}
auto ser = new ChannelSerializer!(S)("test_channel");
ser.load(&containerInsert);
}
}
/// System call. (Not defined in the runtime.)
extern ( C )
{
int mkstemp(char *path_template);
}
/*******************************************************************************
Helper class to write to a file in two steps:
1. Opens a temp file to write to.
2. Once file writing has finished (and succeeded), swap the path of the
temp file to the final path.
*******************************************************************************/
private class TempFile : File
{
import ocean.core.Array : concat;
import core.stdc.stdio : rename;
/// Final path of file.
private cstring final_path;
/// Pointer to buffer to format temp file name.
private mstring* temp_path;
/***************************************************************************
Opens a temp file ready for writing.
Params:
path = final file path
temp_path = pointer to buffer to format temp file name
***************************************************************************/
public this ( cstring final_path, ref mstring temp_path )
{
this.final_path = final_path;
this.temp_path = &temp_path;
(*this.temp_path).concat(this.final_path, "XXXXXX", "\0");
auto fd = mkstemp(this.temp_path.ptr);
if ( fd == -1 )
this.error(); // Throws an IOException
// The oddly-named 'reopen' allows us to set the Device's fd.
this.reopen(cast(Handle)fd);
}
/***************************************************************************
Swaps the temp file to the final file path.
***************************************************************************/
public void swap ( )
{
rename(this.temp_path.ptr, this.final_path.ptr);
}
}