react-native-mmkv 2.11.0 → 2.12.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 (45) hide show
  1. package/MMKV/Core/CodedInputData.cpp +28 -4
  2. package/MMKV/Core/CodedInputData.h +5 -1
  3. package/MMKV/Core/CodedOutputData.cpp +12 -0
  4. package/MMKV/Core/CodedOutputData.h +6 -0
  5. package/MMKV/Core/MMBuffer.cpp +7 -0
  6. package/MMKV/Core/MMBuffer.h +3 -0
  7. package/MMKV/Core/MMKV.cpp +114 -19
  8. package/MMKV/Core/MMKV.h +69 -33
  9. package/MMKV/Core/MMKVPredef.h +1 -1
  10. package/MMKV/Core/MMKV_Android.cpp +10 -8
  11. package/MMKV/Core/MMKV_IO.cpp +386 -60
  12. package/MMKV/Core/MMKV_OSX.cpp +51 -30
  13. package/MMKV/Core/MMKV_OSX.h +10 -4
  14. package/MMKV/Core/MemoryFile.cpp +31 -11
  15. package/MMKV/Core/MemoryFile.h +8 -3
  16. package/MMKV/Core/MemoryFile_Android.cpp +2 -2
  17. package/MMKV/Core/MemoryFile_OSX.cpp +2 -3
  18. package/MMKV/Core/MemoryFile_Win32.cpp +8 -5
  19. package/MMKV/Core/MiniPBCoder.cpp +11 -0
  20. package/MMKV/Core/MiniPBCoder.h +3 -0
  21. package/MMKV/Core/MiniPBCoder_OSX.cpp +8 -0
  22. package/MMKV/Core/ThreadLock.cpp +5 -0
  23. package/MMKV/Core/ThreadLock.h +4 -0
  24. package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +8 -0
  25. package/MMKV/Core/core.vcxproj +3 -3
  26. package/MMKV/README.md +3 -3
  27. package/android/build.gradle +16 -3
  28. package/android/src/hasNamespace/AndroidManifest.xml +3 -0
  29. package/android/src/main/cpp/cpp-adapter.cpp +7 -1
  30. package/ios/MmkvModule.mm +8 -2
  31. package/lib/commonjs/createMMKV.web.js +1 -1
  32. package/lib/commonjs/createMMKV.web.js.map +1 -1
  33. package/lib/commonjs/hooks.js +15 -10
  34. package/lib/commonjs/hooks.js.map +1 -1
  35. package/lib/module/createMMKV.web.js +1 -1
  36. package/lib/module/createMMKV.web.js.map +1 -1
  37. package/lib/module/hooks.js +15 -10
  38. package/lib/module/hooks.js.map +1 -1
  39. package/lib/typescript/createMMKV.web.d.ts.map +1 -1
  40. package/lib/typescript/hooks.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/react-native-mmkv.podspec +1 -1
  43. package/src/createMMKV.web.ts +4 -2
  44. package/src/hooks.ts +18 -13
  45. package/MMKV/LICENSE.TXT +0 -193
package/MMKV/Core/MMKV.h CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  #ifndef MMKV_MMKV_H
22
22
  #define MMKV_MMKV_H
23
- #ifdef __cplusplus
23
+ #ifdef __cplusplus
24
24
 
25
25
  #include "MMBuffer.h"
26
26
  #include <cstdint>
@@ -47,18 +47,18 @@ enum MMKVMode : uint32_t {
47
47
  #endif
48
48
  };
49
49
 
50
- #define OUT
50
+ #define MMKV_OUT
51
51
 
