Skip to content

Commit

Permalink
Use a notify method on the user buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed May 22, 2024
1 parent bcd9df7 commit 1ca8163
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
26 changes: 14 additions & 12 deletions read.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,23 +394,25 @@ export function addReadMethods(

getUserSharedBuffer(id, defaultBuffer, options) {
let keySize;
if (options?.envKey) keySize = this.writeKey(id, keyBytes, 0);
else {
keyBytes.dataView.setUint32(0, this.db.dbi);
keySize = this.writeKey(id, keyBytes, 4);
}
return getUserSharedBuffer(
const setKeyBytes = () => {
if (options?.envKey) keySize = this.writeKey(id, keyBytes, 0);
else {
keyBytes.dataView.setUint32(0, this.db.dbi);
keySize = this.writeKey(id, keyBytes, 4);
}
};
setKeyBytes();
let sharedBuffer = getUserSharedBuffer(
env.address,
keySize,
defaultBuffer,
options?.callback,
);
},

notifyUserCallbacks(id) {
keyBytes.dataView.setUint32(0, this.db.dbi);
let keySize = this.writeKey(id, keyBytes, 4);
return notifyUserCallbacks(env.address, keySize);
sharedBuffer.notify = () => {
setKeyBytes();
return notifyUserCallbacks(env.address, keySize);
};
return sharedBuffer;
},

attemptLock(id, version, callback) {
Expand Down
3 changes: 1 addition & 2 deletions src/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ void WriteWorker::Write() {
std::atomic_fetch_or((std::atomic<uint32_t>*) instructions, (uint32_t) TXN_COMMITTED);
if (had_changes) {
ExtendedEnv* extended_env = (ExtendedEnv*) mdb_env_get_userctx(env);
std::string key("__committed__");
extended_env->notifyUserCallbacks(key);
extended_env->notifyUserCallbacks(std::string("__committed__"));
}
}

Expand Down
14 changes: 9 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -907,12 +907,16 @@ describe('lmdb-js', function () {
let notified;
let shared_buffer;
await new Promise((resolve) => {
db.getUserSharedBuffer('with-callback', shared_number.buffer, {
callback() {
resolve();
shared_buffer = db.getUserSharedBuffer(
'with-callback',
shared_number.buffer,
{
callback() {
resolve();
},
},
});
db.notifyUserCallbacks('with-callback');
);
shared_buffer.notify();
});
});
it('prefetch', async function () {
Expand Down

0 comments on commit 1ca8163

Please sign in to comment.