Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared structures are not used when sharedStructuresKey is set to 0 #270

Closed
kajkal opened this issue Jan 25, 2024 · 1 comment
Closed

Shared structures are not used when sharedStructuresKey is set to 0 #270

kajkal opened this issue Jan 25, 2024 · 1 comment

Comments

@kajkal
Copy link

kajkal commented Jan 25, 2024

It looks like the sharedStructuresKey does not work if it is set to 0.
I would like to use keyEncoding: 'uint32' combined with shared structures thus sharedStructuresKey set to 0 seems to me the optimal choice (in the case in which, due to the key encoding, js Symbols can not be used).

import assert from 'node:assert/strict';
import { open } from 'lmdb';

async function populate(db, idOffset) {
    await db.transaction(() => {
        for (let id = 0; id < 100_000; id++) {
            db.put(id + idOffset, { some: 'structure' });
        }
    });
}

{
    let db = open({
        path: 'shared-structures-are-not-used-when-key-is-0',
        sharedStructuresKey: 0,
        keyEncoding: 'uint32',
    });

    await populate(db, 1); // 0 - sharedStructuresKey; 1,2,3,... - ids

    assert.deepEqual(db.get(0), undefined);
    assert.equal(db.getCount(), 100_000);

    await db.close();
}

{
    let db = open({
        path: 'shared-structures-are-used-when-key-is-1',
        sharedStructuresKey: 1,
        keyEncoding: 'uint32',
    });

    await populate(db, 2); // 1 - sharedStructuresKey; 2,3,4,... - ids

    assert.deepEqual(db.get(1), [ [ 'some' ] ]);
    assert.equal(db.getCount(), 100_001);

    await db.close();
}

0 is a falsy value so condition below will not see the passed key and will conclude that sharedStructuresKey was not defined.

lmdb-js/open.js

Lines 252 to 254 in 35e37a2

this.sharedStructuresKey ? this.setupSharedStructures() : {
copyBuffers: true, // need to copy any embedded buffers that are found since we use unsafe buffers
}, options, dbOptions), this.encoder));

Is this intentional?

@kriszyp
Copy link
Owner

kriszyp commented Jan 28, 2024

No, not intentional, thank you for the clear issue report. I will get this fix published soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants