react-native-mmkv 2.8.0 → 2.9.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.
package/MMKV/Core/MMKV.h CHANGED
@@ -89,18 +89,29 @@ class MMKV {
89
89
  mmkv::InterProcessLock *m_sharedProcessLock;
90
90
  mmkv::InterProcessLock *m_exclusiveProcessLock;
91
91
 
92
+ bool m_enableKeyExpire = false;
93
+ uint32_t m_expiredInSeconds = ExpireNever;
94
+
92
95
  #ifdef MMKV_APPLE
93
96
  using MMKVKey_t = NSString *__unsafe_unretained;
94
97
  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]
95
101
  #else
96
102
  using MMKVKey_t = const std::string &;
97
103
  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)
98
107
  #endif
99
108
 
100
109
  void loadFromFile();
101
110
 
102
111
  void partialLoadFromFile();
103
112
 
113
+ void loadMetaInfoAndCheck();
114
+
104
115
  void checkDataValid(bool &loadFromFile, bool &needFullWriteback);
105
116
 
106
117
  void checkLoadData();
@@ -121,10 +132,16 @@ class MMKV {
121
132
 
122
133
  bool ensureMemorySize(size_t newSize);
123
134
 
135
+ bool expandAndWriteBack(size_t newSize, std::pair<mmkv::MMBuffer, size_t> preparedData);
136
+
124
137
  bool fullWriteback(mmkv::AESCrypt *newCrypter = nullptr);
125
138
 
126
139
  bool doFullWriteBack(std::pair<mmkv::MMBuffer, size_t> preparedData, mmkv::AESCrypt *newCrypter);
127
140
 
141
+ bool doFullWriteBack(mmkv::MMKVVector &&vec);
142
+
143
+ mmkv::MMBuffer getRawDataForKey(MMKVKey_t key);
144
+
128
145
  mmkv::MMBuffer getDataForKey(MMKVKey_t key);
129
146
 
130
147
  // isDataHolder: avoid memory copying
@@ -154,6 +171,11 @@ class MMKV {
154
171
  static bool restoreOneFromDirectory(const std::string &mmapKey, const MMKVPath_t &srcPath, const MMKVPath_t &dstPath, bool compareFullPath);
155
172
  static size_t restoreAllFromDirectory(const MMKVPath_t &srcDir, const MMKVPath_t &dstDir, bool isInSpecialDir);
156
173
 
174
+ static uint32_t getCurrentTimeInSecond();
175
+ uint32_t getExpireTimeForKey(MMKVKey_t key);
176
+ mmkv::MMBuffer getDataWithoutMTimeForKey(MMKVKey_t key);
177
+ size_t filterExpiredKeys();
178
+
157
179
  public:
158
180
  // call this before getting any MMKV instance
159
181
  static void initializeMMKV(const MMKVPath_t &rootDir, MMKVLogLevel logLevel = MMKVLogInfo, mmkv::LogHandler handler = nullptr);
@@ -216,35 +238,49 @@ public:
216
238
  #endif
217
239
 
218
240
  bool set(bool value, MMKVKey_t key);
241
+ bool set(bool value, MMKVKey_t key, uint32_t expireDuration);
219
242
 
220
243
  bool set(int32_t value, MMKVKey_t key);
244
+ bool set(int32_t value, MMKVKey_t key, uint32_t expireDuration);
221
245
 
222
246
  bool set(uint32_t value, MMKVKey_t key);
247
+ bool set(uint32_t value, MMKVKey_t key, uint32_t expireDuration);
223
248
 
224
249
  bool set(int64_t value, MMKVKey_t key);
250
+ bool set(int64_t value, MMKVKey_t key, uint32_t expireDuration);
225
251
 
226
252
  bool set(uint64_t value, MMKVKey_t key);
253
+ bool set(uint64_t value, MMKVKey_t key, uint32_t expireDuration);
227
254
 
228
255
  bool set(float value, MMKVKey_t key);
256
+ bool set(float value, MMKVKey_t key, uint32_t expireDuration);
229
257
 
230
258
  bool set(double value, MMKVKey_t key);
259
+ bool set(double value, MMKVKey_t key, uint32_t expireDuration);
231
260
 
232
261
  // avoid unexpected type conversion (pointer to bool, etc)
233
262
  template <typename T>
234
263
  bool set(T value, MMKVKey_t key) = delete;
264
+ template <typename T>
265
+ bool set(T value, MMKVKey_t key, uint32_t expireDuration) = delete;
235
266
 
236
267
  #ifdef MMKV_APPLE
237
268
  bool set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key);
269
+ bool set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key, uint32_t expireDuration);
238
270
 
