Skip to content

Instantly share code, notes, and snippets.

@yoava
Last active April 1, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoava/8a05ad916efb852cfd0f2b3482d7ec0c to your computer and use it in GitHub Desktop.
Save yoava/8a05ad916efb852cfd0f2b3482d7ec0c to your computer and use it in GitHub Desktop.
// sort3 uses nedb, as suggested by Tamas Hegedus
//////////////////////////////////////////////////////////////////////////////////////
var fs = require('fs'),
readline = require('readline');
var Datastore = require('nedb');
var db = new Datastore({
filename: 'path/to/temp/datafile',
autoload: true
});
var rl = readline.createInterface({
input: fs.createReadStream('./input.txt'),
terminal: false
});
var writer = fs.createWriteStream('out3.txt');
rl.on('line', function (line) {
var tmp = line.split("\t").reverse();
db.insert({
field0: tmp[0],
field1: tmp[1],
field2: tmp[2]
});
});
rl.on('close', function () {
var cursor = db.find({})
.sort({field0: 1}); // sort by field0, ascending
var PAGE_SIZE = 1000;
paginate(0);
function paginate(i) {
cursor.skip(i * PAGE_SIZE).limit(PAGE_SIZE).exec(function (err, docs) {
// handle errors
docs.forEach(o => writer.write(o.field0 + "\t" + o.field1 + "\t" + o.field2 + "\n"));
if (docs.length >= PAGE_SIZE) {
paginate(i + PAGE_SIZE);
} else {
// cleanup temp database
writer.end();
var closetime = Date.now();
console.log('Time. ', (closetime - start) / 1000, ' secs');
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment