Fix occasional error for large offsets during seek.

This commit is contained in:
Moos 2016-12-25 09:16:52 -08:00
parent 220473102e
commit 08f14b4bfd
4 changed files with 29 additions and 24 deletions

View File

@ -289,8 +289,9 @@ See [bench/README](bench).
## Changes ## Changes
1.1.1 1.1.2
- Fix DeprecationWarning for node 7.x - Fix DeprecationWarning for node 7.x (1.1.1)
- Fix occasional error for large offsets during seek
1.1.0 1.1.0
- added seek() method - added seek() method

View File

@ -11,7 +11,7 @@
"verbs" "verbs"
], ],
"description": "wordpos is a set of part-of-speech utilities for Node.js using the WordNet database.", "description": "wordpos is a set of part-of-speech utilities for Node.js using the WordNet database.",
"version": "1.1.1", "version": "1.1.2",
"homepage": "https://github.com/moos/wordpos", "homepage": "https://github.com/moos/wordpos",
"engines": { "engines": {
"node": ">=0.12" "node": ">=0.12"

View File

@ -96,7 +96,7 @@ function readLocation(location, callback) {
file = this, file = this,
str = '', str = '',
len = file.nominalLineLength, len = file.nominalLineLength,
buffer = new Buffer(len); buffer = new Buffer(len); // TODO @deprecated as of node 6.0
readChunk(location, function(err, count) { readChunk(location, function(err, count) {
if (err) { if (err) {
@ -109,7 +109,11 @@ function readLocation(location, callback) {
}); });
function readChunk(pos, cb) { function readChunk(pos, cb) {
var nonDataErr = new Error('no data at offset ' + pos);
fs.read(file.fd, buffer, 0, len, pos, function (err, count) { fs.read(file.fd, buffer, 0, len, pos, function (err, count) {
if (!count) return cb(nonDataErr, count);
str += buffer.toString('ascii'); str += buffer.toString('ascii');
var eol = str.indexOf('\n'); var eol = str.indexOf('\n');
//console.log(' -- read %d bytes at <%d>', count, pos, eol); //console.log(' -- read %d bytes at <%d>', count, pos, eol);
@ -119,7 +123,7 @@ function readLocation(location, callback) {
} }
str = str.substr(0, eol); str = str.substr(0, eol);
if (str === '' && !err) err = new Error('no data at offset ' + pos); if (str === '' && !err) err = nonDataErr;
cb(err, count); cb(err, count);
}); });
} }
@ -167,7 +171,7 @@ function lookup(offsets, callback) {
function openFile() { function openFile() {
if (!self.fd) { if (!self.fd) {
//console.log(' ... opening', self.filePath); // console.log(' ... opening', self.filePath);
self.fd = fs.openSync(self.filePath, 'r'); self.fd = fs.openSync(self.filePath, 'r');
} }
// ref count so we know when to close the main index file // ref count so we know when to close the main index file
@ -177,7 +181,7 @@ function lookup(offsets, callback) {
function closeFile() { function closeFile() {
if (--self.refcount === 0) { if (--self.refcount === 0) {
//console.log(' ... closing', self.filePath); // console.log(' ... closing', self.filePath);
fs.closeSync(self.fd); fs.closeSync(self.fd);
self.fd = null; self.fd = null;
} }

View File

@ -37,8 +37,7 @@ var str = "The angry bear chased the frightened little squirrel",
rest: [ 'The' ] rest: [ 'The' ]
}, },
garble = 'garblegarble', // expect not to find word garble = 'garblegarble', // expect not to find word
offset = 1285602, offset = 1285602;
offset_pos ='a';
@ -361,6 +360,15 @@ describe('randX()...', function() {
describe('seek()...', function() { describe('seek()...', function() {
it('should seek offset', function(done) {
wordpos.seek(offset, 'a', function(err, result) {
assert.equal(result.synsetOffset, offset);
assert.equal(result.pos, 's');
assert.equal(result.lemma, 'amazing');
done();
});
});
it('should handle bad offset', function(done) { it('should handle bad offset', function(done) {
wordpos.seek('foobar', 'a', function(err, result){ wordpos.seek('foobar', 'a', function(err, result){
assert(err instanceof Error); assert(err instanceof Error);
@ -371,7 +379,7 @@ describe('seek()...', function() {
it('should handle wrong offset', function(done) { it('should handle wrong offset', function(done) {
var bad_offset = offset + 1; var bad_offset = offset + 1;
wordpos.seek(bad_offset, offset_pos, function(err, result) { wordpos.seek(bad_offset, 'a', function(err, result) {
assert(err instanceof Error); assert(err instanceof Error);
assert.equal(err.message, 'Bad data at location ' + bad_offset); assert.equal(err.message, 'Bad data at location ' + bad_offset);
assert.deepEqual(result, {}); assert.deepEqual(result, {});
@ -381,7 +389,7 @@ describe('seek()...', function() {
it('should handle very large offset', function(done) { it('should handle very large offset', function(done) {
var bad_offset = offset + 100000000; var bad_offset = offset + 100000000;
wordpos.seek(bad_offset, offset_pos, function(err, result) { wordpos.seek(bad_offset, 'a', function(err, result) {
assert(err instanceof Error); assert(err instanceof Error);
assert.equal(err.message, 'no data at offset ' + bad_offset); assert.equal(err.message, 'no data at offset ' + bad_offset);
assert.deepEqual(result, {}); assert.deepEqual(result, {});
@ -389,7 +397,7 @@ describe('seek()...', function() {
}).catch(_.noop); // UnhandledPromiseRejectionWarning; }).catch(_.noop); // UnhandledPromiseRejectionWarning;
}); });
it('should handle bad pos', function(done) { it('should handle bad POS', function(done) {
wordpos.seek(offset, 'g', function(err, result) { wordpos.seek(offset, 'g', function(err, result) {
assert(err instanceof Error); assert(err instanceof Error);
assert(/Incorrect POS/.test(err.message)); assert(/Incorrect POS/.test(err.message));
@ -397,21 +405,13 @@ describe('seek()...', function() {
}).catch(_.noop); // UnhandledPromiseRejectionWarning; }).catch(_.noop); // UnhandledPromiseRejectionWarning;
}); });
it('should handle wrong pos', function(done) { it('should handle wrong POS', function(done) {
wordpos.seek(offset, 'v', function(err, result){ wordpos.seek(offset, 'v', function(err, result){
assert.equal(err.message, 'Bad data at location ' + offset); assert.equal(err.message, 'Bad data at location ' + offset);
}).catch(_.noop); // UnhandledPromiseRejectionWarning; }).catch(_.noop); // UnhandledPromiseRejectionWarning;
done(); done();
}); });
it('should seek offset', function(done) {
wordpos.seek(offset, offset_pos, function(err, result) {
assert.equal(result.synsetOffset, offset);
assert.equal(result.pos, 's');
assert.equal(result.lemma, 'amazing');
done();
});
});
}); });
@ -474,7 +474,7 @@ describe('Promise pattern', function() {
}); });
it('seek()', function () { it('seek()', function () {
return wordpos.seek(offset, offset_pos).then(function (result) { return wordpos.seek(offset, 'a').then(function (result) {
assert.equal(result.synsetOffset, offset); assert.equal(result.synsetOffset, offset);
assert.equal(result.pos, 's'); assert.equal(result.pos, 's');
assert.equal(result.lemma, 'amazing'); assert.equal(result.lemma, 'amazing');
@ -483,14 +483,14 @@ describe('Promise pattern', function() {
}); });
it('seek() - wrong offset', function () { it('seek() - wrong offset', function () {
return wordpos.seek(offset + 1, offset_pos).catch(function (err) { return wordpos.seek(offset + 1, 'a').catch(function (err) {
assert(err instanceof Error); assert(err instanceof Error);
assert.equal(err.message, 'Bad data at location ' + (offset+1)); assert.equal(err.message, 'Bad data at location ' + (offset+1));
}); });
}); });
it('seek() - bad offset', function () { it('seek() - bad offset', function () {
return wordpos.seek('foobar', offset_pos).catch(function (err) { return wordpos.seek('foobar', 'a').catch(function (err) {
assert(err instanceof Error); assert(err instanceof Error);
assert.equal(err.message, 'offset must be valid positive number.'); assert.equal(err.message, 'offset must be valid positive number.');
}); });