react-native-mmkv 3.2.0 → 4.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/android/CMakeLists.txt +18 -9
  2. package/android/build.gradle +6 -2
  3. package/cpp/{MMKVManagedBuffer.h → ManagedMMBuffer.h} +6 -6
  4. package/cpp/MmkvHostObject.cpp +22 -17
  5. package/cpp/MmkvHostObject.h +1 -2
  6. package/cpp/MmkvTypes.h +50 -0
  7. package/cpp/NativeMmkvModule.cpp +3 -3
  8. package/cpp/NativeMmkvModule.h +1 -8
  9. package/lib/commonjs/MMKV.js.map +1 -1
  10. package/lib/commonjs/Types.js.map +1 -1
  11. package/lib/commonjs/createMMKV.web.js +1 -1
  12. package/lib/commonjs/createMMKV.web.js.map +1 -1
  13. package/lib/module/MMKV.js.map +1 -1
  14. package/lib/module/Types.js.map +1 -1
  15. package/lib/module/createMMKV.web.js +1 -1
  16. package/lib/module/createMMKV.web.js.map +1 -1
  17. package/lib/typescript/src/MMKV.d.ts +1 -1
  18. package/lib/typescript/src/MMKV.d.ts.map +1 -1
  19. package/lib/typescript/src/Types.d.ts +7 -1
  20. package/lib/typescript/src/Types.d.ts.map +1 -1
  21. package/lib/typescript/src/hooks.d.ts +1 -1
  22. package/lib/typescript/src/hooks.d.ts.map +1 -1
  23. package/package.json +22 -27
  24. package/react-native-mmkv.podspec +4 -10
  25. package/react-native.config.js +9 -0
  26. package/src/MMKV.ts +1 -1
  27. package/src/Types.ts +7 -1
  28. package/src/createMMKV.web.ts +1 -1
  29. package/src/hooks.ts +1 -1
  30. package/MMKV/Core/CMakeLists.txt +0 -172
  31. package/MMKV/Core/CodedInputData.cpp +0 -252
  32. package/MMKV/Core/CodedInputData.h +0 -87
  33. package/MMKV/Core/CodedInputDataCrypt.cpp +0 -280
  34. package/MMKV/Core/CodedInputDataCrypt.h +0 -87
  35. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +0 -62
  36. package/MMKV/Core/CodedInputData_OSX.cpp +0 -92
  37. package/MMKV/Core/CodedOutputData.cpp +0 -186
  38. package/MMKV/Core/CodedOutputData.h +0 -88
  39. package/MMKV/Core/Core.xcodeproj/project.pbxproj +0 -707
  40. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
  41. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  42. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +0 -67
  43. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +0 -67
  44. package/MMKV/Core/InterProcessLock.cpp +0 -186
  45. package/MMKV/Core/InterProcessLock.h +0 -119
  46. package/MMKV/Core/InterProcessLock_Android.cpp +0 -103
  47. package/MMKV/Core/InterProcessLock_Win32.cpp +0 -108
  48. package/MMKV/Core/KeyValueHolder.cpp +0 -236
  49. package/MMKV/Core/KeyValueHolder.h +0 -122
  50. package/MMKV/Core/MMBuffer.cpp +0 -210
  51. package/MMKV/Core/MMBuffer.h +0 -111
  52. package/MMKV/Core/MMKV.cpp +0 -1702
  53. package/MMKV/Core/MMKV.h +0 -595
  54. package/MMKV/Core/MMKVLog.cpp +0 -127
  55. package/MMKV/Core/MMKVLog.h +0 -86
  56. package/MMKV/Core/MMKVLog_Android.cpp +0 -134
  57. package/MMKV/Core/MMKVMetaInfo.hpp +0 -99
  58. package/MMKV/Core/MMKVPredef.h +0 -293
  59. package/MMKV/Core/MMKV_Android.cpp +0 -261
  60. package/MMKV/Core/MMKV_IO.cpp +0 -1905
  61. package/MMKV/Core/MMKV_IO.h +0 -57
  62. package/MMKV/Core/MMKV_OSX.cpp +0 -423
  63. package/MMKV/Core/MMKV_OSX.h +0 -57
  64. package/MMKV/Core/MemoryFile.cpp +0 -603
  65. package/MMKV/Core/MemoryFile.h +0 -194
  66. package/MMKV/Core/MemoryFile_Android.cpp +0 -236
  67. package/MMKV/Core/MemoryFile_Linux.cpp +0 -125
  68. package/MMKV/Core/MemoryFile_OSX.cpp +0 -142
  69. package/MMKV/Core/MemoryFile_Win32.cpp +0 -554
  70. package/MMKV/Core/MiniPBCoder.cpp +0 -672
  71. package/MMKV/Core/MiniPBCoder.h +0 -151
  72. package/MMKV/Core/MiniPBCoder_OSX.cpp +0 -237
  73. package/MMKV/Core/PBEncodeItem.hpp +0 -104
  74. package/MMKV/Core/PBUtility.cpp +0 -61
  75. package/MMKV/Core/PBUtility.h +0 -148
  76. package/MMKV/Core/ScopedLock.hpp +0 -69
  77. package/MMKV/Core/ThreadLock.cpp +0 -75
  78. package/MMKV/Core/ThreadLock.h +0 -81
  79. package/MMKV/Core/ThreadLock_Win32.cpp +0 -89
  80. package/MMKV/Core/aes/AESCrypt.cpp +0 -273
  81. package/MMKV/Core/aes/AESCrypt.h +0 -112
  82. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +0 -1243
  83. package/MMKV/Core/aes/openssl/openssl_aes.h +0 -130
  84. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +0 -1044
  85. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +0 -38
  86. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +0 -308
  87. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +0 -84
  88. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +0 -97
  89. package/MMKV/Core/aes/openssl/openssl_md32_common.h +0 -254
  90. package/MMKV/Core/aes/openssl/openssl_md5.h +0 -49
  91. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +0 -166
  92. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +0 -75
  93. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +0 -30
  94. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +0 -271
  95. package/MMKV/Core/core.vcxproj +0 -288
  96. package/MMKV/Core/core.vcxproj.filters +0 -150
  97. package/MMKV/Core/crc32/Checksum.h +0 -75
  98. package/MMKV/Core/crc32/crc32_armv8.cpp +0 -134
  99. package/MMKV/Core/crc32/zlib/CMakeLists.txt +0 -60
  100. package/MMKV/Core/crc32/zlib/crc32.cpp +0 -55
  101. package/MMKV/Core/crc32/zlib/crc32.h +0 -48
  102. package/MMKV/Core/crc32/zlib/zconf.h +0 -380
  103. package/MMKV/Core/crc32/zlib/zutil.h +0 -25
  104. package/MMKV/README.md +0 -354
