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

Changed returned array to object & fixed bufferManagerException error #1383

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions tools/nodejs_api/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ async function executeAllCallback(err, queryResult) {
console.log(err);
} else {
await queryResult.all({"callback": (err, result) => {
console.log("All result received Callback");
console.log(result);
if (err) {
console.log("All result with Callback failed");
console.log(err);
} else {
console.log(result);
console.log("All result received Callback");
}
}});
}
}
Expand All @@ -24,8 +29,8 @@ async function executeAllPromise(err, queryResult) {
console.log(err);
} else {
await queryResult.all().then(result => {
console.log("All result received Promise");
console.log(result);
console.log("All result received Promise");
}).catch(error => {
console.log("All with Promise failed");
console.log(error);
Expand All @@ -44,7 +49,7 @@ const executeQuery = "MATCH (a:person) RETURN a.fName, a.age, a.eyeSight, a.isSt
const parameterizedExecuteQuery = "MATCH (a:person) WHERE a.age > $1 and a.isStudent = $2 and a.fName < $3 RETURN a.fName, a.age, a.eyeSight, a.isStudent;";

connection.execute(executeQuery, {"callback": executeAllPromise});
// connection.execute(parameterizedExecuteQuery, {"callback": executeAllPromise, "params":[["1", 29], ["2", true], ["3", "B"]]});
connection.execute(parameterizedExecuteQuery, {"callback": executeAllPromise, "params":[["1", 29], ["2", true], ["3", "B"]]});

// Extensive Case
database.resizeBufferManager(2000000000);
Expand Down Expand Up @@ -78,8 +83,8 @@ connection.execute(executeQuery).then(async queryResult => {
if (err) {
console.log(err);
} else {
console.log("All result received for execution with a promise");
console.log(result);
console.log("All result received for execution with a promise");
}
}});
}).catch(error => {
Expand All @@ -96,8 +101,8 @@ asyncAwaitExecute(executeQuery).then(async queryResult => {
if (err) {
console.log(err);
} else {
console.log("All result received for execution with await");
console.log(result);
console.log("All result received for execution with await");
}
}});
}).catch(error => {
Expand Down
3 changes: 1 addition & 2 deletions tools/nodejs_api/src_cpp/include/node_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ class NodeConnection : public Napi::ObjectWrap<NodeConnection> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
NodeConnection(const Napi::CallbackInfo& info);
~NodeConnection();
~NodeConnection() = default;

private:
static Napi::FunctionReference constructor;
Napi::Value Execute(const Napi::CallbackInfo& info);
void SetMaxNumThreadForExec(const Napi::CallbackInfo& info);
Napi::Value GetNodePropertyNames(const Napi::CallbackInfo& info);
Expand Down
3 changes: 1 addition & 2 deletions tools/nodejs_api/src_cpp/include/node_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ class NodeDatabase : public Napi::ObjectWrap<NodeDatabase> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
NodeDatabase(const Napi::CallbackInfo& info);
~NodeDatabase();
~NodeDatabase() = default;
friend class NodeConnection;

private:
void ResizeBufferManager(const Napi::CallbackInfo& info);
static Napi::FunctionReference constructor;
unique_ptr<kuzu::main::Database> database;
};
5 changes: 1 addition & 4 deletions tools/nodejs_api/src_cpp/include/node_query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ class NodeQueryResult: public Napi::ObjectWrap<NodeQueryResult> {
static Napi::Object Init(Napi::Env env, Napi::Object exports);
void SetQueryResult(shared_ptr<kuzu::main::QueryResult> & inputQueryResult);
NodeQueryResult(const Napi::CallbackInfo& info);
~NodeQueryResult();
~NodeQueryResult() = default;

private:
static Napi::FunctionReference constructor;
void Close(const Napi::CallbackInfo& info);
Napi::Value All(const Napi::CallbackInfo& info);
Napi::Value Each(const Napi::CallbackInfo& info);
shared_ptr<kuzu::main::QueryResult> queryResult;


};


Expand Down
4 changes: 0 additions & 4 deletions tools/nodejs_api/src_cpp/node_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

using namespace kuzu::main;

Napi::FunctionReference NodeConnection::constructor;

Napi::Object NodeConnection::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand Down Expand Up @@ -41,8 +39,6 @@ NodeConnection::NodeConnection(const Napi::CallbackInfo& info) : Napi::ObjectWra
}
}

NodeConnection::~NodeConnection() {}

