From a22c3fd1beb19bf4aab79375ab03ecdb8131a3ae Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Wed, 29 Jul 2020 11:09:14 +0200 Subject: [PATCH] Add more KV functions --- src/addon.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/addon.cpp b/src/addon.cpp index a3f8575..03ffb65 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -95,21 +95,22 @@ void uWS_us_listen_socket_close(const FunctionCallbackInfo &args) { #include #include -std::unordered_map kvStore; +std::unordered_map kvStoreString; +std::unordered_map kvStoreInteger; std::mutex kvMutex; -void uWS_get(const FunctionCallbackInfo &args) { +void uWS_getString(const FunctionCallbackInfo &args) { NativeString key(args.GetIsolate(), args[0]); if (key.isInvalid(args)) { return; } - std::string value = kvStore[std::string(key.getString())]; + std::string value = kvStoreString[std::string(key.getString())]; args.GetReturnValue().Set(String::NewFromUtf8(args.GetIsolate(), value.data(), NewStringType::kNormal, value.length()).ToLocalChecked()); } -void uWS_set(const FunctionCallbackInfo &args) { +void uWS_setString(const FunctionCallbackInfo &args) { NativeString key(args.GetIsolate(), args[0]); if (key.isInvalid(args)) { return; @@ -119,7 +120,42 @@ void uWS_set(const FunctionCallbackInfo &args) { return; } - kvStore[std::string(key.getString())] = value.getString(); + kvStoreString[std::string(key.getString())] = value.getString(); +} + +void uWS_getInteger(const FunctionCallbackInfo &args) { + NativeString key(args.GetIsolate(), args[0]); + if (key.isInvalid(args)) { + return; + } + + uint32_t value = kvStoreInteger[std::string(key.getString())]; + + args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value)); +} + +void uWS_setInteger(const FunctionCallbackInfo &args) { + NativeString key(args.GetIsolate(), args[0]); + if (key.isInvalid(args)) { + return; + } + + uint32_t value = Local::Cast(args[1])->Value(); + + kvStoreInteger[std::string(key.getString())] = value; +} + +void uWS_incInteger(const FunctionCallbackInfo &args) { + NativeString key(args.GetIsolate(), args[0]); + if (key.isInvalid(args)) { + return; + } + + uint32_t change = Local::Cast(args[1])->Value(); + + uint32_t value = kvStoreInteger[std::string(key.getString())] += change; + + args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value)); } void uWS_lock(const FunctionCallbackInfo &args) { @@ -162,8 +198,11 @@ void Main(Local exports) { exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "free", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_free, externalPerContextData)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); /* Temporary KV store */ - exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "get", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_get)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); - exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "set", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_set)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); + exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); + exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "setString", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_setString)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); + exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "getInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_getInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); + exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "setInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_setInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); + exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "incInteger", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_incInteger)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "lock", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_lock)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked(); exports->Set(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "unlock", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_unlock)->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()).ToChecked();