0
0

More than 1 year has passed since last update.

Couchbase Lite開発: Kotlin拡張機能

Last updated at Posted at 2022-01-04

はじめに

Couchbase Lite Android 3.0.0-beta02では、Kotlinアプリ開発における慣用的なプログラミング作法のサポートが導入されています。

これによって、Kotlin開発者は、一般的なKotlinアプリケーション開発に用いられる実装パターンを用いてアプリを構築できるようになりました。Couchbase Kotlin SDKは、Couchbase Lite for Androidとシームレスに統合されており、Java APIと同等の機能を備えています。

Couchbaseをモバイルアプリケーションで利用する意義については、以下の記事をご参考ください。

Kotlin拡張機能

概要

Kotlin開発者は、Couchbase Liteを利用したアプリケーション構築のために、Java APIとの完全な互換性に加えて、Kotlin拡張機能を利用できます。

Kotlin拡張機能パッケージには以下のサポートが含まれています。

  • ConfigurationFactory
    • Kotlinの名前付きパラメーターを活用してデータベースやレプリケーターなどの各種プロパティ設定を行うことができます。
  • Flow
    • コルーチンやFlowなどのKotlinの機能を使用して、主要なCouchbase Liteオブジェクトの変更を監視することができます。

ConfigurationFactory

Couchbase Liteは、各種の設定オブジェクトを作成するためのファクトリークラスを提供します。

データベース

DatabaseConfigurationを作成するためのDatabaseConfigurationFactory

使用例
database = Database(
  "getting-started",
  DatabaseConfigurationFactory.create(context.filesDir.absolutePath)
  )
定義例
val DatabaseConfigurationFactory: DatabaseConfiguration? = null

fun DatabaseConfiguration?.create(
    databasePath: String? = null,
    encryptionKey: EncryptionKey? = null
)

レプリケーション

ReplicatorConfigurationを作成するためのReplicatorConfigurationFactory

使用例
val replicator =
  Replicator(
    ReplicatorConfigurationFactory.create(
      database = database,
      target = URLEndpoint(URI("ws://localhost:4984/getting-started-db")),
      type = ReplicatorType.PUSH_AND_PULL,
      authenticator = BasicAuthenticator("sync-gateway", "password".toCharArray())
      )
  )
定義例
val ReplicatorConfigurationFactory: ReplicatorConfiguration? = null

fun ReplicatorConfiguration?.create(
    database: Database? = null,
    target: Endpoint? = null,
    type: ReplicatorType? = null,
    continuous: Boolean? = null,
    authenticator: Authenticator? = null,
    headers: Map<String, String>? = null,
    pinnedServerCertificate: ByteArray? = null,
    channels: List<String>? = null,
    documentIDs: List<String>? = null,
    pushFilter: ReplicationFilter? = null,
    pullFilter: ReplicationFilter? = null,
    conflictResolver: ConflictResolver? = null,
    maxAttempts: Int? = null,
    maxAttemptWaitTime: Int? = null,
    heartbeat: Int? = null,
    enableAutoPurge: Boolean? = null,
    acceptOnlySelfSignedServerCertificate: Boolean? = null
)

val MessageEndpointListenerConfigurationFactory: MessageEndpointListenerConfiguration? = null

fun MessageEndpointListenerConfiguration?.create(
    database: Database? = null,
    protocolType: ProtocolType? = null
)

全文検索

FullTextIndexConfigurationを作成するためのFullTextIndexConfigurationFactory

使用例
db.createIndex("fts_index",
      FullTextIndexConfigurationFactory.create(expressions = ["name","location"])
    )
定義例
val FullTextIndexConfigurationFactory: FullTextIndexConfiguration? = null

fun FullTextIndexConfiguration?.create(expression: String? = null)

インデックス作成

ValueIndexConfigurationを作成するためのValueIndexConfigurationFactory

使用例
db.createIndex("name_and_location_index",
      ValueIndexConfigurationFactory.create(expressions = ["name","location"])
    )
定義例
val ValueIndexConfigurationFactory: ValueIndexConfiguration? = null

fun ValueIndexConfiguration?.create(vararg expressions: String = emptyArray())

ログ

LogFileConfigurationを作成するためのLogFileConfigurationFactory

使用例
Database.log.file.let {
  it.config = LogFileConfigurationFactory.create(
    context.cacheDir.absolutePath, 
    maxSize = 10240, 
    maxRotateCount = 5, 
    usePlainText = false
    ) 
    it.level = LogLevel.INFO 

  }
定義例
val LogFileConfigurationFactory: LogFileConfiguration? = null

.LogFileConfiguration.create()

fun LogFileConfiguration?.create(
    directory: String? = null,
    maxSize: Long? = null,
    maxRotateCount: Int? = null,
    usePlainText: Boolean? = null
)

Flow

データベース変更

データベース変更イベントを監視します。

使用例
val updatedDocs = db.databaseChangeFlow()
    .map { it.documentIDs }
    .asLiveData()
定義例
@ExperimentalCoroutinesApi
fun Database.databaseChangeFlow(executor: Executor? = null)

ドキュメント変更

ドキュメントの変更を監視します。

使用例
val docModDate = db.documentChangeFlow("1001", null)
    .map { it.database.getDocument(it.documentID)?.getString("lastModified") }
    .asLiveData()
定義例
@ExperimentalCoroutinesApi

fun Database.documentChangeFlow(documentId: String, executor: Executor? = null)

レプリケーター変更

レプリケータの変化を監視します。

使用例
val replState = repl.replicatorChangesFlow()
    .map { it.status.activityLevel }
    .asLiveData()
定義例
@ExperimentalCoroutinesApi
fun Replicator.replicatorChangesFlow(executor: Executor? = null)

ドキュメントレプリケーション

レプリケーション中のドキュメントの変更を監視します。

使用例
val replicatedDocs = repl.documentReplicationFlow(testSerialExecutor)
    .map { update -> update.documents }
    .onEach { listView.setUpdated(it) }
    .collect()
定義例
@ExperimentalCoroutinesApi
fun Replicator.documentReplicationFlow(executor: Executor? = null)

クエリ変更フロー

クエリ(の結果)の変更を監視します。

使用例
var liveQuery: LiveData<List<Any>?>? = null

@ExperimentalCoroutinesApi
fun watchQuery(query: Query): LiveData<List<Any>?> {
    val queryFlow = query.queryChangeFlow()
        .map {
            val err = it.error
            if (err != null) {
                throw err
            }
            it.results?.allResults()?.flatMap { it.toList() }
        }
        .asLiveData()
    liveQuery = queryFlow
    return queryFlow
}
定義例
@ExperimentalCoroutinesApi
fun Query.queryChangeFlow(executor: Executor? = null)

最後に

Couchbase Liteについての記事を以下の投稿で整理していますので、ご関心に応じて、参照してみてください。

Couchbase Liteは、Couchbase Serverとの自動双方向同期が可能です。

Couchbase Serverについては、日本語で読むことができるまとまった情報として、次の拙著を紹介させていただきます。

関連情報

拡張APIドキュメントについては、以下のAPI Docを参照してください。

本稿の内容の一次情報は下記です。

Kotlin開発の一般的なパターンについては、以下を参照できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0