From 2e7bfa7888e6e990a45c8d3d77112360b673cbca Mon Sep 17 00:00:00 2001 From: knonomura Date: Thu, 6 Jan 2022 19:11:48 +0900 Subject: [PATCH] fix issue with thread_workers --- src/Addon.cc | 9 ++++----- src/AggregationResult.cpp | 8 ++++++++ src/AggregationResult.h | 2 ++ src/Container.cpp | 18 ++++++++++++++++++ src/Container.h | 2 ++ src/ContainerInfo.cpp | 14 ++++++++++++++ src/ContainerInfo.h | 2 ++ src/ExpirationInfo.cpp | 9 +++++++++ src/ExpirationInfo.h | 2 ++ src/GSException.cpp | 18 ++++++++++++++++++ src/GSException.h | 2 ++ src/PartitionController.cpp | 8 ++++++++ src/PartitionController.h | 2 ++ src/Query.cpp | 19 +++++++++++++++++++ src/Query.h | 2 ++ src/QueryAnalysisEntry.cpp | 8 ++++++++ src/QueryAnalysisEntry.h | 2 ++ src/RowKeyPredicate.cpp | 8 ++++++++ src/RowKeyPredicate.h | 9 ++++++--- src/RowSet.cpp | 18 ++++++++++++++++++ src/RowSet.h | 2 ++ src/Store.cpp | 34 ++++++++++++++++++++++++++++++++++ src/Store.h | 3 ++- src/StoreFactory.cpp | 22 ++++++++++++++++++---- src/StoreFactory.h | 3 ++- src/Util.cpp | 22 ++++++++++++++++++++++ src/Util.h | 8 ++++++++ 27 files changed, 242 insertions(+), 14 deletions(-) diff --git a/src/Addon.cc b/src/Addon.cc index 93709df..d26c3ac 100644 --- a/src/Addon.cc +++ b/src/Addon.cc @@ -14,8 +14,6 @@ limitations under the License. */ -#ifndef _ADDON_H_ -#define _ADDON_H_ #include #include "GSException.h" #include "StoreFactory.h" @@ -29,14 +27,16 @@ #include "Store.h" #include "RowKeyPredicate.h" #include "QueryAnalysisEntry.h" +#include "Util.h" Napi::Object init(Napi::Env env, Napi::Object exports); -#endif // _ADDON_H_ - using namespace griddb; Napi::Object init(Napi::Env env, Napi::Object exports) { +#if NAPI_VERSION > 5 + env.SetInstanceData(new AddonData()); +#endif GSException::init(env, exports); StoreFactory::init(env, exports); Store::init(env, exports); @@ -53,4 +53,3 @@ Napi::Object init(Napi::Env env, Napi::Object exports) { } NODE_API_MODULE(addon, init); - diff --git a/src/AggregationResult.cpp b/src/AggregationResult.cpp index 7885c85..30fb921 100644 --- a/src/AggregationResult.cpp +++ b/src/AggregationResult.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference AggregationResult::constructor; +#endif Napi::Object AggregationResult::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -27,8 +29,14 @@ Napi::Object AggregationResult::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass(env, "AggregationResult", { InstanceMethod("get", &AggregationResult::get) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "AggregationResult", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("AggregationResult", func); return exports; } diff --git a/src/AggregationResult.h b/src/AggregationResult.h index 48e5552..7e4a0eb 100644 --- a/src/AggregationResult.h +++ b/src/AggregationResult.h @@ -25,8 +25,10 @@ namespace griddb { class AggregationResult : public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit AggregationResult(const Napi::CallbackInfo &info); diff --git a/src/Container.cpp b/src/Container.cpp index 3dabc82..a7abdd3 100644 --- a/src/Container.cpp +++ b/src/Container.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference Container::constructor; +#endif Napi::Object Container::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -41,8 +43,14 @@ Napi::Object Container::init(Napi::Env env, Napi::Object exports) { InstanceAccessor("type", &Container::getType, nullptr) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "Container", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("Container", func); return exports; } @@ -201,8 +209,13 @@ Napi::Value Container::query(const Napi::CallbackInfo &info) { mContainerInfo); auto gsRowPtr = Napi::External::New(env, mRow); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "Query")-> + New({queryPtr, containerInfoPtr, gsRowPtr})).ToObject(); +#else return scope.Escape(Query::constructor.New( { queryPtr, containerInfoPtr, gsRowPtr })).ToObject(); +#endif } Napi::Value Container::get(const Napi::CallbackInfo &info) { @@ -329,8 +342,13 @@ Napi::Value Container::queryByTimeSeriesRange(const Napi::CallbackInfo &info) { mContainerInfo); auto gsRowPtr = Napi::External::New(env, mRow); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "Query")-> + New({queryPtr, containerInfoPtr, gsRowPtr})).ToObject(); +#else return scope.Escape(Query::constructor.New( { queryPtr, containerInfoPtr, gsRowPtr })).ToObject(); +#endif } Container::~Container() { diff --git a/src/Container.h b/src/Container.h index eca319d..4a6fd4d 100644 --- a/src/Container.h +++ b/src/Container.h @@ -30,8 +30,10 @@ namespace griddb { class Container: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Contructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit Container(const Napi::CallbackInfo &info); diff --git a/src/ContainerInfo.cpp b/src/ContainerInfo.cpp index 4a0b880..29692c5 100644 --- a/src/ContainerInfo.cpp +++ b/src/ContainerInfo.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference ContainerInfo::constructor; +#endif Napi::Object ContainerInfo::init(const Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -42,8 +44,14 @@ Napi::Object ContainerInfo::init(const Napi::Env env, Napi::Object exports) { &ContainerInfo::setExpiration), }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "ContainerInfo", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("ContainerInfo", func); return exports; } @@ -632,9 +640,15 @@ Napi::Value ContainerInfo::getExpiration( mContainerInfo.timeSeriesProperties->expirationDivisionCount; // Create new ExpirationInfo object Napi::EscapableHandleScope scope(env); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "ExpirationInfo")->New( { + Napi::Number::New(env, time), Napi::Number::New(env, unit), + Napi::Number::New(env, division_count) })).ToObject(); +#else return scope.Escape(ExpirationInfo::constructor.New( { Napi::Number::New(env, time), Napi::Number::New(env, unit), Napi::Number::New(env, division_count) })).ToObject(); +#endif } else { return env.Null(); } diff --git a/src/ContainerInfo.h b/src/ContainerInfo.h index 543f039..0c193ae 100644 --- a/src/ContainerInfo.h +++ b/src/ContainerInfo.h @@ -34,8 +34,10 @@ namespace griddb { class ContainerInfo: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static Napi::FunctionReference constructor; +#endif explicit ContainerInfo(const Napi::CallbackInfo &info); static Napi::Object init(Napi::Env env, Napi::Object exports); diff --git a/src/ExpirationInfo.cpp b/src/ExpirationInfo.cpp index eb3e035..c758b16 100644 --- a/src/ExpirationInfo.cpp +++ b/src/ExpirationInfo.cpp @@ -17,10 +17,13 @@ #include "ExpirationInfo.h" #include "GSException.h" #include "Macro.h" +#include "Util.h" namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference ExpirationInfo::constructor; +#endif void ExpirationInfo::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass(env, "ExpirationInfo", @@ -30,8 +33,14 @@ void ExpirationInfo::init(Napi::Env env, Napi::Object exports) { ExpirationInfo::InstanceAccessor("divisionCount", &ExpirationInfo::getDivisionCount, &ExpirationInfo::setDivisionCount), }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "ExpirationInfo", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("ExpirationInfo", func); } diff --git a/src/ExpirationInfo.h b/src/ExpirationInfo.h index edb2f42..c79c16b 100644 --- a/src/ExpirationInfo.h +++ b/src/ExpirationInfo.h @@ -24,8 +24,10 @@ namespace griddb { class ExpirationInfo: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static Napi::FunctionReference constructor; +#endif explicit ExpirationInfo(const Napi::CallbackInfo &info); static void init(Napi::Env env, Napi::Object exports); diff --git a/src/GSException.cpp b/src/GSException.cpp index 970cbfb..8449f00 100644 --- a/src/GSException.cpp +++ b/src/GSException.cpp @@ -16,9 +16,12 @@ #include #include "GSException.h" +#include "Util.h" namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference GSException::constructor; +#endif Napi::Object GSException::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); Napi::Function t = DefineClass(env, "GSException", { @@ -29,8 +32,14 @@ Napi::Object GSException::init(Napi::Env env, Napi::Object exports) { InstanceMethod("getMessage", &GSException::getMessage), InstanceMethod("getLocation", &GSException::getLocation) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(t); + Util::setInstanceData(env, "GSException", constructor); +#else constructor = Napi::Persistent(t); constructor.SuppressDestruct(); +#endif exports.Set("GSException", t); return exports; } @@ -74,12 +83,21 @@ Napi::Object GSException::New(Napi::Env env, std::string message, } Napi::Object GSException::New(Napi::Env env, GSResult code, const char* message, const char* location, void* resource) { +#if NAPI_VERSION > 5 + return Util::getInstanceData(env, "GSException")->New({ + Napi::Number::New(env, code), + message ? Napi::String::New(env, message) : env.Null(), + location ? Napi::String::New(env, location) : env.Null(), + resource ? Napi::External::New(env, resource) : env.Null() + }); +#else return constructor.New( { Napi::Number::New(env, code), message ? Napi::String::New(env, message) : env.Null(), location ? Napi::String::New(env, location) : env.Null(), resource ? Napi::External::New(env, resource) : env.Null() }); +#endif } GSException::~GSException() { } diff --git a/src/GSException.h b/src/GSException.h index 587716b..5165770 100644 --- a/src/GSException.h +++ b/src/GSException.h @@ -28,7 +28,9 @@ namespace griddb { class GSException : public Napi::ObjectWrap{ public: +#if NAPI_VERSION <= 5 static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); static Napi::Object New(Napi::Env env, GSResult code, void* resource = NULL); diff --git a/src/PartitionController.cpp b/src/PartitionController.cpp index 3aa5bec..5de6cdb 100644 --- a/src/PartitionController.cpp +++ b/src/PartitionController.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference PartitionController::constructor; +#endif Napi::Object PartitionController::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -36,8 +38,14 @@ Napi::Object PartitionController::init(Napi::Env env, Napi::Object exports) { &PartitionController::getContainerNames) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "PartitionController", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("PartitionController", func); return exports; } diff --git a/src/PartitionController.h b/src/PartitionController.h index 66a56a3..d1ae21c 100644 --- a/src/PartitionController.h +++ b/src/PartitionController.h @@ -25,8 +25,10 @@ namespace griddb { class PartitionController: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit PartitionController(const Napi::CallbackInfo &info); diff --git a/src/Query.cpp b/src/Query.cpp index 3fc9da6..43f69c1 100644 --- a/src/Query.cpp +++ b/src/Query.cpp @@ -18,7 +18,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference Query::constructor; +#endif Napi::Object Query::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -29,8 +31,14 @@ Napi::Object Query::init(Napi::Env env, Napi::Object exports) { InstanceMethod("getRowSet", &Query::getRowSet), }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "Query", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("Query", func); return exports; } @@ -69,8 +77,14 @@ Napi::Value Query::fetch(const Napi::CallbackInfo &info) { auto gsContainerInfoPtr = Napi::External::New(env, mContainerInfo); auto gsRowPtr = Napi::External::New(env, mRow); +#if NAPI_VERSION > 5 + Napi::Value rowsetWrapper = scope.Escape( + Util::getInstanceData(env, "RowSet")->New({ + rowsetPtr, gsContainerInfoPtr, gsRowPtr })).ToObject(); +#else Napi::Value rowsetWrapper = scope.Escape(RowSet::constructor.New({ rowsetPtr, gsContainerInfoPtr, gsRowPtr })).ToObject(); +#endif deferred.Resolve(rowsetWrapper); return deferred.Promise(); } @@ -132,8 +146,13 @@ Napi::Value Query::getRowSet(const Napi::CallbackInfo &info) { ::New(env, mContainerInfo); auto row_ptr = Napi::External::New(env, mRow); Napi::EscapableHandleScope scope(env); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "RowSet")->New({ + rowset_ptr, containerInfo_ptr, row_ptr })).ToObject(); +#else return scope.Escape(RowSet::constructor.New({ rowset_ptr, containerInfo_ptr, row_ptr })).ToObject(); +#endif } GSQuery* Query::gsPtr() { diff --git a/src/Query.h b/src/Query.h index db964a3..d5d33ba 100644 --- a/src/Query.h +++ b/src/Query.h @@ -26,8 +26,10 @@ namespace griddb { class Query: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit Query(const Napi::CallbackInfo &info); diff --git a/src/QueryAnalysisEntry.cpp b/src/QueryAnalysisEntry.cpp index b2bb3c4..485e0b0 100644 --- a/src/QueryAnalysisEntry.cpp +++ b/src/QueryAnalysisEntry.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference QueryAnalysisEntry::constructor; +#endif Napi::Object QueryAnalysisEntry::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -28,8 +30,14 @@ Napi::Object QueryAnalysisEntry::init(Napi::Env env, Napi::Object exports) { { InstanceMethod("get", &QueryAnalysisEntry::get), }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "QueryAnalysisEntry", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("QueryAnalysisEntry", func); return exports; } diff --git a/src/QueryAnalysisEntry.h b/src/QueryAnalysisEntry.h index 3b42f0a..71153ff 100644 --- a/src/QueryAnalysisEntry.h +++ b/src/QueryAnalysisEntry.h @@ -28,8 +28,10 @@ namespace griddb { class QueryAnalysisEntry: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit QueryAnalysisEntry(const Napi::CallbackInfo &info); diff --git a/src/RowKeyPredicate.cpp b/src/RowKeyPredicate.cpp index 2040fd6..b89127d 100644 --- a/src/RowKeyPredicate.cpp +++ b/src/RowKeyPredicate.cpp @@ -19,7 +19,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference RowKeyPredicate::constructor; +#endif Napi::Object RowKeyPredicate::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -35,8 +37,14 @@ Napi::Object RowKeyPredicate::init(Napi::Env env, Napi::Object exports) { InstanceAccessor("keyType", &RowKeyPredicate::getKeyType, nullptr) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "RowKeyPredicate", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("RowKeyPredicate", func); return exports; } diff --git a/src/RowKeyPredicate.h b/src/RowKeyPredicate.h index 3d86e55..9c1052c 100644 --- a/src/RowKeyPredicate.h +++ b/src/RowKeyPredicate.h @@ -26,11 +26,10 @@ namespace griddb { class RowKeyPredicate: public Napi::ObjectWrap { public: - GSRowKeyPredicate *predicate; - GSType type; - +#if NAPI_VERSION <= 5 // Constructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit RowKeyPredicate(const Napi::CallbackInfo &info); ~RowKeyPredicate(); @@ -42,6 +41,10 @@ class RowKeyPredicate: public Napi::ObjectWrap { Napi::Value getDistinctKeys(const Napi::CallbackInfo &info); Napi::Value getKeyType(const Napi::CallbackInfo &info); GSRowKeyPredicate* getPredicate(); + + private: + GSRowKeyPredicate *predicate; + GSType type; }; } // namespace griddb diff --git a/src/RowSet.cpp b/src/RowSet.cpp index 78370f0..7d5ab44 100644 --- a/src/RowSet.cpp +++ b/src/RowSet.cpp @@ -18,7 +18,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference RowSet::constructor; +#endif Napi::Object RowSet::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -31,8 +33,14 @@ Napi::Object RowSet::init(Napi::Env env, Napi::Object exports) { InstanceAccessor("size", &RowSet::getSize, &RowSet::setReadonlyAttribute) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "RowSet", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("RowSet", func); return exports; } @@ -114,16 +122,26 @@ Napi::Value RowSet::next(const Napi::CallbackInfo &info) { Napi::EscapableHandleScope scope(env); auto aggPtr = Napi::External::New(env, aggResult); +#if NAPI_VERSION > 5 + returnWrapper = scope.Escape( + Util::getInstanceData(env, "AggregationResult")->New({aggPtr})).ToObject(); +#else returnWrapper = scope.Escape( AggregationResult::constructor.New({aggPtr})).ToObject(); +#endif break; } case GS_ROW_SET_QUERY_ANALYSIS: { Napi::EscapableHandleScope scope(env); auto queryPtr = Napi::External::New(env, queryResult); +#if NAPI_VERSION > 5 + returnWrapper = scope.Escape( + Util::getInstanceData(env, "QueryAnalysisEntry")->New({queryPtr})).ToObject(); +#else returnWrapper = scope.Escape( QueryAnalysisEntry::constructor.New({queryPtr})).ToObject(); +#endif break; } diff --git a/src/RowSet.h b/src/RowSet.h index 2b145c9..b8ef319 100644 --- a/src/RowSet.h +++ b/src/RowSet.h @@ -26,8 +26,10 @@ namespace griddb { class RowSet: public Napi::ObjectWrap { public: +#if NAPI_VERSION <= 5 // Constructor static variable static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit RowSet(const Napi::CallbackInfo &info); diff --git a/src/Store.cpp b/src/Store.cpp index cc6d1e9..c2b0ded 100644 --- a/src/Store.cpp +++ b/src/Store.cpp @@ -21,7 +21,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference Store::constructor; +#endif Napi::Object Store::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -46,8 +48,14 @@ Napi::Object Store::init(Napi::Env env, Napi::Object exports) { &Store::getPartitionController, &Store::setReadonlyAttribute) }); +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "Store", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("Store", func); return exports; } @@ -108,8 +116,13 @@ Napi::Value Store::putContainer(const Napi::CallbackInfo &info) { auto containerPtr = Napi::External::New(env, pContainer); auto containerInfoPtr = Napi::External::New(env, gsInfo); Napi::Value containerWrapper; +#if NAPI_VERSION > 5 + containerWrapper = scope.Escape(Util::getInstanceData(env, "Container")-> + New({containerPtr, containerInfoPtr})).ToObject(); +#else containerWrapper = scope.Escape(Container::constructor.New( {containerPtr, containerInfoPtr})).ToObject(); +#endif // Return promise object deferred.Resolve(containerWrapper); return deferred.Promise(); @@ -167,8 +180,13 @@ Napi::Value Store::getContainer(const Napi::CallbackInfo &info) { auto containerInfoPtr = Napi::External::New(env, &containerInfo); Napi::Value containerWrapper; +#if NAPI_VERSION > 5 + containerWrapper = scope.Escape(Util::getInstanceData(env, "Container")-> + New({ containerPtr, containerInfoPtr })).ToObject(); +#else containerWrapper = scope.Escape(Container::constructor.New({ containerPtr, containerInfoPtr })).ToObject(); +#endif // Return promise object deferred.Resolve(containerWrapper); @@ -207,8 +225,14 @@ Napi::Value Store::getContainerInfo(const Napi::CallbackInfo &info) { Napi::EscapableHandleScope scope(env); auto containerInfoPtr = Napi::External::New(env, &gsContainerInfo); +#if NAPI_VERSION > 5 + Napi::Value containerInfoWrapper = scope.Escape( + Util::getInstanceData(env, "ContainerInfo")-> + New({containerInfoPtr})).ToObject(); +#else Napi::Value containerInfoWrapper = scope.Escape( ContainerInfo::constructor.New({containerInfoPtr})).ToObject(); +#endif // Return promise object deferred.Resolve(containerInfoWrapper); return deferred.Promise(); @@ -230,8 +254,13 @@ Napi::Value Store::getPartitionController( Napi::EscapableHandleScope scope(env); auto controllerPtr = Napi::External::New(info.Env(), partitionController); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "PartitionController")-> + New({controllerPtr})).ToObject(); +#else return scope.Escape(PartitionController::constructor.New( { controllerPtr })).ToObject(); +#endif } static void freeMemoryDataMultiPut(const char **listContainerName, @@ -411,8 +440,13 @@ Napi::Value Store::createRowKeyPredicate(const Napi::CallbackInfo &info) { auto predicateNode = Napi::External< GSRowKeyPredicate>::New(env, predicate); auto typeExt = Napi::External::New(env, &type); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "RowKeyPredicate")->New( + {predicateNode, typeExt})).ToObject(); +#else return scope.Escape( RowKeyPredicate::constructor.New( {predicateNode, typeExt})).ToObject(); +#endif } // Free memory for function setMultiContainerNumList diff --git a/src/Store.h b/src/Store.h index 045da67..bc65b01 100644 --- a/src/Store.h +++ b/src/Store.h @@ -30,8 +30,9 @@ namespace griddb { class Store: public Napi::ObjectWrap { public: - // Constructor static variable +#if NAPI_VERSION <= 5 static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit Store(const Napi::CallbackInfo &info); diff --git a/src/StoreFactory.cpp b/src/StoreFactory.cpp index b2d6a9e..f698cb7 100644 --- a/src/StoreFactory.cpp +++ b/src/StoreFactory.cpp @@ -18,7 +18,9 @@ namespace griddb { +#if NAPI_VERSION <= 5 Napi::FunctionReference StoreFactory::constructor; +#endif Napi::Object StoreFactory::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); @@ -27,9 +29,14 @@ Napi::Object StoreFactory::init(Napi::Env env, Napi::Object exports) { InstanceMethod("getStore", &StoreFactory::getStore), InstanceMethod("getVersion", &StoreFactory::getVersion) }); - +#if NAPI_VERSION > 5 + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + Util::setInstanceData(env, "StoreFactory", constructor); +#else constructor = Napi::Persistent(func); constructor.SuppressDestruct(); +#endif exports.Set("StoreFactory", func); return exports; } @@ -120,7 +127,11 @@ Napi::Value StoreFactory::getStore(const Napi::CallbackInfo& info) { // Create new Store object Napi::EscapableHandleScope scope(env); auto storeNode = Napi::External::New(env, store); - return scope.Escape( Store::constructor.New( {storeNode})).ToObject(); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "Store")->New( {storeNode})).ToObject(); +#else + return scope.Escape(Store::constructor.New( {storeNode})).ToObject(); +#endif } Napi::Value StoreFactory::getInstance(const Napi::CallbackInfo &info) { @@ -129,8 +140,11 @@ Napi::Value StoreFactory::getInstance(const Napi::CallbackInfo &info) { Napi::EscapableHandleScope scope(env); auto arg = Napi::External::New(env, factory); - - return scope.Escape(constructor.New( { arg })).ToObject(); +#if NAPI_VERSION > 5 + return scope.Escape(Util::getInstanceData(env, "StoreFactory")->New( { arg })).ToObject(); +#else + return scope.Escape(StoreFactory::constructor.New( { arg })).ToObject(); +#endif } StoreFactory::~StoreFactory() { diff --git a/src/StoreFactory.h b/src/StoreFactory.h index 8ee4a45..ea230d3 100644 --- a/src/StoreFactory.h +++ b/src/StoreFactory.h @@ -29,8 +29,9 @@ namespace griddb { class StoreFactory: public Napi::ObjectWrap { public: - // Constructor static variable +#if NAPI_VERSION <= 5 static Napi::FunctionReference constructor; +#endif static Napi::Object init(Napi::Env env, Napi::Object exports); explicit StoreFactory(const Napi::CallbackInfo &info); diff --git a/src/Util.cpp b/src/Util.cpp index 3259a2a..e5e1e31 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -533,3 +533,25 @@ void Util::toField(const Napi::Env &env, Napi::Value *value, GSRow *row, } } + +void Util::setInstanceData(Napi::Env env, std::string key, + Napi::FunctionReference* function) { + env.GetInstanceData()->insert + (std::pair(key, function)); +} + +Napi::FunctionReference* Util::getInstanceData(Napi::Env env, std::string key) { + return env.GetInstanceData()->find(key)->second; +} + +void Util::addonDataFinalizer(Napi::Env env, AddonData* data) { + if (data) { + for (auto item : *data) { + // Free Napi::FunctionReference* data + if (item.second) { + delete item.second; + } + } + delete data; + } +} diff --git a/src/Util.h b/src/Util.h index bf113bd..a3b2c97 100644 --- a/src/Util.h +++ b/src/Util.h @@ -17,6 +17,7 @@ #ifndef _UTIL_H_ #define _UTIL_H_ +#include #include #include #include "Field.h" @@ -27,6 +28,9 @@ #define MAX_LONG 9007199254740992 #define MIN_LONG -9007199254740992 +// Support store data for each instance library when run multi threads +typedef std::map AddonData; + class Util { public: static const GSChar* strdup(const GSChar *from); @@ -47,5 +51,9 @@ class Util { // Other support methods static void freeStrData(Napi::Env env, void* data); + static void setInstanceData(Napi::Env env, std::string key, + Napi::FunctionReference *function); + static Napi::FunctionReference *getInstanceData(Napi::Env env, std::string key); + static void addonDataFinalizer(Napi::Env env, AddonData* data); }; #endif // _UTIL_H_