persist.js
/*      outcome/
 * Reads all the lines from the
 * file, uses the first line as the
 * shard info and the rest as
 * records.
 */
outcome
Reads all the lines from the
file, uses the first line as the
shard info and the rest as
records.
outcome Reads all the lines from the file uses the first line as the shard info and the rest as records .
function deserialize(file, cb) {
    fs.readFile(file, 'utf8', (err, data) => {
        if(err) cb(err)
        else {
            let lines = data.split('\n')
            if(lines.length < 1) return cb(`${file} is empty`)
            let lndx = 0
            let shard = { records: [] }
            try {
                let si = JSON.parse(lines[lndx])
                if(!si.writer) return cb(`${file} missing writer identifier`)
                shard.writer = si.writer
                if(!si.log) return cb(`${file} missing log identifier`)
                shard.log = si.log
                let chk = shardFileName(shard)
                if(chk != path.basename(file)) return cb(`${file}: name does not match shard info`)
                for(lndx = 1;lndx < lines.length;lndx++) {
                    if(lines[lndx] == "") continue
                    let rec = JSON.parse(lines[lndx])
                    if(!rec._korenum) return cb(`${file}:${lndx+1}:Missing _korenum`)
                    if(!rec._korewho) return cb(`${file}:${lndx+1}:Missing _korewho`)
                    shard.records.push(rec)
                }
            } catch(e) {
                console.log(e)
                cb(`${file}:${lndx+1}: Invalid JSON!`)
                return
            }
            cb(null, shard)
        }
    })
}
function deserialize ( file , cb ) { fs . readFile ( file , 'utf8' , ( err , data ) => { if ( err ) cb ( err ) else { let lines = data . split ( '\n' ) if ( lines . length < 1 ) return cb ( ` ${ file } ` ) let lndx = 0 let shard = { records : [ ] } try { let si = JSON . parse ( lines [ lndx ] ) if ( ! si . writer ) return cb ( ` ${ file } ` ) shard . writer = si . writer if ( ! si . log ) return cb ( ` ${ file } ` ) shard . log = si . log let chk = shardFileName ( shard ) if ( chk != path . basename ( file ) ) return cb ( ` ${ file } ` ) for ( lndx = 1 ; lndx < lines . length ; lndx ++ ) { if ( lines [ lndx ] == "" ) continue let rec = JSON . parse ( lines [ lndx ] ) if ( ! rec . _korenum ) return cb ( ` ${ file } ${ lndx + 1 } ` ) if ( ! rec . _korewho ) return cb ( ` ${ file } ${ lndx + 1 } ` ) shard . records . push ( rec ) } } catch ( e ) { console . log ( e ) cb ( ` ${ file } ${ lndx + 1 } ` ) return } cb ( null , shard ) } } ) }
outcome
 Reads all the lines from the
 file, uses the first line as the
 shard info and the rest as
 records.
outcome Reads all the lines from the file uses the first line as the shard info and the rest as records .