Skip to content

Commit

Permalink
Add Kotlin Coroutines API #1387
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov authored and mp911de committed Sep 15, 2020
1 parent 22d0f05 commit 34f6813
Show file tree
Hide file tree
Showing 63 changed files with 16,352 additions and 54 deletions.
84 changes: 72 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.lettuce</groupId>
Expand Down Expand Up @@ -73,6 +74,8 @@
<rxjava3.version>3.0.6</rxjava3.version>
<reactive-streams-tck.version>1.0.3</reactive-streams-tck.version>
<slf4j.version>1.7.25</slf4j.version>
<kotlin.version>1.4.0</kotlin.version>
<kotlinx-coroutines.version>1.3.9</kotlinx-coroutines.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- You need a running redis+sentinel for all tests, therefore disabled by default -->
Expand All @@ -84,8 +87,8 @@
<connection>scm:git:https://github.com/lettuce-io/lettuce-core.git</connection>
<developerConnection>scm:git:https://github.com/lettuce-io/lettuce-core.git</developerConnection>
<url>http://github.com/lettuce-io/lettuce-core</url>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>

<distributionManagement>
<snapshotRepository>
Expand Down Expand Up @@ -182,6 +185,27 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactive</artifactId>
<version>${kotlinx-coroutines.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-jdk8</artifactId>
<version>${kotlinx-coroutines.version}</version>
<optional>true</optional>
</dependency>

<!-- OS-native transports -->

<dependency>
Expand Down Expand Up @@ -578,14 +602,48 @@
</executions>
</plugin>

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<args>
<arg>-Xopt-in=kotlin.RequiresOptIn</arg>
<arg>-Xopt-in=io.lettuce.core.ExperimentalLettuceCoroutinesApi</arg>
</args>
<sourceDirs>${project.basedir}/src/main/kotlin</sourceDirs>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>

<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<compilerArgument>-Xlint:all,-deprecation,-unchecked</compilerArgument>
<testCompilerArgument>-Xlint:none</testCompilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>false</showDeprecation>
<source>1.8</source>
<target>1.8</target>
</configuration>
Expand Down Expand Up @@ -895,7 +953,7 @@
<arguments>
<argument>-Xmx2G</argument>
<argument>-classpath</argument>
<classpath />
<classpath/>
<argument>org.openjdk.jmh.Main</argument>
<argument>.*</argument>
<argument>-tu</argument>
Expand Down Expand Up @@ -969,7 +1027,9 @@
<phase>process-resources</phase>
<configuration>
<target>
<copy failonerror="false" file="${project.build.directory}/generated-docs/index.pdf" tofile="target/site/reference/pdf/lettuce-reference.pdf" />
<copy failonerror="false"
file="${project.build.directory}/generated-docs/index.pdf"
tofile="target/site/reference/pdf/lettuce-reference.pdf"/>
</target>
</configuration>
<goals>
Expand Down Expand Up @@ -1036,9 +1096,9 @@
<toclevels>3</toclevels>
<numbered>true</numbered>
<ext-doc>https://raw.githubusercontent.com/wiki/lettuce-io/lettuce-core/</ext-doc>
<allow-uri-read />
<docinfo />
<toc2 />
<allow-uri-read/>
<docinfo/>
<toc2/>
<icons>font</icons>
<source-highlighter>coderay</source-highlighter>
</attributes>
Expand Down
38 changes: 38 additions & 0 deletions src/main/asciidoc/kotlin-api.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
= Kotlin API

=== Suspendable API based on either reactive or async operations

Example for retrieving commands and using it:

[source,kt]
----
val cmd1: RedisSuspendableCommands<String, String> = connection.async().asSuspendable()
val cmd2: RedisSuspendableCommands<String, String> = connection.reactive().asSuspendable()
val foo1 = cmd1.set("foo", "bar")
val foo2 = cmd2.keys("fo*")
----

=== Extensions for existing APIs

==== Transactions DSL

Example for sync:

[source,kt]
----
val result: TransactionResult? = connection.sync().multi {
set("foo", "bar")
get("foo")
}
----

Example for async with coroutines:

[source,kt]
----
val result: TransactionResult? = connection.async().multi {
set("foo", "bar").await()
get("foo").await()
}
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.lettuce.core

import kotlin.RequiresOptIn.Level.WARNING
import kotlin.annotation.AnnotationRetention.BINARY
import kotlin.annotation.AnnotationTarget.*

/**
* @author sokomishalov
*/
@RequiresOptIn(level = WARNING)
@Retention(BINARY)
@Target(
CLASS,
ANNOTATION_CLASS,
PROPERTY,
FIELD,
LOCAL_VARIABLE,
VALUE_PARAMETER,
CONSTRUCTOR,
FUNCTION,
PROPERTY_GETTER,
PROPERTY_SETTER,
TYPEALIAS
)
annotation class ExperimentalLettuceCoroutinesApi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.lettuce.core.api.async

import io.lettuce.core.ExperimentalLettuceCoroutinesApi
import io.lettuce.core.TransactionResult
import io.lettuce.core.api.coroutines.RedisSuspendableCommands
import io.lettuce.core.api.coroutines.async.RedisSuspendableAsyncCommandsImpl
import kotlinx.coroutines.future.await


/**
* Extension for [RedisAsyncCommands] to create [RedisSuspendableCommands]
*
* @author Mikhael Sokolov
* @since 6.0
*/
@ExperimentalLettuceCoroutinesApi
fun <K, V> RedisAsyncCommands<K, V>.asSuspendable(): RedisSuspendableCommands<K, V> {
return RedisSuspendableAsyncCommandsImpl(this)
}

/**
* Allows to create transaction DSL block with [RedisAsyncCommands].
*
* @author Mikhael Sokolov
* @since 6.0
*/
@ExperimentalLettuceCoroutinesApi
suspend inline fun <K, V> RedisAsyncCommands<K, V>.multi(action: RedisAsyncCommands<K, V>.() -> Unit): TransactionResult? {
multi().await()
runCatching {
action.invoke(this)
}.onFailure {
discard()
}
return exec().await()
}
Loading

0 comments on commit 34f6813

Please sign in to comment.