persist.js
/*      outcome/
 * Save the given shard records to
 * the given folder location in a
 * 'shard file'. If the file does
 * not exist, create it with the
 * proper shard info.
 */
outcome
Save the given shard records to
the given folder location in a
'shard file'. If the file does
not exist, create it with the
proper shard info.
outcome Save the given shard records to the given folder location in a ' shard file ' . If the file does not exist create it with the proper shard info .
function saveTo(folder, shard, recs, cb) {
    let p = path.join(folder, shardFileName(shard))
    fs.stat(p, (err, stat) => {
        if(err && err.code != 'ENOENT') return cb(err)
        if(err && err.code == 'ENOENT') {
            create_shard_file_1(shard, p, (err) => {
                if(err) cb(err)
                else save_rec_ndx_1(0)
            })
        } else {
            save_rec_ndx_1(0)
        }
    })
    function create_shard_file_1(shard, p, cb) {
        fs.mkdir(folder, {recursive:true}, (err) => {
            if(err && err.code != 'EEXIST') cb(err)
            else {
                let si = {
                    log: shard.log,
                    writer: shard.writer,
                }
                let line1 = JSON.stringify(si) + '\n'
                fs.writeFile(p, line1, 'utf8', cb)
            }
        })
    }
    function save_rec_ndx_1(ndx) {
        if(ndx >= recs.length) return cb()
        let line = JSON.stringify(recs[ndx]) + '\n'
        fs.appendFile(p, line, 'utf8', (err) => {
            if(err) cb(err)
            else save_rec_ndx_1(ndx+1)
        })
    }
}
function saveTo ( folder , shard , recs , cb ) { let p = path . join ( folder , shardFileName ( shard ) ) fs . stat ( p , ( err , stat ) => { if ( err && err . code != 'ENOENT' ) return cb ( err ) if ( err && err . code == 'ENOENT' ) { create_shard_file_1 ( shard , p , ( err ) => { if ( err ) cb ( err ) else save_rec_ndx_1 ( 0 ) } ) } else { save_rec_ndx_1 ( 0 ) } } ) function create_shard_file_1 ( shard , p , cb ) { fs . mkdir ( folder , { recursive : true } , ( err ) => { if ( err && err . code != 'EEXIST' ) cb ( err ) else { let si = { log : shard . log , writer : shard . writer , } let line1 = JSON . stringify ( si ) + '\n' fs . writeFile ( p , line1 , 'utf8' , cb ) } } ) } function save_rec_ndx_1 ( ndx ) { if ( ndx >= recs . length ) return cb ( ) let line = JSON . stringify ( recs [ ndx ] ) + '\n' fs . appendFile ( p , line , 'utf8' , ( err ) => { if ( err ) cb ( err ) else save_rec_ndx_1 ( ndx + 1 ) } ) } }
outcome
 Save the given shard records to
 the given folder location in a
 'shard file'.
outcome Save the given shard records to the given folder location in a ' shard file ' .