Skip to content

Commit

Permalink
Merge pull request #709 from Tencent/dev
Browse files Browse the repository at this point in the history
for v1.2.10
  • Loading branch information
lingol authored Jun 25, 2021
2 parents 00848d5 + 853a256 commit b6e1fc3
Show file tree
Hide file tree
Showing 34 changed files with 649 additions and 225 deletions.
2 changes: 1 addition & 1 deletion Android/MMKV/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.5.0'
ext.kotlin_version = '1.5.10'

repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion Android/MMKV/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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.2.9
VERSION_NAME_PREFIX=1.2.10
#VERSION_NAME_SUFFIX=-SNAPSHOT
VERSION_NAME_SUFFIX=
4 changes: 0 additions & 4 deletions Android/MMKV/mmkv/src/main/cpp/flutter-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ MMKV_EXPORT void mmkvInitialize(const char *rootDir, int32_t logLevel) {
MMKV::initializeMMKV(rootDir, (MMKVLogLevel) logLevel);
}

MMKV_EXPORT void onExit() {
MMKV::onExit();
}

MMKV_EXPORT void *getMMKVWithID(const char *mmapID, int32_t mode, const char *cryptKey, const char *rootPath) {
MMKV *kv = nullptr;
if (!mmapID) {
Expand Down
10 changes: 10 additions & 0 deletions Android/MMKV/mmkv/src/main/cpp/native-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ MMKV_JNI jlong totalSize(JNIEnv *env, jobject instance, jlong handle) {
return 0;
}

MMKV_JNI jlong actualSize(JNIEnv *env, jobject instance, jlong handle) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (kv) {
jlong size = kv->actualSize();
return size;
}
return 0;
}

MMKV_JNI void removeValueForKey(JNIEnv *env, jobject instance, jlong handle, jstring oKey) {
MMKV *kv = reinterpret_cast<MMKV *>(handle);
if (kv && oKey) {
Expand Down Expand Up @@ -829,6 +838,7 @@ static JNINativeMethod g_methods[] = {
{"containsKey", "(JLjava/lang/String;)Z", (void *) mmkv::containsKey},
{"count", "(J)J", (void *) mmkv::count},
{"totalSize", "(J)J", (void *) mmkv::totalSize},
{"actualSize", "(J)J", (void *) mmkv::actualSize},
{"removeValueForKey", "(JLjava/lang/String;)V", (void *) mmkv::removeValueForKey},
{"valueSize", "(JLjava/lang/String;Z)I", (void *) mmkv::valueSize},
{"setLogLevel", "(I)V", (void *) mmkv::setLogLevel},
Expand Down
456 changes: 383 additions & 73 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKV.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.tencent.mmkv;

/**
* Inter-process content change notification.
* Triggered by any method call, such as getXXX() or setXXX() or {@link MMKV#checkContentChangedByOuterProcess()}.
*/
public interface MMKVContentChangeNotification {

// content change notification of other process
// trigger by getXXX() or setXXX() or checkContentChangedByOuterProcess()
/**
* Inter-process content change notification.
* Triggered by any method call, such as getXXX() or setXXX() or {@link MMKV#checkContentChangedByOuterProcess()}.
* @param mmapID The unique ID of the changed MMKV instance.
*/
void onContentChangedByOuterProcess(String mmapID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* A helper class for MMKV based on Anonymous Shared Memory. {@link MMKV#mmkvWithAshmemID}
*/
public class MMKVContentProvider extends ContentProvider {

static protected final String KEY = "KEY";
Expand All @@ -60,16 +63,13 @@ static protected Uri contentUri(Context context) {
return MMKVContentProvider.gUri;
}

private Bundle mmkvFromAshmemID(String ashmemID, int size, int mode, String cryptKey) {
private Bundle mmkvFromAshmemID(String ashmemID, int size, int mode, String cryptKey) throws RuntimeException {
MMKV mmkv = MMKV.mmkvWithAshmemID(getContext(), ashmemID, size, mode, cryptKey);
if (mmkv != null) {
ParcelableMMKV parcelableMMKV = new ParcelableMMKV(mmkv);
Log.i("MMKV", ashmemID + " fd = " + mmkv.ashmemFD() + ", meta fd = " + mmkv.ashmemMetaFD());
Bundle result = new Bundle();
result.putParcelable(MMKVContentProvider.KEY, parcelableMMKV);
return result;
}
return null;
ParcelableMMKV parcelableMMKV = new ParcelableMMKV(mmkv);
Log.i("MMKV", ashmemID + " fd = " + mmkv.ashmemFD() + ", meta fd = " + mmkv.ashmemMetaFD());
Bundle result = new Bundle();
result.putParcelable(MMKVContentProvider.KEY, parcelableMMKV);
return result;
}

private static String queryAuthority(Context context) {
Expand Down Expand Up @@ -129,7 +129,12 @@ public Bundle call(@NonNull String method, @Nullable String mmapID, @Nullable Bu
int size = extras.getInt(MMKVContentProvider.KEY_SIZE);
int mode = extras.getInt(MMKVContentProvider.KEY_MODE);
String cryptKey = extras.getString(MMKVContentProvider.KEY_CRYPT);
return mmkvFromAshmemID(mmapID, size, mode, cryptKey);
try {
return mmkvFromAshmemID(mmapID, size, mode, cryptKey);
} catch (Exception e) {
Log.e("MMKV", e.getMessage());
return null;
}
}
}
return null;
Expand Down
32 changes: 25 additions & 7 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,37 @@

package com.tencent.mmkv;

// callback is called on the operating thread of the MMKV instance
/**
* Callback handler for MMKV.
* Callback is called on the operating thread of the MMKV instance.
*/
public interface MMKVHandler {
// by default MMKV will discard all data on crc32-check failure
// return `OnErrorRecover` to recover any data on the file
/**
* By default MMKV will discard all data on crc32-check failure. {@link MMKVRecoverStrategic#OnErrorDiscard}
* @param mmapID The unique ID of the MMKV instance.
* @return Return {@link MMKVRecoverStrategic#OnErrorRecover} to recover any data on the file.
*/
MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID);

// by default MMKV will discard all data on file length mismatch
// return `OnErrorRecover` to recover any data on the file
/**
* By default MMKV will discard all data on file length mismatch. {@link MMKVRecoverStrategic#OnErrorDiscard}
* @param mmapID The unique ID of the MMKV instance.
* @return Return {@link MMKVRecoverStrategic#OnErrorRecover} to recover any data on the file.
*/
MMKVRecoverStrategic onMMKVFileLengthError(String mmapID);

// return false if you don't want log redirecting
/**
* @return Return False if you don't want log redirecting.
*/
boolean wantLogRedirecting();

// log redirecting
/**
* Log Redirecting.
* @param level The level of this log.
* @param file The file name of this log.
* @param line The line of code of this log.
* @param function The function name of this log.
* @param message The content of this log.
*/
void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message);
}
29 changes: 26 additions & 3 deletions Android/MMKV/mmkv/src/main/java/com/tencent/mmkv/MMKVLogLevel.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
package com.tencent.mmkv;

/**
* The levels of MMKV log.
*/
public enum MMKVLogLevel {
LevelDebug, // not available for release/production build
LevelInfo, // default level
/**
* Debug level. Not available for release/production build.
*/
LevelDebug,

/**
* Info level. The default level.
*/
LevelInfo,

/**
* Warning level.
*/
LevelWarning,

/**
* Error level.
*/
LevelError,
LevelNone // special level used to disable all log messages

/**
* Special level for disabling all logging.
* It's highly NOT suggested to turn off logging. Makes it hard to diagnose online/production bugs.
*/
LevelNone
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@

package com.tencent.mmkv;

/**
* The recover strategic of MMKV on errors. {@link MMKV#registerHandler}
*/
public enum MMKVRecoverStrategic {
/**
* The default strategic is to discard everything on errors.
*/
OnErrorDiscard,

/**
* The recover strategic will try to recover as much data as possible.
*/
OnErrorRecover,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.tencent.mmkv;

/**
* A native memory wrapper, whose underlying memory can be passed to another JNI method directly.
* Avoiding unnecessary JNI boxing & unboxing.
* Must be destroy manually {@link MMKV#destroyNativeBuffer}.
*/
public final class NativeBuffer {
public long pointer;
public int size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import android.os.Parcelable;
import java.io.IOException;

/**
* A helper class for MMKV based on Anonymous Shared Memory. {@link MMKV#mmkvWithAshmemID}
*/
public final class ParcelableMMKV implements Parcelable {
private final String mmapID;
private int ashmemFD = -1;
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 @@ -75,8 +75,8 @@ repositories {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':mmkv')
// implementation 'com.tencent:mmkv:1.2.9'
// implementation 'com.tencent:mmkv-static:1.2.9'
// implementation 'com.tencent:mmkv:1.2.10'
// implementation 'com.tencent:mmkv-static:1.2.10'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,13 @@ public class AshmemMMKVGetter extends IAshmemMMKV.Stub {
private AshmemMMKVGetter() {
// 1M, ashmem cannot change size after opened
final String id = "tetAshmemMMKV";
m_ashmemMMKV = MMKV.mmkvWithAshmemID(BenchMarkBaseService.this, id, AshmemMMKV_Size,
MMKV.MULTI_PROCESS_MODE, CryptKey);
m_ashmemMMKV.encode("bool", true);
try {
m_ashmemMMKV = MMKV.mmkvWithAshmemID(BenchMarkBaseService.this, id, AshmemMMKV_Size,
MMKV.MULTI_PROCESS_MODE, CryptKey);
m_ashmemMMKV.encode("bool", true);
} catch (Exception e) {
Log.e("MMKV", e.getMessage());
}
}

public ParcelableMMKV GetAshmemMMKV() {
Expand All @@ -346,6 +350,10 @@ public IBinder onBind(Intent intent) {
protected void prepareAshmemMMKVByCP() {
// it's ok for other process not knowing cryptKey
final String cryptKey = null;
m_ashmemMMKV = MMKV.mmkvWithAshmemID(this, AshmemMMKV_ID, AshmemMMKV_Size, MMKV.MULTI_PROCESS_MODE, cryptKey);
try {
m_ashmemMMKV = MMKV.mmkvWithAshmemID(this, AshmemMMKV_ID, AshmemMMKV_Size, MMKV.MULTI_PROCESS_MODE, cryptKey);
} catch (Exception e) {
Log.e("MMKV", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ public void onClick(View v) {

String otherDir = getFilesDir().getAbsolutePath() + "/mmkv_3";
MMKV kv = testMMKV("test/AES", "Tencent MMKV", false, otherDir);
if (kv != null) {
kv.checkContentChangedByOuterProcess();
kv.close();
}
kv.checkContentChangedByOuterProcess();
kv.close();

testAshmem();
testReKey();
Expand Down Expand Up @@ -133,13 +131,9 @@ private void testInterProcessLogic() {
Log.d("mmkv", "" + value);
}

@Nullable
private MMKV testMMKV(String mmapID, String cryptKey, boolean decodeOnly, String rootPath) {
//MMKV kv = MMKV.defaultMMKV();
MMKV kv = MMKV.mmkvWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, cryptKey, rootPath);
if (kv == null) {
return null;
}

if (!decodeOnly) {
kv.encode("bool", true);
Expand Down Expand Up @@ -206,7 +200,8 @@ private MMKV testMMKV(String mmapID, String cryptKey, boolean decodeOnly, String
+ ", containsKey:" + kv.contains("null string"));

Log.i("MMKV", "allKeys: " + Arrays.toString(kv.allKeys()));
Log.i("MMKV", "count = " + kv.count() + ", totalSize = " + kv.totalSize());
Log.i("MMKV",
"count = " + kv.count() + ", totalSize = " + kv.totalSize() + ", actualSize = " + kv.actualSize());
Log.i("MMKV", "containsKey[string]: " + kv.containsKey("string"));

kv.removeValueForKey("bool");
Expand Down Expand Up @@ -264,9 +259,6 @@ private void testImportSharedPreferences() {
private void testReKey() {
final String mmapID = "testAES_reKey1";
MMKV kv = testMMKV(mmapID, null, false, null);
if (kv == null) {
return;
}

kv.reKey("Key_seq_1");
kv.clearMemoryCache();
Expand Down Expand Up @@ -318,7 +310,8 @@ private void testAshmem() {
Log.i("MMKV", "bytes: " + new String(kv.decodeBytes("bytes")));

Log.i("MMKV", "allKeys: " + Arrays.toString(kv.allKeys()));
Log.i("MMKV", "count = " + kv.count() + ", totalSize = " + kv.totalSize());
Log.i("MMKV",
"count = " + kv.count() + ", totalSize = " + kv.totalSize() + ", actualSize = " + kv.actualSize());
Log.i("MMKV", "containsKey[string]: " + kv.containsKey("string"));

kv.removeValueForKey("bool");
Expand Down
2 changes: 1 addition & 1 deletion Android/MMKV/mmkvdemo/src/main/kotlin/KotlinUsecase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.tencent.mmkv.MMKV
import java.util.*

fun kotlinFunctionalTest() {
val mmkv = MMKV.mmkvWithID("testKotlin") ?: return
val mmkv = MMKV.mmkvWithID("testKotlin")
mmkv.encode("bool", true)
println("bool = " + mmkv.decodeBool("bool"))

Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# MMKV Change Log

## v1.2.10 / 2021-06-25
This version is mainly for Android & Flutter.

### Android
* Complete **JavaDoc documentation** for all public methods, classes, and interfaces. From now on, you can find the [API reference online](https://javadoc.io/doc/com.tencent/mmkv).
* Drop the support of **armeabi** arch. Due to some local build cache mistake, the last version (v1.2.9) of MMKV still has an unstripped armeabi arch inside. This is fixed.
* Change `MMKV.mmkvWithID()` from returning `null` to throwing exceptions on any error.
* Add `MMKV.actualSize()` to get the actual used size of the file.

### Flutter (v1.2.11)
* Bug Fixed: When building on iOS, occasionally it will fail on symbol conflict with other libs. We have renamed all public native methods to avoid potential conflict.
* Keep up with MMKV native lib v1.2.10.

## v1.2.9 / 2021-05-26
This version is mainly for Android & Flutter.

Expand Down Expand Up @@ -483,7 +496,7 @@ What's new

Known Issues

* Getting a MMKV instance with mmapID that contains `/` may fail.
* Getting an MMKV instance with mmapID that contains `/` may fail.
MMKV uses mmapID as its filename, so don't contain any `/` inside mmapID.
* Storing a value of `type A` and getting by `type B` may not work.
MMKV does type erasure while storing values. That means it's hard for MMKV to do value-type-checking, if not impossible.
Expand Down
2 changes: 1 addition & 1 deletion Core/MMKVPredef.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <vector>
#include <unordered_map>

constexpr auto MMKV_VERSION = "v1.2.9";
constexpr auto MMKV_VERSION = "v1.2.10";

#ifdef DEBUG
# define MMKV_DEBUG
Expand Down
4 changes: 2 additions & 2 deletions MMKV.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "MMKV"
s.version = "1.2.9"
s.version = "1.2.10"
s.summary = "MMKV is a cross-platform key-value storage framework developed by WeChat."

s.description = <<-DESC
Expand Down Expand Up @@ -30,7 +30,7 @@ Pod::Spec.new do |s|
"CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF" => "NO",
}

s.dependency 'MMKVCore', '~> 1.2.9'
s.dependency 'MMKVCore', '~> 1.2.10'

end

Loading

0 comments on commit b6e1fc3

Please sign in to comment.