Created
September 12, 2018 22:47
-
-
Save charleycesar/6bc05c2fba110f55f60731565a160103 to your computer and use it in GitHub Desktop.
generateBatchWithBuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
function MDTR(initialProps) { | |
this.props = initialProps; | |
this.defaultValues = { | |
jobId: 'xpto', | |
bucketStart: 'bucketTest', | |
nomeDoArquivoTxt: 'example.txt' | |
}; | |
this.getLinesizeInBytes = function(buffer) { | |
const lines = buffer.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 { | |
this.readerConfig.range.end += (this.readerConfig.lineBreak * this.readerConfig.linesPerBuffer); | |
randomJson = { | |
...this.defaultValues, | |
range: { | |
start: this.readerConfig.range.start, | |
end: this.readerConfig.range.end | |
}, | |
lineBreak: this.readerConfig.lineBreak | |
} | |
this.readerConfig.queueParts.push(randomJson); | |
this.readerConfig.range.start = this.readerConfig.range.end; | |
} | |
while (this.readerConfig.range.end <= this.readerConfig.size); | |
return this.readerConfig.queueParts; | |
} | |
this.readerConfig = { | |
range: { | |
start: 0, | |
end: this.getLinesizeInBytes(getBuffer()) | |
}, | |
lineBreak: this.getLinesizeInBytes(getBuffer()), | |
queueParts: [], | |
size: this.getFilesizeInBytes(this.defaultValues.nomeDoArquivoTxt), | |
linesPerBuffer: 10000 | |
} | |
} | |
function getBuffer() { | |
const buf = new Buffer(500); | |
const fd = fs.openSync('example.txt', 'r'); | |
fs.readSync(fd, buf, 0, 500, 0); | |
fs.closeSync(fd); | |
return buf; | |
} | |
const mdtr = new MDTR(); | |
//console.log(mdtr.getFilePart('example.txt', 0, 85085).toString()); | |
console.log(mdtr.generateBatches()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment