Skip to content

Commit

Permalink
NodeJS API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
royi-luo committed Jan 17, 2025
1 parent ff0337d commit a2f0f2a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tools/nodejs_api/src_cpp/include/node_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class NodeDatabase : public Napi::ObjectWrap<NodeDatabase> {
bool enableCompression;
bool readOnly;
uint64_t maxDBSize;
bool autoCheckpoint;
uint64_t checkpointThreshold;
std::shared_ptr<Database> database;
};

Expand Down
6 changes: 6 additions & 0 deletions tools/nodejs_api/src_cpp/node_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ NodeDatabase::NodeDatabase(const Napi::CallbackInfo& info) : Napi::ObjectWrap<No
enableCompression = info[2].As<Napi::Boolean>().Value();
readOnly = info[3].As<Napi::Boolean>().Value();
maxDBSize = info[4].As<Napi::Number>().Int64Value();
autoCheckpoint = info[5].As<Napi::Boolean>().Value();
checkpointThreshold = info[6].As<Napi::Number>().Int64Value();
}

Napi::Value NodeDatabase::InitAsync(const Napi::CallbackInfo& info) {
Expand All @@ -48,6 +50,10 @@ void NodeDatabase::InitCppDatabase() {
if (maxDBSize > 0) {
systemConfig.maxDBSize = maxDBSize;
}
systemConfig.autoCheckpoint = autoCheckpoint;
if (checkpointThreshold != std::numeric_limits<uint64_t>::max()) {
systemConfig.checkpointThreshold = checkpointThreshold;
}
this->database = std::make_shared<Database>(databasePath, systemConfig);
}

Expand Down
12 changes: 10 additions & 2 deletions tools/nodejs_api/src_js/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class Database {
bufferManagerSize = 0,
enableCompression = true,
readOnly = false,
maxDBSize = 0
maxDBSize = 0,
autoCheckpoint = false,
checkpointThreshold = -1
) {
if (!databasePath) {
databasePath = ":memory:";
Expand All @@ -38,14 +40,20 @@ class Database {
if (typeof maxDBSize !== "number" || maxDBSize < 0) {
throw new Error("Max DB size must be a positive integer.");
}
if (typeof checkpointThreshold !== "number" || maxDBSize < -1) {
throw new Error("Checkpoint threshold must be a positive integer.");
}
bufferManagerSize = Math.floor(bufferManagerSize);
maxDBSize = Math.floor(maxDBSize);
checkpointThreshold = Math.floor(checkpointThreshold);
this._database = new KuzuNative.NodeDatabase(
databasePath,
bufferManagerSize,
enableCompression,
readOnly,
maxDBSize
maxDBSize,
autoCheckpoint,
checkpointThreshold
);
this._isInitialized = false;
this._initPromise = null;
Expand Down
53 changes: 53 additions & 0 deletions tools/nodejs_api/test/test_database.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,59 @@ describe("Database constructor", function () {
assert.notExists(testDb._initPromise);
});

it("should create a database with auto checkpoint configured", async function () {
const tmpDbPath = await new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, path, _) => {
if (err) {
return reject(err);
}
return resolve(path);
});
});
const testDb = new kuzu.Database(tmpDbPath,
1 << 28 /* 256MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1GB */,
false /* autoCheckpoint */
);
const conn = new kuzu.Connection(testDb);
let res = await conn.query("CALL current_setting('auto_checkpoint') RETURN *");
assert.equal(res.getNumTuples(), 1);
const tuple = await res.getNext();
assert.equal(tuple["auto_checkpoint"], "False");
res.close();
conn.close();
testDb.close();
});

it("should create a database with checkpoint threshold configured", async function () {
const tmpDbPath = await new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, path, _) => {
if (err) {
return reject(err);
}
return resolve(path);
});
});
const testDb = new kuzu.Database(tmpDbPath,
1 << 28 /* 256MB */,
true /* compression */,
false /* readOnly */,
1 << 30 /* 1GB */,
true /* autoCheckpoint */,
1234 /* checkpointThreshold */
);
const conn = new kuzu.Connection(testDb);
let res = await conn.query("CALL current_setting('checkpoint_threshold') RETURN *");
assert.equal(res.getNumTuples(), 1);
const tuple = await res.getNext();
assert.equal(tuple["checkpoint_threshold"], 1234);
res.close();
conn.close();
testDb.close();
});

it("should create a database in read-only mode", async function () {
const tmpDbPath = await new Promise((resolve, reject) => {
tmp.dir({ unsafeCleanup: true }, (err, path, _) => {
Expand Down

0 comments on commit a2f0f2a

Please sign in to comment.