diff --git a/ingester-example/src/main/java/io/greptime/WritePOJOsQuickStart.java b/ingester-example/src/main/java/io/greptime/WritePOJOsQuickStart.java index a0288dd..b372f4d 100644 --- a/ingester-example/src/main/java/io/greptime/WritePOJOsQuickStart.java +++ b/ingester-example/src/main/java/io/greptime/WritePOJOsQuickStart.java @@ -62,10 +62,13 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc Result result = puts.get(); - if (result.isOk()) { - LOG.info("Write result: {}", result.getOk()); + Result simpleResult = result // + .map(WriteOk::getSuccess) // + .mapErr(err -> err.getError().getMessage()); + if (simpleResult.isOk()) { + LOG.info("Write success: {}", simpleResult.getOk()); } else { - LOG.error("Failed to write: {}", result.getErr()); + LOG.error("Failed to write: {}", simpleResult.getErr()); } List> delete_pojos = Arrays.asList(cpus.subList(0, 5), memories.subList(0, 5)); diff --git a/ingester-example/src/main/java/io/greptime/WriteQuickStart.java b/ingester-example/src/main/java/io/greptime/WriteQuickStart.java index 0db2397..2e55e03 100644 --- a/ingester-example/src/main/java/io/greptime/WriteQuickStart.java +++ b/ingester-example/src/main/java/io/greptime/WriteQuickStart.java @@ -76,10 +76,13 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc Result result = future.get(); - if (result.isOk()) { - LOG.info("Write result: {}", result.getOk()); + Result simpleResult = result // + .map(WriteOk::getSuccess) // + .mapErr(err -> err.getError().getMessage()); + if (simpleResult.isOk()) { + LOG.info("Write success: {}", simpleResult.getOk()); } else { - LOG.error("Failed to write: {}", result.getErr()); + LOG.error("Failed to write: {}", simpleResult.getErr()); } List delete_objs = Arrays.asList(cpuMetric.subRange(0, 5), memMetric.subRange(0, 5)); diff --git a/ingester-protocol/src/test/java/io/greptime/models/ResultTest.java b/ingester-protocol/src/test/java/io/greptime/models/ResultTest.java index 8021984..a05020b 100644 --- a/ingester-protocol/src/test/java/io/greptime/models/ResultTest.java +++ b/ingester-protocol/src/test/java/io/greptime/models/ResultTest.java @@ -25,60 +25,60 @@ public class ResultTest { @Test public void testMap() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Result r2 = r1.map(WriteOk::getSuccess); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Result r2 = r1.map(WriteOk::getSuccess); Assert.assertEquals(2, r2.getOk().intValue()); - final Result r5 = Result.err(Err.writeErr(400, null, null)); - final Result r6 = r5.map(WriteOk::getSuccess); + Result r5 = Result.err(Err.writeErr(400, null, null)); + Result r6 = r5.map(WriteOk::getSuccess); Assert.assertFalse(r6.isOk()); } @Test public void testMapOr() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Integer r2 = r1.mapOr(-1, WriteOk::getSuccess); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Integer r2 = r1.mapOr(-1, WriteOk::getSuccess); Assert.assertEquals(2, r2.intValue()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final Integer r4 = r3.mapOr(-1, WriteOk::getSuccess); + Result r3 = Result.err(Err.writeErr(400, null, null)); + Integer r4 = r3.mapOr(-1, WriteOk::getSuccess); Assert.assertEquals(-1, r4.intValue()); } @Test public void testMapOrElse() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Integer r2 = r1.mapOrElse(err -> -1, WriteOk::getSuccess); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Integer r2 = r1.mapOrElse(err -> -1, WriteOk::getSuccess); Assert.assertEquals(2, r2.intValue()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final Integer r4 = r3.mapOrElse(err -> -1, WriteOk::getSuccess); + Result r3 = Result.err(Err.writeErr(400, null, null)); + Integer r4 = r3.mapOrElse(err -> -1, WriteOk::getSuccess); Assert.assertEquals(-1, r4.intValue()); } @Test public void testMapErr() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Result r2 = r1.mapErr(Err::getError); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Result r2 = r1.mapErr(Err::getError); Assert.assertEquals(2, r2.getOk().getSuccess()); IllegalStateException error = new IllegalStateException("error test"); - final Result r3 = Result.err(Err.writeErr(400, error, null)); - final Result r4 = r3.mapErr(Err::getError); + Result r3 = Result.err(Err.writeErr(400, error, null)); + Result r4 = r3.mapErr(Err::getError); Assert.assertEquals("error test", r4.getErr().getMessage()); } @Test public void testAndThen() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Result r2 = r1.andThen(writeOk -> { + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Result r2 = r1.andThen(writeOk -> { WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0); return newOne.mapToResult(); }); Assert.assertEquals(3, r2.getOk().getSuccess()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final Result r4 = r3.andThen(writeOk -> { + Result r3 = Result.err(Err.writeErr(400, null, null)); + Result r4 = r3.andThen(writeOk -> { WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0); return newOne.mapToResult(); }); @@ -87,34 +87,34 @@ public void testAndThen() { @Test public void testOrElse() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final Result r2 = r1.orElse(err -> Result.ok(WriteOk.ok(0, 0))); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + Result r2 = r1.orElse(err -> Result.ok(WriteOk.ok(0, 0))); Assert.assertEquals(2, r2.getOk().getSuccess()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final Result r4 = r3.orElse(err -> Result.ok(WriteOk.ok(0, 0))); + Result r3 = Result.err(Err.writeErr(400, null, null)); + Result r4 = r3.orElse(err -> Result.ok(WriteOk.ok(0, 0))); Assert.assertEquals(0, r4.getOk().getSuccess()); } @Test public void testUnwrapOr() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final WriteOk r2 = r1.unwrapOr(WriteOk.emptyOk()); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + WriteOk r2 = r1.unwrapOr(WriteOk.emptyOk()); Assert.assertEquals(2, r2.getSuccess()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final WriteOk r4 = r3.unwrapOr(WriteOk.emptyOk()); + Result r3 = Result.err(Err.writeErr(400, null, null)); + WriteOk r4 = r3.unwrapOr(WriteOk.emptyOk()); Assert.assertEquals(0, r4.getSuccess()); } @Test public void testUnwrapOrElse() { - final Result r1 = Result.ok(WriteOk.ok(2, 0)); - final WriteOk r2 = r1.unwrapOrElse(err -> WriteOk.emptyOk()); + Result r1 = Result.ok(WriteOk.ok(2, 0)); + WriteOk r2 = r1.unwrapOrElse(err -> WriteOk.emptyOk()); Assert.assertEquals(2, r2.getSuccess()); - final Result r3 = Result.err(Err.writeErr(400, null, null)); - final WriteOk r4 = r3.unwrapOrElse(err -> WriteOk.emptyOk()); + Result r3 = Result.err(Err.writeErr(400, null, null)); + WriteOk r4 = r3.unwrapOrElse(err -> WriteOk.emptyOk()); Assert.assertEquals(0, r4.getSuccess()); } }