user479947
user479947

Reputation:

LevelDB unexpected sorting results

The levelup API (https://github.com/Level/levelup#api) describes the sorting options as:

You can supply an options object as the first parameter to createReadStream() with the following properties:

gt (greater than), gte (greater than or equal) define the lower bound of the range to be streamed. Only entries where the key is greater than (or equal to) this option will be included in the range.

lt (less than), lte (less than or equal) define the higher bound of the range to be streamed. Only entries where the key is less than (or equal to) this option will be included in the range.

I setup a quick test to try to debug the odd results I'm seeing in my levelDB-based kv store.

const level = require('level');
const levelDb = level('./db');

const put = (key, val) => new Promise(resolve => {
    levelDb.put(key, val, err => {
        if (err) resolve(false);
        resolve(true);
    });
});

const stream = options => new Promise(resolve => {
    let output = [];
    levelDb.createReadStream(options)
        .on('data', data => { output.push({ k: data.key, v: data.value }); })
        .on('end', () => { resolve(output); });
});

const runTest = async() => {
    await put('a', 123);
    await put('b:ab', 123);
    await put('b:X345345:234', 123);
    await put('c', 123);

    const results = await stream({ gte: 'b:0', lte:'b:Z' });
    console.log(results);
}

runTest();

I have a key b:ab and a key b:X345345:234, which would seemingly fall between b:0 and b:Z, yet I only receive b:X345345:234 in the results.

Why is that?

Upvotes: 0

Views: 501

Answers (1)

dgrogan
dgrogan

Reputation: 2720

b:ab doesn't fall within b:0 and b:Z because a comes after Z in the ascii table.

Upvotes: 0

Related Questions