Skip to content

Commit

Permalink
Merge pull request #239 from ydb-platform/release_v2.1.12
Browse files Browse the repository at this point in the history
Release v2.1.12
  • Loading branch information
alex268 authored Mar 4, 2024
2 parents dc49ce1 + ef14404 commit 4db66a7
Show file tree
Hide file tree
Showing 20 changed files with 253 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.1.12 ##

* Test common: Changed visibility of YdbDockerContainer
* Core: Added async helpers to build the future chains

## 2.1.11 ##

* Transports: Add option withGrpcKeepAliveTime to enable grpc keep-alives
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Firstly you can import YDB Java BOM to specify correct versions of SDK modules.
<dependency>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-bom</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>tech.ydb</groupId>
<version>2.1.11</version>
<version>2.1.12</version>
<artifactId>ydb-sdk-bom</artifactId>
<name>Java SDK Bill of Materials</name>
<description>Java SDK Bill of Materials (BOM)</description>
Expand Down
2 changes: 1 addition & 1 deletion coordination/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-parent</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>
</parent>

<artifactId>ydb-sdk-coordination</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-parent</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>
</parent>

<artifactId>ydb-sdk-core</artifactId>
Expand Down
131 changes: 131 additions & 0 deletions core/src/main/java/tech/ydb/core/Result.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.core;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import javax.annotation.Nonnull;
Expand All @@ -21,6 +22,12 @@ public interface Result<T> {
@Nonnull
<U> Result<U> map(Function<T, U> mapper);

@Nonnull
<U> CompletableFuture<Result<U>> mapResultFuture(Function<T, CompletableFuture<Result<U>>> mapper);

@Nonnull
CompletableFuture<Status> mapStatusFuture(Function<T, CompletableFuture<Status>> mapper);

default boolean isSuccess() {
return getStatus().getCode() == StatusCode.SUCCESS;
}
Expand Down Expand Up @@ -48,6 +55,85 @@ static <V> Result<V> error(String message, Throwable throwable) {
return new Error<>(message, throwable);
}

/**
* Create functor to compose the successful result to next completable future with another result. Failed results
* will be passed as is.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Execute one query, with opening new transaction
* session.executeDataQuery(...)
* // Execute second query if first was successful
* .thenCompose(Result.compose(fisrt -> session.executeDataQuery(...)))
* // Commit transaction after two successful query executions
* .thenCompose(Result.composeStatus(second -> session.commitTransaction(...)));
* }</pre>
* @param <T> type of value in Result
* @param <U> type of resulting value in returning future
* @param mapper mapper from successful value to completable future with another result
* @return functor which composes successful results to completable future with another result
*/
static <T, U> Function<Result<T>, CompletableFuture<Result<U>>> compose(
Function<T, CompletableFuture<Result<U>>> mapper) {
return result -> result.mapResultFuture(mapper);
}

/**
* Create functor to compose the successful result to next completable future with status. Failed results
* will be composed to its statuses.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Execute one query, with opening new transaction
* session.executeDataQuery(...)
* // Execute second query if first was successful
* .thenCompose(Result.compose(fisrt -> session.executeDataQuery(...)))
* // Commit transaction after two successful query executions
* .thenCompose(Result.composeStatus(second -> session.commitTransaction(...)));
* }</pre>
* @param <T> type of value in Result
* @param mapper mapper from successful value to completable future with status
* @return functor which composes successful results to completable future with status
*/
static <T> Function<Result<T>, CompletableFuture<Status>> composeStatus(
Function<T, CompletableFuture<Status>> mapper) {
return result -> result.mapStatusFuture(mapper);
}

/**
* Create functor to compose the successful result to completed future with specified value. Failed results
* will be passed as is.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Execute one query
* session.executeDataQuery(...)
* // Execute second query if first was successful
* .thenCompose(Result.compose(fisrt -> session
* .executeDataQuery(...)
* // But use first request result as the result of
* .thenCompose(Result.composeValue(first))
* )
* )
* }</pre>
* @param <T> type of value in Result
* @param <U> type of composed value
* @param value value to create completed future
* @return functor which composes successful results to completed future with specified value
*/
static <T, U> Function<Result<T>, CompletableFuture<Result<U>>> composeValue(U value) {
return result -> result.mapResultFuture(v -> CompletableFuture.completedFuture(Result.success(value)));
}

/*
* SUCCESS
*/
Expand Down Expand Up @@ -76,6 +162,16 @@ public <U> Success<U> map(Function<V, U> mapper) {
return new Success<>(mapper.apply(value), status);
}

@Override
public <U> CompletableFuture<Result<U>> mapResultFuture(Function<V, CompletableFuture<Result<U>>> mapper) {
return mapper.apply(value);
}

@Override
public CompletableFuture<Status> mapStatusFuture(Function<V, CompletableFuture<Status>> mapper) {
return mapper.apply(value);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -118,6 +214,17 @@ public <U> Fail<U> map(Function<V, U> mapper) {
return (Fail<U>) this;
}

@Override
@SuppressWarnings("unchecked")
public <U> CompletableFuture<Result<U>> mapResultFuture(Function<V, CompletableFuture<Result<U>>> mapper) {
return CompletableFuture.completedFuture((Fail<U>) this);
}

@Override
public CompletableFuture<Status> mapStatusFuture(Function<V, CompletableFuture<Status>> mapper) {
return CompletableFuture.completedFuture(status);
}

@Override
public Status getStatus() {
return status;
Expand Down Expand Up @@ -179,6 +286,17 @@ public <U> Unexpected<U> map(Function<V, U> mapper) {
return (Unexpected<U>) this;
}

@Override
@SuppressWarnings("unchecked")
public <U> CompletableFuture<Result<U>> mapResultFuture(Function<V, CompletableFuture<Result<U>>> mapper) {
return CompletableFuture.completedFuture((Unexpected<U>) this);
}

@Override
public CompletableFuture<Status> mapStatusFuture(Function<V, CompletableFuture<Status>> mapper) {
return CompletableFuture.completedFuture(cause.getStatus());
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -238,6 +356,19 @@ public <U> Error<U> map(Function<V, U> mapper) {
return (Error<U>) this;
}

@Override
@SuppressWarnings("unchecked")
public <U> CompletableFuture<Result<U>> mapResultFuture(Function<V, CompletableFuture<Result<U>>> mapper) {
CompletableFuture<Result<U>> future = new CompletableFuture<>();
future.completeExceptionally(cause);
return future;
}

@Override
public CompletableFuture<Status> mapStatusFuture(Function<V, CompletableFuture<Status>> mapper) {
return CompletableFuture.completedFuture(status);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
79 changes: 79 additions & 0 deletions core/src/main/java/tech/ydb/core/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Supplier;


/**
Expand All @@ -24,6 +27,82 @@ private Status(StatusCode code, Double consumedRu, Issue[] issues) {
this.issues = issues;
}

/**
* Create functor to compose the successful status to next completable future with another status. Failed statuses
* will be passed as is.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Create one table
* session.createTable(...)
* // Create second table if first was created successful
* .thenCompose(Status.compose(() -> session.createTable(...)));
* }</pre>
* @param supplier generator for completable future with another status
* @return functor which composes successful status to completable future with another status
*/
public static Function<Status, CompletableFuture<Status>> compose(
Supplier<CompletableFuture<Status>> supplier) {
return status -> status.isSuccess() ? supplier.get() : CompletableFuture.completedFuture(status);
}

/**
* Create functor to compose the successful status to next completable future with result. Failed statuses
* will be composed to failed results.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Create one table
* session.createTable(...)
* // Execute query if table was created successful
* .thenCompose(Status.composeResult(() -> session.executeDataQuery(...)));
* }</pre>
* @param <T> type of value in Result
* @param supplier generator for completable future with result
* @return functor which composes successful status to completable future with result
*/
public static <T> Function<Status, CompletableFuture<Result<T>>> composeResult(
Supplier<CompletableFuture<Result<T>>> supplier) {
return status -> status.isSuccess() ? supplier.get() : CompletableFuture.completedFuture(Result.fail(status));
}

/**
* Create functor to compose the successful status to completed future with specified value. Failed statuses
* will be composed to failed results.
* <p>
* This helper is designed to be used as {@link CompletableFuture#thenCompose(java.util.function.Function) }
* argument
* <p>
* Example of usage:
* <pre> {@code
* // Start new transaction
* session.beginTransaction(...)
* // Execute query in opened transaction
* .thenCompose(Result.compose(transaction -> session.executeDataQuery(...)
* // Commmit transaction
* .thenCompose(Result.compose(result -> transaction.commit()
* // And return result of query if commit was successful
* .thenCompose(Status.bindValue(result.getResultSet(0)))
* ))
* ));
* }</pre>
* @param <T> type of value in Result
* @param value value to create completed future
* @return functor which composes successful status to completed future with specified value
*/
public static <T> Function<Status, CompletableFuture<Result<T>>> bindValue(T value) {
return status -> {
Result<T> res = status.isSuccess() ? Result.success(value) : Result.fail(status);
return CompletableFuture.completedFuture(res);
};
}

public static Status of(StatusCode code, Double consumedRu, Issue... issues) {
boolean hasIssues = issues != null && issues.length > 0;
if (code == StatusCode.SUCCESS && consumedRu == null && !hasIssues) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/tech/ydb/core/utils/Async.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.grpc.netty.shaded.io.netty.util.internal.SystemPropertyUtil;



/**
* @author Sergey Polovko
*/
Expand All @@ -38,6 +39,7 @@ public static <T> CompletableFuture<T> failedFuture(Throwable t) {
return f;
}

@Deprecated
public static <R> CompletableFuture<R> safeCall(Supplier<CompletableFuture<R>> fn) {
try {
return fn.get();
Expand All @@ -46,6 +48,7 @@ public static <R> CompletableFuture<R> safeCall(Supplier<CompletableFuture<R>> f
}
}

@Deprecated
public static <T, R> CompletableFuture<R> safeCall(T t, Function<T, CompletableFuture<R>> fn) {
try {
return fn.apply(t);
Expand All @@ -54,6 +57,7 @@ public static <T, R> CompletableFuture<R> safeCall(T t, Function<T, CompletableF
}
}

@Deprecated
public static <T, U, R> CompletableFuture<R> safeCall(T t, U u, BiFunction<T, U, CompletableFuture<R>> fn) {
try {
return fn.apply(t, u);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.1.11
version=2.1.12
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-parent</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>

<name>Java SDK for YDB</name>
<description>Java SDK for YDB</description>
Expand Down Expand Up @@ -37,7 +37,7 @@
<log4j2.version>2.17.2</log4j2.version>
<gson.version>2.8.9</gson.version>
<mockito.version>4.11.0</mockito.version>
<testcontainers.version>1.17.6</testcontainers.version>
<testcontainers.version>1.19.3</testcontainers.version>
</properties>

<licenses>
Expand Down
2 changes: 1 addition & 1 deletion scheme/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-parent</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>
</parent>

<artifactId>ydb-sdk-scheme</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion table/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>tech.ydb</groupId>
<artifactId>ydb-sdk-parent</artifactId>
<version>2.1.11</version>
<version>2.1.12</version>
</parent>

<artifactId>ydb-sdk-table</artifactId>
Expand Down
Loading

0 comments on commit 4db66a7

Please sign in to comment.