Skip to content

Commit

Permalink
Refactored API in a separate module (#141)
Browse files Browse the repository at this point in the history
* Refactored API in a separate module

* Spotless

* Fixed integration tests import

* spotless

* More rename fixes

* maven name
  • Loading branch information
merlimat authored May 2, 2024
1 parent e48a075 commit 9e9585d
Show file tree
Hide file tree
Showing 29 changed files with 247 additions and 132 deletions.
38 changes: 38 additions & 0 deletions client-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright © 2022-2024 StreamNative Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<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-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.streamnative.oxia</groupId>
<artifactId>oxia-java</artifactId>
<version>0.1.7-SNAPSHOT</version>
</parent>

<artifactId>oxia-client-api</artifactId>
<name>Oxia Client API</name>

<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.streamnative.oxia.client.api;

import io.streamnative.oxia.client.api.exceptions.UnexpectedVersionIdException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
import static io.streamnative.oxia.client.api.DeleteOption.VersionIdDeleteOption.IfVersionIdEquals;
import static io.streamnative.oxia.client.api.DeleteOption.VersionIdDeleteOption.Unconditionally;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public sealed interface DeleteOption permits VersionIdDeleteOption {

default boolean cannotCoExistWith(DeleteOption option) {
Expand Down Expand Up @@ -67,31 +61,4 @@ public Long toVersionId() {
static VersionIdDeleteOption ifVersionIdEquals(long versionId) {
return new IfVersionIdEquals(versionId);
}

static Set<DeleteOption> validate(DeleteOption... args) {
if (args == null || args.length == 0) {
return Set.of(Unconditionally);
}
Arrays.stream(args)
.forEach(
a -> {
if (Arrays.stream(args)
.filter(c -> !c.equals(a))
.anyMatch(c -> a.cannotCoExistWith(c))) {
throw new IllegalArgumentException(
"Incompatible "
+ DeleteOption.class.getSimpleName()
+ "s: "
+ Arrays.toString(args));
}
});
return new HashSet<>(Arrays.asList(args));
}

static Optional<Long> toVersionId(Collection<DeleteOption> options) {
return options.stream()
.filter(o -> o instanceof VersionIdDeleteOption)
.findAny()
.map(o -> ((VersionIdDeleteOption) o).toVersionId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.streamnative.oxia.client.api;

import io.streamnative.oxia.proto.GetResponse;
import lombok.NonNull;
import lombok.Value;

Expand All @@ -27,9 +26,4 @@ public class GetResult {

/** Metadata for the record associated with the key specified in the call. */
@NonNull Version version;

public static @NonNull GetResult fromProto(@NonNull GetResponse response) {
return new GetResult(
response.getValue().toByteArray(), Version.fromProto(response.getVersion()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@
import static io.streamnative.oxia.client.api.PutOption.VersionIdPutOption.IfVersionIdEquals;
import static io.streamnative.oxia.client.api.PutOption.VersionIdPutOption.Unconditionally;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

public sealed interface PutOption permits VersionIdPutOption, AsEphemeralRecord {

default boolean cannotCoExistWith(PutOption option) {
Expand Down Expand Up @@ -80,35 +74,4 @@ record AsEphemeralRecord() implements PutOption {}
static VersionIdPutOption ifVersionIdEquals(long versionId) {
return new IfVersionIdEquals(versionId);
}

static Set<PutOption> validate(PutOption... args) {
if (args == null || args.length == 0) {
return Set.of(Unconditionally);
}
Arrays.stream(args)
.forEach(
a -> {
if (Arrays.stream(args)
.filter(c -> !c.equals(a))
.anyMatch(c -> a.cannotCoExistWith(c))) {
throw new IllegalArgumentException(
"Incompatible "
+ PutOption.class.getSimpleName()
+ "s: "
+ Arrays.toString(args));
}
});
return new HashSet<>(Arrays.asList(args));
}

static Optional<Long> toVersionId(Collection<PutOption> options) {
return options.stream()
.filter(o -> o instanceof VersionIdPutOption)
.findAny()
.map(o -> ((VersionIdPutOption) o).toVersionId());
}

static boolean toEphemeral(Collection<PutOption> options) {
return options.stream().anyMatch(o -> o instanceof PutOption.AsEphemeralRecord);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@
*/
package io.streamnative.oxia.client.api;

import io.streamnative.oxia.proto.PutResponse;
import lombok.NonNull;

/**
* The result of a client get request.
*
* @param version Metadata for the record associated with the key specified in the call.
*/
public record PutResult(@NonNull Version version) {
public static @NonNull PutResult fromProto(@NonNull PutResponse response) {
return new PutResult(Version.fromProto(response.getVersion()));
}
}
public record PutResult(@NonNull Version version) {}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.streamnative.oxia.client.api;

import io.streamnative.oxia.client.api.exceptions.UnexpectedVersionIdException;
import java.util.List;
import java.util.function.Consumer;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,4 @@ public static void requireValidModificationsCount(long modificationsCount) {
throw new IllegalArgumentException("Invalid modificationsCount: " + modificationsCount);
}
}

public static @NonNull Version fromProto(@NonNull io.streamnative.oxia.proto.Version version) {
return new Version(
version.getVersionId(),
version.getCreatedTimestamp(),
version.getModifiedTimestamp(),
version.getModificationsCount(),
version.hasSessionId() ? Optional.of(version.getSessionId()) : Optional.empty(),
version.hasClientIdentity() ? Optional.of(version.getClientIdentity()) : Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.oxia.client.api;
package io.streamnative.oxia.client.api.exceptions;

import lombok.Getter;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.oxia.client.api;
package io.streamnative.oxia.client.api.exceptions;

/** A super-class of exceptions describing errors that occurred on an Oxia server. */
public abstract class OxiaException extends Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.oxia.client.api;
package io.streamnative.oxia.client.api.exceptions;

/** The session does not exist on the server. */
public class SessionDoesNotExistException extends OxiaException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.oxia.client.api;
package io.streamnative.oxia.client.api.exceptions;

import lombok.Getter;
import lombok.NonNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import io.streamnative.oxia.client.OxiaClientBuilder;
import io.streamnative.oxia.client.api.AsyncOxiaClient;
import io.streamnative.oxia.client.api.DeleteOption;
import io.streamnative.oxia.client.api.KeyAlreadyExistsException;
import io.streamnative.oxia.client.api.Notification;
import io.streamnative.oxia.client.api.Notification.KeyCreated;
import io.streamnative.oxia.client.api.Notification.KeyDeleted;
import io.streamnative.oxia.client.api.Notification.KeyModified;
import io.streamnative.oxia.client.api.PutOption;
import io.streamnative.oxia.client.api.UnexpectedVersionIdException;
import io.streamnative.oxia.client.api.exceptions.KeyAlreadyExistsException;
import io.streamnative.oxia.client.api.exceptions.UnexpectedVersionIdException;
import io.streamnative.oxia.testcontainers.OxiaContainer;
import java.util.ArrayList;
import java.util.List;
Expand Down
5 changes: 5 additions & 0 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>oxia-client-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,12 @@ class AsyncOxiaClientImpl implements AsyncOxiaClient {
gaugePendingPutRequests.increment();
gaugePendingPutBytes.add(value.length);

var validatedOptions = PutOption.validate(options);
var validatedOptions = PutOptionsUtil.validate(options);
var shardId = shardManager.get(key);
var versionId = PutOption.toVersionId(validatedOptions);
var versionId = PutOptionsUtil.toVersionId(validatedOptions);
var op =
new PutOperation(
callback, key, value, versionId, PutOption.toEphemeral(validatedOptions));
callback, key, value, versionId, PutOptionsUtil.toEphemeral(validatedOptions));
writeBatchManager.getBatcher(shardId).add(op);
} catch (RuntimeException e) {
callback.completeExceptionally(e);
Expand Down Expand Up @@ -263,9 +263,9 @@ class AsyncOxiaClientImpl implements AsyncOxiaClient {
try {
checkIfClosed();
Objects.requireNonNull(key);
var validatedOptions = DeleteOption.validate(options);
var validatedOptions = DeleteOptionsUtil.validate(options);
var shardId = shardManager.get(key);
var versionId = DeleteOption.toVersionId(validatedOptions);
var versionId = DeleteOptionsUtil.toVersionId(validatedOptions);
writeBatchManager.getBatcher(shardId).add(new DeleteOperation(callback, key, versionId));
} catch (RuntimeException e) {
callback.completeExceptionally(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright © 2022-2024 StreamNative Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.streamnative.oxia.client;

import io.streamnative.oxia.client.api.DeleteOption;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import lombok.experimental.UtilityClass;

@UtilityClass
public class DeleteOptionsUtil {

private static final Set<DeleteOption> DefaultDeleteOptions =
Collections.singleton(DeleteOption.Unconditionally);

public static Set<DeleteOption> validate(DeleteOption... args) {
if (args == null || args.length == 0) {
return DefaultDeleteOptions;
}

Arrays.stream(args)
.forEach(
a -> {
if (Arrays.stream(args)
.filter(c -> !c.equals(a))
.anyMatch(c -> a.cannotCoExistWith(c))) {
throw new IllegalArgumentException(
"Incompatible "
+ DeleteOption.class.getSimpleName()
+ "s: "
+ Arrays.toString(args));
}
});
return new HashSet<>(Arrays.asList(args));
}

public static Optional<Long> toVersionId(Collection<DeleteOption> options) {
return options.stream()
.filter(o -> o instanceof DeleteOption.VersionIdDeleteOption)
.findAny()
.map(o -> ((DeleteOption.VersionIdDeleteOption) o).toVersionId());
}
}
29 changes: 29 additions & 0 deletions client/src/main/java/io/streamnative/oxia/client/ProtoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@
*/
package io.streamnative.oxia.client;

import io.streamnative.oxia.client.api.GetResult;
import io.streamnative.oxia.client.api.PutResult;
import io.streamnative.oxia.client.api.Version;
import io.streamnative.oxia.proto.GetResponse;
import io.streamnative.oxia.proto.PutResponse;
import java.nio.ByteBuffer;
import java.util.Optional;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

@UtilityClass
public class ProtoUtil {

public static int longToUint32(long value) {
Expand All @@ -26,4 +35,24 @@ public static int longToUint32(long value) {
public static long uint32ToLong(int unit32AsInt) {
return ByteBuffer.allocate(8).putInt(0).putInt(unit32AsInt).flip().getLong();
}

public static @NonNull PutResult getPutResultFromProto(@NonNull PutResponse response) {
return new PutResult(getVersionFromProto(response.getVersion()));
}

public static @NonNull GetResult getResultFromProto(@NonNull GetResponse response) {
return new GetResult(
response.getValue().toByteArray(), getVersionFromProto(response.getVersion()));
}

public static @NonNull Version getVersionFromProto(
@NonNull io.streamnative.oxia.proto.Version version) {
return new Version(
version.getVersionId(),
version.getCreatedTimestamp(),
version.getModifiedTimestamp(),
version.getModificationsCount(),
version.hasSessionId() ? Optional.of(version.getSessionId()) : Optional.empty(),
version.hasClientIdentity() ? Optional.of(version.getClientIdentity()) : Optional.empty());
}
}
Loading

0 comments on commit 9e9585d

Please sign in to comment.