diff --git a/src/main/java/bdledger/api/Client.java b/src/main/java/bdledger/api/Client.java index efa43a8..ee81af4 100644 --- a/src/main/java/bdledger/api/Client.java +++ b/src/main/java/bdledger/api/Client.java @@ -328,11 +328,11 @@ public class Client { public ListenableFuture getBlocks(String ledger, long startUnixTime, long endUnixTime) { info( - "*** getBlock: ledger={0} startUnixTime={1} endUnixTime={2}", + "*** getBlocks: ledger={0} startUnixTime={1} endUnixTime={2}", ledger, startUnixTime, endUnixTime); try { - return queryFutureStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime)); + return queryFutureStub.getBlocks(blocksRequest(ledger, startUnixTime, endUnixTime)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; @@ -356,11 +356,11 @@ public class Client { public GetBlocksResponse getBlocksSync(String ledger, long startUnixTime, long endUnixTime) { info( - "*** getBlockSync: ledger={0} startUnixTime={1} endUnixTime={2}", + "*** getBlocksSync: ledger={0} startUnixTime={1} endUnixTime={2}", ledger, startUnixTime, endUnixTime); try { - return queryBlockingStub.getBlocks(getBlocksRequest(ledger, startUnixTime, endUnixTime)); + return queryBlockingStub.getBlocks(blocksRequest(ledger, startUnixTime, endUnixTime)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; @@ -377,7 +377,7 @@ public class Client { info("*** blockNumber: ledger={0}", ledger); try { - return queryFutureStub.countBlocks(getBlocksRequest(ledger, -1, -1)); + return queryFutureStub.countBlocks(blocksRequest(ledger, -1, -1)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; @@ -394,14 +394,14 @@ public class Client { info("*** blockNumberSync: ledger={0}", ledger); try { - return queryBlockingStub.countBlocks(getBlocksRequest(ledger, -1, -1)); + return queryBlockingStub.countBlocks(blocksRequest(ledger, -1, -1)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; } } - private BlocksRequest getBlocksRequest(String ledger, long startTimestamp, long endTimestamp) { + private BlocksRequest blocksRequest(String ledger, long startTimestamp, long endTimestamp) { BlocksRequest.Builder reqBuilder = BlocksRequest.newBuilder().setLedger(ledger); @@ -515,6 +515,98 @@ public class Client { return reqBuilder.build(); } + /** + * 返回时间范围内的事务 + * (非阻塞) + */ + public ListenableFuture getTransactions(String ledger, ZonedDateTime startDateTime) { + return getTransactions(ledger, startDateTime.toEpochSecond()); + } + + /** + * 返回时间范围内的事务 + * (非阻塞) + */ + public ListenableFuture getTransactions(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) { + return getTransactions(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond()); + } + + /** + * 返回时间范围内的事务 + * (阻塞) + */ + public GetTransactionsResponse getTransactionsSync(String ledger, ZonedDateTime startDateTime) { + return getTransactionsSync(ledger, startDateTime.toEpochSecond()); + } + + /** + * 返回时间范围内的事务 + * (阻塞) + */ + public GetTransactionsResponse getTransactionsSync(String ledger, ZonedDateTime startDateTime, ZonedDateTime endDateTime) { + return getTransactionsSync(ledger, startDateTime.toEpochSecond(), endDateTime.toEpochSecond()); + } + + /** + * 返回时间范围内的事务 + * (非阻塞) + */ + public ListenableFuture getTransactions(String ledger, long startUnixTime) { + return getTransactions(ledger, startUnixTime, -1); + } + + /** + * 返回时间范围内的事务 + * (非阻塞) + */ + public ListenableFuture getTransactions(String ledger, long startUnixTime, long endUnixTime) { + + info( + "*** getTransactions: ledger={0} startUnixTime={1} endUnixTime={2}", + ledger, startUnixTime, endUnixTime); + + try { + return queryFutureStub.getTransactions(transactionsRequest(ledger, startUnixTime, endUnixTime)); + } catch (StatusRuntimeException e) { + warning("RPC failed: {0}", e.getStatus()); + return null; + } + } + + /** + * 返回时间范围内的事务 + * (阻塞) + */ + public GetTransactionsResponse getTransactionsSync(String ledger, long startUnixTime) { + return getTransactionsSync(ledger, startUnixTime, -1); + } + + /** + * 返回时间范围内的事务 + * (阻塞) + */ + public GetTransactionsResponse getTransactionsSync(String ledger, long startUnixTime, long endUnixTime) { + + info( + "*** getTransactionsSync: ledger={0} startUnixTime={1} endUnixTime={2}", + ledger, startUnixTime, endUnixTime); + + try { + return queryBlockingStub.getTransactions(transactionsRequest(ledger, startUnixTime, endUnixTime)); + } catch (StatusRuntimeException e) { + warning("RPC failed: {0}", e.getStatus()); + return null; + } + } + /** * 返回账本中的事务数量 @@ -525,7 +617,7 @@ public class Client { info("*** blockNumber: ledger={0}", ledger); try { - return queryFutureStub.countTransactions(transactionsRequest(ledger)); + return queryFutureStub.countTransactions(transactionsRequest(ledger, -1, -1)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; @@ -542,15 +634,25 @@ public class Client { info("*** blockNumberSync: ledger={0}", ledger); try { - return queryBlockingStub.countTransactions(transactionsRequest(ledger)); + return queryBlockingStub.countTransactions(transactionsRequest(ledger, -1, -1)); } catch (StatusRuntimeException e) { warning("RPC failed: {0}", e.getStatus()); return null; } } - private TransactionsRequest transactionsRequest(String ledger) { - return TransactionsRequest.newBuilder().setLedger(ledger).build(); + private TransactionsRequest transactionsRequest(String ledger, long startTimestamp, long endTimestamp) { + + TransactionsRequest.Builder reqBuilder = + TransactionsRequest.newBuilder().setLedger(ledger); + if (startTimestamp != -1) { + reqBuilder.setStartTimestamp(startTimestamp); + } + if (endTimestamp != -1) { + reqBuilder.setEndTimestamp(endTimestamp); + } + + return reqBuilder.build(); } private void info(String msg, Object... params) { diff --git a/src/main/java/bdledger/api/grpc/query/BlocksRequest.java b/src/main/java/bdledger/api/grpc/query/BlocksRequest.java index f24a5dd..921397e 100644 --- a/src/main/java/bdledger/api/grpc/query/BlocksRequest.java +++ b/src/main/java/bdledger/api/grpc/query/BlocksRequest.java @@ -150,10 +150,6 @@ private static final long serialVersionUID = 0L; public static final int START_TIMESTAMP_FIELD_NUMBER = 2; private long startTimestamp_; /** - *
-   * required
-   * 
- * * int64 start_timestamp = 2; * @return The startTimestamp. */ @@ -679,10 +675,6 @@ private static final long serialVersionUID = 0L; private long startTimestamp_ ; /** - *
-     * required
-     * 
- * * int64 start_timestamp = 2; * @return The startTimestamp. */ @@ -690,10 +682,6 @@ private static final long serialVersionUID = 0L; return startTimestamp_; } /** - *
-     * required
-     * 
- * * int64 start_timestamp = 2; * @param value The startTimestamp to set. * @return This builder for chaining. @@ -705,10 +693,6 @@ private static final long serialVersionUID = 0L; return this; } /** - *
-     * required
-     * 
- * * int64 start_timestamp = 2; * @return This builder for chaining. */ diff --git a/src/main/java/bdledger/api/grpc/query/BlocksRequestOrBuilder.java b/src/main/java/bdledger/api/grpc/query/BlocksRequestOrBuilder.java index 77ecc59..3cbefda 100644 --- a/src/main/java/bdledger/api/grpc/query/BlocksRequestOrBuilder.java +++ b/src/main/java/bdledger/api/grpc/query/BlocksRequestOrBuilder.java @@ -20,10 +20,6 @@ public interface BlocksRequestOrBuilder extends getLedgerBytes(); /** - *
-   * required
-   * 
- * * int64 start_timestamp = 2; * @return The startTimestamp. */ diff --git a/src/main/java/bdledger/api/grpc/query/QueryGrpc.java b/src/main/java/bdledger/api/grpc/query/QueryGrpc.java index 05a1074..00b5260 100644 --- a/src/main/java/bdledger/api/grpc/query/QueryGrpc.java +++ b/src/main/java/bdledger/api/grpc/query/QueryGrpc.java @@ -300,6 +300,9 @@ public final class QueryGrpc { } /** + *
+     * start_timestamp is required
+     * 
*/ public void getBlocks(bdledger.api.grpc.query.BlocksRequest request, io.grpc.stub.StreamObserver responseObserver) { @@ -419,6 +422,9 @@ public final class QueryGrpc { } /** + *
+     * start_timestamp is required
+     * 
*/ public void getBlocks(bdledger.api.grpc.query.BlocksRequest request, io.grpc.stub.StreamObserver responseObserver) { @@ -489,6 +495,9 @@ public final class QueryGrpc { } /** + *
+     * start_timestamp is required
+     * 
*/ public bdledger.api.grpc.query.GetBlocksResponse getBlocks(bdledger.api.grpc.query.BlocksRequest request) { return blockingUnaryCall( @@ -554,6 +563,9 @@ public final class QueryGrpc { } /** + *
+     * start_timestamp is required
+     * 
*/ public com.google.common.util.concurrent.ListenableFuture getBlocks( bdledger.api.grpc.query.BlocksRequest request) { diff --git a/src/test/java/bdledger/api/ClientTests.java b/src/test/java/bdledger/api/ClientTests.java index 28902fb..2fb7493 100644 --- a/src/test/java/bdledger/api/ClientTests.java +++ b/src/test/java/bdledger/api/ClientTests.java @@ -226,6 +226,29 @@ class ClientTests { assertEquals(tx, t); } + // TODO + @Test + @DisplayName("getTransactions#1") + void getTransactions1() throws ExecutionException, InterruptedException { + client.getTransactions(ledger, 0).get(); + } + + // TODO + @Test + @DisplayName("getTransactions#2") + void getTransactions2() throws ExecutionException, InterruptedException { + client.getTransactions(ledger, 0, 0).get(); + } + + @Test + @DisplayName("getTransactions#3") + void getTransactions3() { + Throwable e = assertThrows(Exception.class, () -> client.getTransactions("", 0, 0).get()); + Status s = Status.fromThrowable(e); + assertEquals(Status.Code.INVALID_ARGUMENT, s.getCode()); + assertEquals("ledger must not be empty", s.getDescription()); + } + @Test @DisplayName("CountTransactions#1") void CountTransactions1() throws ExecutionException, InterruptedException {