52
52
  class MMKV {
53
53
  #ifndef MMKV_ANDROID
54
54
  std::string m_mmapKey;
55
- MMKV(const std::string &mmapID, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath);
55
+ MMKV(const std::string &mmapID, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath, size_t expectedCapacity = 0);
56
56
  #else // defined(MMKV_ANDROID)
57
57
  mmkv::FileLock *m_fileModeLock;
58
58
  mmkv::InterProcessLock *m_sharedProcessModeLock;
59
59
  mmkv::InterProcessLock *m_exclusiveProcessModeLock;
60
60
 
61
- MMKV(const std::string &mmapID, int size, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath);
61
+ MMKV(const std::string &mmapID, int size, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath, size_t expectedCapacity = 0);
62
62
 
63
63
  MMKV(const std::string &mmapID, int ashmemFD, int ashmemMetaFd, std::string *cryptKey = nullptr);
64
64
  #endif
@@ -71,6 +71,8 @@ class MMKV {
71
71
  mmkv::MMKVMap *m_dic;
72
72
  mmkv::MMKVMapCrypt *m_dicCrypt;
73
73
 
74
+ size_t m_expectedCapacity;
75
+
74
76
  mmkv::MemoryFile *m_file;
75
77
  size_t m_actualSize;
76
78
  mmkv::CodedOutputData *m_output;
@@ -92,18 +94,20 @@ class MMKV {
92
94
  bool m_enableKeyExpire = false;
93
95
  uint32_t m_expiredInSeconds = ExpireNever;
94
96
 
97
+ bool m_enableCompareBeforeSet = false;
98
+
95
99
  #ifdef MMKV_APPLE
96
100
  using MMKVKey_t = NSString *__unsafe_unretained;
97
101
  static bool isKeyEmpty(MMKVKey_t key) { return key.length <= 0; }
98
- # define key_length(key) key.length
99
- # define retain_key(key) [key retain]
100
- # define release_key(key) [key release]
102
+ # define mmkv_key_length(key) key.length
103
+ # define mmkv_retain_key(key) [key retain]
104
+ # define mmkv_release_key(key) [key release]
101
105
  #else
102
106
  using MMKVKey_t = const std::string &;
103
107
  static bool isKeyEmpty(MMKVKey_t key) { return key.empty(); }
104
- # define key_length(key) key.length()
105
- # define retain_key(key) ((void)0)
106
- # define release_key(key) ((void)0)
108
+ # define mmkv_key_length(key) key.length()
109
+ # define mmkv_retain_key(key) ((void) 0)
110
+ # define mmkv_release_key(key) ((void) 0)
107
111
  #endif
108
112
 
109
113
  void loadFromFile();
@@ -121,6 +125,7 @@ class MMKV {
121
125
  bool checkFileCRCValid(size_t actualSize, uint32_t crcDigest);
122
126
 
123
127
  void recaculateCRCDigestWithIV(const void *iv);
128
+ void recaculateCRCDigestOnly();
124
129
 
125
130
  void updateCRCDigest(const uint8_t *ptr, size_t length);
126
131
 
@@ -132,11 +137,11 @@ class MMKV {
132
137
 
133
138
  bool ensureMemorySize(size_t newSize);
134
139
 
135
- bool expandAndWriteBack(size_t newSize, std::pair<mmkv::MMBuffer, size_t> preparedData);
140
+ bool expandAndWriteBack(size_t newSize, std::pair<mmkv::MMBuffer, size_t> preparedData, bool needSync = true);
136
141
 
137
- bool fullWriteback(mmkv::AESCrypt *newCrypter = nullptr);
142
+ bool fullWriteback(mmkv::AESCrypt *newCrypter = nullptr, bool onlyWhileExpire = false);
138
143
 
139
- bool doFullWriteBack(std::pair<mmkv::MMBuffer, size_t> preparedData, mmkv::AESCrypt *newCrypter);
144
+ bool doFullWriteBack(std::pair<mmkv::MMBuffer, size_t> preparedData, mmkv::AESCrypt *newCrypter, bool needSync = true);
140
145
 
141
146
  bool doFullWriteBack(mmkv::MMKVVector &&vec);
142
147
 
@@ -154,11 +159,20 @@ class MMKV {
154
159
  KVHolderRet_t doAppendDataWithKey(const mmkv::MMBuffer &data, const mmkv::MMBuffer &key, bool isDataHolder, uint32_t keyLength);
155
160
  KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, MMKVKey_t key, bool isDataHolder = false);
156
161
  KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data, const mmkv::KeyValueHolder &kvHolder, bool isDataHolder = false);
162
+
163
+ KVHolderRet_t doOverrideDataWithKey(const mmkv::MMBuffer &data, const mmkv::MMBuffer &key, bool isDataHolder, uint32_t keyLength);
164
+ KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data, const mmkv::KeyValueHolder &kvHolder, bool isDataHolder = false);
165
+ KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data, MMKVKey_t key, bool isDataHolder = false);
166
+ bool checkSizeForOverride(size_t size);
157
167
  #ifdef MMKV_APPLE
158
168
  KVHolderRet_t appendDataWithKey(const mmkv::MMBuffer &data,
159
169
  MMKVKey_t key,
160
170
  const mmkv::KeyValueHolderCrypt &kvHolder,
161
171
  bool isDataHolder = false);
172
+ KVHolderRet_t overrideDataWithKey(const mmkv::MMBuffer &data,
173
+ MMKVKey_t key,
174
+ const mmkv::KeyValueHolderCrypt &kvHolder,
175
+ bool isDataHolder = false);
162
176
  #endif
163
177
 
164
178
  void notifyContentChanged();
@@ -180,11 +194,6 @@ public:
180
194
  // call this before getting any MMKV instance
181
195
  static void initializeMMKV(const MMKVPath_t &rootDir, MMKVLogLevel logLevel = MMKVLogInfo, mmkv::LogHandler handler = nullptr);
182
196
 
183
- #ifdef MMKV_APPLE
184
- // protect from some old code that don't call initializeMMKV()
185
- static void minimalInit(MMKVPath_t defaultRootDir);
186
- #endif
187
-
188
197
  // a generic purpose instance
189
198
  static MMKV *defaultMMKV(MMKVMode mode = MMKV_SINGLE_PROCESS, std::string *cryptKey = nullptr);
190
199
 
@@ -196,7 +205,8 @@ public:
196
205
  static MMKV *mmkvWithID(const std::string &mmapID,
197
206
  MMKVMode mode = MMKV_SINGLE_PROCESS,
198
207
  std::string *cryptKey = nullptr,
199
- MMKVPath_t *rootPath = nullptr);
208
+ MMKVPath_t *rootPath = nullptr,
209
+ size_t expectedCapacity = 0);
200
210
 
201
211
  #else // defined(MMKV_ANDROID)
202
212
 
@@ -207,7 +217,8 @@ public:
207
217
  int size = mmkv::DEFAULT_MMAP_SIZE,
208
218
  MMKVMode mode = MMKV_SINGLE_PROCESS,
209
219
  std::string *cryptKey = nullptr,
210
- MMKVPath_t *rootPath = nullptr);
220
+ MMKVPath_t *rootPath = nullptr,
221
+ size_t expectedCapacity = 0);
211
222
 
