Skip to content

Commit

Permalink
Merge pull request #152 from Tencent/dev
Browse files Browse the repository at this point in the history
v1.0.15
  • Loading branch information
lingol authored Dec 14, 2018
2 parents bcd8234 + 2b7aa79 commit d2ba9f0
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Android/MMKV/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ org.gradle.jvmargs=-Xmx1536m
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME_PREFIX=1.0.14
VERSION_NAME_PREFIX=1.0.15
#VERSION_NAME_SUFFIX=-SNAPSHOT
VERSION_NAME_SUFFIX=
2 changes: 1 addition & 1 deletion Android/MMKV/mmkv/src/main/cpp/MMKV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ bool MMKV::ensureMemorySize(size_t newSize) {
return false;
}
} else {
size_t avgItemSize = lenNeeded / m_dic.size();
size_t avgItemSize = lenNeeded / std::max<size_t>(1, m_dic.size());
size_t futureUsage = avgItemSize * std::max<size_t>(8, (m_dic.size() + 1) / 2);
// 1. no space for a full rewrite, double it
// 2. or space is not large enough for future usage, double it to avoid frequently full rewrite
Expand Down
35 changes: 23 additions & 12 deletions Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ static jclass g_cls = nullptr;
static jfieldID g_fileID = nullptr;
static jmethodID g_callbackOnCRCFailID = nullptr;
static jmethodID g_callbackOnFileLengthErrorID = nullptr;
static JNIEnv *g_currentEnv = nullptr;
static JavaVM *g_currentJVM = nullptr;

extern "C" JNIEXPORT JNICALL jint JNI_OnLoad(JavaVM *vm, void *reserved) {
g_currentJVM = vm;
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
Expand Down Expand Up @@ -138,28 +139,41 @@ static jobjectArray vector2jarray(JNIEnv *env, const vector<string> &arr) {
return nullptr;
}

static JNIEnv *getCurrentEnv() {
if (g_currentJVM) {
JNIEnv *currentEnv = nullptr;
auto ret = g_currentJVM->GetEnv(reinterpret_cast<void **>(&currentEnv), JNI_VERSION_1_6);
if (ret == JNI_OK) {
return currentEnv;
} else {
MMKVError("fail to get current JNIEnv: %d", ret);
}
}
return nullptr;
}

MMKVRecoverStrategic onMMKVCRCCheckFail(const std::string &mmapID) {
if (g_currentEnv && g_callbackOnCRCFailID) {
jstring str = string2jstring(g_currentEnv, mmapID);
auto strategic = g_currentEnv->CallStaticIntMethod(g_cls, g_callbackOnCRCFailID, str);
auto currentEnv = getCurrentEnv();
if (g_currentJVM && g_callbackOnCRCFailID) {
jstring str = string2jstring(currentEnv, mmapID);
auto strategic = currentEnv->CallStaticIntMethod(g_cls, g_callbackOnCRCFailID, str);
return static_cast<MMKVRecoverStrategic>(strategic);
}
return OnErrorDiscard;
}

MMKVRecoverStrategic onMMKVFileLengthError(const std::string &mmapID) {
if (g_currentEnv && g_callbackOnFileLengthErrorID) {
jstring str = string2jstring(g_currentEnv, mmapID);
auto strategic =
g_currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str);
auto currentEnv = getCurrentEnv();
if (currentEnv && g_callbackOnFileLengthErrorID) {
jstring str = string2jstring(currentEnv, mmapID);
auto strategic = currentEnv->CallStaticIntMethod(g_cls, g_callbackOnFileLengthErrorID, str);
return static_cast<MMKVRecoverStrategic>(strategic);
}
return OnErrorDiscard;
}

extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithID(
JNIEnv *env, jobject obj, jstring mmapID, jint mode, jstring cryptKey) {
g_currentEnv = env;
MMKV *kv = nullptr;
if (!mmapID) {
return (jlong) kv;
Expand All @@ -181,7 +195,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithID(

extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithIDAndSize(
JNIEnv *env, jobject obj, jstring mmapID, jint size, jint mode, jstring cryptKey) {
g_currentEnv = env;
MMKV *kv = nullptr;
if (!mmapID || size < 0) {
return (jlong) kv;
Expand All @@ -204,7 +217,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getDefaultMMKV(JNI
jobject obj,
jint mode,
jstring cryptKey) {
g_currentEnv = env;
MMKV *kv = nullptr;

if (cryptKey != nullptr) {
Expand All @@ -222,7 +234,6 @@ extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getDefaultMMKV(JNI

extern "C" JNIEXPORT JNICALL jlong Java_com_tencent_mmkv_MMKV_getMMKVWithAshmemFD(
JNIEnv *env, jobject obj, jstring mmapID, jint fd, jint metaFD, jstring cryptKey) {
g_currentEnv = env;
MMKV *kv = nullptr;
if (!mmapID || fd < 0 || metaFD < 0) {
return (jlong) kv;
Expand Down
3 changes: 1 addition & 2 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass) {
}

@SuppressWarnings("unchecked")
public <T extends Parcelable>
T decodeParcelable(String key, Class<T> tClass, T defaultValue) {
public <T extends Parcelable> T decodeParcelable(String key, Class<T> tClass, T defaultValue) {
if (tClass == null) {
return defaultValue;
}
Expand Down
4 changes: 2 additions & 2 deletions Android/MMKV/mmkvdemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// implementation project(':mmkv')
implementation 'com.tencent:mmkv:1.0.14'
// implementation 'com.tencent:mmkv-static:1.0.14'
implementation 'com.tencent:mmkv:1.0.15'
// implementation 'com.tencent:mmkv-static:1.0.15'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public void onClick(View v) {
}
});

//testHolderForMultiThread();

//prepareInterProcessAshmem();
//prepareInterProcessAshmemByContentProvider(KEY_1);

Expand Down Expand Up @@ -330,6 +332,29 @@ private void testInterProcessReKey() {
prepareInterProcessAshmemByContentProvider(KEY_2);
}

private void testHolderForMultiThread() {
final int COUNT = 1;
final int THREAD_COUNT = 1;
final String ID = "Hotel";
final String KEY = "California";
final String VALUE = "You can checkout any time you like, but you can never leave.";

final MMKV mmkv = MMKV.mmkvWithID(ID);
Runnable task = new Runnable() {
public void run() {
for (int i = 0; i < COUNT; ++i) {
mmkv.putString(KEY, VALUE);
mmkv.getString(KEY, null);
mmkv.remove(KEY);
}
}
};

for (int i = 0; i < THREAD_COUNT; ++i) {
new Thread(task, "MMKV-" + i).start();
}
}

@Override
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
return MMKVRecoverStrategic.OnErrorRecover;
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# MMKV Change Log

## v1.0.15 / 2018-12-13
### iOS / macOS
What's new

* Storing **NSString/NSData/NSDate** directly by calling `setString`/`getSring`, `setData`/`getData`, `setDate`/getDate`.
* Fix a potencial crash due to divided by zero.


### Android
What's new

* Fix a stack overflow crash due to the **callback** feature introduced by v1.0.13.
* Fix a potencial crash due to divided by zero.

### Win32
MMKV for Win32 in under construction. Hopefully will come out in next release. For those who are interested, check out branch `dev_win32` for the latest development.

## v1.0.14 / 2018-11-30
### iOS / macOS
What's new
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.txt)
[![license](https://img.shields.io/badge/license-BSD_3-brightgreen.svg?style=flat)](https://github.com/Tencent/MMKV/blob/master/LICENSE.TXT)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/MMKV/pulls)
[![Release Version](https://img.shields.io/badge/release-1.0.14-brightgreen.svg)](https://github.com/Tencent/MMKV/releases)
[![Release Version](https://img.shields.io/badge/release-1.0.15-brightgreen.svg)](https://github.com/Tencent/MMKV/releases)
[![Platform](https://img.shields.io/badge/Platform-%20iOS%20%7C%20Android-brightgreen.svg)](https://github.com/Tencent/MMKV/wiki/home)

中文版本请参看[这里](./readme_cn.md)
Expand Down Expand Up @@ -75,8 +75,8 @@ Add the following lines to `build.gradle` on your app module:
```gradle
dependencies {
implementation 'com.tencent:mmkv:1.0.14'
// replace "1.0.14" with any available version
implementation 'com.tencent:mmkv:1.0.15'
// replace "1.0.15" with any available version
}
```

Expand Down
18 changes: 16 additions & 2 deletions iOS/MMKV/MMKV/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)reKey:(nullable NSData *)newKey NS_SWIFT_NAME(reset(cryptKey:));
- (nullable NSData *)cryptKey;

// object: NSString/NSData/NSDate/id<NSCoding>
- (BOOL)setObject:(nullable id)object forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));
- (BOOL)setObject:(nullable NSObject<NSCoding> *)object forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setBool:(BOOL)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

Expand All @@ -56,6 +55,12 @@ NS_ASSUME_NONNULL_BEGIN

- (BOOL)setDouble:(double)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setString:(NSString *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setDate:(NSDate *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setData:(NSData *)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (nullable id)getObjectOfClass:(Class)cls forKey:(NSString *)key NS_SWIFT_NAME(object(of:forKey:));

- (BOOL)getBoolForKey:(NSString *)key __attribute__((swift_name("bool(forKey:)")));
Expand All @@ -79,6 +84,15 @@ NS_ASSUME_NONNULL_BEGIN
- (double)getDoubleForKey:(NSString *)key NS_SWIFT_NAME(double(forKey:));
- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue NS_SWIFT_NAME(double(forKey:defaultValue:));

- (nullable NSString *)getStringForKey:(NSString *)key NS_SWIFT_NAME(string(forKey:));
- (nullable NSString *)getStringForKey:(NSString *)key defaultValue:(nullable NSString *)defaultValue NS_SWIFT_NAME(string(forKey:defaultValue:));

- (nullable NSDate *)getDateForKey:(NSString *)key NS_SWIFT_NAME(date(forKey:));
- (nullable NSDate *)getDateForKey:(NSString *)key defaultValue:(nullable NSDate *)defaultValue NS_SWIFT_NAME(date(forKey:defaultValue:));

- (nullable NSData *)getDataForKey:(NSString *)key NS_SWIFT_NAME(data(forKey:));
- (nullable NSData *)getDataForKey:(NSString *)key defaultValue:(nullable NSData *)defaultValue NS_SWIFT_NAME(data(forKey:defaultValue:));

- (BOOL)containsKey:(NSString *)key NS_SWIFT_NAME(contains(key:));

- (size_t)count;
Expand Down
Loading

0 comments on commit d2ba9f0

Please sign in to comment.