package/MMKV/Core/MMKV.h DELETED
@@ -1,595 +0,0 @@
1
- /*
2
- * Tencent is pleased to support the open source community by making
3
- * MMKV available.
4
- *
5
- * Copyright (C) 2018 THL A29 Limited, a Tencent company.
6
- * All rights reserved.
7
- *
8
- * Licensed under the BSD 3-Clause License (the "License"); you may not use
9
- * this file except in compliance with the License. You may obtain a copy of
10
- * the License at
11
- *
12
- * https://opensource.org/licenses/BSD-3-Clause
13
- *
14
- * Unless required by applicable law or agreed to in writing, software
15
- * distributed under the License is distributed on an "AS IS" BASIS,
16
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- * See the License for the specific language governing permissions and
18
- * limitations under the License.
19
- */
20
-
21
- #ifndef MMKV_MMKV_H
22
- #define MMKV_MMKV_H
23
- #ifdef __cplusplus
24
- #include "MMKVPredef.h"
25
-
26
- #ifdef MMKV_APPLE
27
-
28
- # include "MMBuffer.h"
29
- # ifdef MMKV_HAS_CPP20
30
- # include <span>
31
- # endif
32
-
33
- #else
34
- # include "MiniPBCoder.h"
35
- #endif
36
-
37
- #include <cstdint>
38
- #include <type_traits>
39
- #include <cstring>
40
-
41
- namespace mmkv {
42
- class CodedOutputData;
43
- class MemoryFile;
44
- class AESCrypt;
45
- struct MMKVMetaInfo;
46
- class FileLock;
47
- class InterProcessLock;
48
- class ThreadLock;
49
- } // namespace mmkv
50
-
51
- MMKV_NAMESPACE_BEGIN
52
-
53
- enum MMKVMode : uint32_t {
54
- MMKV_SINGLE_PROCESS = 1 << 0,
55
- MMKV_MULTI_PROCESS = 1 << 1,
56
- #ifdef MMKV_ANDROID
57
- CONTEXT_MODE_MULTI_PROCESS = 1 << 2, // in case someone mistakenly pass Context.MODE_MULTI_PROCESS
58
- MMKV_ASHMEM = 1 << 3,
59
- MMKV_BACKUP = 1 << 4,
60
- #endif
61
- MMKV_READ_ONLY = 1 << 5,
62
- };
63
-
64
- static inline MMKVMode operator | (MMKVMode one, MMKVMode other) {
65
- return static_cast<MMKVMode>(static_cast<uint32_t>(one) | static_cast<uint32_t>(other));
66
- }
67
-
68
- #define MMKV_OUT
69
-
70
- #ifdef MMKV_HAS_CPP20
71
- template <class T>
72
- struct mmkv_is_vector { static constexpr bool value = false; };
73
- template <class T, class A>
74
- struct mmkv_is_vector<std::vector<T, A>> { static constexpr bool value = true; };
75
- template <class T, size_t S>
76
- struct mmkv_is_vector<std::span<T, S>> { static constexpr bool value = true; };
77
- template <class T>
78
- inline constexpr bool mmkv_is_vector_v = mmkv_is_vector<T>::value;
79
-
80
- template <class T>
81
- concept MMKV_SUPPORTED_PRIMITIVE_VALUE_TYPE = std::is_integral_v<T> || std::is_floating_point_v<T>;
82
-
83
- template <class T>
84
- concept MMKV_SUPPORTED_POD_VALUE_TYPE = std::is_same_v<T, const char*> || std::is_same_v<T, std::string> ||
85
- std::is_same_v<T, mmkv::MMBuffer>;
86
-
87
- template <class T>
88
- concept MMKV_SUPPORTED_VECTOR_VALUE_TYPE = mmkv_is_vector_v<T> &&
89
- (MMKV_SUPPORTED_PRIMITIVE_VALUE_TYPE<typename T::value_type> || MMKV_SUPPORTED_POD_VALUE_TYPE<typename T::value_type>);
90
-
91
- template <class T>
92
- concept MMKV_SUPPORTED_VALUE_TYPE = MMKV_SUPPORTED_PRIMITIVE_VALUE_TYPE<T> || MMKV_SUPPORTED_POD_VALUE_TYPE<T> ||
93
- MMKV_SUPPORTED_VECTOR_VALUE_TYPE<T>;
94
- #endif // MMKV_HAS_CPP20
95
-
96
- class MMKV {
97
- #ifndef MMKV_ANDROID
98
- std::string m_mmapKey;
99
- MMKV(const std::string &mmapID, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath, size_t expectedCapacity = 0);
100
- #else // defined(MMKV_ANDROID)
101
- mmkv::FileLock *m_fileModeLock;
102
- mmkv::InterProcessLock *m_sharedProcessModeLock;
103
- mmkv::InterProcessLock *m_exclusiveProcessModeLock;
104
-
105
- MMKV(const std::string &mmapID, int size, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath, size_t expectedCapacity = 0);
106
-
107
- MMKV(const std::string &mmapID, int ashmemFD, int ashmemMetaFd, std::string *cryptKey = nullptr);
108
- #endif
109
-
110
- ~MMKV();
111
-
112
- std::string m_mmapID;
113
- const MMKVMode m_mode;
114
- MMKVPath_t m_path;
115
- MMKVPath_t m_crcPath;
116
- mmkv::MMKVMap *m_dic;
117
- mmkv::MMKVMapCrypt *m_dicCrypt;
118
-
119
- size_t m_expectedCapacity;
120
-
121
- mmkv::MemoryFile *m_file;
122
- size_t m_actualSize;
123
- mmkv::CodedOutputData *m_output;
124
-
125
- bool m_needLoadFromFile;
126
- bool m_hasFullWriteback;
127
-
128
- uint32_t m_crcDigest;
129
- mmkv::MemoryFile *m_metaFile;
130
- mmkv::MMKVMetaInfo *m_metaInfo;
131
-
132
- mmkv::AESCrypt *m_crypter;
133
-
134
- mmkv::ThreadLock *m_lock;
135
- mmkv::FileLock *m_fileLock;
136
- mmkv::InterProcessLock *m_sharedProcessLock;
137
- mmkv::InterProcessLock *m_exclusiveProcessLock;
138
-
139
- bool m_enableKeyExpire = false;
140
- uint32_t m_expiredInSeconds = ExpireNever;
141
-
142
- bool m_enableCompareBeforeSet = false;
143
-
144
- #ifdef MMKV_APPLE
145
- using MMKVKey_t = NSString *__unsafe_unretained;
146
- static bool isKeyEmpty(MMKVKey_t key) { return key.length <= 0; }
147
- # define mmkv_key_length(key) key.length
148
- # define mmkv_retain_key(key) [key retain]
149
- # define mmkv_release_key(key) [key release]
150
- #else
151
- using MMKVKey_t = std::string_view;
152
- static bool isKeyEmpty(MMKVKey_t key) { return key.empty(); }
153
- # define mmkv_key_length(key) key.length()
154
- # define mmkv_retain_key(key) ((void) 0)
155
- # define mmkv_release_key(key) ((void) 0)
156
- #endif // !MMKV_APPLE
157
-
158
- void loadFromFile();
159
-
160
- void partialLoadFromFile();
161
-
162
- void loadMetaInfoAndCheck();
163
-
164
- void checkDataValid(bool &loadFromFile, bool &needFullWriteback);
165
-
166
- void checkLoadData();
167
-
168
- bool isFileValid();
169
-
170
- bool checkFileCRCValid(size_t actualSize, uint32_t crcDigest);
171
-
172
- void recalculateCRCDigestWithIV(const void *iv);
173
- void recalculateCRCDigestOnly();
174
-
175
- void updateCRCDigest(const uint8_t *ptr, size_t length);
176
-
177
- size_t readActualSize();
178
-
179
- void oldStyleWriteActualSize(size_t actualSize);
180
-
181
- bool writeActualSize(size_t size, uint32_t crcDigest, const void *iv, bool increaseSequence);
182
-
183
- bool ensureMemorySize(size_t newSize);
184
-
185
- bool expandAndWriteBack(size_t newSize, std::pair<mmkv::MMBuffer, size_t> preparedData, bool needSync = true);
186
-
187
- bool fullWriteback(mmkv::AESCrypt *newCrypter = nullptr, bool onlyWhileExpire = false);
188
-
189
- bool doFullWriteBack(std::pair<mmkv::MMBuffer, size_t> preparedData, mmkv::AESCrypt *newCrypter, bool needSync = true);
190
-
191
- bool doFullWriteBack(mmkv::MMKVVector &&vec);
192
-
193
- mmkv::MMBuffer getRawDataForKey(MMKVKey_t key);
194
-
195
- mmkv::MMBuffer getDataForKey(MMKVKey_t key);
196
-
197
- // isDataHolder: avoid memory copying
198
- bool setDataForKey(mmkv::MMBuffer &&data, MMKVKey_t key, bool isDataHolder = false);
199
- #ifndef MMKV_APPLE
200
- bool setDataForKey(mmkv::MMBuffer &&data, MMKVKey_t key, uint32_t expireDuration);
201
- #endif
202
-
203
- bool removeDataForKey(MMKVKey_t key);
204
-
205
- using KVHolderRet_t = std::pair<bool, mmkv::KeyValueHolder>;
206
- // isDataHolder: avoid memory copying
207
- KVHolderRet_t doAppendDataWithKey(const mmkv::MMBuffer &data, const mmkv::MMBuffer &key, bool isDataHolder, uint32_t keyLength);
208
- KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, MMKVKey_t key, bool isDataHolder = false);
209
- KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, const mmkv::KeyValueHolder &kvHolder, bool isDataHolder = false);
210
-
211
- KVHolderRet_t doOverrideDataWithKey(const mmkv::MMBuffer &data, const mmkv::MMBuffer &key, bool isDataHolder, uint32_t keyLength);
212
- KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data, const mmkv::KeyValueHolder &kvHolder, bool isDataHolder = false);
213
- KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data, MMKVKey_t key, bool isDataHolder = false);
214
- bool checkSizeForOverride(size_t size);
215
- #ifdef MMKV_APPLE
216
- KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data,
217
- MMKVKey_t key,
218
- const mmkv::KeyValueHolderCrypt &kvHolder,
219
- bool isDataHolder = false);
220
- KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data,
221
- MMKVKey_t key,
222
- const mmkv::KeyValueHolderCrypt &kvHolder,
223
- bool isDataHolder = false);
224
- #endif
225
-
226
- void notifyContentChanged();
227
-
228
- #if defined(MMKV_ANDROID) && !defined(MMKV_DISABLE_CRYPT)
229
- void checkReSetCryptKey(int fd, int metaFD, std::string *cryptKey);
230
- #endif
231
- static bool backupOneToDirectory(const std::string &mmapKey, const MMKVPath_t &dstPath, const MMKVPath_t &srcPath, bool compareFullPath);
232
- static size_t backupAllToDirectory(const MMKVPath_t &dstDir, const MMKVPath_t &srcDir, bool isInSpecialDir);
233
- static bool restoreOneFromDirectory(const std::string &mmapKey, const MMKVPath_t &srcPath, const MMKVPath_t &dstPath, bool compareFullPath);
234
- static size_t restoreAllFromDirectory(const MMKVPath_t &srcDir, const MMKVPath_t &dstDir, bool isInSpecialDir);
235
-
236
- static uint32_t getCurrentTimeInSecond();
237
- uint32_t getExpireTimeForKey(MMKVKey_t key);
238
- mmkv::MMBuffer getDataWithoutMTimeForKey(MMKVKey_t key);
239
- size_t filterExpiredKeys();
240
-
241
- #ifndef MMKV_APPLE
242
- static constexpr uint32_t ConstFixed32Size = 4;
243
- void shared_lock();
244
- void shared_unlock();
245
- #endif
246
-
247
- public:
248
- // call this before getting any MMKV instance
249
- static void initializeMMKV(const MMKVPath_t &rootDir, MMKVLogLevel logLevel = MMKVLogInfo, mmkv::LogHandler handler = nullptr);
250
-
251
- // a generic purpose instance
252
- static MMKV *defaultMMKV(MMKVMode mode = MMKV_SINGLE_PROCESS, std::string *cryptKey = nullptr);
253
-
254
- #ifndef MMKV_ANDROID
255
-
256
- // mmapID: any unique ID (com.tencent.xin.pay, etc.)
257
- // if you want a per-user mmkv, you could merge user-id within mmapID
258
- // cryptKey: 16 bytes at most
259
- static MMKV *mmkvWithID(const std::string &mmapID,
260
- MMKVMode mode = MMKV_SINGLE_PROCESS,
261
- std::string *cryptKey = nullptr,
262
- MMKVPath_t *rootPath = nullptr,
263
- size_t expectedCapacity = 0);
264
-
265
- #else // defined(MMKV_ANDROID)
266
-
267
- // mmapID: any unique ID (com.tencent.xin.pay, etc.)
268
- // if you want a per-user mmkv, you could merge user-id within mmapID
269
- // cryptKey: 16 bytes at most
270
- static MMKV *mmkvWithID(const std::string &mmapID,
271
- int size = mmkv::DEFAULT_MMAP_SIZE,
272
- MMKVMode mode = MMKV_SINGLE_PROCESS,
273
- std::string *cryptKey = nullptr,
274
- MMKVPath_t *rootPath = nullptr,
275
- size_t expectedCapacity = 0);
276
-
277
- static MMKV *mmkvWithAshmemFD(const std::string &mmapID, int fd, int metaFD, std::string *cryptKey = nullptr);
278
-
279
- int ashmemFD();
280
-
281
- int ashmemMetaFD();
282
-
283
- bool checkProcessMode();
284
- #endif // MMKV_ANDROID
285
-
286
- // you can call this on application termination, it's totally fine if you don't call
287
- static void onExit();
288
-
289
- const std::string &mmapID() const;
290
- #ifndef MMKV_ANDROID
291
- bool isMultiProcess() const { return (m_mode & MMKV_MULTI_PROCESS) != 0; }
292
- #else
293
- bool isMultiProcess() const {
294
- return (m_mode & MMKV_MULTI_PROCESS) != 0
295
- || (m_mode & CONTEXT_MODE_MULTI_PROCESS) != 0
296
- || (m_mode & MMKV_ASHMEM) != 0; // ashmem is always multi-process
297
- }
298
- #endif
299
- bool isReadOnly() const { return (m_mode & MMKV_READ_ONLY) != 0; }
300
-
301
- #ifndef MMKV_DISABLE_CRYPT
302
- std::string cryptKey() const;
303
-
304
- // transform plain text into encrypted text, or vice versa with empty cryptKey
305
- // you can change existing crypt key with different cryptKey
306
- bool reKey(const std::string &cryptKey);
307
-
308
- // just reset cryptKey (will not encrypt or decrypt anything)
309
- // usually you should call this method after other process reKey() the multi-process mmkv
310
- void checkReSetCryptKey(const std::string *cryptKey);
311
- #endif
312
-
313
- bool set(bool value, MMKVKey_t key);
314
- bool set(bool value, MMKVKey_t key, uint32_t expireDuration);
315
-
316
- bool set(int32_t value, MMKVKey_t key);
317
- bool set(int32_t value, MMKVKey_t key, uint32_t expireDuration);
318
-
319
- bool set(uint32_t value, MMKVKey_t key);
320
- bool set(uint32_t value, MMKVKey_t key, uint32_t expireDuration);
321
-
322
- bool set(int64_t value, MMKVKey_t key);
323
- bool set(int64_t value, MMKVKey_t key, uint32_t expireDuration);
324
-
325
- bool set(uint64_t value, MMKVKey_t key);
326
- bool set(uint64_t value, MMKVKey_t key, uint32_t expireDuration);
327
-
328
- bool set(float value, MMKVKey_t key);
329
- bool set(float value, MMKVKey_t key, uint32_t expireDuration);
330
-
331
- bool set(double value, MMKVKey_t key);
332
- bool set(double value, MMKVKey_t key, uint32_t expireDuration);
333
-
334
- #ifdef MMKV_HAS_CPP20
335
- // avoid unexpected type conversion (pointer to bool, etc.)
336
- template <typename T>
337
- requires(!MMKV_SUPPORTED_VALUE_TYPE<T>)
338
- bool set(T value, MMKVKey_t key) = delete;
339
-
340
- // avoid unexpected type conversion (pointer to bool, etc.)
341
- template <typename T>
342
- requires(!MMKV_SUPPORTED_VALUE_TYPE<T>)
343
- bool set(T value, MMKVKey_t key, uint32_t expireDuration) = delete;
344
- #else
345
- // avoid unexpected type conversion (pointer to bool, etc.)
346
- template <typename T>
347
- bool set(T value, MMKVKey_t key, uint32_t expireDuration) = delete;
348
- #endif
349
-
350
- #ifdef MMKV_APPLE
351
- bool set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key);
352
- bool set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key, uint32_t expireDuration);
353
-
354
- NSObject *getObject(MMKVKey_t key, Class cls);
355
- #else // !defined(MMKV_APPLE)
356
- bool set(const char *value, MMKVKey_t key);
357
- bool set(const char *value, MMKVKey_t key, uint32_t expireDuration);
358
-
359
- bool set(const std::string &value, MMKVKey_t key);
360
- bool set(const std::string &value, MMKVKey_t key, uint32_t expireDuration);
361
-
362
- bool set(std::string_view value, MMKVKey_t key);
363
- bool set(std::string_view value, MMKVKey_t key, uint32_t expireDuration);
364
-
365
- bool set(const mmkv::MMBuffer &value, MMKVKey_t key);
366
- bool set(const mmkv::MMBuffer &value, MMKVKey_t key, uint32_t expireDuration);
367
-
368
- bool set(const std::vector<std::string> &vector, MMKVKey_t key);
369
- bool set(const std::vector<std::string> &vector, MMKVKey_t key, uint32_t expireDuration);
370
-
371
- #ifdef MMKV_HAS_CPP20
372
- template<MMKV_SUPPORTED_VECTOR_VALUE_TYPE T>
373
- bool set(const T& value, MMKVKey_t key) {
374
- return set<T>(value, key, m_expiredInSeconds);
375
- }
376
-
377
- template<MMKV_SUPPORTED_VECTOR_VALUE_TYPE T>
378
- bool set(const T& value, MMKVKey_t key, uint32_t expireDuration);
379
-
380
- template<MMKV_SUPPORTED_VECTOR_VALUE_TYPE T>
381
- bool getVector(MMKVKey_t key, T &result);
382
- #endif
383
-
384
- // inplaceModification is recommended for faster speed
385
- bool getString(MMKVKey_t key, std::string &result, bool inplaceModification = true);
386
-
387
- mmkv::MMBuffer getBytes(MMKVKey_t key);
388
-
389
- bool getBytes(MMKVKey_t key, mmkv::MMBuffer &result);
390
-
391
- bool getVector(MMKVKey_t key, std::vector<std::string> &result);
392
- #endif // MMKV_APPLE
393
-
394
- bool getBool(MMKVKey_t key, bool defaultValue = false, MMKV_OUT bool *hasValue = nullptr);
395
-
396
- int32_t getInt32(MMKVKey_t key, int32_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
397
-
398
- uint32_t getUInt32(MMKVKey_t key, uint32_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
399
-
400
- int64_t getInt64(MMKVKey_t key, int64_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
401
-
402
- uint64_t getUInt64(MMKVKey_t key, uint64_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
403
-
404
- float getFloat(MMKVKey_t key, float defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
405
-
406
- double getDouble(MMKVKey_t key, double defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
407
-
408
- // return the actual size consumption of the key's value
409
- // pass actualSize = true to get value's length
410
- size_t getValueSize(MMKVKey_t key, bool actualSize);
411
-
412
- // return size written into buffer
413
- // return -1 on any error
414
- int32_t writeValueToBuffer(MMKVKey_t key, void *ptr, int32_t size);
415
-
416
- bool containsKey(MMKVKey_t key);
417
-
418
- // filterExpire: return count of all non-expired keys, keep in mind it comes with cost
419
- size_t count(bool filterExpire = false);
420
-
421
- size_t totalSize();
422
-
423
- size_t actualSize();
424
-
425
- static constexpr uint32_t ExpireNever = 0;
426
-
427
- // all keys created (or last modified) longer than expiredInSeconds will be deleted on next full-write-back
428
- // expiredInSeconds = MMKV::ExpireNever (0) means no common expiration duration for all keys, aka each key will have it's own expiration duration
429
- bool enableAutoKeyExpire(uint32_t expiredInSeconds = 0);
430
-
431
- bool disableAutoKeyExpire();
432
-
433
- // compare value for key before set, to reduce the possibility of file expanding
434
- bool enableCompareBeforeSet();
435
- bool disableCompareBeforeSet();
436
-
437
- bool isExpirationEnabled() const { return m_enableKeyExpire; }
438
- bool isEncryptionEnabled() const { return m_dicCrypt; }
439
- bool isCompareBeforeSetEnabled() const { return m_enableCompareBeforeSet && !m_enableKeyExpire && !m_dicCrypt; }
440
-
441
- #ifdef MMKV_APPLE
442
- // filterExpire: return all non-expired keys, keep in mind it comes with cost
443
- NSArray *allKeys(bool filterExpire = false);
444
-
445
- bool removeValuesForKeys(NSArray *arrKeys);
446
-
447
- typedef void (^EnumerateBlock)(NSString *key, BOOL *stop);
448
- void enumerateKeys(EnumerateBlock block);
449
-
450
- # ifdef MMKV_IOS
451
- static void setIsInBackground(bool isInBackground);
452
- static bool isInBackground();
453
- # endif
454
- #else // !defined(MMKV_APPLE)
455
- // filterExpire: return all non-expired keys, keep in mind it comes with cost
456
- std::vector<std::string> allKeys(bool filterExpire = false);
457
-
458
- bool removeValuesForKeys(const std::vector<std::string> &arrKeys);
459
- #endif // MMKV_APPLE
460
-
461
- bool removeValueForKey(MMKVKey_t key);
462
-
463
- // keepSpace: remove all keys but keep the file size not changed, running faster
464
- void clearAll(bool keepSpace = false);
465
-
466
- // MMKV's size won't reduce after deleting key-values
467
- // call this method after lots of deleting if you care about disk usage
468
- // note that `clearAll` has the similar effect of `trim`
469
- void trim();
470
-
471
- // call this method if the instance is no longer needed in the near future
472
- // any subsequent call to the instance is undefined behavior
473
- void close();
474
-
475
- // call this method if you are facing memory-warning
476
- // any subsequent call to the instance will load all key-values from file again
477
- // keepSpace: remove all keys but keep the file size not changed, running faster
478
- void clearMemoryCache(bool keepSpace = false);
479
-
480
- // you don't need to call this, really, I mean it
481
- // unless you worry about running out of battery
482
- void sync(SyncFlag flag = MMKV_SYNC);
483
-
484
- // get exclusive access
485
- void lock();
486
- void unlock();
487
- bool try_lock();
488
-
489
- // get thread lock
490
- #ifndef MMKV_WIN32
491
- void lock_thread();
492
- void unlock_thread();
493
- bool try_lock_thread();
494
- #endif
495
-
496
- static const MMKVPath_t &getRootDir();
497
-
498
- // backup one MMKV instance from srcDir to dstDir
499
- // if srcDir is null, then backup from the root dir of MMKV
500
- static bool backupOneToDirectory(const std::string &mmapID, const MMKVPath_t &dstDir, const MMKVPath_t *srcDir = nullptr);
501
-
502
- // restore one MMKV instance from srcDir to dstDir
503
- // if dstDir is null, then restore to the root dir of MMKV
504
- static bool restoreOneFromDirectory(const std::string &mmapID, const MMKVPath_t &srcDir, const MMKVPath_t *dstDir = nullptr);
505
-
506
- // backup all MMKV instance from srcDir to dstDir
507
- // if srcDir is null, then backup from the root dir of MMKV
508
- // return count of MMKV successfully backuped
509
- static size_t backupAllToDirectory(const MMKVPath_t &dstDir, const MMKVPath_t *srcDir = nullptr);
510
-
511
- // restore all MMKV instance from srcDir to dstDir
512
- // if dstDir is null, then restore to the root dir of MMKV
513
- // return count of MMKV successfully restored
514
- static size_t restoreAllFromDirectory(const MMKVPath_t &srcDir, const MMKVPath_t *dstDir = nullptr);
515
-
516
- // check if content been changed by other process
517
- void checkContentChanged();
518
-
519
- // called when content is changed by other process
520
- // doesn't guarantee real-time notification
521
- static void registerContentChangeHandler(mmkv::ContentChangeHandler handler);
522
- static void unRegisterContentChangeHandler();
523
-
524
- // by default MMKV will discard all datas on failure
525
- // return `OnErrorRecover` to recover any data from file
526
- static void registerErrorHandler(mmkv::ErrorHandler handler);
527
- static void unRegisterErrorHandler();
528
-
529
- // MMKVLogInfo by default
530
- // pass MMKVLogNone to disable all logging
531
- static void setLogLevel(MMKVLogLevel level);
532
-
533
- // by default MMKV will print log to the console
534
- // implement this method to redirect MMKV's log
535
- static void registerLogHandler(mmkv::LogHandler handler);
536
- static void unRegisterLogHandler();
537
-
538
- // detect if the MMKV file is valid or not
539
- // Note: Don't use this to check the existence of the instance, the return value is undefined if the file was never created.
540
- static bool isFileValid(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);
541
-
542
- // remove the storage of the MMKV, including the data file & meta file (.crc)
543
- // Note: the existing instance (if any) will be closed & destroyed
544
- static bool removeStorage(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);
545
-
546
- // just forbid it for possibly misuse
547
- explicit MMKV(const MMKV &other) = delete;
548
- MMKV &operator=(const MMKV &other) = delete;
549
- };
550
-
551
- #if defined(MMKV_HAS_CPP20) && !defined(MMKV_APPLE)
552
- template<MMKV_SUPPORTED_VECTOR_VALUE_TYPE T>
553
- bool MMKV::set(const T& value, MMKVKey_t key, uint32_t expireDuration) {
554
- if (isKeyEmpty(key)) {
555
- return false;
556
- }
557
- mmkv::MMBuffer data;
558
- if constexpr (std::is_same_v<T, std::vector<bool>>) {
559
- data = mmkv::MiniPBCoder::encodeDataWithObject(value);
560
- } else {
561
- data = mmkv::MiniPBCoder::encodeDataWithObject(std::span(value));
562
- }
563
- if (mmkv_unlikely(m_enableKeyExpire) && data.length() > 0) {
564
- auto tmp = mmkv::MMBuffer(data.length() + ConstFixed32Size);
565
- auto ptr = (uint8_t *) tmp.getPtr();
566
- memcpy(ptr, data.getPtr(), data.length());
567
- auto time = (expireDuration != ExpireNever) ? getCurrentTimeInSecond() + expireDuration : ExpireNever;
568
- memcpy(ptr + data.length(), &time, ConstFixed32Size);
569
- data = std::move(tmp);
570
- }
571
- return setDataForKey(std::move(data), key);
572
- }
573
-
574
- template<MMKV_SUPPORTED_VECTOR_VALUE_TYPE T>
575
- bool MMKV::getVector(MMKVKey_t key, T &result) {
576
- if (isKeyEmpty(key)) {
577
- return false;
578
- }
579
- shared_lock();
580
-
581
- bool ret = false;
582
- auto data = getDataForKey(key);
583
- if (data.length() > 0) {
584
- ret = mmkv::MiniPBCoder::decodeVector(data, result);
585
- }
586
-
587
- shared_unlock();
588
- return ret;
589
- }
590
- #endif // MMKV_HAS_CPP20 && !MMKV_APPLE
591
-
592
- MMKV_NAMESPACE_END
593
-
594
- #endif
595
- #endif // MMKV_MMKV_H