Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
style: palantir java format
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed May 11, 2023
1 parent 4ef5ee5 commit 98f5446
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 101 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
</format>
</formats>
<java>
<palantirJavaFormat>
<version>2.30.0</version>
</palantirJavaFormat>
<!-- static imports first, then others, no blank lines -->
<importOrder>
<order>\#|</order>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ public class ZeroServer implements AutoCloseable {
private final RaftServer server;

public ZeroServer() throws IOException {
final RaftPeer peer = RaftPeer.newBuilder()
.setId("n0")
.setAddress("127.0.0.1:21096")
.build();
final RaftPeer peer =
RaftPeer.newBuilder().setId("n0").setAddress("127.0.0.1:21096").build();
final int port = NetUtils.createSocketAddr(peer.getAddress()).getPort();
final RaftProperties properties = new RaftProperties();
GrpcConfigKeys.Server.setPort(properties, port);
Expand All @@ -64,7 +62,6 @@ public ZeroServer() throws IOException {
.build();
}


public ZeroServer start() throws IOException {
this.server.start();
return this;
Expand All @@ -82,7 +79,8 @@ public static void main(String[] args) throws IOException {
.setAddress("127.0.0.1:21096")
.build();
final RaftProperties properties = new RaftProperties();
final GrpcClientRpc rpc = new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), properties);
final GrpcClientRpc rpc =
new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), properties);
final RaftGroupId groupId = RaftGroupId.valueOf(new UUID(0, 1));
final RaftGroup group = RaftGroup.valueOf(groupId, peer);
final RaftClient client = RaftClient.newBuilder()
Expand All @@ -91,24 +89,30 @@ public static void main(String[] args) throws IOException {
.setClientRpc(rpc)
.build();
try (client) {
var resp = client.io().send(Message.valueOf(
RequestOp.newBuilder().setRequestPut(PutRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.setValue(ByteString.copyFromUtf8("bar"))
.build()).build()));
var resp = client.io()
.send(Message.valueOf(RequestOp.newBuilder()
.setRequestPut(PutRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.setValue(ByteString.copyFromUtf8("bar"))
.build())
.build()));
System.out.println(ResponseOp.parseFrom(resp.getMessage().getContent()));
resp = client.io().sendReadOnly(Message.valueOf(RangeRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.build()));
System.out.println(RangeResponse.parseFrom(resp.getMessage().getContent()));
resp = client.io().send(Message.valueOf(
RequestOp.newBuilder().setRequestDeleteRange(DeleteRangeRequest.newBuilder()
resp = client.io()
.sendReadOnly(Message.valueOf(RangeRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.build()).build()));
.build()));
System.out.println(RangeResponse.parseFrom(resp.getMessage().getContent()));
resp = client.io()
.send(Message.valueOf(RequestOp.newBuilder()
.setRequestDeleteRange(DeleteRangeRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.build())
.build()));
System.out.println(ResponseOp.parseFrom(resp.getMessage().getContent()));
resp = client.io().sendReadOnly(Message.valueOf(RangeRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.build()));
resp = client.io()
.sendReadOnly(Message.valueOf(RangeRequest.newBuilder()
.setKey(ByteString.copyFromUtf8("foo"))
.build()));
System.out.println(RangeResponse.parseFrom(resp.getMessage().getContent()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

package io.korandoru.zeronos.server.exception;

public sealed abstract class ZeronosServerException extends RuntimeException {
public abstract sealed class ZeronosServerException extends RuntimeException {

public static final class FatalError extends ZeronosServerException {
}

public static final class RevisionNotFound extends ZeronosServerException {
}
public static final class FatalError extends ZeronosServerException {}

public static final class RevisionNotFound extends ZeronosServerException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ public int walk(Predicate<Revision> predicate) {
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public void put(Revision revision) {
public IndexGetResult get(long revision) {
if (generations.isEmpty()) {
final ZeronosServerException e = new ZeronosServerException.FatalError();
log.atError()
.addKeyValue("key", new String(key))
.log("'get' got an unexpected empty keyIndex", e);
log.atError().addKeyValue("key", new String(key)).log("'get' got an unexpected empty keyIndex", e);
throw e;
}

Expand Down Expand Up @@ -127,9 +125,7 @@ Generation findGeneration(long revision) {
public void tombstone(Revision revision) {
if (generations.isEmpty()) {
final ZeronosServerException e = new ZeronosServerException.FatalError();
log.atError()
.addKeyValue("key", new String(key))
.log("'tombstone' got an unexpected empty keyIndex", e);
log.atError().addKeyValue("key", new String(key)).log("'tombstone' got an unexpected empty keyIndex", e);
throw e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,4 @@ private void unsafeVisit(byte[] key, byte[] end, Function<KeyIndex, Boolean> f)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ public class IndexGetResult {
private final Revision modified;
private final Revision created;
private final long version;

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ public class IndexRangeResult {
private final List<Revision> revisions;
private final List<KeyBytes> keys;
private final long total;

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public CompletableFuture<Message> applyTransaction(TransactionContext trx) {

backend.unsafePut(Namespace.KEY, revision.toBytes(), kv.toByteArray());
treeIndex.put(key, revision);
message = Message.valueOf(ResponseOp.newBuilder().setResponsePut(PutResponse.newBuilder().build()).build());
message = Message.valueOf(ResponseOp.newBuilder()
.setResponsePut(PutResponse.newBuilder().build())
.build());
}
case REQUEST_DELETE_RANGE -> {
final DeleteRangeRequest req = op.getRequestDeleteRange();
Expand All @@ -179,13 +181,16 @@ public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
final IndexRangeResult r = treeIndex.range(key, end, revision);
final Revision rev = new Revision(revision);
for (KeyBytes k : r.getKeys()) {
final KeyValue kv = KeyValue.newBuilder()
.setKey(k.toByteString())
.build();
final KeyValue kv =
KeyValue.newBuilder().setKey(k.toByteString()).build();
backend.unsafePut(Namespace.KEY, rev.toBytes(true), kv.toByteArray());
treeIndex.tombstone(k.getKey(), rev);
}
message = Message.valueOf(ResponseOp.newBuilder().setResponseDeleteRange(DeleteRangeResponse.newBuilder().setDeleted(r.getTotal()).build()).build());
message = Message.valueOf(ResponseOp.newBuilder()
.setResponseDeleteRange(DeleteRangeResponse.newBuilder()
.setDeleted(r.getTotal())
.build())
.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public interface Backend {
BackendRangeResult unsafeRange(Namespace ns, byte[] key, byte[] end, long limit);

void unsafePut(Namespace ns, byte[] key, byte[] value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ public void unsafePut(Namespace ns, byte[] key, byte[] value) {
try {
db.put(bytes, value);
} catch (RocksDBException e) {
log.atError()
.addKeyValue("namespace", ns)
.log("failed to write to the backend", e);
log.atError().addKeyValue("namespace", ns).log("failed to write to the backend", e);
throw new UncheckedIOException(new IOException(e));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,25 @@ class GenerationTest {
@Test
void testIsEmpty() {
assertThat(new Generation(0, new Revision(), List.of()).isEmpty()).isTrue();
assertThat(new Generation(0, new Revision(), List.of(new Revision(1))).isEmpty()).isFalse();
assertThat(new Generation(0, new Revision(), List.of(new Revision(1))).isEmpty())
.isFalse();
}

@Test
void testWalk() {
final List<Revision> revisions = List.of(new Revision(2), new Revision(4), new Revision(6));
final Generation generation = new Generation(0, revisions.get(0), revisions);
record TestCase(Predicate<Revision> predicate, int result) {
}
final TestCase[] tests = new TestCase[]{
new TestCase(r -> r.getMain() >= 7, 2),
new TestCase(r -> r.getMain() >= 6, 1),
new TestCase(r -> r.getMain() >= 5, 1),
new TestCase(r -> r.getMain() >= 4, 0),
new TestCase(r -> r.getMain() >= 3, 0),
new TestCase(r -> r.getMain() >= 2, -1),
record TestCase(Predicate<Revision> predicate, int result) {}
final TestCase[] tests = new TestCase[] {
new TestCase(r -> r.getMain() >= 7, 2),
new TestCase(r -> r.getMain() >= 6, 1),
new TestCase(r -> r.getMain() >= 5, 1),
new TestCase(r -> r.getMain() >= 4, 0),
new TestCase(r -> r.getMain() >= 3, 0),
new TestCase(r -> r.getMain() >= 2, -1),
};
for (TestCase test : tests) {
assertThat(generation.walk(test.predicate)).isEqualTo(test.result);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ void testTombstone() {
{
final KeyIndex keyIndex1 = KeyIndex.of("foo");
keyIndex1.setModified(new Revision(7));
keyIndex1.getGenerations().add(new Generation(2, new Revision(5), List.of(new Revision(5), new Revision(7))));
keyIndex1
.getGenerations()
.add(new Generation(2, new Revision(5), List.of(new Revision(5), new Revision(7))));
assertThat(keyIndex0).isEqualTo(keyIndex1);
}

Expand All @@ -44,8 +46,13 @@ void testTombstone() {
{
final KeyIndex keyIndex1 = KeyIndex.of("foo");
keyIndex1.setModified(new Revision(15));
keyIndex1.getGenerations().add(new Generation(2, new Revision(5), List.of(new Revision(5), new Revision(7))));
keyIndex1.getGenerations().add(new Generation(3, new Revision(8), List.of(new Revision(8), new Revision(9), new Revision(15))));
keyIndex1
.getGenerations()
.add(new Generation(2, new Revision(5), List.of(new Revision(5), new Revision(7))));
keyIndex1
.getGenerations()
.add(new Generation(
3, new Revision(8), List.of(new Revision(8), new Revision(9), new Revision(15))));
assertThat(keyIndex0).isEqualTo(keyIndex1);
}

Expand Down Expand Up @@ -86,5 +93,4 @@ private static KeyIndex createTestKeyIndex() {
keyIndex.tombstone(new Revision(16));
return keyIndex;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ class RevisionTest {

@Test
void testRevision() {
final Revision[] revisions = new Revision[]{
new Revision(),
new Revision(1),
new Revision(1, 1),
new Revision(2),
new Revision(Long.MAX_VALUE, Long.MAX_VALUE),
final Revision[] revisions = new Revision[] {
new Revision(),
new Revision(1),
new Revision(1, 1),
new Revision(2),
new Revision(Long.MAX_VALUE, Long.MAX_VALUE),
};

for (int i = 0; i < revisions.length - 1; i++) {
assertThat(revisions[i]).isLessThan(revisions[i + 1]);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,48 @@ record TestCase(byte[] key, byte[] end, long revision, int limit, List<Revision>
revision,
limit,
Arrays.stream(revisions).toList(),
count
);
count);
}
}

final TestCase[] tests = new TestCase[]{
// single key that not found
new TestCase("bar", null, 6, 0, 0),
// single key that found
new TestCase("foo", null, 6, 0, 1, new Revision(6)),
// various range keys, fixed atRev, unlimited
new TestCase("foo", "foo1", 6, 0, 1, new Revision(6)),
new TestCase("foo", "foo2", 6, 0, 2, new Revision(6), new Revision(5)),
new TestCase("foo", "fop", 6, 0, 3, new Revision(6), new Revision(5), new Revision(4)),
new TestCase("foo1", "fop", 6, 0, 2, new Revision(5), new Revision(4)),
new TestCase("foo2", "fop", 6, 0, 1, new Revision(4)),
new TestCase("foo3", "fop", 6, 0, 0),
// fixed range keys, various atRev, unlimited
new TestCase("foo1", "fop", 1, 0, 0),
new TestCase("foo1", "fop", 2, 1, 1, new Revision(2)),
new TestCase("foo1", "fop", 3, 2, 2, new Revision(2), new Revision(3)),
new TestCase("foo1", "fop", 4, 2, 2, new Revision(2), new Revision(4)),
new TestCase("foo1", "fop", 5, 2, 2, new Revision(5), new Revision(4)),
new TestCase("foo1", "fop", 6, 2, 2, new Revision(5), new Revision(4)),
// fixed range keys, fixed atRev, various limit
new TestCase("foo", "fop", 6, 1, 3, new Revision(6)),
new TestCase("foo", "fop", 6, 2, 3, new Revision(6), new Revision(5)),
new TestCase("foo", "fop", 6, 3, 3, new Revision(6), new Revision(5), new Revision(4)),
new TestCase("foo", "fop", 3, 1, 3, new Revision(1)),
new TestCase("foo", "fop", 3, 2, 3, new Revision(1), new Revision(2)),
new TestCase("foo", "fop", 3, 3, 3, new Revision(1), new Revision(2), new Revision(3)),
final TestCase[] tests = new TestCase[] {
// single key that not found
new TestCase("bar", null, 6, 0, 0),
// single key that found
new TestCase("foo", null, 6, 0, 1, new Revision(6)),
// various range keys, fixed atRev, unlimited
new TestCase("foo", "foo1", 6, 0, 1, new Revision(6)),
new TestCase("foo", "foo2", 6, 0, 2, new Revision(6), new Revision(5)),
new TestCase("foo", "fop", 6, 0, 3, new Revision(6), new Revision(5), new Revision(4)),
new TestCase("foo1", "fop", 6, 0, 2, new Revision(5), new Revision(4)),
new TestCase("foo2", "fop", 6, 0, 1, new Revision(4)),
new TestCase("foo3", "fop", 6, 0, 0),
// fixed range keys, various atRev, unlimited
new TestCase("foo1", "fop", 1, 0, 0),
new TestCase("foo1", "fop", 2, 1, 1, new Revision(2)),
new TestCase("foo1", "fop", 3, 2, 2, new Revision(2), new Revision(3)),
new TestCase("foo1", "fop", 4, 2, 2, new Revision(2), new Revision(4)),
new TestCase("foo1", "fop", 5, 2, 2, new Revision(5), new Revision(4)),
new TestCase("foo1", "fop", 6, 2, 2, new Revision(5), new Revision(4)),
// fixed range keys, fixed atRev, various limit
new TestCase("foo", "fop", 6, 1, 3, new Revision(6)),
new TestCase("foo", "fop", 6, 2, 3, new Revision(6), new Revision(5)),
new TestCase("foo", "fop", 6, 3, 3, new Revision(6), new Revision(5), new Revision(4)),
new TestCase("foo", "fop", 3, 1, 3, new Revision(1)),
new TestCase("foo", "fop", 3, 2, 3, new Revision(1), new Revision(2)),
new TestCase("foo", "fop", 3, 3, 3, new Revision(1), new Revision(2), new Revision(3)),
};
for (TestCase test : tests) {
assertThat(treeIndex.range(test.key, test.end, test.revision).getTotal()).isEqualTo(test.count);
assertThat(treeIndex.range(test.key, test.end, test.revision, test.limit).getTotal()).isEqualTo(test.count);
assertThat(treeIndex.range(test.key, test.end, test.revision, test.limit).getRevisions()).isEqualTo(test.revisions);
assertThat(treeIndex.range(test.key, test.end, test.revision).getTotal())
.isEqualTo(test.count);
assertThat(treeIndex
.range(test.key, test.end, test.revision, test.limit)
.getTotal())
.isEqualTo(test.count);
assertThat(treeIndex
.range(test.key, test.end, test.revision, test.limit)
.getRevisions())
.isEqualTo(test.revisions);
}
}

Expand All @@ -112,7 +118,8 @@ void testTombstone() {
treeIndex.put(key.getKey(), new Revision(1));
treeIndex.tombstone(key.getKey(), new Revision(2));
assertThrows(ZeronosServerException.RevisionNotFound.class, () -> treeIndex.get(key.getKey(), 2));
assertThrows(ZeronosServerException.RevisionNotFound.class, () -> treeIndex.tombstone(key.getKey(), new Revision(3)));
assertThrows(
ZeronosServerException.RevisionNotFound.class,
() -> treeIndex.tombstone(key.getKey(), new Revision(3)));
}

}

0 comments on commit 98f5446

Please sign in to comment.