From 08f14b4bfd6eb071fd30c01521381921c56edebb Mon Sep 17 00:00:00 2001 From: Moos Date: Sun, 25 Dec 2016 09:16:52 -0800 Subject: [PATCH] Fix occasional error for large offsets during seek. --- README.md | 5 +++-- package.json | 2 +- src/dataFile.js | 12 ++++++++---- test/wordpos_test.js | 34 +++++++++++++++++----------------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index a656e86..7b9840a 100644 --- a/README.md +++ b/README.md @@ -289,8 +289,9 @@ See [bench/README](bench). ## Changes -1.1.1 - - Fix DeprecationWarning for node 7.x +1.1.2 + - Fix DeprecationWarning for node 7.x (1.1.1) + - Fix occasional error for large offsets during seek 1.1.0 - added seek() method diff --git a/package.json b/package.json index 3f2572a..5cf2798 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "verbs" ], "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", "engines": { "node": ">=0.12" diff --git a/src/dataFile.js b/src/dataFile.js index 950366b..b5a0400 100644 --- a/src/dataFile.js +++ b/src/dataFile.js @@ -96,7 +96,7 @@ function readLocation(location, callback) { file = this, str = '', len = file.nominalLineLength, - buffer = new Buffer(len); + buffer = new Buffer(len); // TODO @deprecated as of node 6.0 readChunk(location, function(err, count) { if (err) { @@ -109,7 +109,11 @@ function readLocation(location, callback) { }); function readChunk(pos, cb) { + var nonDataErr = new Error('no data at offset ' + pos); + fs.read(file.fd, buffer, 0, len, pos, function (err, count) { + if (!count) return cb(nonDataErr, count); + str += buffer.toString('ascii'); var eol = str.indexOf('\n'); //console.log(' -- read %d bytes at <%d>', count, pos, eol); @@ -119,7 +123,7 @@ function readLocation(location, callback) { } str = str.substr(0, eol); - if (str === '' && !err) err = new Error('no data at offset ' + pos); + if (str === '' && !err) err = nonDataErr; cb(err, count); }); } @@ -167,7 +171,7 @@ function lookup(offsets, callback) { function openFile() { if (!self.fd) { - //console.log(' ... opening', self.filePath); + // console.log(' ... opening', self.filePath); self.fd = fs.openSync(self.filePath, 'r'); } // ref count so we know when to close the main index file @@ -177,7 +181,7 @@ function lookup(offsets, callback) { function closeFile() { if (--self.refcount === 0) { - //console.log(' ... closing', self.filePath); + // console.log(' ... closing', self.filePath); fs.closeSync(self.fd); self.fd = null; } diff --git a/test/wordpos_test.js b/test/wordpos_test.js index db63110..512def1 100644 --- a/test/wordpos_test.js +++ b/test/wordpos_test.js @@ -37,8 +37,7 @@ var str = "The angry bear chased the frightened little squirrel", rest: [ 'The' ] }, garble = 'garblegarble', // expect not to find word - offset = 1285602, - offset_pos ='a'; + offset = 1285602; @@ -361,6 +360,15 @@ describe('randX()...', 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) { wordpos.seek('foobar', 'a', function(err, result){ assert(err instanceof Error); @@ -371,7 +379,7 @@ describe('seek()...', function() { it('should handle wrong offset', function(done) { 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.equal(err.message, 'Bad data at location ' + bad_offset); assert.deepEqual(result, {}); @@ -381,7 +389,7 @@ describe('seek()...', function() { it('should handle very large offset', function(done) { 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.equal(err.message, 'no data at offset ' + bad_offset); assert.deepEqual(result, {}); @@ -389,7 +397,7 @@ describe('seek()...', function() { }).catch(_.noop); // UnhandledPromiseRejectionWarning; }); - it('should handle bad pos', function(done) { + it('should handle bad POS', function(done) { wordpos.seek(offset, 'g', function(err, result) { assert(err instanceof Error); assert(/Incorrect POS/.test(err.message)); @@ -397,21 +405,13 @@ describe('seek()...', function() { }).catch(_.noop); // UnhandledPromiseRejectionWarning; }); - it('should handle wrong pos', function(done) { + it('should handle wrong POS', function(done) { wordpos.seek(offset, 'v', function(err, result){ assert.equal(err.message, 'Bad data at location ' + offset); }).catch(_.noop); // UnhandledPromiseRejectionWarning; 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 () { - 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.pos, 's'); assert.equal(result.lemma, 'amazing'); @@ -483,14 +483,14 @@ describe('Promise pattern', 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.equal(err.message, 'Bad data at location ' + (offset+1)); }); }); 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.equal(err.message, 'offset must be valid positive number.'); });