WCDB是微信开源的移动数据库框架

网友投稿 1038 2022-10-27

WCDB是微信开源的移动数据库框架

WCDB是微信开源的移动数据库框架

WCDB

中文版本请参看这里

WCDB is an efficient, complete, easy-to-use mobile database framework used in the WeChat application. It's currently available on iOS, macOS and Android.

WCDB for iOS/macOS

Features

Getting Started

Prerequisites

Apps using WCDB can target: iOS 7 or later, macOS 10.9 or later.Xcode 8.0 or later required.Objective-C++ required.

Installation

Via Cocoapods:Install CocoaPods.Run pod repo update to make CocoaPods aware of the latest available WCDB versions.In your Podfile, add pod 'WCDB' to your app target.From the command line, run pod install.Use the .xcworkspace file generated by CocoaPods to work on your project.Add #import at the top of your Objective-C++ source files and start your WCDB journey.Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm. Via Carthage:Install Carthage.Add github "Tencent/WCDB" to your Cartfile.Run carthage update.Drag WCDB.framework from the appropriate platform directory in Carthage/Build/ to the Linked Binary and Libraries section of your Xcode project’s Build Phases settings.On your application targets' Build Phases settings tab, click the "+" icon and choose New Run Script Phase. Create a Run Script with carthage copy-frameworks and add the paths to the frameworks under Input Files: $(SRCROOT)/Carthage/Build/iOS/WCDB.framework or $(SRCROOT)/Carthage/Build/Mac/WCDB.framework.Add #import at the top of your Objective-C++ source files and start your WCDB journey.Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm. Via Dynamic Framework: Note that Dynamic frameworks are not compatible with iOS 7. See “Static Framework” for iOS 7 support.Getting source code from git repository and update the submodule of sqlcipher. git clone https://github.com/Tencent/wcdb.gitcd wcdbgit submodule update --init sqlcipher Drag WCDB.xcodeproj in wcdb/apple/ into your project.Add WCDB.framework to the Enbedded Binaries section of your Xcode project's General settings. Note that there are two frameworks here and the dynamic one should be chosen. You can check it at Build Phases->Target Dependencies. The right one is WCDB while `WCDB iOS Static is used for static lib.Add #import at the top of your Objective-C++ source files and start your WCDB journey.Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm. Via Static Framework:Getting source code from git repository and update the submodule of sqlcipher. git clone https://github.com/Tencent/wcdb.gitcd wcdbgit submodule update --init sqlcipher Drag WCDB.xcodeproj in wcdb/apple/ into your project.Add WCDB iOS Static to the Target Dependencies section of your Xcode project's Build Phases settings.Add WCDB.framework, libz.tbd to the Linked Binary and Libraries section of your Xcode project's Build Phases settings. Note that there are two WCDB.framework, you should choose the one from WCDB iOS Static target.Add -all_load and -ObjC to the Other Linker Flags section of your Xcode project's Build Settings.Add #import at the top of your Objective-C++ source files and start your WCDB journey.Since WCDB is an Objective-C++ framework, for those files in your project that includes WCDB, you should rename their extension .m to .mm.

Tutorials

Tutorials can be found here.

Documentations

Documentations can be found on our Wiki.API references for iOS/macOS can be found here.Performance data can be found on our benchmark.

WCDB for Android

Features

Database encryption via SQLCipher.ORM/persistence solution via Room from Android Architecture Components.Concurrent access via connection pooling from modern Android framework.Repair toolkit for database corruption recovery.Database backup and recovery utility optimized for small backup size.Log redirection and various tracing facilities.API 14 (Android 4.0) and above are supported.

Getting Started

To include WCDB to your project, choose either way: import via Maven or via AAR package.

Import via Maven

To import WCDB via Maven repositories, add the following lines to build.gradle on your app module:

