@@ -6,6 +6,7 @@ const mapSeries = require('p-each-series')
6
6
const Log = require ( 'ipfs-log' )
7
7
const Index = require ( './Index' )
8
8
const Replicator = require ( './Replicator' )
9
+ const ReplicationInfo = require ( './replication-info' )
9
10
10
11
const Logger = require ( 'logplease' )
11
12
const logger = Logger . create ( "orbit-db.store" , { color : Logger . Colors . Blue } )
@@ -60,12 +61,7 @@ class Store {
60
61
this . _oplog = new Log ( this . _ipfs , this . id , null , null , null , this . _key , this . access . write )
61
62
62
63
// Replication progress info
63
- this . _replicationInfo = {
64
- buffered : 0 ,
65
- queued : 0 ,
66
- progress : 0 ,
67
- max : 0 ,
68
- }
64
+ this . _replicationStatus = new ReplicationInfo ( )
69
65
70
66
// Statistics
71
67
this . _stats = {
@@ -82,34 +78,33 @@ class Store {
82
78
this . _loader = this . _replicator
83
79
this . _replicator . on ( 'load.added' , ( entry ) => {
84
80
// Update the latest entry state (latest is the entry with largest clock time)
85
- this . _replicationInfo . queued ++
86
- this . _replicationInfo . max = Math . max . apply ( null , [ this . _replicationInfo . max , this . _oplog . length , entry . clock ? entry . clock . time : 0 ] )
81
+ this . _replicationStatus . queued ++
82
+ this . _recalculateReplicationMax ( entry . clock ? entry . clock . time : 0 )
87
83
// logger.debug(`<replicate>`)
88
84
this . events . emit ( 'replicate' , this . address . toString ( ) , entry )
89
85
} )
90
86
this . _replicator . on ( 'load.progress' , ( id , hash , entry , have , bufferedLength ) => {
91
- // console.log(">>", this._oplog.length, this._replicationInfo.progress, this._replicationInfo.buffered, bufferedLength)
92
- if ( this . _replicationInfo . buffered > bufferedLength ) {
93
- this . _replicationInfo . progress = this . _replicationInfo . progress + bufferedLength
87
+ if ( this . _replicationStatus . buffered > bufferedLength ) {
88
+ this . _recalculateReplicationProgress ( this . replicationStatus . progress + bufferedLength )
94
89
} else {
95
- this . _replicationInfo . progress = Math . max . apply ( null , [ this . _oplog . length , this . _replicationInfo . progress , this . _oplog . length + bufferedLength ] )
90
+ this . _recalculateReplicationProgress ( this . _oplog . length + bufferedLength )
96
91
}
97
- // console.log(">>>", this._replicationInfo.progress)
98
- this . _replicationInfo . buffered = bufferedLength
99
- this . _replicationInfo . max = Math . max . apply ( null , [ this . _replicationInfo . max , this . _replicationInfo . progress ] )
92
+ this . _replicationStatus . buffered = bufferedLength
93
+ this . _recalculateReplicationMax ( this . replicationStatus . progress )
100
94
// logger.debug(`<replicate.progress>`)
101
- this . events . emit ( 'replicate.progress' , this . address . toString ( ) , hash , entry , this . _replicationInfo . progress , have )
95
+ this . events . emit ( 'replicate.progress' , this . address . toString ( ) , hash , entry , this . replicationStatus . progress , null )
102
96
} )
103
97
104
98
const onLoadCompleted = async ( logs , have ) => {
105
99
try {
106
100
for ( let log of logs ) {
107
101
await this . _oplog . join ( log , - 1 , this . _oplog . id )
108
102
}
109
- this . _replicationInfo . max = Math . max ( this . _replicationInfo . max , this . _oplog . length )
103
+ this . _replicationStatus . queued -= logs . length
104
+ this . _replicationStatus . buffered = this . _replicator . _buffer . length
105
+ this . _recalculateReplicationMax ( )
110
106
this . _index . updateIndex ( this . _oplog )
111
- this . _replicationInfo . progress = Math . max . apply ( null , [ this . _replicationInfo . progress , this . _oplog . length ] )
112
- this . _replicationInfo . queued -= logs . length
107
+ this . _recalculateReplicationProgress ( )
113
108
// logger.debug(`<replicated>`)
114
109
this . events . emit ( 'replicated' , this . address . toString ( ) , logs . length )
115
110
} catch ( e ) {
@@ -136,24 +131,29 @@ class Store {
136
131
return this . _key
137
132
}
138
133
134
+ /**
135
+ * Returns the database's current replication status information
136
+ * @return {[Object] } [description]
137
+ */
138
+ get replicationStatus ( ) {
139
+ return this . _replicationStatus
140
+ }
141
+
139
142
async close ( ) {
140
143
if ( this . options . onClose )
141
144
await this . options . onClose ( this . address . toString ( ) )
142
145
143
146
// Reset replication statistics
144
- this . _replicationInfo = {
145
- buffered : 0 ,
146
- queued : 0 ,
147
- progress : 0 ,
148
- max : 0 ,
149
- }
147
+ this . _replicationStatus . reset ( )
148
+
150
149
// Reset database statistics
151
150
this . _stats = {
152
151
snapshot : {
153
152
bytesLoaded : - 1 ,
154
153
} ,
155
154
syncRequestsReceieved : 0 ,
156
155
}
156
+
157
157
// Remove all event listeners
158
158
this . events . removeAllListeners ( 'load' )
159
159
this . events . removeAllListeners ( 'load.progress' )
@@ -173,9 +173,14 @@ class Store {
173
173
return Promise . resolve ( )
174
174
}
175
175
176
+ /**
177
+ * Drops a database and removes local data
178
+ * @return {[None] }
179
+ */
176
180
async drop ( ) {
177
181
await this . close ( )
178
182
await this . _cache . destroy ( )
183
+ // Reset
179
184
this . _index = new this . options . Index ( this . id )
180
185
this . _oplog = new Log ( this . _ipfs , this . id , null , null , null , this . _key , this . access . write )
181
186
this . _cache = this . options . cache
@@ -192,11 +197,10 @@ class Store {
192
197
this . events . emit ( 'load' , this . address . toString ( ) , heads )
193
198
194
199
await mapSeries ( heads , async ( head ) => {
195
- this . _replicationInfo . max = Math . max ( this . _replicationInfo . max , head . clock . time )
200
+ this . _recalculateReplicationMax ( head . clock . time )
196
201
let log = await Log . fromEntryHash ( this . _ipfs , head . hash , this . _oplog . id , amount , this . _oplog . values , this . key , this . access . write , this . _onLoadProgress . bind ( this ) )
197
202
await this . _oplog . join ( log , amount , this . _oplog . id )
198
- this . _replicationInfo . progress = Math . max . apply ( null , [ this . _replicationInfo . progress , this . _oplog . length ] )
199
- this . _replicationInfo . max = Math . max . apply ( null , [ this . _replicationInfo . max , this . _replicationInfo . progress ] )
203
+ this . _recalculateReplicationProgress ( )
200
204
} )
201
205
202
206
// Update the index
@@ -301,6 +305,8 @@ class Store {
301
305
async loadFromSnapshot ( onProgressCallback ) {
302
306
this . events . emit ( 'load' , this . address . toString ( ) )
303
307
308
+ const maxClock = ( res , val ) => Math . max ( res , val . clock . time )
309
+
304
310
const queue = await this . _cache . get ( 'queue' )
305
311
this . sync ( queue || [ ] )
306
312
@@ -364,10 +370,8 @@ class Store {
364
370
365
371
if ( header ) {
366
372
this . _type = header . type
367
- this . _replicationInfo . max = Math . max ( this . _replicationInfo . max , values . reduce ( ( res , val ) => Math . max ( res , val . clock . time ) , 0 ) )
368
373
resolve ( { values : values , id : header . id , heads : header . heads , type : header . type } )
369
374
} else {
370
- this . _replicationInfo . max = 0
371
375
resolve ( { values : values , id : null , heads : null , type : null } )
372
376
}
373
377
}
@@ -377,34 +381,34 @@ class Store {
377
381
}
378
382
379
383
const onProgress = ( hash , entry , count , total ) => {
380
- this . _replicationInfo . max = Math . max ( this . _replicationInfo . max , entry . clock . time )
381
- this . _replicationInfo . progress = Math . max . apply ( null , [ this . _replicationInfo . progress , count , this . _oplog . length ] )
382
- this . _onLoadProgress ( hash , entry , this . _replicationInfo . progress , this . _replicationInfo . max )
384
+ this . _recalculateReplicationStatus ( count , entry . clock . time )
385
+ this . _onLoadProgress ( hash , entry )
383
386
}
384
387
385
388
// Fetch the entries
386
389
// Timeout 1 sec to only load entries that are already fetched (in order to not get stuck at loading)
387
390
const snapshotData = await loadSnapshotData ( )
391
+ this . _recalculateReplicationMax ( snapshotData . values . reduce ( maxClock , 0 ) )
388
392
if ( snapshotData ) {
389
393
const log = await Log . fromJSON ( this . _ipfs , snapshotData , - 1 , this . _key , this . access . write , 1000 , onProgress )
390
394
await this . _oplog . join ( log , - 1 , this . _oplog . id )
391
- this . _replicationInfo . max = Math . max . apply ( null , [ this . _replicationInfo . max , this . _replicationInfo . progress , this . _oplog . length ] )
392
- this . _replicationInfo . progress = Math . max ( this . _replicationInfo . progress , this . _oplog . length )
395
+ this . _recalculateReplicationMax ( )
393
396
this . _index . updateIndex ( this . _oplog )
397
+ this . _recalculateReplicationProgress ( )
394
398
this . events . emit ( 'replicated' , this . address . toString ( ) )
395
399
}
396
400
this . events . emit ( 'ready' , this . address . toString ( ) , this . _oplog . heads )
397
401
} else {
398
402
throw new Error ( `Snapshot for ${ this . address } not found!` )
399
403
}
404
+
400
405
return this
401
406
}
402
407
403
408
async _addOperation ( data , batchOperation , lastOperation , onProgressCallback ) {
404
409
if ( this . _oplog ) {
405
410
const entry = await this . _oplog . append ( data , this . options . referenceCount )
406
- this . _replicationInfo . progress ++
407
- this . _replicationInfo . max = Math . max . apply ( null , [ this . _replicationInfo . max , this . _replicationInfo . progress , entry . clock . time ] )
411
+ this . _recalculateReplicationStatus ( this . replicationStatus . progress + 1 , entry . clock . time )
408
412
await this . _cache . set ( '_localHeads' , [ entry ] )
409
413
this . _index . updateIndex ( this . _oplog )
410
414
this . events . emit ( 'write' , this . address . toString ( ) , entry , this . _oplog . heads )
@@ -418,7 +422,32 @@ class Store {
418
422
}
419
423
420
424
_onLoadProgress ( hash , entry , progress , total ) {
421
- this . events . emit ( 'load.progress' , this . address . toString ( ) , hash , entry , Math . max ( this . _oplog . length , progress ) , Math . max ( this . _oplog . length || 0 , this . _replicationInfo . max || 0 ) )
425
+ this . _recalculateReplicationStatus ( progress , total )
426
+ this . events . emit ( 'load.progress' , this . address . toString ( ) , hash , entry , this . replicationStatus . progress , this . replicationStatus . max )
427
+ }
428
+
429
+ /* Replication Status state updates */
430
+
431
+ _recalculateReplicationProgress ( max ) {
432
+ this . _replicationStatus . progress = Math . max . apply ( null , [
433
+ this . _replicationStatus . progress ,
434
+ this . _oplog . length ,
435
+ max || 0 ,
436
+ ] )
437
+ this . _recalculateReplicationMax ( this . replicationStatus . progress )
438
+ }
439
+
440
+ _recalculateReplicationMax ( max ) {
441
+ this . _replicationStatus . max = Math . max . apply ( null , [
442
+ this . _replicationStatus . max ,
443
+ this . _oplog . length ,
444
+ max || 0 ,
445
+ ] )
446
+ }
447
+
448
+ _recalculateReplicationStatus ( maxProgress , maxTotal ) {
449
+ this . _recalculateReplicationProgress ( maxProgress )
450
+ this . _recalculateReplicationMax ( maxTotal )
422
451
}
423
452
}
424
453
0 commit comments