forked from gritzko/swarm
/
FileStorage.js
172 lines (149 loc) · 4.73 KB
/
FileStorage.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
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
"use strict";
var fs = require('fs');
var path = require('path');
var env = require('./env');
var Spec = require('./Spec');
var Storage = require('./Storage');
var SecondPreciseClock = require('./SecondPreciseClock');
/**
* An improvised filesystem-based storage implementation.
* Objects are saved into separate files in a hashed directory
* tree. Ongoing operations are streamed into a log file.
* One can go surprisingly far with this kind of an approach.
* https://news.ycombinator.com/item?id=7872239 */
function FileStorage (dir) {
Storage.call(this);
this._host = null; //will be set during Host creation
this.dir = path.resolve(dir);
if (!fs.existsSync(this.dir)) {
fs.mkdirSync(this.dir);
}
this._id = 'file';
this.tail = {};
var clock_fn = env.clock || SecondPreciseClock;
this.clock = new clock_fn(this._id);
this.loadLog();
this.rotateLog();
}
FileStorage.prototype = new Storage();
module.exports = FileStorage;
FileStorage.prototype.stateFileName = function (spec) {
var base = path.resolve(this.dir, spec.type());
var file = path.resolve(base, spec.id());
return file; // TODO hashing?
};
FileStorage.prototype.logFileName = function () {
return path.resolve(this.dir, "_log");
};
FileStorage.prototype.writeState = function (spec, state, cb) {
var self = this;
var ti = spec.filter('/#');
var fileName = this.stateFileName(ti);
var dir = path.dirname(fileName);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
var save = JSON.stringify(state, undefined, 2);
// dump JSON to the tmp file
delete self.tails[ti]; // TODO save 'em in case write fails
fs.writeFile(fileName, save, function onSave(err) {
if (err) {
console.error("failed to flush state; can't trim the log", err);
}
cb(err);
});
};
FileStorage.prototype.writeOp = function (spec, value, cb) {
var self = this;
var ti = spec.filter('/#');
var vm = spec.filter('!.');
var tail = this.tails[ti] || (this.tails[ti] = {});
if (vm in tail) {
console.error('op replay @storage',vm,new Error().stack);
return;
}
var clone = JSON.parse(JSON.stringify(value)); // FIXME performance please
tail[vm] = clone;
var record = ',\n"'+spec+'":\t'+JSON.stringify(clone);
this.logFile.write (record, function onFail(err) {
if (err) { self.close(null,err); }
cb(err);
});
};
FileStorage.prototype.readState = function (ti, callback) {
var statefn = this.stateFileName(ti);
// read in the state
fs.readFile(statefn, function onRead(err, data) { // FIXME fascism
var state = err ? {_version: '!0'} : JSON.parse(data.toString());
callback(null,state||null); // important: no state is "null"
});
};
FileStorage.prototype.readOps = function (ti, callback) {
var tail = this.tails[ti];
if (tail) {
var unjsoned = {};
for(var key in tail) {
unjsoned[key] = tail[key];
}
tail = unjsoned;
}
callback(null, tail||null);
};
FileStorage.prototype.close = function (callback,error) {
if (error) {
console.log("fatal IO error", error);
}
if (this.logFile) {
this.rotateLog(true, callback);
}
};
FileStorage.prototype.rotateLog = function (noOpen, callback) {
var self = this;
if (this.logFile) {
this.logFile.end('}', callback);
this.logFile = null;
callback = undefined;
}
if (!noOpen) {
if (fs.existsSync(this.logFileName())) {
fs.rename(this.logFileName(),this.logFileName()+'.bak');
}
this.logFile = fs.createWriteStream(this.logFileName()); // TODO file swap
this.logFile.on('error', function (err) {
self.close(null,err);
});
var json = JSON.stringify(this.tails, undefined, 2);
json = '{"":\n' + json; // open-ended JSON
this.logFile.write (json, function onFail(err) {
if (err) { self.close(null,err); }
});
}
if (callback) {
callback();
}
};
FileStorage.prototype.loadLog = function () {
if ( !fs.existsSync(this.logFileName()) ) {
return;
}
var json = fs.readFileSync(this.logFileName(), {encoding:"utf8"});
if (!json) { return; }
var log;
try {
log = JSON.parse(json);
} catch (ex) {
// open-ended JSON
log = JSON.parse(json + '}');
}
this.tails = log[''];
delete log[''];
for(var s in log) {
var spec = new Spec(s);
if (spec.pattern()==='/#!.') {
var ti = spec.filter('/#');
var vm = spec.filter('!.');
var tail = this.tails[ti] || (this.tails[ti] = {});
tail[vm] = log[spec];
}
}
};