react-native-mmkv 3.0.2 → 3.1.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.cpp +51 -15
- package/MMKV/Core/MMKV.h +19 -5
- package/MMKV/Core/MMKVPredef.h +2 -2
- package/MMKV/Core/MMKV_Android.cpp +13 -13
- package/MMKV/Core/MMKV_IO.cpp +37 -6
- package/MMKV/Core/MMKV_OSX.cpp +15 -4
- package/MMKV/Core/MemoryFile.cpp +24 -10
- package/MMKV/Core/MemoryFile.h +8 -2
- package/MMKV/Core/MemoryFile_Android.cpp +4 -3
- package/MMKV/Core/MemoryFile_Win32.cpp +22 -11
- package/MMKV/Core/aes/AESCrypt.cpp +19 -24
- package/MMKV/Core/aes/AESCrypt.h +4 -1
- package/MMKV/README.md +354 -0
- package/README.md +5 -0
- package/cpp/MmkvHostObject.cpp +18 -3
- package/cpp/NativeMmkvModule.h +1 -1
- package/lib/commonjs/MMKV.js +5 -5
- package/lib/commonjs/MMKV.js.map +1 -1
- package/lib/commonjs/MemoryWarningListener.js +31 -0
- package/lib/commonjs/MemoryWarningListener.js.map +1 -0
- package/lib/commonjs/NativeMmkv.js +5 -5
- package/lib/commonjs/NativeMmkv.js.map +1 -1
- package/lib/commonjs/NativeMmkvPlatformContext.js +4 -4
- package/lib/commonjs/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/commonjs/createMMKV.js +6 -0
- package/lib/commonjs/createMMKV.js.map +1 -1
- package/lib/commonjs/createMMKV.mock.js +1 -0
- package/lib/commonjs/createMMKV.mock.js.map +1 -1
- package/lib/commonjs/createMMKV.web.js +1 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -1
- package/lib/module/MMKV.js +5 -5
- package/lib/module/MMKV.js.map +1 -1
- package/lib/module/MemoryWarningListener.js +27 -0
- package/lib/module/MemoryWarningListener.js.map +1 -0
- package/lib/module/NativeMmkv.js +5 -5
- package/lib/module/NativeMmkv.js.map +1 -1
- package/lib/module/NativeMmkvPlatformContext.js +4 -4
- package/lib/module/NativeMmkvPlatformContext.js.map +1 -1
- package/lib/module/createMMKV.js +7 -1
- package/lib/module/createMMKV.js.map +1 -1
- package/lib/module/createMMKV.mock.js +1 -0
- package/lib/module/createMMKV.mock.js.map +1 -1
- package/lib/module/createMMKV.web.js +1 -0
- package/lib/module/createMMKV.web.js.map +1 -1
- package/lib/typescript/src/MMKV.d.ts +1 -0
- package/lib/typescript/src/MMKV.d.ts.map +1 -1
- package/lib/typescript/src/MemoryWarningListener.d.ts +3 -0
- package/lib/typescript/src/MemoryWarningListener.d.ts.map +1 -0
- package/lib/typescript/src/NativeMmkv.d.ts +4 -0
- package/lib/typescript/src/NativeMmkv.d.ts.map +1 -1
- package/lib/typescript/src/NativeMmkvPlatformContext.d.ts.map +1 -1
- package/lib/typescript/src/Types.d.ts +5 -0
- package/lib/typescript/src/Types.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.mock.d.ts.map +1 -1
- package/lib/typescript/src/createMMKV.web.d.ts.map +1 -1
- package/package.json +12 -14
- package/react-native-mmkv.podspec +1 -1
- package/src/MMKV.ts +5 -5
- package/src/MemoryWarningListener.ts +29 -0
- package/src/NativeMmkv.ts +9 -5
- package/src/NativeMmkvPlatformContext.ts +6 -4
- package/src/Types.ts +5 -0
- package/src/createMMKV.mock.ts +1 -0
- package/src/createMMKV.ts +8 -1
- package/src/createMMKV.web.ts +1 -0
- package/img/banner-dark.png +0 -0
- package/img/banner-light.png +0 -0
package/MMKV/Core/MemoryFile.h
CHANGED
|
@@ -47,6 +47,7 @@ enum class OpenFlag : uint32_t {
|
|
|
47
47
|
Excel = 1 << 3, // fail if Create is set but the file already exist
|
|
48
48
|
Truncate = 1 << 4,
|
|
49
49
|
};
|
|
50
|
+
constexpr uint32_t OpenFlagRWMask = 0x3; // mask for Read Write mode
|
|
50
51
|
|
|
51
52
|
static inline OpenFlag operator | (OpenFlag left, OpenFlag right) {
|
|
52
53
|
return static_cast<OpenFlag>(static_cast<uint32_t>(left) | static_cast<uint32_t>(right));
|
|
@@ -56,6 +57,10 @@ static inline bool operator & (OpenFlag left, OpenFlag right) {
|
|
|
56
57
|
return ((static_cast<uint32_t>(left) & static_cast<uint32_t>(right)) != 0);
|
|
57
58
|
}
|
|
58
59
|
|
|
60
|
+
static inline OpenFlag operator & (OpenFlag left, uint32_t right) {
|
|
61
|
+
return static_cast<OpenFlag>(static_cast<uint32_t>(left) & right);
|
|
62
|
+
}
|
|
63
|
+
|
|
59
64
|
template <typename T>
|
|
60
65
|
T roundUp(T numToRound, T multiple) {
|
|
61
66
|
return ((numToRound + multiple - 1) / multiple) * multiple;
|
|
@@ -109,6 +114,7 @@ class MemoryFile {
|
|
|
109
114
|
#endif
|
|
110
115
|
void *m_ptr;
|
|
111
116
|
size_t m_size;
|
|
117
|
+
const bool m_readOnly;
|
|
112
118
|
|
|
113
119
|
bool mmap();
|
|
114
120
|
|
|
@@ -116,9 +122,9 @@ class MemoryFile {
|
|
|
116
122
|
|
|
117
123
|
public:
|
|
118
124
|
#ifndef MMKV_ANDROID
|
|
119
|
-
explicit MemoryFile(MMKVPath_t path, size_t expectedCapacity = 0);
|
|
125
|
+
explicit MemoryFile(MMKVPath_t path, size_t expectedCapacity = 0, bool readOnly = false);
|
|
120
126
|
#else
|
|
121
|
-
MemoryFile(MMKVPath_t path, size_t size, FileType fileType, size_t expectedCapacity = 0);
|
|
127
|
+
MemoryFile(MMKVPath_t path, size_t size, FileType fileType, size_t expectedCapacity = 0, bool readOnly = false);
|
|
122
128
|
explicit MemoryFile(MMKVFileHandle_t ashmemFD);
|
|
123
129
|
|
|
124
130
|
const FileType m_fileType;
|
|
@@ -70,8 +70,9 @@ File::File(MMKVFileHandle_t ashmemFD)
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
MemoryFile::MemoryFile(string path, size_t size, FileType fileType, size_t expectedCapacity)
|
|
74
|
-
: m_diskFile(std::move(path), OpenFlag::ReadWrite | OpenFlag::Create, size, fileType),
|
|
73
|
+
MemoryFile::MemoryFile(string path, size_t size, FileType fileType, size_t expectedCapacity, bool isReadOnly)
|
|
74
|
+
: m_diskFile(std::move(path), isReadOnly ? OpenFlag::ReadOnly : (OpenFlag::ReadWrite | OpenFlag::Create), size, fileType),
|
|
75
|
+
m_ptr(nullptr), m_size(0), m_fileType(fileType), m_readOnly(isReadOnly) {
|
|
75
76
|
if (m_fileType == MMFILE_TYPE_FILE) {
|
|
76
77
|
reloadFromFile(expectedCapacity);
|
|
77
78
|
} else {
|
|
@@ -86,7 +87,7 @@ MemoryFile::MemoryFile(string path, size_t size, FileType fileType, size_t expec
|
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
MemoryFile::MemoryFile(int ashmemFD)
|
|
89
|
-
: m_diskFile(ashmemFD), m_ptr(nullptr), m_size(0), m_fileType(MMFILE_TYPE_ASHMEM) {
|
|
90
|
+
: m_diskFile(ashmemFD), m_ptr(nullptr), m_size(0), m_fileType(MMFILE_TYPE_ASHMEM), m_readOnly(false) {
|
|
90
91
|
if (!m_diskFile.isFileValid()) {
|
|
91
92
|
MMKVError("fd %d invalid", ashmemFD);
|
|
92
93
|
} else {
|
|
@@ -43,7 +43,7 @@ File::File(MMKVPath_t path, OpenFlag flag) : m_path(std::move(path)), m_fd(INVAL
|
|
|
43
43
|
|
|
44
44
|
static pair<int, int> OpenFlag2NativeFlag(OpenFlag flag) {
|
|
45
45
|
int access = 0, create = OPEN_EXISTING;
|
|
46
|
-
if (flag & OpenFlag::ReadWrite) {
|
|
46
|
+
if ((flag & OpenFlagRWMask) == OpenFlag::ReadWrite) {
|
|
47
47
|
access = (GENERIC_READ | GENERIC_WRITE);
|
|
48
48
|
} else if (flag & OpenFlag::ReadOnly) {
|
|
49
49
|
access |= GENERIC_READ;
|
|
@@ -70,10 +70,10 @@ bool File::open() {
|
|
|
70
70
|
m_fd = CreateFile(m_path.c_str(), pair.first, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
|
|
71
71
|
pair.second, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
72
72
|
if (!isFileValid()) {
|
|
73
|
-
MMKVError("fail to open:[%ls], %d", m_path.c_str(), GetLastError());
|
|
73
|
+
MMKVError("fail to open:[%ls], flag %x, error %d", m_path.c_str(), m_flag, GetLastError());
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
|
-
MMKVInfo("open fd[%p], %ls", m_fd, m_path.c_str());
|
|
76
|
+
MMKVInfo("open fd[%p], flag %x, %ls", m_fd, m_flag, m_path.c_str());
|
|
77
77
|
return true;
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -94,11 +94,12 @@ size_t File::getActualFileSize() const {
|
|
|
94
94
|
return size;
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
MemoryFile::MemoryFile(MMKVPath_t path, size_t expectedCapacity)
|
|
98
|
-
: m_diskFile(std::move(path), OpenFlag::ReadWrite | OpenFlag::Create)
|
|
97
|
+
MemoryFile::MemoryFile(MMKVPath_t path, size_t expectedCapacity, bool readOnly)
|
|
98
|
+
: m_diskFile(std::move(path), readOnly ? OpenFlag::ReadOnly : (OpenFlag::ReadWrite | OpenFlag::Create))
|
|
99
99
|
, m_fileMapping(nullptr)
|
|
100
100
|
, m_ptr(nullptr)
|
|
101
|
-
, m_size(0)
|
|
101
|
+
, m_size(0)
|
|
102
|
+
, m_readOnly(readOnly) {
|
|
102
103
|
reloadFromFile(expectedCapacity);
|
|
103
104
|
}
|
|
104
105
|
|
|
@@ -109,6 +110,10 @@ bool MemoryFile::truncate(size_t size) {
|
|
|
109
110
|
if (size == m_size) {
|
|
110
111
|
return true;
|
|
111
112
|
}
|
|
113
|
+
if (m_readOnly) {
|
|
114
|
+
// truncate readonly file not allow
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
112
117
|
|
|
113
118
|
auto oldSize = m_size;
|
|
114
119
|
m_size = size;
|
|
@@ -151,6 +156,10 @@ bool MemoryFile::truncate(size_t size) {
|
|
|
151
156
|
}
|
|
152
157
|
|
|
153
158
|
bool MemoryFile::msync(SyncFlag syncFlag) {
|
|
159
|
+
if (m_readOnly) {
|
|
160
|
+
// there's no point in msync() readonly memory
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
154
163
|
if (m_ptr) {
|
|
155
164
|
if (FlushViewOfFile(m_ptr, m_size)) {
|
|
156
165
|
if (syncFlag == MMKV_SYNC) {
|
|
@@ -168,14 +177,16 @@ bool MemoryFile::msync(SyncFlag syncFlag) {
|
|
|
168
177
|
}
|
|
169
178
|
|
|
170
179
|
bool MemoryFile::mmap() {
|
|
171
|
-
|
|
180
|
+
auto mode = m_readOnly ? PAGE_READONLY : PAGE_READWRITE;
|
|
181
|
+
m_fileMapping = CreateFileMapping(m_diskFile.getFd(), nullptr, mode, 0, 0, nullptr);
|
|
172
182
|
if (!m_fileMapping) {
|
|
173
|
-
MMKVError("fail to CreateFileMapping [%ls], %d", m_diskFile.m_path.c_str(), GetLastError());
|
|
183
|
+
MMKVError("fail to CreateFileMapping [%ls], mode %x, %d", m_diskFile.m_path.c_str(), mode, GetLastError());
|
|
174
184
|
return false;
|
|
175
185
|
} else {
|
|
176
|
-
|
|
186
|
+
auto viewMode = m_readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
|
|
187
|
+
m_ptr = (char *) MapViewOfFile(m_fileMapping, viewMode, 0, 0, 0);
|
|
177
188
|
if (!m_ptr) {
|
|
178
|
-
MMKVError("fail to mmap [%ls], %d", m_diskFile.m_path.c_str(), GetLastError());
|
|
189
|
+
MMKVError("fail to mmap [%ls], mode %x, %d", m_diskFile.m_path.c_str(), viewMode, GetLastError());
|
|
179
190
|
return false;
|
|
180
191
|
}
|
|
181
192
|
MMKVInfo("mmap to address [%p], [%ls]", m_ptr, m_diskFile.m_path.c_str());
|
|
@@ -199,7 +210,7 @@ void MemoryFile::reloadFromFile(size_t expectedCapacity) {
|
|
|
199
210
|
mmkv::getFileSize(m_diskFile.getFd(), m_size);
|
|
200
211
|
size_t expectedSize = std::max<size_t>(DEFAULT_MMAP_SIZE, roundUp<size_t>(expectedCapacity, DEFAULT_MMAP_SIZE));
|
|
201
212
|
// round up to (n * pagesize)
|
|
202
|
-
if (m_size < expectedSize || (m_size % DEFAULT_MMAP_SIZE != 0)) {
|
|
213
|
+
if (!m_readOnly && (m_size < expectedSize || (m_size % DEFAULT_MMAP_SIZE != 0))) {
|
|
203
214
|
size_t roundSize = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;;
|
|
204
215
|
roundSize = std::max<size_t>(expectedSize, roundSize);
|
|
205
216
|
truncate(roundSize);
|
|
@@ -24,13 +24,28 @@
|
|
|
24
24
|
#include <cstdlib>
|
|
25
25
|
#include <cstring>
|
|
26
26
|
#include <ctime>
|
|
27
|
+
#include "../MMKVLog.h"
|
|
28
|
+
#include "../MemoryFile.h"
|
|
29
|
+
|
|
30
|
+
namespace mmkv {
|
|
31
|
+
|
|
32
|
+
// assuming size in [1, 5]
|
|
33
|
+
uint32_t AESCrypt::randomItemSizeHolder(uint32_t size) {
|
|
34
|
+
constexpr uint32_t ItemSizeHolders[] = {0, 0x80, 0x4000, 0x200000, 0x10000000, 0};
|
|
35
|
+
auto ItemSizeHolderMin = ItemSizeHolders[size - 1];
|
|
36
|
+
auto ItemSizeHolderMax = ItemSizeHolders[size] - 1;
|
|
37
|
+
|
|
38
|
+
srand((unsigned) time(nullptr));
|
|
39
|
+
auto result = static_cast<uint32_t>(rand());
|
|
40
|
+
result = result % (ItemSizeHolderMax - ItemSizeHolderMin + 1);
|
|
41
|
+
result += ItemSizeHolderMin;
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
27
44
|
|
|
28
45
|
#ifndef MMKV_DISABLE_CRYPT
|
|
29
46
|
|
|
30
47
|
using namespace openssl;
|
|
31
48
|
|
|
32
|
-
namespace mmkv {
|
|
33
|
-
|
|
34
49
|
AESCrypt::AESCrypt(const void *key, size_t keyLength, const void *iv, size_t ivLength) {
|
|
35
50
|
if (key && keyLength > 0) {
|
|
36
51
|
memcpy(m_key, key, (keyLength > AES_KEY_LEN) ? AES_KEY_LEN : keyLength);
|
|
@@ -102,19 +117,6 @@ void AESCrypt::fillRandomIV(void *vector) {
|
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
|
|
105
|
-
// assuming size in [1, 5]
|
|
106
|
-
uint32_t AESCrypt::randomItemSizeHolder(uint32_t size) {
|
|
107
|
-
constexpr uint32_t ItemSizeHolders[] = {0, 0x80, 0x4000, 0x200000, 0x10000000, 0};
|
|
108
|
-
auto ItemSizeHolderMin = ItemSizeHolders[size - 1];
|
|
109
|
-
auto ItemSizeHolderMax = ItemSizeHolders[size] - 1;
|
|
110
|
-
|
|
111
|
-
srand((unsigned) time(nullptr));
|
|
112
|
-
auto result = static_cast<uint32_t>(rand());
|
|
113
|
-
result = result % (ItemSizeHolderMax - ItemSizeHolderMin + 1);
|
|
114
|
-
result += ItemSizeHolderMin;
|
|
115
|
-
return result;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
120
|
static inline void
|
|
119
121
|
Rollback_cfb_decrypt(const uint8_t *input, const uint8_t *output, size_t len, AES_KEY *key, AESCryptStatus &status) {
|
|
120
122
|
auto ivec = status.m_vector;
|
|
@@ -174,15 +176,8 @@ AESCrypt AESCrypt::cloneWithStatus(const AESCryptStatus &status) const {
|
|
|
174
176
|
return AESCrypt(*this, status);
|
|
175
177
|
}
|
|
176
178
|
|
|
177
|
-
} // namespace mmkv
|
|
178
|
-
|
|
179
179
|
# ifdef MMKV_DEBUG
|
|
180
180
|
|
|
181
|
-
# include "../MMKVLog.h"
|
|
182
|
-
# include "../MemoryFile.h"
|
|
183
|
-
|
|
184
|
-
namespace mmkv {
|
|
185
|
-
|
|
186
181
|
void testRandomPlaceHolder() {
|
|
187
182
|
for (uint32_t size = 1; size < 6; size++) {
|
|
188
183
|
auto holder = AESCrypt::randomItemSizeHolder(size);
|
|
@@ -272,7 +267,7 @@ void AESCrypt::testAESCrypt() {
|
|
|
272
267
|
delete[] decryptText;
|
|
273
268
|
}
|
|
274
269
|
|
|
275
|
-
} // namespace mmkv
|
|
276
|
-
|
|
277
270
|
# endif // MMKV_DEBUG
|
|
278
271
|
#endif // MMKV_DISABLE_CRYPT
|
|
272
|
+
|
|
273
|
+
} // namespace mmkv
|
package/MMKV/Core/aes/AESCrypt.h
CHANGED
package/MMKV/README.md
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
[](https://github.com/Tencent/MMKV/blob/master/LICENSE.TXT)
|
|
2
|
+
[](https://github.com/Tencent/MMKV/pulls)
|
|
3
|
+
[](https://github.com/Tencent/MMKV/releases)
|
|
4
|
+
[](https://github.com/Tencent/MMKV/wiki/home)
|
|
5
|
+
|
|
6
|
+
中文版本请参看[这里](./README_CN.md)
|
|
7
|
+
|
|
8
|
+
MMKV is an **efficient**, **small**, **easy-to-use** mobile key-value storage framework used in the WeChat application. It's currently available on **Android**, **iOS/macOS**, **Windows**, **POSIX** and **HarmonyOS NEXT**.
|
|
9
|
+
|
|
10
|
+
# MMKV for Android
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
* **Efficient**. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of Android to achieve the best performance.
|
|
15
|
+
* **Multi-Process concurrency**: MMKV supports concurrent read-read and read-write access between processes.
|
|
16
|
+
|
|
17
|
+
* **Easy-to-use**. You can use MMKV as you go. All changes are saved immediately, no `sync`, no `apply` calls needed.
|
|
18
|
+
|
|
19
|
+
* **Small**.
|
|
20
|
+
* **A handful of files**: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
|
|
21
|
+
* **About 50K in binary size**: MMKV adds about 50K per architecture on App size, and much less when zipped (APK).
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## Getting Started
|
|
25
|
+
|
|
26
|
+
### Installation Via Maven
|
|
27
|
+
Add the following lines to `build.gradle` on your app module:
|
|
28
|
+
|
|
29
|
+
```gradle
|
|
30
|
+
dependencies {
|
|
31
|
+
implementation 'com.tencent:mmkv:2.0.0'
|
|
32
|
+
// replace "2.0.0" with any available version
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
For other installation options, see [Android Setup](https://github.com/Tencent/MMKV/wiki/android_setup).
|
|
37
|
+
|
|
38
|
+
### Quick Tutorial
|
|
39
|
+
You can use MMKV as you go. All changes are saved immediately, no `sync`, no `apply` calls needed.
|
|
40
|
+
Setup MMKV on App startup, say your `Application` class, add these lines:
|
|
41
|
+
|
|
42
|
+
```Java
|
|
43
|
+
public void onCreate() {
|
|
44
|
+
super.onCreate();
|
|
45
|
+
|
|
46
|
+
String rootDir = MMKV.initialize(this);
|
|
47
|
+
System.out.println("mmkv root: " + rootDir);
|
|
48
|
+
//……
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
MMKV has a global instance, that can be used directly:
|
|
53
|
+
|
|
54
|
+
```Java
|
|
55
|
+
import com.tencent.mmkv.MMKV;
|
|
56
|
+
|
|
57
|
+
MMKV kv = MMKV.defaultMMKV();
|
|
58
|
+
|
|
59
|
+
kv.encode("bool", true);
|
|
60
|
+
boolean bValue = kv.decodeBool("bool");
|
|
61
|
+
|
|
62
|
+
kv.encode("int", Integer.MIN_VALUE);
|
|
63
|
+
int iValue = kv.decodeInt("int");
|
|
64
|
+
|
|
65
|
+
kv.encode("string", "Hello from mmkv");
|
|
66
|
+
String str = kv.decodeString("string");
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
MMKV also supports **Multi-Process Access**. Full tutorials can be found here [Android Tutorial](https://github.com/Tencent/MMKV/wiki/android_tutorial).
|
|
70
|
+
|
|
71
|
+
## Performance
|
|
72
|
+
Writing random `int` for 1000 times, we get this chart:
|
|
73
|
+

|
|
74
|
+
For more benchmark data, please refer to [our benchmark](https://github.com/Tencent/MMKV/wiki/android_benchmark).
|
|
75
|
+
|
|
76
|
+
# MMKV for iOS/macOS
|
|
77
|
+
|
|
78
|
+
## Features
|
|
79
|
+
|
|
80
|
+
* **Efficient**. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of iOS/macOS to achieve the best performance.
|
|
81
|
+
|
|
82
|
+
* **Easy-to-use**. You can use MMKV as you go, no configurations are needed. All changes are saved immediately, no `synchronize` calls are needed.
|
|
83
|
+
|
|
84
|
+
* **Small**.
|
|
85
|
+
* **A handful of files**: MMKV contains encode/decode helpers and mmap logics and nothing more. It's really tidy.
|
|
86
|
+
* **Less than 30K in binary size**: MMKV adds less than 30K per architecture on App size, and much less when zipped (IPA).
|
|
87
|
+
|
|
88
|
+
## Getting Started
|
|
89
|
+
|
|
90
|
+
### Installation Via CocoaPods:
|
|
91
|
+
1. Install [CocoaPods](https://guides.CocoaPods.org/using/getting-started.html);
|
|
92
|
+
2. Open the terminal, `cd` to your project directory, run `pod repo update` to make CocoaPods aware of the latest available MMKV versions;
|
|
93
|
+
3. Edit your Podfile, add `pod 'MMKV'` to your app target;
|
|
94
|
+
4. Run `pod install`;
|
|
95
|
+
5. Open the `.xcworkspace` file generated by CocoaPods;
|
|
96
|
+
6. Add `#import <MMKV/MMKV.h>` to your source file and we are done.
|
|
97
|
+
|
|
98
|
+
For other installation options, see [iOS/macOS Setup](https://github.com/Tencent/MMKV/wiki/iOS_setup).
|
|
99
|
+
|
|
100
|
+
### Quick Tutorial
|
|
101
|
+
You can use MMKV as you go, no configurations are needed. All changes are saved immediately, no `synchronize` calls are needed.
|
|
102
|
+
Setup MMKV on App startup, in your `-[MyApp application: didFinishLaunchingWithOptions:]`, add these lines:
|
|
103
|
+
|
|
104
|
+
```objective-c
|
|
105
|
+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
|
106
|
+
// init MMKV in the main thread
|
|
107
|
+
[MMKV initializeMMKV:nil];
|
|
108
|
+
|
|
109
|
+
//...
|
|
110
|
+
return YES;
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
MMKV has a global instance, that can be used directly:
|
|
115
|
+
|
|
116
|
+
```objective-c
|
|
117
|
+
MMKV *mmkv = [MMKV defaultMMKV];
|
|
118
|
+
|
|
119
|
+
[mmkv setBool:YES forKey:@"bool"];
|
|
120
|
+
BOOL bValue = [mmkv getBoolForKey:@"bool"];
|
|
121
|
+
|
|
122
|
+
[mmkv setInt32:-1024 forKey:@"int32"];
|
|
123
|
+
int32_t iValue = [mmkv getInt32ForKey:@"int32"];
|
|
124
|
+
|
|
125
|
+
[mmkv setString:@"hello, mmkv" forKey:@"string"];
|
|
126
|
+
NSString *str = [mmkv getStringForKey:@"string"];
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
MMKV also supports **Multi-Process Access**. Full tutorials can be found [here](https://github.com/Tencent/MMKV/wiki/iOS_tutorial).
|
|
130
|
+
|
|
131
|
+
## Performance
|
|
132
|
+
Writing random `int` for 10000 times, we get this chart:
|
|
133
|
+

|
|
134
|
+
For more benchmark data, please refer to [our benchmark](https://github.com/Tencent/MMKV/wiki/iOS_benchmark).
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# MMKV for Windows
|
|
138
|
+
|
|
139
|
+
## Features
|
|
140
|
+
|
|
141
|
+
* **Efficient**. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of Windows to achieve the best performance.
|
|
142
|
+
* **Multi-Process concurrency**: MMKV supports concurrent read-read and read-write access between processes.
|
|
143
|
+
|
|
144
|
+
* **Easy-to-use**. You can use MMKV as you go. All changes are saved immediately, no `save`, no `sync` calls are needed.
|
|
145
|
+
|
|
146
|
+
* **Small**.
|
|
147
|
+
* **A handful of files**: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
|
|
148
|
+
* **About 10K in binary size**: MMKV adds about 10K on application size, and much less when zipped.
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
## Getting Started
|
|
152
|
+
|
|
153
|
+
### Installation Via Source
|
|
154
|
+
1. Getting source code from git repository:
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
git clone https://github.com/Tencent/MMKV.git
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
2. Add `Core/core.vcxproj` to your solution;
|
|
161
|
+
3. Add `MMKV` project to your project's dependencies;
|
|
162
|
+
4. Add `$(OutDir)include` to your project's `C/C++` -> `General` -> `Additional Include Directories`;
|
|
163
|
+
5. Add `$(OutDir)` to your project's `Linker` -> `General` -> `Additional Library Directories`;
|
|
164
|
+
6. Add `mmkv.lib` to your project's `Linker` -> `Input` -> `Additional Dependencies`;
|
|
165
|
+
7. Add `#include <MMKV/MMKV.h>` to your source file and we are done.
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
note:
|
|
169
|
+
|
|
170
|
+
1. MMKV is compiled with `MT/MTd` runtime by default. If your project uses `MD/MDd`, you should change MMKV's setting to match your project's (`C/C++` -> `Code Generation` -> `Runtime Library`), or vice versa.
|
|
171
|
+
2. MMKV is developed with Visual Studio 2017, change the `Platform Toolset` if you use a different version of Visual Studio.
|
|
172
|
+
|
|
173
|
+
For other installation options, see [Windows Setup](https://github.com/Tencent/MMKV/wiki/windows_setup).
|
|
174
|
+
|
|
175
|
+
### Quick Tutorial
|
|
176
|
+
You can use MMKV as you go. All changes are saved immediately, no `sync`, no `save` calls needed.
|
|
177
|
+
Setup MMKV on App startup, say in your `main()`, add these lines:
|
|
178
|
+
|
|
179
|
+
```C++
|
|
180
|
+
#include <MMKV/MMKV.h>
|
|
181
|
+
|
|
182
|
+
int main() {
|
|
183
|
+
std::wstring rootDir = getYourAppDocumentDir();
|
|
184
|
+
MMKV::initializeMMKV(rootDir);
|
|
185
|
+
//...
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
MMKV has a global instance, that can be used directly:
|
|
190
|
+
|
|
191
|
+
```C++
|
|
192
|
+
auto mmkv = MMKV::defaultMMKV();
|
|
193
|
+
|
|
194
|
+
mmkv->set(true, "bool");
|
|
195
|
+
std::cout << "bool = " << mmkv->getBool("bool") << std::endl;
|
|
196
|
+
|
|
197
|
+
mmkv->set(1024, "int32");
|
|
198
|
+
std::cout << "int32 = " << mmkv->getInt32("int32") << std::endl;
|
|
199
|
+
|
|
200
|
+
mmkv->set("Hello, MMKV for Windows", "string");
|
|
201
|
+
std::string result;
|
|
202
|
+
mmkv->getString("string", result);
|
|
203
|
+
std::cout << "string = " << result << std::endl;
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
MMKV also supports **Multi-Process Access**. Full tutorials can be found here [Windows Tutorial](https://github.com/Tencent/MMKV/wiki/windows_tutorial).
|
|
207
|
+
|
|
208
|
+
# MMKV for POSIX
|
|
209
|
+
|
|
210
|
+
## Features
|
|
211
|
+
|
|
212
|
+
* **Efficient**. MMKV uses mmap to keep memory synced with files, and protobuf to encode/decode values, making the most of POSIX to achieve the best performance.
|
|
213
|
+
* **Multi-Process concurrency**: MMKV supports concurrent read-read and read-write access between processes.
|
|
214
|
+
|
|
215
|
+
* **Easy-to-use**. You can use MMKV as you go. All changes are saved immediately, no `save`, no `sync` calls are needed.
|
|
216
|
+
|
|
217
|
+
* **Small**.
|
|
218
|
+
* **A handful of files**: MMKV contains process locks, encode/decode helpers and mmap logics, and nothing more. It's really tidy.
|
|
219
|
+
* **About 7K in binary size**: MMKV adds about 7K on application size, and much less when zipped.
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
## Getting Started
|
|
223
|
+
|
|
224
|
+
### Installation Via CMake
|
|
225
|
+
1. Getting source code from the git repository:
|
|
226
|
+
|
|
227
|
+
```
|
|
228
|
+
git clone https://github.com/Tencent/MMKV.git
|
|
229
|
+
```
|
|
230
|
+
2. Edit your `CMakeLists.txt`, add those lines:
|
|
231
|
+
|
|
232
|
+
```cmake
|
|
233
|
+
add_subdirectory(mmkv/POSIX/src mmkv)
|
|
234
|
+
target_link_libraries(MyApp
|
|
235
|
+
mmkv)
|
|
236
|
+
```
|
|
237
|
+
3. Add `#include "MMKV.h"` to your source file and we are done.
|
|
238
|
+
|
|
239
|
+
For other installation options, see [POSIX Setup](https://github.com/Tencent/MMKV/wiki/posix_setup).
|
|
240
|
+
|
|
241
|
+
### Quick Tutorial
|
|
242
|
+
You can use MMKV as you go. All changes are saved immediately, no `sync`, no `save` calls needed.
|
|
243
|
+
Setup MMKV on App startup, say in your `main()`, add these lines:
|
|
244
|
+
|
|
245
|
+
```C++
|
|
246
|
+
#include "MMKV.h"
|
|
247
|
+
|
|
248
|
+
int main() {
|
|
249
|
+
std::string rootDir = getYourAppDocumentDir();
|
|
250
|
+
MMKV::initializeMMKV(rootDir);
|
|
251
|
+
//...
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
MMKV has a global instance, that can be used directly:
|
|
256
|
+
|
|
257
|
+
```C++
|
|
258
|
+
auto mmkv = MMKV::defaultMMKV();
|
|
259
|
+
|
|
260
|
+
mmkv->set(true, "bool");
|
|
261
|
+
std::cout << "bool = " << mmkv->getBool("bool") << std::endl;
|
|
262
|
+
|
|
263
|
+
mmkv->set(1024, "int32");
|
|
264
|
+
std::cout << "int32 = " << mmkv->getInt32("int32") << std::endl;
|
|
265
|
+
|
|
266
|
+
mmkv->set("Hello, MMKV for Windows", "string");
|
|
267
|
+
std::string result;
|
|
268
|
+
mmkv->getString("string", result);
|
|
269
|
+
std::cout << "string = " << result << std::endl;
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
MMKV also supports **Multi-Process Access**. Full tutorials can be found here [POSIX Tutorial](https://github.com/Tencent/MMKV/wiki/posix_tutorial).
|
|
273
|
+
|
|
274
|
+
# MMKV for HarmonyOS NEXT
|
|
275
|
+
|
|
276
|
+
## Features
|
|
277
|
+
|
|
278
|
+
* **Efficient**. MMKV uses mmap to keep memory synced with file, and protobuf to encode/decode values, making the most of native platform to achieve best performance.
|
|
279
|
+
* **Multi-Process concurrency**: MMKV supports concurrent read-read and read-write access between processes.
|
|
280
|
+
|
|
281
|
+
* **Easy-to-use**. You can use MMKV as you go. All changes are saved immediately, no `sync`, no `flush` calls needed.
|
|
282
|
+
|
|
283
|
+
* **Small**.
|
|
284
|
+
* **A handful of files**: MMKV contains process locks, encode/decode helpers and mmap logics and nothing more. It's really tidy.
|
|
285
|
+
* **About 600K in binary size**: MMKV adds about 600K per architecture on App size, and much less when zipped (HAR/HAP).
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
## Getting Started
|
|
289
|
+
### Installation via OHPM:
|
|
290
|
+
|
|
291
|
+
```bash
|
|
292
|
+
ohpm install @tencent/mmkv
|
|
293
|
+
```
|
|
294
|
+
### Quick Tutorial
|
|
295
|
+
You can use MMKV as you go. All changes are saved immediately, no `sync`, no `apply` calls needed.
|
|
296
|
+
Setup MMKV on App startup, say your `EntryAbility.onCreate()` function, add these lines:
|
|
297
|
+
|
|
298
|
+
```js
|
|
299
|
+
import { MMKV } from '@tencent/mmkv';
|
|
300
|
+
|
|
301
|
+
export default class EntryAbility extends UIAbility {
|
|
302
|
+
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
|
303
|
+
let appCtx = this.context.getApplicationContext();
|
|
304
|
+
let mmkvRootDir = MMKV.initialize(appCtx);
|
|
305
|
+
console.info('mmkv rootDir: ', mmkvRootDir);
|
|
306
|
+
……
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
MMKV has a global instance, that can be used directly:
|
|
311
|
+
|
|
312
|
+
```js
|
|
313
|
+
import { MMKV } from '@tencent/mmkv';
|
|
314
|
+
|
|
315
|
+
let mmkv = MMKV.defaultMMKV();
|
|
316
|
+
mmkv.encodeBool('bool', true);
|
|
317
|
+
console.info('bool = ', mmkv.decodeBool('bool'));
|
|
318
|
+
|
|
319
|
+
mmkv.encodeInt32('int32', Math.pow(2, 31) - 1);
|
|
320
|
+
console.info('max int32 = ', mmkv.decodeInt32('int32'));
|
|
321
|
+
|
|
322
|
+
mmkv.encodeInt64('int', BigInt(2**63) - BigInt(1));
|
|
323
|
+
console.info('max int64 = ', mmkv.decodeInt64('int'));
|
|
324
|
+
|
|
325
|
+
let str: string = 'Hello OpenHarmony from MMKV';
|
|
326
|
+
mmkv.encodeString('string', str);
|
|
327
|
+
console.info('string = ', mmkv.decodeString('string'));
|
|
328
|
+
|
|
329
|
+
let arrayBuffer: ArrayBuffer = StringToArrayBuffer('Hello OpenHarmony from MMKV with bytes');
|
|
330
|
+
mmkv.encodeBytes('bytes', arrayBuffer);
|
|
331
|
+
let bytes = mmkv.decodeBytes('bytes');
|
|
332
|
+
console.info('bytes = ', ArrayBufferToString(bytes));
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
As you can see, MMKV is quite easy to use.
|
|
336
|
+
For the full documentation, see [HarmonyOS NEXT Tutorial](https://github.com/Tencent/MMKV/wiki/ohos_setup).
|
|
337
|
+
|
|
338
|
+
## License
|
|
339
|
+
MMKV is published under the BSD 3-Clause license. For details check out the [LICENSE.TXT](./LICENSE.TXT).
|
|
340
|
+
|
|
341
|
+
## Change Log
|
|
342
|
+
Check out the [CHANGELOG.md](./CHANGELOG.md) for details of change history.
|
|
343
|
+
|
|
344
|
+
## Contributing
|
|
345
|
+
|
|
346
|
+
If you are interested in contributing, check out the [CONTRIBUTING.md](./CONTRIBUTING.md), also join our [Tencent OpenSource Plan](https://opensource.tencent.com/contribution).
|
|
347
|
+
|
|
348
|
+
To give clarity of what is expected of our members, MMKV has adopted the code of conduct defined by the Contributor Covenant, which is widely used. And we think it articulates our values well. For more, check out the [Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
349
|
+
|
|
350
|
+
## FAQ & Feedback
|
|
351
|
+
Check out the [FAQ](https://github.com/Tencent/MMKV/wiki/FAQ) first. Should there be any questions, don't hesitate to create [issues](https://github.com/Tencent/MMKV/issues).
|
|
352
|
+
|
|
353
|
+
## Personal Information Protection Rules
|
|
354
|
+
User privacy is taken very seriously: MMKV does not obtain, collect or upload any personal information. Please refer to the [MMKV SDK Personal Information Protection Rules](https://support.weixin.qq.com/cgi-bin/mmsupportacctnodeweb-bin/pages/aY5BAtRiO1BpoHxo) for details.
|
package/README.md
ADDED
package/cpp/MmkvHostObject.cpp
CHANGED
|
@@ -26,6 +26,10 @@ MmkvHostObject::MmkvHostObject(const facebook::react::MMKVConfig& config) {
|
|
|
26
26
|
std::string* pathPtr = path.size() > 0 ? &path : nullptr;
|
|
27
27
|
std::string* encryptionKeyPtr = encryptionKey.size() > 0 ? &encryptionKey : nullptr;
|
|
28
28
|
MMKVMode mode = getMMKVMode(config);
|
|
29
|
+
if (config.readOnly.has_value() && config.readOnly.value()) {
|
|
30
|
+
MmkvLogger::log("RNMMKV", "Instance is read-only!");
|
|
31
|
+
mode = mode | MMKVMode::MMKV_READ_ONLY;
|
|
32
|
+
}
|
|
29
33
|
|
|
30
34
|
#ifdef __APPLE__
|
|
31
35
|
instance = MMKV::mmkvWithID(config.id, mode, encryptionKeyPtr, pathPtr);
|
|
@@ -62,7 +66,7 @@ MmkvHostObject::~MmkvHostObject() {
|
|
|
62
66
|
std::vector<jsi::PropNameID> MmkvHostObject::getPropertyNames(jsi::Runtime& rt) {
|
|
63
67
|
return jsi::PropNameID::names(rt, "set", "getBoolean", "getBuffer", "getString", "getNumber",
|
|
64
68
|
"contains", "delete", "getAllKeys", "deleteAll", "recrypt", "trim",
|
|
65
|
-
"size");
|
|
69
|
+
"size", "isReadOnly");
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
MMKVMode MmkvHostObject::getMMKVMode(const facebook::react::MMKVConfig& config) {
|
|
@@ -130,7 +134,12 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
130
134
|
}
|
|
131
135
|
|
|
132
136
|
if (!successful) [[unlikely]] {
|
|
133
|
-
|
|
137
|
+
if (instance->isReadOnly()) {
|
|
138
|
+
throw jsi::JSError(runtime,
|
|
139
|
+
"Failed to set " + keyName + "! This instance is read-only!");
|
|
140
|
+
} else {
|
|
141
|
+
throw jsi::JSError(runtime, "Failed to set " + keyName + "!");
|
|
142
|
+
}
|
|
134
143
|
}
|
|
135
144
|
|
|
136
145
|
return jsi::Value::undefined();
|
|
@@ -310,7 +319,7 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
310
319
|
}
|
|
311
320
|
|
|
312
321
|
if (!successful) [[unlikely]] {
|
|
313
|
-
throw
|
|
322
|
+
throw jsi::JSError(runtime, "Failed to recrypt MMKV instance!");
|
|
314
323
|
}
|
|
315
324
|
|
|
316
325
|
return jsi::Value::undefined();
|
|
@@ -336,5 +345,11 @@ jsi::Value MmkvHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pro
|
|
|
336
345
|
return jsi::Value(static_cast<int>(size));
|
|
337
346
|
}
|
|
338
347
|
|
|
348
|
+
if (propName == "isReadOnly") {
|
|
349
|
+
// MMKV.isReadOnly
|
|
350
|
+
bool isReadOnly = instance->isReadOnly();
|
|
351
|
+
return jsi::Value(isReadOnly);
|
|
352
|
+
}
|
|
353
|
+
|
|
339
354
|
return jsi::Value::undefined();
|
|
340
355
|
}
|
package/cpp/NativeMmkvModule.h
CHANGED
|
@@ -22,7 +22,7 @@ namespace facebook::react {
|
|
|
22
22
|
// The MMKVConfiguration type from JS
|
|
23
23
|
using MMKVConfig =
|
|
24
24
|
NativeMmkvConfiguration<std::string, std::optional<std::string>, std::optional<std::string>,
|
|
25
|
-
std::optional<NativeMmkvMode>>;
|
|
25
|
+
std::optional<NativeMmkvMode>, std::optional<bool>>;
|
|
26
26
|
template <> struct Bridging<MMKVConfig> : NativeMmkvConfigurationBridging<MMKVConfig> {};
|
|
27
27
|
|
|
28
28
|
// The TurboModule itself
|