@ -158,6 +158,8 @@ class Persistence {
const dataById = { }
const dataById = { }
const tdata = [ ]
const tdata = [ ]
const indexes = { }
const indexes = { }
// Last line of every data file is usually blank so not really corrupt
let corruptItems = - 1
let corruptItems = - 1
for ( const datum of data ) {
for ( const datum of data ) {
@ -192,9 +194,11 @@ class Persistence {
const dataById = { }
const dataById = { }
const tdata = [ ]
const tdata = [ ]
const indexes = { }
const indexes = { }
let corruptItems = 0
const lineStream = byline ( rawStream )
// Last line of every data file is usually blank so not really corrupt
let corruptItems = - 1
const lineStream = byline ( rawStream , { keepEmptyLines : true } )
let length = 0
let length = 0
lineStream . on ( 'data' , ( line ) => {
lineStream . on ( 'data' , ( line ) => {
@ -214,16 +218,17 @@ class Persistence {
lineStream . on ( 'end' , ( ) => {
lineStream . on ( 'end' , ( ) => {
// A bit lenient on corruption
// A bit lenient on corruption
let err = null
if ( length > 0 && corruptItems / length > this . corruptAlertThreshold ) {
if ( length > 0 && corruptItems / length > this . corruptAlertThreshold ) {
err = new Error ( ` More than ${ Math . floor ( 100 * this . corruptAlertThreshold ) } % of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss ` )
const err = new Error ( ` More than ${ Math . floor ( 100 * this . corruptAlertThreshold ) } % of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss ` )
cb ( err , null )
return
}
}
Object . keys ( dataById ) . forEach ( function ( k ) {
Object . keys ( dataById ) . forEach ( function ( k ) {
tdata . push ( dataById [ k ] )
tdata . push ( dataById [ k ] )
} )
} )
cb ( err , { data : tdata , indexes : indexes } )
cb ( null , { data : tdata , indexes : indexes } )
} )
} )
lineStream . on ( 'error' , function ( err ) {
lineStream . on ( 'error' , function ( err ) {