heapwolf / lev

The complete REPL & CLI for managing LevelDB instances.
MIT License
296 stars 36 forks source link

Problem with the REPL #13

Closed alessioalex closed 11 years ago

alessioalex commented 11 years ago
alessio@alessioalex:~/www/levlit$ lev tmp/gw.db/                                                  

compression = true                                                                                
encoding = json                                                                                   
keyEncoding = json                                                                                
valueEncoding = json                                                                              
levelup version = 0.8.0                                                                           

tmp/gw.db/>                                                                                       
EncodingError: Unexpected token a 

The db is ok, because I write / read data to it and everything works fine, but cannot get the REPL or actually lev to work (lev --read ..etc).

I'm using Node v0.8.19

juliangruber commented 11 years ago

That's an encoding issue, since lev expects json encodings and tries to JSON.parse something that isn't JSON. I think this has to do with the key cache, @hij1nx?

For now just pass --encoding=utf8 and everything should work.

heapwolf commented 11 years ago

yep. its the key cache, we need a way to handle key caching for multiple encodings

juliangruber commented 11 years ago

Maybe we can make levelup handle json encodings like this:

try { return JSON.stringify(value); }
catch (e) { return value; }

So invalid JSON will just be a string.

juliangruber commented 11 years ago

levelup 0.9 will have the possibility to specify encodings for streams too, so we'll be able to do:

db.createReadStream({ encoding : 'utf8' }).pipe(through(function (data) {
  try {
    this.queue(JSON.parse(data));
  } catch (e) {
    this.queue(data);
  }
}));

That at least won't break lev with the default JSON encoding.

heapwolf commented 11 years ago

ah! this is a great idea!

heapwolf commented 11 years ago

this should work now.