212
223
  static MMKV *mmkvWithAshmemFD(const std::string &mmapID, int fd, int metaFD, std::string *cryptKey = nullptr);
213
224
 
@@ -282,7 +293,8 @@ public:
282
293
  bool set(const std::vector<std::string> &vector, MMKVKey_t key);
283
294
  bool set(const std::vector<std::string> &vector, MMKVKey_t key, uint32_t expireDuration);
284
295
 
285
- bool getString(MMKVKey_t key, std::string &result);
296
+ // inplaceModification is recommended for faster speed
297
+ bool getString(MMKVKey_t key, std::string &result, bool inplaceModification = true);
286
298
 
287
299
  mmkv::MMBuffer getBytes(MMKVKey_t key);
288
300
 
@@ -291,19 +303,19 @@ public:
291
303
  bool getVector(MMKVKey_t key, std::vector<std::string> &result);
292
304
  #endif // MMKV_APPLE
293
305
 
294
- bool getBool(MMKVKey_t key, bool defaultValue = false, OUT bool *hasValue = nullptr);
306
+ bool getBool(MMKVKey_t key, bool defaultValue = false, MMKV_OUT bool *hasValue = nullptr);
295
307
 
296
- int32_t getInt32(MMKVKey_t key, int32_t defaultValue = 0, OUT bool *hasValue = nullptr);
308
+ int32_t getInt32(MMKVKey_t key, int32_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
297
309
 
298
- uint32_t getUInt32(MMKVKey_t key, uint32_t defaultValue = 0, OUT bool *hasValue = nullptr);
310
+ uint32_t getUInt32(MMKVKey_t key, uint32_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
299
311
 
300
- int64_t getInt64(MMKVKey_t key, int64_t defaultValue = 0, OUT bool *hasValue = nullptr);
312
+ int64_t getInt64(MMKVKey_t key, int64_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
301
313
 
302
- uint64_t getUInt64(MMKVKey_t key, uint64_t defaultValue = 0, OUT bool *hasValue = nullptr);
314
+ uint64_t getUInt64(MMKVKey_t key, uint64_t defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
303
315
 
304
- float getFloat(MMKVKey_t key, float defaultValue = 0, OUT bool *hasValue = nullptr);
316
+ float getFloat(MMKVKey_t key, float defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
305
317
 
306
- double getDouble(MMKVKey_t key, double defaultValue = 0, OUT bool *hasValue = nullptr);
318
+ double getDouble(MMKVKey_t key, double defaultValue = 0, MMKV_OUT bool *hasValue = nullptr);
307
319
 
308
320
  // return the actual size consumption of the key's value
309
321
  // pass actualSize = true to get value's length
@@ -315,7 +327,8 @@ public:
315
327
 
316
328
  bool containsKey(MMKVKey_t key);
317
329
 
318
- size_t count();
330
+ // filterExpire: return count of all non-expired keys, keep in mind it comes with cost
331
+ size_t count(bool filterExpire = false);
319
332
 
320
333
  size_t totalSize();
321
334
 
@@ -329,8 +342,17 @@ public:
329
342
 
330
343
  bool disableAutoKeyExpire();
331
344
 
345
+ // compare value for key before set, to reduce the possibility of file expanding
346
+ bool enableCompareBeforeSet();
347
+ bool disableCompareBeforeSet();
348
+
349
+ bool isExpirationEnabled() { return m_enableKeyExpire; }
350
+ bool isEncryptionEnabled() { return m_dicCrypt; }
351
+ bool isCompareBeforeSetEnabled() { return m_enableCompareBeforeSet && !m_enableKeyExpire && !m_dicCrypt; }
352
+
332
353
  #ifdef MMKV_APPLE
333
- NSArray *allKeys();
354
+ // filterExpire: return all non-expired keys, keep in mind it comes with cost
355
+ NSArray *allKeys(bool filterExpire = false);
334
356
 
335
357
  void removeValuesForKeys(NSArray *arrKeys);
336
358
 
@@ -342,14 +364,16 @@ public:
342
364
  static bool isInBackground();
343
365
  # endif
344
366
  #else // !defined(MMKV_APPLE)
345
- std::vector<std::string> allKeys();
367
+ // filterExpire: return all non-expired keys, keep in mind it comes with cost
368
+ std::vector<std::string> allKeys(bool filterExpire = false);
346
369
 
347
370
  void removeValuesForKeys(const std::vector<std::string> &arrKeys);
348
371
  #endif // MMKV_APPLE
349
372
 
350
373
  void removeValueForKey(MMKVKey_t key);
351
374
 
352
- void clearAll();
375
+ // keepSpace: remove all keys but keep the file size not changed, running faster
376
+ void clearAll(bool keepSpace = false);
353
377
 
354
378
  // MMKV's size won't reduce after deleting key-values
355
379
  // call this method after lots of deleting if you care about disk usage
@@ -362,7 +386,8 @@ public:
362
386
 
363
387
  // call this method if you are facing memory-warning
364
388
  // any subsequent call to the instance will load all key-values from file again
365
- void clearMemoryCache();
389
+ // keepSpace: remove all keys but keep the file size not changed, running faster
390
+ void clearMemoryCache(bool keepSpace = false);
366
391
 
367
392
  // you don't need to call this, really, I mean it
368
393
  // unless you worry about running out of battery
@@ -373,6 +398,13 @@ public:
373
398
  void unlock();
374
399
  bool try_lock();
375
400
 
401
+ // get thread lock
402
+ #ifndef MMKV_WIN32
403
+ void lock_thread();
404
+ void unlock_thread();
405
+ bool try_lock_thread();
406
+ #endif
407
+
376
408
  static const MMKVPath_t &getRootDir();
377
409
 
378
410
  // backup one MMKV instance from srcDir to dstDir
@@ -419,6 +451,10 @@ public:
419
451
  // Note: Don't use this to check the existence of the instance, the return value is undefined if the file was never created.
420
452
  static bool isFileValid(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);
421
453
 
454
+ // remove the storage of the MMKV, including the data file & meta file (.crc)
455
+ // Note: the existing instance (if any) will be closed & destroyed
456
+ static bool removeStorage(const std::string &mmapID, MMKVPath_t *relatePath = nullptr);
457
+
422
458
  // just forbid it for possibly misuse
423
459
  explicit MMKV(const MMKV &other) = delete;
424
460
  MMKV &operator=(const MMKV &other) = delete;
@@ -34,7 +34,7 @@
34
34
  #include <vector>
35
35
  #include <unordered_map>
36
36
 
37
- constexpr auto MMKV_VERSION = "v1.3.0";
37
+ constexpr auto MMKV_VERSION = "v1.3.3";
38
38
 
39
39
  #ifdef DEBUG
40
40
  # define MMKV_DEBUG
@@ -38,13 +38,14 @@ using namespace mmkv;
38
38
  extern unordered_map<string, MMKV *> *g_instanceDic;
39
39
  extern ThreadLock *g_instanceLock;
40
40
 
41
- MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath)
41
+ MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath, size_t expectedCapacity)
42
42
  : m_mmapID((mode & MMKV_BACKUP) ? mmapID : mmapedKVKey(mmapID, rootPath)) // historically Android mistakenly use mmapKey as mmapID
43
43
  , m_path(mappedKVPathWithID(m_mmapID, mode, rootPath))
44
44
  , m_crcPath(crcPathWithID(m_mmapID, mode, rootPath))
45
45
  , m_dic(nullptr)
46
46
  , m_dicCrypt(nullptr)
47
- , m_file(new MemoryFile(m_path, size, (mode & MMKV_ASHMEM) ? MMFILE_TYPE_ASHMEM : MMFILE_TYPE_FILE))
47
+ , m_expectedCapacity(std::max<size_t>(DEFAULT_MMAP_SIZE, roundUp<size_t>(expectedCapacity, DEFAULT_MMAP_SIZE)))
48
+ , m_file(new MemoryFile(m_path, size, (mode & MMKV_ASHMEM) ? MMFILE_TYPE_ASHMEM : MMFILE_TYPE_FILE, m_expectedCapacity))
48
49
  , m_metaFile(new MemoryFile(m_crcPath, DEFAULT_MMAP_SIZE, m_file->m_fileType))
49
50
  , m_metaInfo(new MMKVMetaInfo())
50
51
  , m_crypter(nullptr)
@@ -80,10 +81,10 @@ MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, string *cryptKey, stri
80
81
  m_exclusiveProcessLock->m_enable = m_isInterProcess;
81
82
 
82
83
  // sensitive zone
83
- {
84
+ /*{
84
85
  SCOPED_LOCK(m_sharedProcessLock);
85
86
  loadFromFile();
86
- }
87
+ }*/
87
88
  }
88
89
 
89
90
  MMKV::MMKV(const string &mmapID, int ashmemFD, int ashmemMetaFD, string *cryptKey)
@@ -129,13 +130,14 @@ MMKV::MMKV(const string &mmapID, int ashmemFD, int ashmemMetaFD, string *cryptKe
129
130
  m_exclusiveProcessLock->m_enable = m_isInterProcess;
130
131
 
131
132
  // sensitive zone
132
- {
133
+ /*{
133
134
  SCOPED_LOCK(m_sharedProcessLock);
134
135
  loadFromFile();
135
- }
136
+ }*/
136
137
  }
137
138
 
138
- MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath) {
139
+ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath,
140
+ size_t expectedCapacity) {
139
141
  if (mmapID.empty()) {
140
142
  return nullptr;
141
143
  }
@@ -155,7 +157,7 @@ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cr
155
157
  }
156
158
  MMKVInfo("prepare to load %s (id %s) from rootPath %s", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
157
159
  }
158
- auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath);
160
+ auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath, expectedCapacity);
159
161
  (*g_instanceDic)[mmapKey] = kv;
160
162
  return kv;
161
163
  }