react-native-mmkv-dz 2.5.1

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 (138) hide show
  1. package/LICENSE +219 -0
  2. package/MMKV/CHANGELOG.md +553 -0
  3. package/MMKV/Core/CMakeLists.txt +153 -0
  4. package/MMKV/Core/CodedInputData.cpp +228 -0
  5. package/MMKV/Core/CodedInputData.h +83 -0
  6. package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
  7. package/MMKV/Core/CodedInputDataCrypt.h +87 -0
  8. package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
  9. package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
  10. package/MMKV/Core/CodedOutputData.cpp +174 -0
  11. package/MMKV/Core/CodedOutputData.h +82 -0
  12. package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
  13. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  14. package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  15. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
  16. package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
  17. package/MMKV/Core/InterProcessLock.cpp +186 -0
  18. package/MMKV/Core/InterProcessLock.h +119 -0
  19. package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
  20. package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
  21. package/MMKV/Core/KeyValueHolder.cpp +236 -0
  22. package/MMKV/Core/KeyValueHolder.h +118 -0
  23. package/MMKV/Core/MMBuffer.cpp +185 -0
  24. package/MMKV/Core/MMBuffer.h +107 -0
  25. package/MMKV/Core/MMKV.cpp +1418 -0
  26. package/MMKV/Core/MMKV.h +386 -0
  27. package/MMKV/Core/MMKVLog.cpp +127 -0
  28. package/MMKV/Core/MMKVLog.h +86 -0
  29. package/MMKV/Core/MMKVLog_Android.cpp +79 -0
  30. package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
  31. package/MMKV/Core/MMKVPredef.h +245 -0
  32. package/MMKV/Core/MMKV_Android.cpp +259 -0
  33. package/MMKV/Core/MMKV_IO.cpp +1119 -0
  34. package/MMKV/Core/MMKV_IO.h +57 -0
  35. package/MMKV/Core/MMKV_OSX.cpp +347 -0
  36. package/MMKV/Core/MMKV_OSX.h +51 -0
  37. package/MMKV/Core/MemoryFile.cpp +537 -0
  38. package/MMKV/Core/MemoryFile.h +182 -0
  39. package/MMKV/Core/MemoryFile_Android.cpp +211 -0
  40. package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
  41. package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
  42. package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
  43. package/MMKV/Core/MiniPBCoder.cpp +366 -0
  44. package/MMKV/Core/MiniPBCoder.h +129 -0
  45. package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
  46. package/MMKV/Core/PBEncodeItem.hpp +86 -0
  47. package/MMKV/Core/PBUtility.cpp +61 -0
  48. package/MMKV/Core/PBUtility.h +153 -0
  49. package/MMKV/Core/ScopedLock.hpp +69 -0
  50. package/MMKV/Core/ThreadLock.cpp +68 -0
  51. package/MMKV/Core/ThreadLock.h +78 -0
  52. package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
  53. package/MMKV/Core/aes/AESCrypt.cpp +256 -0
  54. package/MMKV/Core/aes/AESCrypt.h +107 -0
  55. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
  56. package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
  57. package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
  58. package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
  59. package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
  60. package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
  61. package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
  62. package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
  63. package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
  64. package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
  65. package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
  66. package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
  67. package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
  68. package/MMKV/Core/core.vcxproj +186 -0
  69. package/MMKV/Core/core.vcxproj.filters +150 -0
  70. package/MMKV/Core/crc32/Checksum.h +67 -0
  71. package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
  72. package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
  73. package/MMKV/Core/crc32/zlib/crc32.h +48 -0
  74. package/MMKV/Core/crc32/zlib/zconf.h +380 -0
  75. package/MMKV/Core/crc32/zlib/zutil.h +25 -0
  76. package/MMKV/LICENSE.TXT +193 -0
  77. package/MMKV/README.md +288 -0
  78. package/README.md +221 -0
  79. package/android/CMakeLists.txt +71 -0
  80. package/android/build.gradle +371 -0
  81. package/android/gradle.properties +4 -0
  82. package/android/src/main/AndroidManifest.xml +4 -0
  83. package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
  84. package/android/src/main/cpp/MmkvHostObject.h +26 -0
  85. package/android/src/main/cpp/cpp-adapter.cpp +65 -0
  86. package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
  87. package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
  88. package/cpp/TypedArray.cpp +341 -0
  89. package/cpp/TypedArray.h +175 -0
  90. package/ios/JSIUtils.h +50 -0
  91. package/ios/JSIUtils.mm +194 -0
  92. package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
  93. package/ios/MmkvHostObject.h +27 -0
  94. package/ios/MmkvHostObject.mm +299 -0
  95. package/ios/MmkvModule.h +5 -0
  96. package/ios/MmkvModule.mm +73 -0
  97. package/lib/commonjs/MMKV.js +146 -0
  98. package/lib/commonjs/MMKV.js.map +1 -0
  99. package/lib/commonjs/PlatformChecker.js +16 -0
  100. package/lib/commonjs/PlatformChecker.js.map +1 -0
  101. package/lib/commonjs/createMMKV.js +66 -0
  102. package/lib/commonjs/createMMKV.js.map +1 -0
  103. package/lib/commonjs/createMMKV.mock.js +40 -0
  104. package/lib/commonjs/createMMKV.mock.js.map +1 -0
  105. package/lib/commonjs/createMMKV.web.js +77 -0
  106. package/lib/commonjs/createMMKV.web.js.map +1 -0
  107. package/lib/commonjs/createTextEncoder.js +24 -0
  108. package/lib/commonjs/createTextEncoder.js.map +1 -0
  109. package/lib/commonjs/hooks.js +200 -0
  110. package/lib/commonjs/hooks.js.map +1 -0
  111. package/lib/commonjs/index.js +32 -0
  112. package/lib/commonjs/index.js.map +1 -0
  113. package/lib/module/MMKV.js +134 -0
  114. package/lib/module/MMKV.js.map +1 -0
  115. package/lib/module/PlatformChecker.js +9 -0
  116. package/lib/module/PlatformChecker.js.map +1 -0
  117. package/lib/module/createMMKV.js +55 -0
  118. package/lib/module/createMMKV.js.map +1 -0
  119. package/lib/module/createMMKV.mock.js +31 -0
  120. package/lib/module/createMMKV.mock.js.map +1 -0
  121. package/lib/module/createMMKV.web.js +67 -0
  122. package/lib/module/createMMKV.web.js.map +1 -0
  123. package/lib/module/createTextEncoder.js +17 -0
  124. package/lib/module/createTextEncoder.js.map +1 -0
  125. package/lib/module/hooks.js +181 -0
  126. package/lib/module/hooks.js.map +1 -0
  127. package/lib/module/index.js +3 -0
  128. package/lib/module/index.js.map +1 -0
  129. package/lib/typescript/MMKV.d.ts +137 -0
  130. package/lib/typescript/PlatformChecker.d.ts +1 -0
  131. package/lib/typescript/createMMKV.d.ts +6 -0
  132. package/lib/typescript/createMMKV.mock.d.ts +2 -0
  133. package/lib/typescript/createMMKV.web.d.ts +2 -0
  134. package/lib/typescript/createTextEncoder.d.ts +1 -0
  135. package/lib/typescript/hooks.d.ts +81 -0
  136. package/lib/typescript/index.d.ts +2 -0
  137. package/package.json +168 -0
  138. package/react-native-mmkv.podspec +32 -0
