creationix / nstore

nStore is a simple, in-process key/value database for node.js
MIT License
392 stars 31 forks source link

Length extends beyond buffer #39

Open mauron85 opened 11 years ago

mauron85 commented 11 years ago

Hi, I'm storing results of Google Maps Places search and also Facebook Places into nStore. But after some time I get following error:

version of nStore: 0.5.2

fs.js:457
  binding.read(fd, buffer, offset, length, position, wrapper);
          ^
Error: Length extends beyond buffer
    at Object.fs.read (fs.js:457:11)
    at readChunk (/private/var/www/myapp-nodejs/node_modules/nstore/lib/file.js:18:8)
    at fsRead (/private/var/www/myapp-nodejs/node_modules/nstore/lib/file.js:30:3)
    at checkQueue (/private/var/www/myapp-nodejs/node_modules/nstore/lib/file.js:63:5)
    at /private/var/www/myapp-nodejs/node_modules/nstore/lib/file.js:66:9
    at /private/var/www/myapp-nodejs/node_modules/nstore/lib/file.js:27:7
    at Object.wrapper [as oncomplete] (fs.js:454:17)
DEBUG: Program node server.js exited with code 8
matthiasdg commented 11 years ago

I had this problem as well. Did you edit your .db file manually? I noticed that this problem occurs if you have empty lines in the file; e.g. an empty line at the beginning, or you delete some entries manually and leave your cursor at the beginning of the last -now empty- line instead of at the end of the last populated line upon saving.

mauron85 commented 11 years ago

No I didn't edit db file manually. I can replicate the issue with my this db: https://dl.dropboxusercontent.com/u/150742/nStore-bug.zip

and followin testing code

var nStore = require('nstore');
nStore = nStore.extend(require('nstore/query')());
var provider = nStore.new('provider.db', function () {
  console.log('DB provider loaded');
  provider.all(function (err, results) {
    if (err) console.log(err);
    else {
      console.log(results);
      results.forEach(function(result) {
        console.log(result);
      });
    }
  });
});
matthiasdg commented 11 years ago

I took a quick look at your file and there is definitely something wrong with it; if you have an editor that shows line numbers, you'll see there is a line (25) starting with "photo" instead of a dbase key (after "were_here_count":3383 ). Normally, new lines start with a key. If you delete that line completely (no idea what the missing part is; line 24 is complete, so it looks like there is quite a lot missing from 25), it works.

And in your test code, results is a single object (its keys are the dbase keys) so forEach won't work.

for(var key in results){
    console.log(results[key]);
}

So now your problem is reduced to finding out how your db file got corrupt :-)

mauron85 commented 11 years ago

You're right. Right now i'm investigating what caused the wrong data was written.