We have 500GB file with integer rows. How we can sort it with only 512Mb RAM using Node.js? I think something like that:
- Break main file on chunks by 256Mb
- Sort every chunk
- Get first row of every chunk, sort and push it to final file
- Do the 3th step for every rows in chunks.
Some ideas?
UPDATE: Thanks to user some-random-it-boy This solution based on child-process with native sort util. I think it should work)
var fs = require('fs'),
spawn = require('child_process').spawn,
sort = spawn('sort', ['in.txt']);
var writer = fs.createWriteStream('out.txt');
sort.stdout.on('data', function (data) {
writer.write(data)
});
sort.on('exit', function (code) {
if (code) console.log(code); //if some error
writer.end();
});