@@ -0,0 +1,386 @@
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
+
25
+ #include "MMBuffer.h"
26
+ #include <cstdint>
27
+
28
+ namespace mmkv {
29
+ class CodedOutputData;
30
+ class MemoryFile;
31
+ class AESCrypt;
32
+ struct MMKVMetaInfo;
33
+ class FileLock;
34
+ class InterProcessLock;
35
+ class ThreadLock;
36
+ } // namespace mmkv
37
+
38
+ MMKV_NAMESPACE_BEGIN
39
+
40
+ enum MMKVMode : uint32_t {
41
+ MMKV_SINGLE_PROCESS = 1 << 0,
42
+ MMKV_MULTI_PROCESS = 1 << 1,
43
+ #ifdef MMKV_ANDROID
44
+ CONTEXT_MODE_MULTI_PROCESS = 1 << 2, // in case someone mistakenly pass Context.MODE_MULTI_PROCESS
45
+ MMKV_ASHMEM = 1 << 3,
46
+ MMKV_BACKUP = 1 << 4,
47
+ #endif
48
+ };
49
+
50
+ #define OUT
51
+
52
+ class MMKV {
53
+ #ifndef MMKV_ANDROID
54
+ std::string m_mmapKey;
55
+ MMKV(const std::string &mmapID, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath);
56
+ #else // defined(MMKV_ANDROID)
57
+ mmkv::FileLock *m_fileModeLock;
58
+ mmkv::InterProcessLock *m_sharedProcessModeLock;
59
+ mmkv::InterProcessLock *m_exclusiveProcessModeLock;
60
+
61
+ MMKV(const std::string &mmapID, int size, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath);
62
+
63
+ MMKV(const std::string &mmapID, int ashmemFD, int ashmemMetaFd, std::string *cryptKey = nullptr);
64
+ #endif
65
+
66
+ ~MMKV();
67
+
68
+ std::string m_mmapID;
69
+ MMKVPath_t m_path;
70
+ MMKVPath_t m_crcPath;
71
+ mmkv::MMKVMap *m_dic;
72
+ mmkv::MMKVMapCrypt *m_dicCrypt;
73
+
74
+ mmkv::MemoryFile *m_file;
75
+ size_t m_actualSize;
76
+ mmkv::CodedOutputData *m_output;
77
+
78
+ bool m_needLoadFromFile;
79
+ bool m_hasFullWriteback;
80
+
81
+ uint32_t m_crcDigest;
82
+ mmkv::MemoryFile *m_metaFile;
83
+ mmkv::MMKVMetaInfo *m_metaInfo;
84
+
85
+ mmkv::AESCrypt *m_crypter;
86
+
87
+ mmkv::ThreadLock *m_lock;
88
+ mmkv::FileLock *m_fileLock;
89
+ mmkv::InterProcessLock *m_sharedProcessLock;
90
+ mmkv::InterProcessLock *m_exclusiveProcessLock;
91
+
92
+ #ifdef MMKV_APPLE
93
+ using MMKVKey_t = NSString *__unsafe_unretained;
94
+ static bool isKeyEmpty(MMKVKey_t key) { return key.length <= 0; }
95
+ #else
96
+ using MMKVKey_t = const std::string &;
97
+ static bool isKeyEmpty(MMKVKey_t key) { return key.empty(); }
98
+ #endif
99
+
100
+ void loadFromFile();
101
+
102
+ void partialLoadFromFile();
103
+
104
+ void checkDataValid(bool &loadFromFile, bool &needFullWriteback);
105
+
106
+ void checkLoadData();
107
+
108
+ bool isFileValid();
109
+
110
+ bool checkFileCRCValid(size_t actualSize, uint32_t crcDigest);
111
+
112
+ void recaculateCRCDigestWithIV(const void *iv);
113
+
114
+ void updateCRCDigest(const uint8_t *ptr, size_t length);
115
+
116
+ size_t readActualSize();
117
+
118
+ void oldStyleWriteActualSize(size_t actualSize);
119
+
120
+ bool writeActualSize(size_t size, uint32_t crcDigest, const void *iv, bool increaseSequence);
121
+
122
+ bool ensureMemorySize(size_t newSize);
123
+
124
+ bool fullWriteback(mmkv::AESCrypt *newCrypter = nullptr);
125
+
126
+ bool doFullWriteBack(std::pair<mmkv::MMBuffer, size_t> preparedData, mmkv::AESCrypt *newCrypter);
127
+
128
+ mmkv::MMBuffer getDataForKey(MMKVKey_t key);
129
+
130
+ // isDataHolder: avoid memory copying
131
+ bool setDataForKey(mmkv::MMBuffer &&data, MMKVKey_t key, bool isDataHolder = false);
132
+
133
+ bool removeDataForKey(MMKVKey_t key);
134
+
135
+ using KVHolderRet_t = std::pair<bool, mmkv::KeyValueHolder>;
136
+ // isDataHolder: avoid memory copying
137
+ KVHolderRet_t doAppendDataWithKey(const mmkv::MMBuffer &data, const mmkv::MMBuffer &key, bool isDataHolder, uint32_t keyLength);
138
+ KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, MMKVKey_t key, bool isDataHolder = false);
139
+ KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, const mmkv::KeyValueHolder &kvHolder, bool isDataHolder = false);
140
+ #ifdef MMKV_APPLE
141
+ KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data,
142
+ MMKVKey_t key,
143
+ const mmkv::KeyValueHolderCrypt &kvHolder,
144
+ bool isDataHolder = false);
145
+ #endif
146
+
147
+ void notifyContentChanged();
148
+
149
+ #if defined(MMKV_ANDROID) && !defined(MMKV_DISABLE_CRYPT)
150
+ void checkReSetCryptKey(int fd, int metaFD, std::string *cryptKey);
151
+ #endif
152
+ static bool backupOneToDirectory(const std::string &mmapKey, const MMKVPath_t &dstPath, const MMKVPath_t &srcPath, bool compareFullPath);
153
+ static size_t backupAllToDirectory(const MMKVPath_t &dstDir, const MMKVPath_t &srcDir, bool isInSpecialDir);
154
+ static bool restoreOneFromDirectory(const std::string &mmapKey, const MMKVPath_t &srcPath, const MMKVPath_t &dstPath, bool compareFullPath);
155
+ static size_t restoreAllFromDirectory(const MMKVPath_t &srcDir, const MMKVPath_t &dstDir, bool isInSpecialDir);
156
+
157
+ public:
158
+ // call this before getting any MMKV instance
159
+ static void initializeMMKV(const MMKVPath_t &rootDir, MMKVLogLevel logLevel = MMKVLogInfo);
160
+
161
+ #ifdef MMKV_APPLE
162
+ // protect from some old code that don't call initializeMMKV()
163
+ static void minimalInit(MMKVPath_t defaultRootDir);
164
+ #endif
165
+
166
+ // a generic purpose instance
167
+ static MMKV *defaultMMKV(MMKVMode mode = MMKV_SINGLE_PROCESS, std::string *cryptKey = nullptr);
168
+
169
+ #ifndef MMKV_ANDROID
170
+
171
+ // mmapID: any unique ID (com.tencent.xin.pay, etc)
172
+ // if you want a per-user mmkv, you could merge user-id within mmapID
173
+ // cryptKey: 16 bytes at most
174
+ static MMKV *mmkvWithID(const std::string &mmapID,
175
+ MMKVMode mode = MMKV_SINGLE_PROCESS,
176
+ std::string *cryptKey = nullptr,
177
+ MMKVPath_t *rootPath = nullptr);
178
+
179
+ #else // defined(MMKV_ANDROID)
180
+
181
+ // mmapID: any unique ID (com.tencent.xin.pay, etc)
182
+ // if you want a per-user mmkv, you could merge user-id within mmapID
183
+ // cryptKey: 16 bytes at most
184
+ static MMKV *mmkvWithID(const std::string &mmapID,
185
+ int size = mmkv::DEFAULT_MMAP_SIZE,
186
+ MMKVMode mode = MMKV_SINGLE_PROCESS,
187
+ std::string *cryptKey = nullptr,
188
+ MMKVPath_t *rootPath = nullptr);
189
+
190
+ static MMKV *mmkvWithAshmemFD(const std::string &mmapID, int fd, int metaFD, std::string *cryptKey = nullptr);
191
+
192
+ int ashmemFD();
193
+
194
+ int ashmemMetaFD();
195
+
196
+ bool checkProcessMode();
197
+ #endif // MMKV_ANDROID
198
+
199
+ // you can call this on application termination, it's totally fine if you don't call
200
+ static void onExit();
201
+
202
+ const std::string &mmapID() const;
203
+
204
+ const bool m_isInterProcess;
205
+
206
+ #ifndef MMKV_DISABLE_CRYPT
207
+ std::string cryptKey() const;
208
+
209
+ // transform plain text into encrypted text, or vice versa with empty cryptKey
210
+ // you can change existing crypt key with different cryptKey
211
+ bool reKey(const std::string &cryptKey);
212
+
213
+ // just reset cryptKey (will not encrypt or decrypt anything)
214
+ // usually you should call this method after other process reKey() the multi-process mmkv
215
+ void checkReSetCryptKey(const std::string *cryptKey);
216
+ #endif
217
+
218
+ bool set(bool value, MMKVKey_t key);
219
+
220
+ bool set(int32_t value, MMKVKey_t key);
221
+
222
+ bool set(uint32_t value, MMKVKey_t key);
223
+
224
+ bool set(int64_t value, MMKVKey_t key);
225
+
226
+ bool set(uint64_t value, MMKVKey_t key);
227
+
228
+ bool set(float value, MMKVKey_t key);
229
+
230
+ bool set(double value, MMKVKey_t key);
231
+
232
+ // avoid unexpected type conversion (pointer to bool, etc)
233
+ template <typename T>
234
+ bool set(T value, MMKVKey_t key) = delete;
235
+
236
+ #ifdef MMKV_APPLE
237
+ bool set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key);
238
+
239
+ NSObject *getObject(MMKVKey_t key, Class cls);
240
+ #else // !defined(MMKV_APPLE)
241
+ bool set(const char *value, MMKVKey_t key);
242
+
243
+ bool set(const std::string &value, MMKVKey_t key);
244
+
245
+ bool set(const mmkv::MMBuffer &value, MMKVKey_t key);
246
+
247
+ bool set(const std::vector<std::string> &vector, MMKVKey_t key);
248
+
249
+ bool getString(MMKVKey_t key, std::string &result);
250
+
251
+ mmkv::MMBuffer getBytes(MMKVKey_t key);
252
+
253
+ bool getBytes(MMKVKey_t key, mmkv::MMBuffer &result);
254
+
255
+ bool getVector(MMKVKey_t key, std::vector<std::string> &result);
256
+ #endif // MMKV_APPLE
257
+
258
+ bool getBool(MMKVKey_t key, bool defaultValue = false, OUT bool *hasValue = nullptr);
259
+
260
+ int32_t getInt32(MMKVKey_t key, int32_t defaultValue = 0, OUT bool *hasValue = nullptr);
261
+
262
+ uint32_t getUInt32(MMKVKey_t key, uint32_t defaultValue = 0, OUT bool *hasValue = nullptr);
263
+
264
+ int64_t getInt64(MMKVKey_t key, int64_t defaultValue = 0, OUT bool *hasValue = nullptr);
265
+
266
+ uint64_t getUInt64(MMKVKey_t key, uint64_t defaultValue = 0, OUT bool *hasValue = nullptr);
267
+
268
+ float getFloat(MMKVKey_t key, float defaultValue = 0, OUT bool *hasValue = nullptr);
269
+
270
+ double getDouble(MMKVKey_t key, double defaultValue = 0, OUT bool *hasValue = nullptr);
271
+
272
+ // return the actual size consumption of the key's value
273
+ // pass actualSize = true to get value's length
274
+ size_t getValueSize(MMKVKey_t key, bool actualSize);
275
+
276
+ // return size written into buffer
277
+ // return -1 on any error
278
+ int32_t writeValueToBuffer(MMKVKey_t key, void *ptr, int32_t size);
279
+
280
+ bool containsKey(MMKVKey_t key);
281
+
282
+ size_t count();
283
+
284
+ size_t totalSize();
285
+
286
+ size_t actualSize();
287
+
288
+ #ifdef MMKV_APPLE
289
+ NSArray *allKeys();
290
+
291
+ void removeValuesForKeys(NSArray *arrKeys);
292
+
293
+ typedef void (^EnumerateBlock)(NSString *key, BOOL *stop);
294
+ void enumerateKeys(EnumerateBlock block);
295
+
296
+ # ifdef MMKV_IOS
297
+ static void setIsInBackground(bool isInBackground);
298
+ static bool isInBackground();
299
+ # endif
300
+ #else // !defined(MMKV_APPLE)
301
+ std::vector<std::string> allKeys();
302
+
303
+ void removeValuesForKeys(const std::vector<std::string> &arrKeys);
304
+ #endif // MMKV_APPLE
305
+
306
+ void removeValueForKey(MMKVKey_t key);
307
+
308
+ void clearAll();
309
+
310
+ // MMKV's size won't reduce after deleting key-values
311
+ // call this method after lots of deleting if you care about disk usage
312
+ // note that `clearAll` has the similar effect of `trim`
313
+ void trim();
314
+
315
+ // call this method if the instance is no longer needed in the near future
316
+ // any subsequent call to the instance is undefined behavior
317
+ void close();
318
+
319
+ // call this method if you are facing memory-warning
320
+ // any subsequent call to the instance will load all key-values from file again
321
+ void clearMemoryCache();
322
+
323
+ // you don't need to call this, really, I mean it
324
+ // unless you worry about running out of battery
325
+ void sync(SyncFlag flag = MMKV_SYNC);
326
+
327
+ // get exclusive access
328
+ void lock();
329
+ void unlock();
330
+ bool try_lock();
331
+
332
+ static const MMKVPath_t &getRootDir();
333
+
334
+ // backup one MMKV instance from srcDir to dstDir
335
+ // if srcDir is null, then backup from the root dir of MMKV
336
+ static bool backupOneToDirectory(const std::string &mmapID, const MMKVPath_t &dstDir, const MMKVPath_t *srcDir = nullptr);
337
+
338
+ // restore one MMKV instance from srcDir to dstDir
339
+ // if dstDir is null, then restore to the root dir of MMKV
340
+ static bool restoreOneFromDirectory(const std::string &mmapID, const MMKVPath_t &srcDir, const MMKVPath_t *dstDir = nullptr);
341
+
342
+ // backup all MMKV instance from srcDir to dstDir
343
+ // if srcDir is null, then backup from the root dir of MMKV
344
+ // return count of MMKV successfully backuped
345
+ static size_t backupAllToDirectory(const MMKVPath_t &dstDir, const MMKVPath_t *srcDir = nullptr);
346
+
347
+ // restore all MMKV instance from srcDir to dstDir
348
+ // if dstDir is null, then restore to the root dir of MMKV
349
+ // return count of MMKV successfully restored
350
+ static size_t restoreAllFromDirectory(const MMKVPath_t &srcDir, const MMKVPath_t *dstDir = nullptr);
351
+
352
+ // check if content been changed by other process
353
+ void checkContentChanged();
354
+
355
+ // called when content is changed by other process
356
+ // doesn't guarantee real-time notification
357
+ static void registerContentChangeHandler(mmkv::ContentChangeHandler handler);
358
+ static void unRegisterContentChangeHandler();
359
+
360
+ // by default MMKV will discard all datas on failure
361
+ // return `OnErrorRecover` to recover any data from file
362
+ static void registerErrorHandler(mmkv::ErrorHandler handler);
363
+ static void unRegisterErrorHandler();
364
+
365
+ // MMKVLogInfo by default
366
+ // pass MMKVLogNone to disable all logging
367
+ static void setLogLevel(MMKVLogLevel level);
368
+
369
+ // by default MMKV will print log to the console
370
+ // implement this method to redirect MMKV's log
371
+ static void registerLogHandler(mmkv::LogHandler handler);
372
+ static void unRegisterLogHandler();
373
+
374
+ // detect if the MMKV file is valid or not
375
+ // Note: Don't use this to check the existence of the instance, the return value is undefined if the file was never created.
376
+ static bool isFileValid(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);
377
+
378
+ // just forbid it for possibly misuse
379
+ explicit MMKV(const MMKV &other) = delete;
380
+ MMKV &operator=(const MMKV &other) = delete;
381
+ };
382
+
383
+ MMKV_NAMESPACE_END
384
+
385
+ #endif
386
+ #endif // MMKV_MMKV_H
@@ -0,0 +1,127 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making
3
+ * MMKV available.
4
+ *
5
+ * Copyright (C) 2019 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
+ #include "MMKVLog.h"
22
+
23
+ MMKV_NAMESPACE_BEGIN
24
+
25
+ #ifdef MMKV_DEBUG
26
+ MMKVLogLevel g_currentLogLevel = MMKVLogDebug;
27
+ #else
28
+ MMKVLogLevel g_currentLogLevel = MMKVLogInfo;
29
+ #endif
30
+
31
+ mmkv::LogHandler g_logHandler;
32
+
33
+ #ifndef __FILE_NAME__
34
+ const char *_getFileName(const char *path) {
35
+ const char *ptr = strrchr(path, '/');
36
+ if (!ptr) {
37
+ ptr = strrchr(path, '\\');
38
+ }
39
+ if (ptr) {
40
+ return ptr + 1;
41
+ } else {
42
+ return path;
43
+ }
44
+ }
45
+ #endif
46
+
47
+ MMKV_NAMESPACE_END
48
+
49
+
50
+ #ifdef ENABLE_MMKV_LOG
51
+ # include <cstdarg>
52
+ # include <string>
53
+
54
+ using namespace mmkv;
55
+
56
+ # ifndef MMKV_ANDROID
57
+
58
+ static const char *MMKVLogLevelDesc(MMKVLogLevel level) {
59
+ switch (level) {
60
+ case MMKVLogDebug:
61
+ return "D";
62
+ case MMKVLogInfo:
63
+ return "I";
64
+ case MMKVLogWarning:
65
+ return "W";
66
+ case MMKVLogError:
67
+ return "E";
68
+ default:
69
+ return "N";
70
+ }
71
+ }
72
+
73
+ # ifdef MMKV_APPLE
74
+
75
+ void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
76
+ if (level >= g_currentLogLevel) {
77
+ NSString *nsFormat = [NSString stringWithUTF8String:format];
78
+ va_list argList;
79
+ va_start(argList, format);
80
+ NSString *message = [[NSString alloc] initWithFormat:nsFormat arguments:argList];
81
+ va_end(argList);
82
+
83
+ if (g_logHandler) {
84
+ g_logHandler(level, filename, line, func, message);
85
+ } else {
86
+ NSLog(@"[%s] <%s:%d::%s> %@", MMKVLogLevelDesc(level), filename, line, func, message);
87
+ }
88
+ }
89
+ }
90
+
91
+ # else
92
+
93
+ void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
94
+ if (level >= g_currentLogLevel) {
95
+ std::string message;
96
+ char buffer[16];
97
+
98
+ va_list args;
99
+ va_start(args, format);
100
+ auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
101
+ va_end(args);
102
+
103
+ if (length < 0) { // something wrong
104
+ message = {};
105
+ } else if (length < sizeof(buffer)) {
106
+ message = std::string(buffer, static_cast<unsigned long>(length));
107
+ } else {
108
+ message.resize(static_cast<unsigned long>(length), '\0');
109
+ va_start(args, format);
110
+ std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
111
+ va_end(args);
112
+ }
113
+
114
+ if (g_logHandler) {
115
+ g_logHandler(level, filename, line, func, message);
116
+ } else {
117
+ printf("[%s] <%s:%d::%s> %s\n", MMKVLogLevelDesc(level), filename, line, func, message.c_str());
118
+ //fflush(stdout);
119
+ }
120
+ }
121
+ }
122
+
123
+ # endif // MMKV_APPLE
124
+
125
+ # endif // MMKV_ANDROID
126
+
127
+ #endif // ENABLE_MMKV_LOG
@@ -0,0 +1,86 @@
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_MMKVLOG_H
22
+ #define MMKV_MMKVLOG_H
23
+ #ifdef __cplusplus
24
+
25
+ #include "MMKVPredef.h"
26
+
27
+ #include <cerrno>
28
+ #include <cstdint>
29
+ #include <cstring>
30
+
31
+ void _MMKVLogWithLevel(
32
+ MMKV_NAMESPACE_PREFIX::MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...);
33
+
34
+ MMKV_NAMESPACE_BEGIN
35
+
36
+ extern MMKVLogLevel g_currentLogLevel;
37
+ extern mmkv::LogHandler g_logHandler;
38
+
39
+ // enable logging
40
+ #define ENABLE_MMKV_LOG
41
+
42
+ #ifdef ENABLE_MMKV_LOG
43
+
44
+ # ifdef __FILE_NAME__
45
+ # define __MMKV_FILE_NAME__ __FILE_NAME__
46
+ # else
47
+ const char *_getFileName(const char *path);
48
+ # define __MMKV_FILE_NAME__ MMKV_NAMESPACE_PREFIX::_getFileName(__FILE__)
49
+ # endif
50
+
51
+ # define MMKVError(format, ...) \
52
+ _MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogError, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
53
+ ##__VA_ARGS__)
54
+ # define MMKVWarning(format, ...) \
55
+ _MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogWarning, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
56
+ ##__VA_ARGS__)
57
+ # define MMKVInfo(format, ...) \
58
+ _MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogInfo, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
59
+ ##__VA_ARGS__)
60
+
61
+ # ifdef MMKV_DEBUG
62
+ # define MMKVDebug(format, ...) \
63
+ _MMKVLogWithLevel(MMKV_NAMESPACE_PREFIX::MMKVLogDebug, __MMKV_FILE_NAME__, __func__, __LINE__, format, \
64
+ ##__VA_ARGS__)
65
+ # else
66
+ # define MMKVDebug(format, ...) \
67
+ {}
68
+ # endif
69
+
70
+ #else
71
+
72
+ # define MMKVError(format, ...) \
73
+ {}
74
+ # define MMKVWarning(format, ...) \
75
+ {}
76
+ # define MMKVInfo(format, ...) \
77
+ {}
78
+ # define MMKVDebug(format, ...) \
79
+ {}
80
+
81
+ #endif
82
+
83
+ MMKV_NAMESPACE_END
84
+
85
+ #endif
86
+ #endif //MMKV_MMKVLOG_H
@@ -0,0 +1,79 @@
1
+ /*
2
+ * Tencent is pleased to support the open source community by making
3
+ * MMKV available.
4
+ *
5
+ * Copyright (C) 2019 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
+ #include "MMKVLog.h"
22
+
23
+ #ifdef ENABLE_MMKV_LOG
24
+ # ifdef MMKV_ANDROID
25
+ # include <android/log.h>
26
+ # include <cstdarg>
27
+ # include <string>
28
+
29
+ using namespace std;
30
+
31
+ constexpr auto APP_NAME = "MMKV";
32
+
33
+ static android_LogPriority MMKVLogLevelDesc(MMKVLogLevel level) {
34
+ switch (level) {
35
+ case MMKVLogDebug:
36
+ return ANDROID_LOG_DEBUG;
37
+ case MMKVLogInfo:
38
+ return ANDROID_LOG_INFO;
39
+ case MMKVLogWarning:
40
+ return ANDROID_LOG_WARN;
41
+ case MMKVLogError:
42
+ return ANDROID_LOG_ERROR;
43
+ default:
44
+ return ANDROID_LOG_UNKNOWN;
45
+ }
46
+ }
47
+
48
+ void _MMKVLogWithLevel(MMKVLogLevel level, const char *filename, const char *func, int line, const char *format, ...) {
49
+ if (level >= g_currentLogLevel) {
50
+ string message;
51
+ char buffer[16];
52
+
53
+ va_list args;
54
+ va_start(args, format);
55
+ auto length = std::vsnprintf(buffer, sizeof(buffer), format, args);
56
+ va_end(args);
57
+
58
+ if (length < 0) { // something wrong
59
+ message = {};
60
+ } else if (length < sizeof(buffer)) {
61
+ message = string(buffer, static_cast<unsigned long>(length));
62
+ } else {
63
+ message.resize(static_cast<unsigned long>(length), '\0');
64
+ va_start(args, format);
65
+ std::vsnprintf(const_cast<char *>(message.data()), static_cast<size_t>(length) + 1, format, args);
66
+ va_end(args);
67
+ }
68
+
69
+ if (g_logHandler) {
70
+ g_logHandler(level, filename, line, func, message);
71
+ } else {
72
+ auto desc = MMKVLogLevelDesc(level);
73
+ __android_log_print(desc, APP_NAME, "<%s:%d::%s> %s", filename, line, func, message.c_str());
74
+ }
75
+ }
76
+ }
77
+ # endif // MMKV_ANDROID
78
+
79
+ #endif // ENABLE_MMKV_LOG