Skip to content

Instantly share code, notes, and snippets.

@charleycesar
Created September 12, 2018 04:29
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 charleycesar/8669de4c1a10bd7cce650d127bc28e01 to your computer and use it in GitHub Desktop.
Save charleycesar/8669de4c1a10bd7cce650d127bc28e01 to your computer and use it in GitHub Desktop.
generateBatch
const fs = require('fs')
function MDTR(initialProps) {
this.props = initialProps;
this.defaultValues = {
jobId: 'xpto',
bucketStart: 'bucketTest',
nomeDoArquivoTxt: 'example.txt'
};
this.getLinesizeInBytes = function(filename) {
const buf = new Buffer(500 - 0);
const fd = fs.openSync(filename, 'r');
//resolver isso ainda!
fs.readSync(fd, buf, 0, 500 - 0, 0);
fs.closeSync(fd);
const lines = buf.toString().split('\n')
if (lines.length) return lines[0].length + 1;
};
this.getFilesizeInBytes = function(filename) {
const stats = fs.statSync(filename)
const fileSizeInBytes = stats.size
return fileSizeInBytes + 1
}
this.getFilePart = function(filename, start, end) {
if (start < 0 || end < 0 || end < start || end - start > 0x3fffffff)
throw new Error('bad start, end');
if (end - start === 0)
return new Buffer(0);
const buf = new Buffer(end - start);
const fd = fs.openSync(filename, 'r');
fs.readSync(fd, buf, 0, end - start, start);
fs.closeSync(fd);
lines = buf.toString().split('\n')
return buf;
}
this.generateBatches = function () {
do {
randomJson = {
...this.defaultValues,
range: {
start: this.readerConfig.range.start,
end: this.readerConfig.range.end
}
}
this.readerConfig.queueParts.push(randomJson);
this.readerConfig.range.start = this.readerConfig.range.end;
this.readerConfig.range.end += this.readerConfig.lineBreak;
}
while (this.readerConfig.range.end <= this.readerConfig.size);
return this.readerConfig.queueParts;
}
this.readerConfig = {
range: {
start: 0,
end: this.getLinesizeInBytes(this.defaultValues.nomeDoArquivoTxt)
},
lineBreak: this.getLinesizeInBytes(this.defaultValues.nomeDoArquivoTxt),
queueParts: [],
size: this.getFilesizeInBytes(this.defaultValues.nomeDoArquivoTxt)
}
}
const mdtr = new MDTR();
console.log(mdtr.generateBatches());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment