From f70071a15a552d632f89172d6c7a67d3ed923bec Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 7 Aug 2020 15:06:51 +0200 Subject: [PATCH] Group values into collections --- src/addon.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/addon.cpp b/src/addon.cpp index c859ee2..65f2ad3 100644 --- a/src/addon.cpp +++ b/src/addon.cpp @@ -95,17 +95,23 @@ void uWS_us_listen_socket_close(const FunctionCallbackInfo &args) { #include #include -std::unordered_map kvStoreString; -std::unordered_map kvStoreInteger; +std::unordered_map> kvStoreString; +std::unordered_map> kvStoreInteger; std::mutex kvMutex; +// getString(key, collection) void uWS_getString(const FunctionCallbackInfo &args) { NativeString key(args.GetIsolate(), args[0]); if (key.isInvalid(args)) { return; } - std::string value = kvStoreString[std::string(key.getString())]; + NativeString collection(args.GetIsolate(), args[1]); + if (collection.isInvalid(args)) { + return; + } + + std::string value = kvStoreString[std::string(collection.getString())][std::string(key.getString())]; args.GetReturnValue().Set(String::NewFromUtf8(args.GetIsolate(), value.data(), NewStringType::kNormal, value.length()).ToLocalChecked()); } @@ -120,7 +126,12 @@ void uWS_setString(const FunctionCallbackInfo &args) { return; } - kvStoreString[std::string(key.getString())] = value.getString(); + NativeString collection(args.GetIsolate(), args[2]); + if (collection.isInvalid(args)) { + return; + } + + kvStoreString[std::string(collection.getString())][std::string(key.getString())] = value.getString(); } void uWS_getInteger(const FunctionCallbackInfo &args) { @@ -129,7 +140,12 @@ void uWS_getInteger(const FunctionCallbackInfo &args) { return; } - uint32_t value = kvStoreInteger[std::string(key.getString())]; + NativeString collection(args.GetIsolate(), args[1]); + if (collection.isInvalid(args)) { + return; + } + + uint32_t value = kvStoreInteger[std::string(collection.getString())][std::string(key.getString())]; args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value)); } @@ -140,9 +156,14 @@ void uWS_setInteger(const FunctionCallbackInfo &args) { return; } + NativeString collection(args.GetIsolate(), args[1]); + if (collection.isInvalid(args)) { + return; + } + uint32_t value = Local::Cast(args[1])->Value(); - kvStoreInteger[std::string(key.getString())] = value; + kvStoreInteger[std::string(collection.getString())][std::string(key.getString())] = value; } void uWS_incInteger(const FunctionCallbackInfo &args) { @@ -153,7 +174,12 @@ void uWS_incInteger(const FunctionCallbackInfo &args) { uint32_t change = Local::Cast(args[1])->Value(); - uint32_t value = kvStoreInteger[std::string(key.getString())] += change; + NativeString collection(args.GetIsolate(), args[2]); + if (collection.isInvalid(args)) { + return; + } + + uint32_t value = kvStoreInteger[std::string(collection.getString())][std::string(key.getString())] += change; args.GetReturnValue().Set(Integer::New(args.GetIsolate(), value)); } @@ -161,11 +187,16 @@ void uWS_incInteger(const FunctionCallbackInfo &args) { /* This one will spike memory usage for large stores */ void uWS_getStringKeys(const FunctionCallbackInfo &args) { + NativeString collection(args.GetIsolate(), args[0]); + if (collection.isInvalid(args)) { + return; + } + Local stringKeys = Array::New(args.GetIsolate(), kvStoreString.size()); int offset = 0; - for (auto p : kvStoreString) { + for (auto p : kvStoreString[std::string(collection.getString())]) { stringKeys->Set(args.GetIsolate()->GetCurrentContext(), offset++, String::NewFromUtf8(args.GetIsolate(), p.first.data(), NewStringType::kNormal, p.first.length()).ToLocalChecked()); } @@ -174,11 +205,16 @@ void uWS_getStringKeys(const FunctionCallbackInfo &args) { void uWS_getIntegerKeys(const FunctionCallbackInfo &args) { + NativeString collection(args.GetIsolate(), args[0]); + if (collection.isInvalid(args)) { + return; + } + Local integerKeys = Array::New(args.GetIsolate(), kvStoreInteger.size()); int offset = 0; - for (auto p : kvStoreInteger) { + for (auto p : kvStoreInteger[std::string(collection.getString())]) { integerKeys->Set(args.GetIsolate()->GetCurrentContext(), offset++, String::NewFromUtf8(args.GetIsolate(), p.first.data(), NewStringType::kNormal, p.first.length()).ToLocalChecked()); }