persist.js
/*      outcome/
 * Similar to deserialize - we read
 * the file treating the first line
 * as info and the rest as records.
 * BUT we do not fail at any point
 * doggedly walking through all records
 * even when we face errors.
 */
outcome
Similar to deserialize - we read
the file treating the first line
as info and the rest as records.
BUT we do not fail at any point
doggedly walking through all records
even when we face errors.
outcome Similar to deserialize - we read the file treating the first line as info and the rest as records . BUT we do not fail at any point doggedly walking through all records even when we face errors .
function roughDeserialize(file, cb) {
    let errs = []
    let shard = { writer: "NO WRITER FOUND", log: "NO LOG FOUND", records: [] }
    fs.readFile(file, 'utf8', (err, data) => {
        if(err) {
            errs.push(`${file}: Read Failed`)
            errs.push(err)
        } else {
            let lines = data.split('\n')
            if(lines.length < 1) {
                errs.push(`${file}: Error - empty file`)
            } else {
                let lndx = 0
                try {
                    let si = JSON.parse(lines[lndx])
                    if(si.writer) shard.writer = si.writer
                    if(si.log) shard.log = si.log
                    let chk = shardFileName(shard)
                    if(chk != path.basename(file)) errs.push(`${file}: name does not match shard info`)
                } catch(e) {
                    errs.push(`${file}: Failed to parse shard info (1st line)`)
                    errs.push(e)
                }
                for(lndx = 1;lndx < lines.length;lndx++) {
                    if(lines[lndx] == "") continue
                    try {
                        let rec = JSON.parse(lines[lndx])
                        if(!rec._korenum) errs.push(`${file}:${lndx+1}:Missing _korenum`)
                        if(!rec._korewho) errs.push(`${file}:${lndx+1}:Missing _korewho`)
                        shard.records.push(rec)
                    } catch(e) {
                        errs.push(`${file}:${lndx+1}:Failed to parse record`)
                        errs.push(e)
                    }
                }
            }
        }
        cb(errs, shard)
    })
}
function roughDeserialize ( file , cb ) { let errs = [ ] let shard = { writer : "NO WRITER FOUND" , log : "NO LOG FOUND" , records : [ ] } fs . readFile ( file , 'utf8' , ( err , data ) => { if ( err ) { errs . push ( ` ${ file } ` ) errs . push ( err ) } else { let lines = data . split ( '\n' ) if ( lines . length < 1 ) { errs . push ( ` ${ file } ` ) } else { let lndx = 0 try { let si = JSON . parse ( lines [ lndx ] ) if ( si . writer ) shard . writer = si . writer if ( si . log ) shard . log = si . log let chk = shardFileName ( shard ) if ( chk != path . basename ( file ) ) errs . push ( ` ${ file } ` ) } catch ( e ) { errs . push ( ` ${ file } ` ) errs . push ( e ) } for ( lndx = 1 ; lndx < lines . length ; lndx ++ ) { if ( lines [ lndx ] == "" ) continue try { let rec = JSON . parse ( lines [ lndx ] ) if ( ! rec . _korenum ) errs . push ( ` ${ file } ${ lndx + 1 } ` ) if ( ! rec . _korewho ) errs . push ( ` ${ file } ${ lndx + 1 } ` ) shard . records . push ( rec ) } catch ( e ) { errs . push ( ` ${ file } ${ lndx + 1 } ` ) errs . push ( e ) } } } } cb ( errs , shard ) } ) }
outcome
 Similar to deserialize - we read
 the file treating the first line
 as info and the rest as records.
outcome Similar to deserialize - we read the file treating the first line as info and the rest as records .