-
-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathHybridMMKV.cpp
More file actions
221 lines (201 loc) · 7.31 KB
/
HybridMMKV.cpp
File metadata and controls
221 lines (201 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// HybridMMKV.cpp
// react-native-mmkv
//
// Created by Marc Rousavy on 21.08.2025.
//
#include "HybridMMKV.hpp"
#include "MMKVTypes.hpp"
#include "MMKVValueChangedListenerRegistry.hpp"
#include "ManagedMMBuffer.hpp"
#include <NitroModules/NitroLogger.hpp>
namespace margelo::nitro::mmkv {
HybridMMKV::HybridMMKV(const Configuration& config) : HybridObject(TAG) {
std::string path = config.path.has_value() ? config.path.value() : "";
std::string encryptionKey = config.encryptionKey.has_value() ? config.encryptionKey.value() : "";
bool hasEncryptionKey = encryptionKey.size() > 0;
Logger::log(LogLevel::Info, TAG, "Creating MMKV instance \"%s\"... (Path: %s, Encrypted: %s)", config.id.c_str(), path.c_str(),
hasEncryptionKey ? "true" : "false");
std::string* pathPtr = path.size() > 0 ? &path : nullptr;
std::string* encryptionKeyPtr = encryptionKey.size() > 0 ? &encryptionKey : nullptr;
MMKVMode mode = getMMKVMode(config);
if (config.readOnly.has_value() && config.readOnly.value()) {
Logger::log(LogLevel::Info, TAG, "Instance is read-only!");
mode = mode | ::mmkv::MMKV_READ_ONLY;
}
#ifdef __APPLE__
instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr);
#else
instance = MMKV::mmkvWithID(config.id, DEFAULT_MMAP_SIZE, mode, encryptionKeyPtr, pathPtr);
#endif
if (instance == nullptr) [[unlikely]] {
// Check if instanceId is invalid
if (config.id.empty()) [[unlikely]] {
throw std::runtime_error("Failed to create MMKV instance! `id` cannot be empty!");
}
// Check if encryptionKey is invalid
if (encryptionKey.size() > 16) [[unlikely]] {
throw std::runtime_error("Failed to create MMKV instance! `encryptionKey` cannot be longer "
"than 16 bytes!");
}
// Check if path is maybe invalid
if (path.empty()) [[unlikely]] {
throw std::runtime_error("Failed to create MMKV instance! `path` cannot be empty!");
}
throw std::runtime_error("Failed to create MMKV instance!");
}
}
std::string HybridMMKV::getId() {
return instance->mmapID();
}
double HybridMMKV::getSize() {
return instance->actualSize();
}
bool HybridMMKV::getIsReadOnly() {
return instance->isReadOnly();
}
// helper: overload pattern matching for lambdas
template <class... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
void HybridMMKV::set(const std::string& key, const std::variant<bool, std::shared_ptr<ArrayBuffer>, std::string, double>& value) {
if (key.empty()) [[unlikely]] {
throw std::runtime_error("Cannot set a value for an empty key!");
}
// Pattern-match each potential value in std::variant
bool didSet = std::visit(overloaded{[&](bool b) {
// boolean
return instance->set(b, key);
},
[&](const std::shared_ptr<ArrayBuffer>& buf) {
// ArrayBuffer
MMBuffer buffer(buf->data(), buf->size(), MMBufferCopyFlag::MMBufferNoCopy);
return instance->set(std::move(buffer), key);
},
[&](const std::string& string) {
// string
return instance->set(string, key);
},
[&](double number) {
// number
return instance->set(number, key);
}},
value);
if (!didSet) {
throw std::runtime_error("Failed to set value for key \"" + key + "\"!");
}
// Notify on changed
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
}
std::optional<bool> HybridMMKV::getBoolean(const std::string& key) {
bool hasValue;
bool result = instance->getBool(key, /* defaultValue */ false, &hasValue);
if (hasValue) {
return result;
} else {
return std::nullopt;
}
}
std::optional<std::string> HybridMMKV::getString(const std::string& key) {
std::string result;
bool hasValue = instance->getString(key, result, /* inplaceModification */ true);
if (hasValue) {
return result;
} else {
return std::nullopt;
}
}
std::optional<double> HybridMMKV::getNumber(const std::string& key) {
bool hasValue;
double result = instance->getDouble(key, /* defaultValue */ 0.0, &hasValue);
if (hasValue) {
return result;
} else {
return std::nullopt;
}
}
std::optional<std::shared_ptr<ArrayBuffer>> HybridMMKV::getBuffer(const std::string& key) {
MMBuffer result;
#ifdef __APPLE__
// iOS: Convert std::string to NSString* for MMKVCore pod compatibility
bool hasValue = instance->getBytes(@(key.c_str()), result);
#else
// Android/other platforms: Use std::string directly (converts to
// std::string_view)
bool hasValue = instance->getBytes(key, result);
#endif
if (hasValue) {
return std::make_shared<ManagedMMBuffer>(std::move(result));
} else {
return std::nullopt;
}
}
bool HybridMMKV::contains(const std::string& key) {
return instance->containsKey(key);
}
bool HybridMMKV::remove(const std::string& key) {
bool wasRemoved = instance->removeValueForKey(key);
if (wasRemoved) {
// Notify on changed
MMKVValueChangedListenerRegistry::notifyOnValueChanged(instance->mmapID(), key);
}
return wasRemoved;
}
std::vector<std::string> HybridMMKV::getAllKeys() {
return instance->allKeys();
}
void HybridMMKV::clearAll() {
auto mmkvID = instance->mmapID();
if (!MMKVValueChangedListenerRegistry::hasListeners(mmkvID)) {
instance->clearAll();
return;
}
auto keysBefore = getAllKeys();
instance->clearAll();
for (const auto& key : keysBefore) {
MMKVValueChangedListenerRegistry::notifyOnValueChanged(mmkvID, key);
}
}
void HybridMMKV::recrypt(const std::optional<std::string>& key) {
bool successful = false;
if (key.has_value()) {
// Encrypt with the given key
successful = instance->reKey(key.value());
} else {
// Remove the encryption key by setting it to a blank string
successful = instance->reKey(std::string());
}
if (!successful) {
throw std::runtime_error("Failed to recrypt MMKV instance!");
}
}
void HybridMMKV::trim() {
instance->trim();
instance->clearMemoryCache();
}
Listener HybridMMKV::addOnValueChangedListener(const std::function<void(const std::string& /* key */)>& onValueChanged) {
// Add listener
auto mmkvID = instance->mmapID();
auto listenerID = MMKVValueChangedListenerRegistry::addListener(instance->mmapID(), onValueChanged);
return Listener([=]() {
// remove()
MMKVValueChangedListenerRegistry::removeListener(mmkvID, listenerID);
});
}
MMKVMode HybridMMKV::getMMKVMode(const Configuration& config) {
if (!config.mode.has_value()) {
return ::mmkv::MMKV_SINGLE_PROCESS;
}
switch (config.mode.value()) {
case Mode::SINGLE_PROCESS:
return ::mmkv::MMKV_SINGLE_PROCESS;
case Mode::MULTI_PROCESS:
return ::mmkv::MMKV_MULTI_PROCESS;
default:
[[unlikely]] throw std::runtime_error("Invalid MMKV Mode value!");
}
}
} // namespace margelo::nitro::mmkv