-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.js
267 lines (248 loc) · 7.94 KB
/
file.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
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
// This was written by the wonderful d3x0r
//console.log( "module:",module, module.filename.includes( "node_modules/gun-file" ));
const Gun = require(module.filename.includes( "node_modules/gun-file" )?'../gun/gun':'gun/gun');
const json6 = require( 'json-6' );
const fs = require('fs');
const _debug = false;
const _json_debug = false;
const _debug_write_time = false;
const rel_ = Gun.val.rel._; // '#'
const val_ = Gun.obj.has._; // '.'
const node_ = Gun.node._; // '_'
const state_ = Gun.state._;// '>';
const soul_ = Gun.node.soul._; // '#'
const ACK_ = '@';
const SEQ_ = '#';
const fileStates = {};
exports.attach = function( Gun ) {
Gun.on('opt', function(ctx){
this.to.next(ctx);
var opt = ctx.opt;
if(ctx.once){ return }
var fileName = String(opt['file-name'] || 'data.json6');
var fileMode = opt['file-mode'] || 0666;
var filePretty = ('file-pretty' in opt)?opt['file-pretty']:true;
var fileDelay = opt['file-delay'] || 100;
var gun = ctx.gun;
var graph = ctx.graph, acks = {}, count = 0, to;
//var reading = false;
var pending = [];
var pendingPut = [];
var pend = null;
var pendPut = null;
var wantFlush = false;
var loaded = false;
var fileState = fileStates[fileName] || ( fileStates[fileName] = { reading:false, writing:false, flushPending : false, disk : {} } );
preloadDisk(opt, fileState.disk);
//Gun.log.once(
// 'file-warning',
// 'WARNING! This `gun-file` pre-alpha module for gun for testing only!'
//);
var skip_put;
function doPut( at ) {
//_debug && console.log( "So this should be updating db?", skip_put, pend );
Gun.graph.is(at.put, null, map);
if( !loaded ) {
pendingPut.push( at );
return;
}
if( skip_put && skip_put == at['@'] ) {
//_debug && console.log( "skipping put in-get", skip_put, at['@'] );
return;
}
if(!at['@']){ acks[at[SEQ_]] = true; } // only ack non-acks.
else if( pend && at['@'] == pend.at[SEQ_] ) {
_debug && console.log( "Prevent self flush", pend );
return;
}
//console.log( "WILL FLUSH WITH at:", at );
//console.log( "pend:", pend );
//console.log( "skip:", skip_put );
count += 1;
if(count >= (opt.batch || 10000)){
clearTimeout( to );
to = null;
return flush();
}
if( !to )
_debug && console.log( "put happened?", to !== null );
if(to) clearTimeout( to );
to = setTimeout(flush, fileDelay );
fileState.flushPending = true;
ctx.on('in', {[ACK_]: at[rel_], gun:gun, ok: 1});
}
ctx.on('put', function(at) {
this.to.next(at);
doPut(at)
});
ctx.on('get', function(at){
this.to.next(at);
var gun = at.gun, lex = at.get, soul, data, opt, u;
if(!lex || !(soul = lex[soul_])){ return }
var field = lex[val_];
if( fileState.reading || fileState.flushPending || !loaded ) {
_debug && console.log( "Still reading... pushing request." );
pending.push( {gun:gun, ctx:ctx, soul:soul, at:at, field:field} );
} else {
data = fileState.disk[soul] || u;
if(data && field){
_debug && console.log( "get field?", field, data );
data = Gun.state.to(data, field);
}
//else console.log( "not data or not field?", field );
if( data ) {
skip_put = at[SEQ_];
ctx.on('in', {[ACK_]: at[SEQ_], gun:gun, put: Gun.graph.node(data)});
//_debug && console.log( "getting data:", data );
skip_put = null;
}
else {
_debug && console.log( "didn't get dat for", soul );
ctx.on('in', {[ACK_]: at[SEQ_], gun:gun, err: "no data"});
}
}
//},11);
});
var map = function(val, key, node, soul){
//_debug && console.log( "mapping graph?", soul );
fileState.disk[soul] = Gun.state.to(node, key, fileState.disk[soul]);
}
function preloadDisk( opt, disk ) {
if( fileState.loaded )
return; // already have the disk image in memory
if( fileState.flushPending || fileState.writing ) {
_debug && console.log( "wait for pending flush on another connection?" );
setTimeout( ()=>preloadDisk( opt, disk ), fileDelay/3 );
return;
}
fileState.reading = true;
_debug && console.log( new Date(), "preload happened." );
const stream = fs.createReadStream( fileName, { flags:"r", encoding:"utf8"} );
const parser = json6.begin( function(val) {
//_json_debug && console.log( "Recover:", val[0] );
disk[val[0]] = val[1];
} );
stream.on('open', function() {
_debug && console.log( new Date(), "Read stream opened.." );
} );
stream.on('error', function( err ){
if( err.code !== 'ENOENT' )
console.log( "READ STREAM ERROR:", err );
loaded = true;
fileState.reading = false;
handlePending();
} );
stream.on('data', function(chunk) {
//_json_debug && console.log( "got stream data",chunk );
parser.write( chunk );
});
stream.on( "close", function(){
_debug && console.log( new Date(), "reading done..." );
loaded = true;
fileState.reading = false;
//console.log( "File done" );
_debug && console.log( "Handle ", pending.length, "pending reads" );
handlePending();
} );
function handlePending() {
while( pendPut = pendingPut.shift() ) {
doPut( pendPut );
}
while( pend = pending.shift() ) {
var data = disk[pend.soul] || pend.u;
if(data && pend.field){
data = Gun.state.to(data, pend.field);
}
_debug && console.log( "Sending pending response...", pend.at[SEQ_] );
pend.ctx.on('in', {[ACK_]: pend.at[SEQ_], put: Gun.graph.node(data)});
_debug && console.log( "Sent pending response...", pend.at[SEQ_] );
}
if( wantFlush ) {
_debug && console.log( "WANTED FLUSH during READ?", to );
if(to) clearTimeout( to );
to = setTimeout(flush, fileDelay );
}
}
}
var startWrite;
var flush = function(){
//_debug && console.log( Date.now(), "DOING FLUSH", reading, writing, count );
if( to )
clearTimeout(to);
to = null;
count = 0;
if(fileState.reading) {
_debug && console.log( "Still reading, don't write", fileState.flushPending );
to = setTimeout( flush, fileDelay/3 );
fileState.flushPending = true;
wantFlush = true;
return;
}
if(fileState.writing){
to = setTimeout( flush, fileDelay );
_debug && console.log( "Still flushing, don't flush", fileState.flushPending );
fileState.flushPending = true;
return;
}
_debug && console.log( Date.now(), "DOING FLUSH", fileState.reading, fileState.writing, count );
var ack = acks;
acks = {};
fileState.writing = true;
fileState.flushPending = false;
var pretty = filePretty?3:null;
var stream = fs.createWriteStream( fileName, {encoding:'utf8', mode:fileMode, flags:"w+"} );
var waitDrain = false;
stream.on('open', function () {
var keys = Object.keys( fileState.disk );
var n = 0;
if( _debug_write_time ) startWrite = Date.now();
_debug && console.log( new Date(), "stream write opened..." );
function writeOne() {
if( n >= keys.length ) {
fileState.writing = false;
_debug_write_time && console.log( "Write to file:", Date.now() - startWrite );
_debug && console.log( "done; closing stream." );
stream.end();
//var tmp = count;
count = 0;
Gun.obj.map(ack, function(yes, id){
ctx.on('in', {
[ACK_]: id,
gun: gun,
err: false,
ok: 1
});
});
//if(1 < tmp){ flush() }
return;
}
var key = keys[n++];
var out = JSON.stringify( [key, fileState.disk[key]], null, pretty ) + (pretty?"\n":"");
if( !stream.write( out, 'utf8', function() {
if( !waitDrain ){
if( fileState.writing ) writeOne(); // otherwise already completed writing everything.
}
else {
//_debug && console.log( "Skipped doing next?" );
}
} ) ) {
// writing for on_something before next write.
waitDrain = true;
//_debug && console.log( "wait DRAIN" );
stream.once('drain', function(){
//_debug && console.log( "Continue after drain happened." );
if( waitDrain ) {
waitDrain = false;
writeOne();
} else {
// just the last write finishing don't retrigger.
}
});
}
}
writeOne();
} );
}
});
}
exports.attach( Gun );