-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-log.js
65 lines (60 loc) · 1.46 KB
/
convert-log.js
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
const path = require('path');
const bipf = require('bipf');
const push = require('push-stream');
const AAOL = require('async-append-only-log');
const DATASET = 'data64';
const bipfLog = AAOL(path.join(__dirname, DATASET, 'db2', 'log.bipf'), {
blockSize: 64 * 1024,
codec: {
encode(msg) {
return bipf.allocAndEncode(msg);
},
decode(buf) {
return bipf.decode(buf, 0);
},
},
validateRecord(buf) {
try {
bipf.decode(buf, 0);
return true;
} catch {
return false;
}
},
});
const jsonLog = AAOL(path.join(__dirname, DATASET, 'memdb-log.bin'), {
blockSize: 64 * 1024,
codec: {
encode(msg) {
return Buffer.from(JSON.stringify(msg), 'utf8');
},
decode(buf) {
return JSON.parse(buf.toString('utf8'));
},
},
validateRecord(buf) {
try {
JSON.parse(buf.toString('utf8'));
return true;
} catch {
return false;
}
},
});
console.log('dataset: ' + DATASET);
const msgs = [];
bipfLog.stream({offsets: false, values: true, sizes: false}).pipe(
push.drain(
function drainEach(msg) {
if (!msg) return; // deleted record
msgs.push(msg);
},
function drainEnd(err) {
console.log('bipfLog read with ' + msgs.length + ' messages');
jsonLog.append(msgs, (err) => {
if (err) throw new Error('Failed to append to jsonLog', {cause: err});
else console.log('jsonLog written with ' + msgs.length + ' messages');
});
},
),
);