forked from tigerbeetle/tigerbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournal.js
45 lines (40 loc) · 950 Bytes
/
journal.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
const assert = require('assert');
const Crypto = require('@ronomon/crypto-async');
const Journal = {
fs: require('fs'),
fd: null,
path: null,
position: 0,
writing: false
};
Journal.open = function(path) {
const self = this;
self.path = path;
self.fd = self.fs.openSync(self.path, 'r+');
};
Journal.write = function(buffer) {
const self = this;
// TODO: Batch Records
// TODO: Checksums
// TODO: Snapshot
// TODO: Wrap around journal file
assert(self.fd >= 0);
assert(self.position >= 0);
assert(self.writing === false);
assert(buffer.length > 0);
self.writing = true;
// Simulate CPU cost of checkum:
Crypto.hash('sha256', buffer);
var bytesWritten = self.fs.writeSync(
self.fd,
buffer,
0,
buffer.length,
self.position
);
assert(bytesWritten === buffer.length);
self.fs.fsyncSync(self.fd);
self.position += buffer.length;
self.writing = false;
};
module.exports = Journal;