Skip to content

Commit

Permalink
misc cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Burgazzoli <lburgazzoli@gmail.com>
  • Loading branch information
lburgazzoli committed Jun 10, 2023
1 parent 36f3421 commit 73c7120
Show file tree
Hide file tree
Showing 24 changed files with 456 additions and 383 deletions.
16 changes: 13 additions & 3 deletions jetcd-core/src/main/java/io/etcd/jetcd/ByteSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public boolean startsWith(ByteSequence prefix) {
if (prefix == null) {
return false;
}
ByteString baseByteString = byteString;
ByteString prefixByteString = prefix.byteString;
return baseByteString.startsWith(prefixByteString);
return byteString.startsWith(prefix.byteString);
}

/**
Expand Down Expand Up @@ -157,10 +155,22 @@ public static ByteSequence from(String source, Charset charset) {
return new ByteSequence(ByteString.copyFrom(bytes));
}

/**
* Create new ByteSequence from a {@link ByteString}.
*
* @param source input {@link ByteString}
* @return the ByteSequence
*/
public static ByteSequence from(ByteString source) {
return new ByteSequence(source);
}

/**
* Create new ByteSequence from raw bytes.
*
* @param source input bytes
* @return the ByteSequence
*/
public static ByteSequence from(byte[] source) {
return new ByteSequence(ByteString.copyFrom(source));
}
Expand Down
6 changes: 3 additions & 3 deletions jetcd-core/src/main/java/io/etcd/jetcd/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
* Constants of Etcd.
*/
public class Constants {

public static final String TOKEN = "token";
public static final ByteSequence NULL_KEY = ByteSequence.from(new byte[] { '\0' });

public static final Metadata.Key<String> REQUIRE_LEADER_KEY = Metadata.Key.of("hasleader",
public static final Metadata.Key<String> REQUIRE_LEADER_KEY = Metadata.Key.of(
"hasleader",
Metadata.ASCII_STRING_MARSHALLER);

public static final String REQUIRE_LEADER_VALUE = "true";
}
7 changes: 5 additions & 2 deletions jetcd-core/src/main/java/io/etcd/jetcd/KeyValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public class KeyValue {

public KeyValue(io.etcd.jetcd.api.KeyValue kv, ByteSequence namespace) {
this.kv = kv;
this.unprefixedKey = ByteSequence
.from(kv.getKey().isEmpty() ? kv.getKey() : Util.unprefixNamespace(kv.getKey(), namespace));
this.value = ByteSequence.from(kv.getValue());

this.unprefixedKey = ByteSequence.from(
kv.getKey().isEmpty()
? kv.getKey()
: Util.unprefixNamespace(kv.getKey(), namespace));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion jetcd-core/src/main/java/io/etcd/jetcd/cluster/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
/**
* Util class for Cluster models.
*/
public class Util {
final class Util {
private Util() {
}

/**
* Converts a list of API member to a List of client side member.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void refresh() {
meta = null;
}

@SuppressWarnings("rawtypes")
private void authenticate(MetadataApplier applier) {
checkArgument(!manager.builder().user().isEmpty(), "username can not be empty.");
checkArgument(!manager.builder().password().isEmpty(), "password can not be empty.");
Expand Down
2 changes: 1 addition & 1 deletion jetcd-core/src/main/java/io/etcd/jetcd/impl/LeaseImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long leaseId) {
leaseStub
.leaseKeepAlive(s -> s.write(LeaseKeepAliveRequest.newBuilder().setID(leaseId).build()))
.handler(r -> future.complete(new LeaseKeepAliveResponse(r)))
.exceptionHandler(t -> future.completeExceptionally(t));
.exceptionHandler(future::completeExceptionally);

return future;
}
Expand Down
21 changes: 7 additions & 14 deletions jetcd-core/src/main/java/io/etcd/jetcd/impl/WatchImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Watch;
import io.etcd.jetcd.api.VertxWatchGrpc;
import io.etcd.jetcd.api.WatchCancelRequest;
import io.etcd.jetcd.api.WatchCreateRequest;
import io.etcd.jetcd.api.WatchProgressRequest;
import io.etcd.jetcd.api.WatchRequest;
import io.etcd.jetcd.api.WatchResponse;
import io.etcd.jetcd.api.*;
import io.etcd.jetcd.common.exception.ErrorCode;
import io.etcd.jetcd.common.exception.EtcdException;
import io.etcd.jetcd.options.OptionsUtil;
Expand All @@ -50,10 +46,7 @@
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.newClosedWatchClientException;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.newCompactedException;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.newEtcdException;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.toEtcdException;
import static io.etcd.jetcd.common.exception.EtcdExceptionFactory.*;

/**
* watch Implementation.
Expand Down Expand Up @@ -125,8 +118,8 @@ final class WatcherImpl implements Watcher {
private final AtomicBoolean closed;

//private StreamObserver<WatchRequest> stream;
private AtomicReference<WriteStream<WatchRequest>> wstream;
private AtomicBoolean started;
private final AtomicReference<WriteStream<WatchRequest>> wstream;
private final AtomicBoolean started;
private ReadStream<WatchResponse> rstream;
private long revision;
private long id;
Expand Down Expand Up @@ -355,8 +348,8 @@ private boolean shouldReschedule(final Status status) {
private void reschedule() {
Futures.addCallback(executor.schedule(this::resume, 500, TimeUnit.MILLISECONDS), new FutureCallback<Object>() {
@Override
public void onFailure(Throwable throwable) {
LOG.warn("scheduled resume failed", throwable);
public void onFailure(@NonNull Throwable t) {
LOG.warn("scheduled resume failed", t);
}

@Override
Expand Down
36 changes: 23 additions & 13 deletions jetcd-core/src/main/java/io/etcd/jetcd/options/CompactOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@
package io.etcd.jetcd.options;

public final class CompactOption {
public static final CompactOption DEFAULT = builder().build();

public static final CompactOption DEFAULT = newBuilder().build();
private final boolean physical;

private CompactOption(boolean physical) {
this.physical = physical;
}

public boolean isPhysical() {
return physical;
}

/**
* Returns the builder.
*
* @deprecated use {@link #builder()}
* @return the builder
*/
@SuppressWarnings("InlineMeSuggester")
@Deprecated
public static Builder newBuilder() {
return new Builder();
return builder();
}

public static class Builder {
public static Builder builder() {
return new Builder();
}

public static final class Builder {
private boolean physical = false;

private Builder() {
Expand All @@ -49,14 +69,4 @@ public CompactOption build() {
return new CompactOption(this.physical);
}
}

private final boolean physical;

private CompactOption(boolean physical) {
this.physical = physical;
}

public boolean isPhysical() {
return physical;
}
}
74 changes: 42 additions & 32 deletions jetcd-core/src/main/java/io/etcd/jetcd/options/DeleteOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,53 @@
import static com.google.common.base.Preconditions.checkNotNull;

public final class DeleteOption {
public static final DeleteOption DEFAULT = builder().build();

public static final DeleteOption DEFAULT = newBuilder().build();
private final ByteSequence endKey;
private final boolean prevKV;
private final boolean prefix;

private DeleteOption(ByteSequence endKey, boolean prevKV, boolean prefix) {
this.endKey = endKey;
this.prevKV = prevKV;
this.prefix = prefix;
}

public Optional<ByteSequence> getEndKey() {
return Optional.ofNullable(endKey);
}

/**
* Whether to get the previous key/value pairs before deleting them.
*
* @return true if get the previous key/value pairs before deleting them, otherwise false.
*/
public boolean isPrevKV() {
return prevKV;
}

public boolean isPrefix() {
return prefix;
}

/**
* Returns the builder.
*
* @deprecated use {@link #builder()}
* @return the builder
*/
@SuppressWarnings("InlineMeSuggester")
@Deprecated
public static Builder newBuilder() {
return new Builder();
return builder();
}

public static class Builder {
public static Builder builder() {
return new Builder();
}

private Optional<ByteSequence> endKey = Optional.empty();
public static final class Builder {
private ByteSequence endKey;
private boolean prevKV = false;
private boolean prefix = false;

Expand All @@ -59,7 +96,7 @@ private Builder() {
* @return builder
*/
public Builder withRange(ByteSequence endKey) {
this.endKey = Optional.ofNullable(endKey);
this.endKey = endKey;
return this;
}

Expand Down Expand Up @@ -112,31 +149,4 @@ public DeleteOption build() {
}

}

private final Optional<ByteSequence> endKey;
private final boolean prevKV;
private final boolean prefix;

private DeleteOption(Optional<ByteSequence> endKey, boolean prevKV, boolean prefix) {
this.endKey = endKey;
this.prevKV = prevKV;
this.prefix = prefix;
}

public Optional<ByteSequence> getEndKey() {
return endKey;
}

/**
* Whether to get the previous key/value pairs before deleting them.
*
* @return true if get the previous key/value pairs before deleting them, otherwise false.
*/
public boolean isPrevKV() {
return prevKV;
}

public boolean isPrefix() {
return prefix;
}
}
Loading

0 comments on commit 73c7120

Please sign in to comment.