239
271
  NSObject *getObject(MMKVKey_t key, Class cls);
240
272
  #else // !defined(MMKV_APPLE)
241
273
  bool set(const char *value, MMKVKey_t key);
274
+ bool set(const char *value, MMKVKey_t key, uint32_t expireDuration);
242
275
 
243
276
  bool set(const std::string &value, MMKVKey_t key);
277
+ bool set(const std::string &value, MMKVKey_t key, uint32_t expireDuration);
244
278
 
245
279
  bool set(const mmkv::MMBuffer &value, MMKVKey_t key);
280
+ bool set(const mmkv::MMBuffer &value, MMKVKey_t key, uint32_t expireDuration);
246
281
 
247
282
  bool set(const std::vector<std::string> &vector, MMKVKey_t key);
283
+ bool set(const std::vector<std::string> &vector, MMKVKey_t key, uint32_t expireDuration);
248
284
 
249
285
  bool getString(MMKVKey_t key, std::string &result);
250
286
 
@@ -285,6 +321,14 @@ public:
285
321
 
286
322
  size_t actualSize();
287
323
 
324
+ static constexpr uint32_t ExpireNever = 0;
325
+
326
+ // all keys created (or last modified) longer than expiredInSeconds will be deleted on next full-write-back
327
+ // expiredInSeconds = MMKV::ExpireNever (0) means no common expiration duration for all keys, aka each key will have it's own expiration duration
328
+ bool enableAutoKeyExpire(uint32_t expiredInSeconds = 0);
329
+
330
+ bool disableAutoKeyExpire();
331
+
288
332
  #ifdef MMKV_APPLE
289
333
  NSArray *allKeys();
290
334
 
@@ -39,6 +39,15 @@ enum MMKVVersion : uint32_t {
39
39
 
40
40
  // store actual size together with crc checksum, try to reduce file corruption
41
41
  MMKVVersionActualSize = 3,
42
+
43
+ // store extra flags
44
+ MMKVVersionFlag = 4,
45
+
46
+ // preserved for next use
47
+ MMKVVersionNext = 5,
48
+
49
+ // always large than next, a placeholder for error check
50
+ MMKVVersionHolder = MMKVVersionNext + 1,
42
51
  };
43
52
 
44
53
  struct MMKVMetaInfo {
@@ -55,6 +64,15 @@ struct MMKVMetaInfo {
55
64
  uint32_t _reserved[16] = {};
56
65
  } m_lastConfirmedMetaInfo;
57
66
 
67
+ uint64_t m_flags = 0;
68
+
69
+ enum MMKVMetaInfoFlag : uint64_t {
70
+ EnableKeyExipre = 1 << 0,
71
+ };
72
+ bool hasFlag(MMKVMetaInfoFlag flag) { return (m_flags & flag) != 0; }
73
+ void setFlag(MMKVMetaInfoFlag flag) { m_flags |= flag; }
74
+ void unsetFlag(MMKVMetaInfoFlag flag) { m_flags &= ~flag; }
75
+
58
76
  void write(void *ptr) const {
59
77
  MMKV_ASSERT(ptr);
60
78
  memcpy(ptr, this, sizeof(MMKVMetaInfo));
@@ -34,7 +34,7 @@
34
34
  #include <vector>
35
35
  #include <unordered_map>
36
36
 
37
- constexpr auto MMKV_VERSION = "v1.2.15";
37
+ constexpr auto MMKV_VERSION = "v1.3.0";
38
38
 
39
39
  #ifdef DEBUG
40
40
  # define MMKV_DEBUG
@@ -84,7 +84,7 @@ constexpr auto MMKV_VERSION = "v1.2.15";
84
84
  # include <windows.h>
85
85
 
86
86
  constexpr auto MMKV_PATH_SLASH = L"\\";
87
- # define MMKV_PATH_FORMAT "%ws"
87
+ # define MMKV_PATH_FORMAT "%ls"
88
88
  using MMKVFileHandle_t = HANDLE;
89
89
  using MMKVPath_t = std::wstring;
90
90
  extern MMKVPath_t string2MMKVPath_t(const std::string &str);