Napi::Value NodeConnection::Execute(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Expand Down
4 changes: 0 additions & 4 deletions tools/nodejs_api/src_cpp/node_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using namespace kuzu::main;

Napi::FunctionReference NodeDatabase::constructor;

Napi::Object NodeDatabase::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand Down Expand Up @@ -40,8 +38,6 @@ NodeDatabase::NodeDatabase(const Napi::CallbackInfo& info) : Napi::ObjectWrap<No
}
}

NodeDatabase::~NodeDatabase() {}

void NodeDatabase::ResizeBufferManager(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Expand Down
6 changes: 1 addition & 5 deletions tools/nodejs_api/src_cpp/node_query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ using namespace kuzu::main;
std::thread nativeThread;
Napi::ThreadSafeFunction tsfn;

Napi::FunctionReference NodeQueryResult::constructor;

Napi::Object NodeQueryResult::Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);

Expand Down Expand Up @@ -97,6 +95,4 @@ Napi::Value NodeQueryResult::Each(const Napi::CallbackInfo& info) {
// Return the deferred's Promise. This Promise is resolved in the thread-safe
// function's finalizer callback.
return testData->deferred.Promise();
}

NodeQueryResult::~NodeQueryResult() {}
}
10 changes: 2 additions & 8 deletions tools/nodejs_api/src_cpp/tsfn_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ void TsfnContext::threadEntry(TsfnContext* context) {
Napi::Array arr = Napi::Array::New(env);
size_t i = 0;
auto columnNames = context->queryResult->getColumnNames();
Napi::Array colArray = Napi::Array::New(env);
for (auto i = 0u; i < columnNames.size(); ++i) {
colArray.Set(i, columnNames[i]);
}
if (context->type == TsfnContext::EACH) { jsCallback.Call({env.Null(), colArray}); }
else { arr.Set(i++, colArray); }
while (context->queryResult->hasNext()) {
auto row = context->queryResult->getNext();
Napi::Array rowArray = Napi::Array::New(env);
Napi::Object rowArray = Napi::Object::New(env);
for (size_t j = 0; j < row->len(); j++) {
Napi::Value val = Util::ConvertToNapiObject(*row->getValue(j), env);
rowArray.Set(j, val);
rowArray.Set( (j >= columnNames.size()) ? "" : columnNames[j], val);
}
if (context->type == TsfnContext::EACH) { jsCallback.Call({env.Null(), rowArray}); }
else { arr.Set(i++, rowArray); }
Expand Down
6 changes: 4 additions & 2 deletions tools/nodejs_api/src_nodejs/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const QueryResult = require("./queryResult.js");

class Connection {
#connection;
#database;
constructor(database, numThreads = 0) {
if (typeof database !== "object" || database.constructor.name !== "Database" || typeof numThreads !== "number" || !Number.isInteger(numThreads)){
throw new Error("Connection constructor requires a database object and optional numThreads integer as argument(s)");
}
this.#database = database;
this.#connection = new kuzu.NodeConnection(database.database, numThreads);
}

Expand All @@ -24,20 +26,20 @@ class Connection {
}

const nodeQueryResult = new kuzu.NodeQueryResult();
const queryResult = new QueryResult(this, nodeQueryResult);
if ('callback' in opts) {
const callback = opts['callback'];
if (typeof callback !== "function" || callback.length !== 2){
throw new Error("if execute is given a callback, it must take 2 arguments: (err, result)");
}
this.#connection.execute(query, err => {
const queryResult = new QueryResult(nodeQueryResult);
callback(err, queryResult);
}, nodeQueryResult, params);
} else {
return new Promise ((resolve, reject) => {
this.#connection.execute(query, err => {
if (err) { return reject(err); }
return resolve(new QueryResult(nodeQueryResult));
return resolve(queryResult);
}, nodeQueryResult, params);
})
}
Expand Down
12 changes: 4 additions & 8 deletions tools/nodejs_api/src_nodejs/queryResult.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class QueryResult {
#connection
#database;
#queryResult
#isClosed
constructor(queryResult) {
constructor(connection, queryResult) {
if (typeof queryResult !== "object" || queryResult.constructor.name !== "NodeQueryResult"){
throw new Error("QueryResult constructor requires a NodeQueryResult object as an argument");
}
this.#connection = connection;
this.#queryResult = queryResult;
this.#isClosed = false;
}

checkForQueryResultClose(){
Expand All @@ -15,12 +17,6 @@ class QueryResult {
}
}

close(){
this.checkForQueryResultClose();
this.#queryResult.close();
this.#isClosed = true;
}

async each(rowCallback, doneCallback) {
this.checkForQueryResultClose();
if (typeof rowCallback !== 'function' || rowCallback.length !== 2 || typeof doneCallback !== 'function' || doneCallback.length !== 0) {
Expand Down