dependencies { compile 'com.tencent.wcdb:wcdb-android:1.0.8' // Replace "1.0.8" to any available version.}

This will cause Gradle to download AAR package from JCenter while building your application.

If you want to use Room persistence library, you need to add the Google Maven repository to build.gradle to your root project.

allprojects { repositories { jcenter() google() // Add this line }}

Also add dependencies to module build.gradle.

dependencies { compile 'com.tencent.wcdb:room:1.0.8' // Replace "1.0.8" to any available version. annotationProcessor 'android.arch.persistence.room:compiler:1.1.1' // Don't forget to include Room annotation compiler from Google.}

Import Prebuilt AAR Package

1. Download AAR package from release page.   2. Import the AAR as new module. In Android Studio, select File -> New -> New Module... menu and choose "Import JAR/AAR Package".   3. Add a dependency on the new module. This can be done using File -> Project Structure... in Android Studio, or by adding following code to application's build.gradle:

dependencies { // Change "wcdb" to the actual module name specified in step 2. compile project(':wcdb')}

Migrate from Plain-text SQLite Databases

WCDB has interfaces very similar to Android SQLite Database APIs. To migrate you application from AOSP API, change import path from android.database.* to com.tencent.wcdb.*, and android.database.sqlite.* to com.tencent.wcdb.database.*. After import path update, your application links to WCDB instead of AOSP API.

To open or create an encrypted database, use with-password versions of SQLiteDatabase.openOrCreateDatabase(), SQLiteOpenHelper.getWritableDatabase(), or Context.openOrCreateDatabase().

Note: WCDB uses byte[] for password instead of String in SQLCipher Android Binding.

String password = "MyPassword";SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("/path/to/database", password.getBytes(), null, null);

See sample-encryptdb for sample for transferring data between plain-text and encrypted databases.

Use WCDB with Room

To use WCDB with Room library, follow the Room instructions. The only code to change is specifying WCDBOpenHelperFactory when getting instance of the database.

SQLiteCipherSpec cipherSpec = new SQLiteCipherSpec() .setPageSize(4096) .setKDFIteration(64000);WCDBOpenHelperFactory factory = new WCDBOpenHelperFactory() .passphrase("passphrase".getBytes()) // passphrase to the database, remove this line for plain-text .cipherSpec(cipherSpec) // cipher to use, remove for default settings .writeAheadLoggingEnabled(true) // enable WAL mode, remove if not needed .asyncCheckpointEnabled(true); // enable asynchronous checkpoint, remove if not neededAppDatabase db = Room.databaseBuilder(this, AppDatabase.class, "app-db") .allowMainThreadQueries() .openHelperFactory(factory) // specify WCDBOpenHelperFactory when opening database .build();

See sample-persistence for samples using Room library with WCDB. See sample-room-with-a-view for samples using Room library with WCDB and other architecture components from Google.

sample-room-with-a-view comes from Google's CodeLabs with modification of just a few rows. Search for [WCDB] keyword for the modification.See here for the original tutorial.

Corruption Recovery

See sample-repairdb for instructions how to recover corrupted databases using RepairKit.

Redirect Log Output

By default, WCDB prints its log message to system logcat. You may want to change this behavior in order to, for example, save logs for troubleshooting. WCDB can redirect all of its log outputs to user-defined routine using Log.setLogger(LogCallback) method.

Build from Sources

Build WCDB Android with Prebuilt Dependencies

WCDB itself can be built apart from its dependencies using Gradle or Android Studio. To build WCDB Android library, run Gradle on android directory:

$ cd android$ ./gradlew build

Building WCDB requires Android NDK installed. If Gradle failed to find your SDK and/or NDK, you may need to create a file named local.properties on the android directory with content:

sdk.dir=path/to/sdkndk.dir=path/to/ndk

Android Studio will do this for you when the project is imported.

Build Dependencies from Sources

WCDB depends on OpenSSL crypto library and SQLCipher. You can rebuild all dependencies if you wish. In this case, a working C compiler on the host system, Perl 5, Tcl and a bash environment is needed to be installed on your system.

To build dependencies, checkout all submodules, set ANDROID_NDK_ROOT environment variable to your NDK path, then run build-depends-android.sh:

$ export ANDROID_NDK_ROOT=/path/to/ndk$ ./build-depends-android.sh

This will build OpenSSL crypto library and generate SQLCipher amalgamation sources and place them to proper locations suitable for WCDB library building.

Documentations

Documentations can be found on our Wiki.API references for Android can be found here.

Contributing

If you are interested in contributing, check out the [CONTRIBUTING.md], also join our Tencent OpenSource Plan.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:高可用架构etcd选主故障主备秒级切换实现
下一篇:Caffe:一个快速开放深度学习的框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~