subleveldown: bytewise key-encoding on sub-level causes not found on other level

I’m getting odd behavior when using bytewise encoding on a sublevel. The intention is have bytewise encode a number to lex-sortable key, and then have that key appended on the end of the sub-level. It works if I write one value, but the moment I write a second, I can’t read keys from other nested subs. Any idea where I’m going wrong? Docs says a sub-level must encode to a buffer and bytewise should be doing just that.

var sub = require('subleveldown')
var memdown = require('memdown')
var levelup = require('levelup');
var encoding = require('encoding-down');
var bytewise = require('bytewise');
var msgpack = require('msgpack-lite');
var {streamToRx} = require('rxjs-stream');

var db = levelup(encoding(memdown()));

var test1= sub(db, "logs", {valueEncoding: "json" });
var test2 = sub(db, "data");
var nested1 = sub(test2, '1234', { keyEncoding: bytewise, valueEncoding: msgpack })

async function main(){
  await test1.put("1234", "FOO");

  console.log("Got: " + await test1.get("1234"));
  console.log("put one..")
  await nested1.put(10,  10);

  console.log("Got: " + await test1.get("1234"));

  await dumpKeys(db);

  await nested1.put(20,  20);
  console.log("put another..")

  await dumpKeys(db);

  console.log(await test1.get("1234"));

  await dumpKeys(db);
}

async function dumpKeys(db){
  console.log("DUMP:")
  await streamToRx(db.createKeyStream()).forEach(x=>console.log(" " + x.toString()));
}

main().catch(console.log);

Console output:

Got: FOO
put one..
Got: FOO
DUMP:
 !logs!1234
 !data!!1234!B@$
put another..
DUMP:
 !logs!1234
 !data!!1234!B@$
 !data!!1234!B@4
NotFoundError: Key not found in database [1234]
    at D:\Code\geo\node_modules\levelup\lib\levelup.js:160:15
    at D:\Code\geo\node_modules\encoding-down\index.js:50:21
    at Immediate.callNext (D:\Code\geo\node_modules\memdown\memdown.js:162:7)
    at runCallback (timers.js:694:18)
    at tryOnImmediate (timers.js:665:5)
    at processImmediate (timers.js:647:5)

deps:

    "bytewise": "^1.1.0",
    "encoding-down": "^6.0.2",
    "leveldown": "^5.1.0",
    "levelup": "^4.0.2",
    "msgpack-lite": "^0.1.26",
    "rxjs": "^6.5.1",
    "rxjs-stream": "^3.0.2",
    "subleveldown": "^4.0.0",

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 20 (20 by maintainers)

Commits related to this issue

Most upvoted comments

fixed - still failing. I’ve also made sure that my local version of level-codec with your suggested change (https://github.com/Level/codec/compare/master...MeirionHughes:master) is defiantly being loaded.

… anyway I can use charwise and keep all my keys as strings for now.

okaydoky I’ll test if that fixes my unit-test locally then do the pr.