Skip to content

Commit

Permalink
Merge pull request Tencent#43 from Tencent/dev
Browse files Browse the repository at this point in the history
Refactor Swift Interface
  • Loading branch information
lingol authored Sep 27, 2018
2 parents 6556640 + 4519736 commit 6d579e3
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ xcuserdata
*.xcuserstate
*.xcscmblueprint
*.xcscheme

## Xcode
DerivedData
6 changes: 3 additions & 3 deletions Android/MMKV/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1'
classpath 'digital.wup:android-maven-publish:3.6.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand All @@ -33,9 +33,9 @@ task clean(type: Delete) {
ext {
// TODO: Change these to satisfy your project
minSdkVersion = 16
compileSdkVersion = 27
compileSdkVersion = 28
targetSdkVersion = compileSdkVersion
buildToolsVersion = '27.0.3'
buildToolsVersion = '28.0.2'
supportLibVersion = "25.4.0"
javaVersion = JavaVersion.VERSION_1_7

Expand Down
2 changes: 1 addition & 1 deletion Android/MMKV/mmkv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-annotations:27.1.1'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
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 @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
compileSdkVersion 28

signingConfigs {
config {
Expand All @@ -16,7 +16,7 @@ android {
defaultConfig {
applicationId "com.tencent.mmkvdemo"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"

Expand Down
66 changes: 35 additions & 31 deletions iOS/MMKV/MMKV/MMKV.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,74 @@

@interface MMKV : NSObject

NS_ASSUME_NONNULL_BEGIN

// a generic purpose instance
+ (instancetype)defaultMMKV;

// mmapID: any unique ID (com.tencent.xin.pay, etc)
// if you want a per-user mmkv, you could merge user-id within mmapID
+ (instancetype)mmkvWithID:(NSString *)mmapID;
+ (nullable instancetype)mmkvWithID:(NSString *)mmapID NS_SWIFT_NAME(init(mmapID:));

// mmapID: any unique ID (com.tencent.xin.pay, etc)
// if you want a per-user mmkv, you could merge user-id within mmapID
// cryptKey: 16 byte at most
+ (instancetype)mmkvWithID:(NSString *)mmapID cryptKey:(NSData *)cryptKey;
+ (nullable instancetype)mmkvWithID:(NSString *)mmapID cryptKey:(nullable NSData *)cryptKey NS_SWIFT_NAME(init(mmapID:cryptKey:));

- (BOOL)reKey:(NSData *)newKey;
- (BOOL)reKey:(nullable NSData *)newKey NS_SWIFT_NAME(reset(cryptKey:));
- (NSData *)cryptKey;

// object: NSString/NSData/NSDate
- (BOOL)setObject:(id)obj forKey:(NSString *)key;

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

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

- (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key;
- (BOOL)setInt32:(int32_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setInt64:(int64_t)value forKey:(NSString *)key;
- (BOOL)setUInt32:(uint32_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key;
- (BOOL)setInt64:(int64_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

- (BOOL)setFloat:(float)value forKey:(NSString *)key;
- (BOOL)setUInt64:(uint64_t)value forKey:(NSString *)key NS_SWIFT_NAME(set(_:forKey:));

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

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

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

- (int32_t)getInt32ForKey:(NSString *)key;
- (int32_t)getInt32ForKey:(NSString *)key defaultValue:(int32_t)defaultValue;
- (bool)getBoolForKey:(NSString *)key NS_SWIFT_NAME(boolValue(forKey:));
- (bool)getBoolForKey:(NSString *)key defaultValue:(bool)defaultValue NS_SWIFT_NAME(boolValue(forKey:defaultValue:));

- (uint32_t)getUInt32ForKey:(NSString *)key;
- (uint32_t)getUInt32ForKey:(NSString *)key defaultValue:(uint32_t)defaultValue;
- (int32_t)getInt32ForKey:(NSString *)key NS_SWIFT_NAME(int32(forKey:));
- (int32_t)getInt32ForKey:(NSString *)key defaultValue:(int32_t)defaultValue NS_SWIFT_NAME(int32(forKey:defaultValue:));

- (int64_t)getInt64ForKey:(NSString *)key;
- (int64_t)getInt64ForKey:(NSString *)key defaultValue:(int64_t)defaultValue;
- (uint32_t)getUInt32ForKey:(NSString *)key NS_SWIFT_NAME(uint32(forKey:));
- (uint32_t)getUInt32ForKey:(NSString *)key defaultValue:(uint32_t)defaultValue NS_SWIFT_NAME(uint32(forKey:defaultValue:));

- (uint64_t)getUInt64ForKey:(NSString *)key;
- (uint64_t)getUInt64ForKey:(NSString *)key defaultValue:(uint64_t)defaultValue;
- (int64_t)getInt64ForKey:(NSString *)key NS_SWIFT_NAME(int64(forKey:));
- (int64_t)getInt64ForKey:(NSString *)key defaultValue:(int64_t)defaultValue NS_SWIFT_NAME(int64(forKey:defaultValue:));

- (float)getFloatForKey:(NSString *)key;
- (float)getFloatForKey:(NSString *)key defaultValue:(float)defaultValue;
- (uint64_t)getUInt64ForKey:(NSString *)key NS_SWIFT_NAME(uint64(forKey:));
- (uint64_t)getUInt64ForKey:(NSString *)key defaultValue:(uint64_t)defaultValue NS_SWIFT_NAME(uint64(forKey:defaultValue:));

- (float)getFloatForKey:(NSString *)key NS_SWIFT_NAME(float(forKey:));
- (float)getFloatForKey:(NSString *)key defaultValue:(float)defaultValue NS_SWIFT_NAME(float(forKey:defaultValue:));

- (double)getDoubleForKey:(NSString *)key;
- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue;
- (double)getDoubleForKey:(NSString *)key NS_SWIFT_NAME(double(forKey:));
- (double)getDoubleForKey:(NSString *)key defaultValue:(double)defaultValue NS_SWIFT_NAME(double(forKey:defaultValue:));

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

- (size_t)count;

- (size_t)totalSize;

- (void)enumerateKeys:(void (^)(NSString *key, BOOL *stop))block;

- (void)removeValueForKey:(NSString *)key;
- (void)removeValueForKey:(NSString *)key NS_SWIFT_NAME(removeValue(forKey:));

- (void)removeValuesForKeys:(NSArray *)arrKeys;
- (void)removeValuesForKeys:(NSArray *)arrKeys NS_SWIFT_NAME(removeValues(forKeys:));

- (void)clearMemoryCache;

Expand All @@ -98,6 +100,8 @@
- (void)sync;

// for CrashProtected Only!!
+ (BOOL)isFileValid:(NSString *)mmapID;
+ (BOOL)isFileValid:(NSString *)mmapID NS_SWIFT_NAME(isFileValid(for:));

NS_ASSUME_NONNULL_END

@end
30 changes: 16 additions & 14 deletions iOS/MMKV/MMKV/MMKV.mm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
static NSRecursiveLock *g_instanceLock;

#define DEFAULT_MMAP_ID @"mmkv.default"
#define CRC_FILE_SIZE 4

@implementation MMKV {
NSRecursiveLock *m_lock;
Expand Down Expand Up @@ -150,7 +151,7 @@ - (void)dealloc {
munmap(m_ptr, m_size);
m_ptr = nullptr;
}
if (m_fd > 0) {
if (m_fd >= 0) {
close(m_fd);
m_fd = -1;
}
Expand All @@ -167,7 +168,7 @@ - (void)dealloc {
munmap(m_crcPtr, pbFixed32Size(0));
m_crcPtr = nullptr;
}
if (m_crcFd > 0) {
if (m_crcFd >= 0) {
close(m_crcFd);
m_crcFd = -1;
}
Expand Down Expand Up @@ -212,7 +213,7 @@ - (void)didBecomeActive {

- (void)loadFromFile {
m_fd = open(m_path.UTF8String, O_RDWR, S_IRWXU);
if (m_fd <= 0) {
if (m_fd < 0) {
MMKVError(@"fail to open:%@, %s", m_path, strerror(errno));
} else {
m_size = 0;
Expand All @@ -226,6 +227,7 @@ - (void)loadFromFile {
if (ftruncate(m_fd, m_size) != 0) {
MMKVError(@"fail to truncate [%@] to size %zu, %s", m_mmapID, m_size, strerror(errno));
m_size = (size_t) st.st_size;
return;
}
}
m_ptr = (char *) mmap(nullptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
Expand Down Expand Up @@ -324,7 +326,7 @@ - (void)clearAll {
}
m_ptr = nullptr;

if (m_fd > 0) {
if (m_fd >= 0) {
if (m_size != DEFAULT_MMAP_SIZE) {
MMKVInfo(@"truncating [%@] from %zu to %d", m_mmapID, m_size, DEFAULT_MMAP_SIZE);
if (ftruncate(m_fd, DEFAULT_MMAP_SIZE) != 0) {
Expand All @@ -335,7 +337,7 @@ - (void)clearAll {
MMKVError(@"fail to close [%@], %s", m_mmapID, strerror(errno));
}
}
m_fd = 0;
m_fd = -1;
m_size = 0;
m_actualSize = 0;
m_crcDigest = 0;
Expand Down Expand Up @@ -370,12 +372,12 @@ - (void)clearMemoryCache {
}
m_ptr = nullptr;

if (m_fd > 0) {
if (m_fd >= 0) {
if (close(m_fd) != 0) {
MMKVError(@"fail to close [%@], %s", m_mmapID, strerror(errno));
}
}
m_fd = 0;
m_fd = -1;
m_size = 0;
m_actualSize = 0;

Expand Down Expand Up @@ -641,7 +643,7 @@ - (BOOL)fullWriteback {
}

- (BOOL)isFileValid {
if (m_fd > 0 && m_size > 0 && m_output != nullptr && m_ptr != nullptr && m_ptr != MAP_FAILED) {
if (m_fd >= 0 && m_size > 0 && m_output != nullptr && m_ptr != nullptr && m_ptr != MAP_FAILED) {
return YES;
}
// MMKVWarning(@"[%@] file not valid", m_mmapID);
Expand Down Expand Up @@ -722,7 +724,7 @@ - (void)prepareCRCFile {
createFile(m_crcPath);
}
m_crcFd = open(m_crcPath.UTF8String, O_RDWR, S_IRWXU);
if (m_crcFd <= 0) {
if (m_crcFd < 0) {
MMKVError(@"fail to open:%@, %s", m_crcPath, strerror(errno));
removeFile(m_crcPath);
} else {
Expand All @@ -731,8 +733,8 @@ - (void)prepareCRCFile {
if (fstat(m_crcFd, &st) != -1) {
size = (size_t) st.st_size;
}
int fileLegth = DEFAULT_MMAP_SIZE;
if (size < fileLegth) {
int fileLegth = CRC_FILE_SIZE;
if (size != fileLegth) {
size = fileLegth;
if (ftruncate(m_crcFd, size) != 0) {
MMKVError(@"fail to truncate [%@] to size %zu, %s", m_crcPath, size, strerror(errno));
Expand Down Expand Up @@ -803,11 +805,11 @@ - (BOOL)reKey:(NSData *)newKey {

#pragma mark - set & get

- (BOOL)setObject:(id)obj forKey:(NSString *)key {
if (obj == nil || key.length <= 0) {
- (BOOL)setObject:(id)object forKey:(NSString *)key {
if (object == nil || key.length <= 0) {
return NO;
}
NSData *data = [MiniPBCoder encodeDataWithObject:obj];
NSData *data = [MiniPBCoder encodeDataWithObject:object];
return [self setData:data forKey:key];
}

Expand Down
77 changes: 40 additions & 37 deletions iOS/MMKVDemo/MMKVDemo/DemoSwiftUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,45 @@ import Foundation

@objc class DemoSwiftUsage : NSObject {
func testSwiftFunctionality() {
let mmkv = MMKV(id: "testSwift")!;

mmkv.setBool(true, forKey: "bool");
print("Swift: bool = \(mmkv.getBoolForKey("bool"))");

mmkv.setInt32(-1024, forKey: "int32");
print("Swift: int32 = \(mmkv.getInt32ForKey("int32"))");

mmkv.setUInt32(UInt32.max, forKey: "uint32");
print("Swift: uint32 = \(mmkv.getUInt32(forKey: "uint32"))");

mmkv.setInt64(Int64.min, forKey: "int64");
print("Swift: int64 = \(mmkv.getInt64ForKey("int64"))");

mmkv.setUInt64(UInt64.max, forKey: "uint64");
print("Swift: uint64 = \(mmkv.getUInt64(forKey:"uint64"))");

mmkv.setFloat(-3.1415926, forKey: "float");
print("Swift: float = \(mmkv.getFloatForKey("float"))");

mmkv.setDouble(Double.infinity, forKey: "double");
print("Swift: double = \(mmkv.getDoubleForKey("double"))");

mmkv.setObject("Hello from Swift", forKey: "string");
print("Swift: string = \(String(describing: mmkv.getObjectOf(NSString.self, forKey:"string")))");

mmkv.setObject(NSDate(), forKey: "date");
let date = mmkv.getObjectOf(NSDate.self, forKey:"date") as! Date;
print("Swift: date = \(date.description(with: .current))");

mmkv.setObject("Hello from Swift".data(using: .utf8), forKey: "data");
let data = mmkv.getObjectOf(NSData.self, forKey:"data");
let str = String(data: data as! Data, encoding: .utf8);
print("Swift: data = \(String(describing: str))");

mmkv.removeValue(forKey: "bool");
print("Swift: after delete bool = \(mmkv.getBoolForKey("bool"))");

guard let mmkv = MMKV(mmapID: "testSwift") else {
return
}

mmkv.set(true, forKey: "bool")
print("Swift: bool = \(mmkv.boolValue(forKey: "bool"))")

mmkv.set(Int32(-1024), forKey: "int32")
print("Swift: int32 = \(mmkv.int32(forKey: "int32"))")

mmkv.set(UInt32.max, forKey: "uint32")
print("Swift: uint32 = \(mmkv.uint32(forKey: "uint32"))")

mmkv.set(Int64.min, forKey: "int64")
print("Swift: int64 = \(mmkv.int64(forKey: "int64"))")

mmkv.set(UInt64.max, forKey: "uint64")
print("Swift: uint64 = \(mmkv.uint64(forKey: "uint64"))")

mmkv.set(Float(-3.1415926), forKey: "float")
print("Swift: float = \(mmkv.float(forKey: "float"))")

mmkv.set(Double.infinity, forKey: "double")
print("Swift: double = \(mmkv.float(forKey: "double"))")

mmkv.set("Hello from Swift", forKey: "string")
print("Swift: string = \(mmkv.object(of: NSString.self, forKey: "string") ?? "")")

mmkv.set(NSDate(), forKey: "date")
let date = mmkv.object(of: NSDate.self, forKey: "date") as? Date
print("Swift: date = \(date?.description(with: .current) ?? "null")")

mmkv.set("Hello from Swift".data(using: .utf8) ?? Data(), forKey: "data")
let data = mmkv.object(of: NSData.self, forKey: "data") as? Data
let str = String(data: data ?? Data(), encoding: .utf8) ?? ""
print("Swift: data = \(str)")

mmkv.removeValue(forKey: "bool")
print("Swift: after delete bool = \(mmkv.boolValue(forKey: "bool"))")
}
}

0 comments on commit 6d579e3

Please sign in to comment.