|
17 | 17 | package com.relationalai; |
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | +import com.jsoniter.any.Any; |
20 | 21 | import org.apache.arrow.memory.RootAllocator; |
21 | 22 | import org.apache.arrow.vector.VectorSchemaRoot; |
22 | 23 | import org.apache.arrow.vector.ipc.ArrowStreamReader; |
@@ -335,10 +336,10 @@ private String parseArrowResponse(ByteArrayOutputStream out) throws IOException |
335 | 336 | result.put(f.getName(), String.valueOf(readBatch.getVector(f))); |
336 | 337 | } |
337 | 338 | } |
| 339 | + // serialize map to json string |
338 | 340 | return new ObjectMapper().writeValueAsString(result); |
339 | 341 | } |
340 | 342 |
|
341 | | - |
342 | 343 | static void printRequest(HttpRequest request) { |
343 | 344 | System.out.printf("%s %s\n", request.method(), request.uri()); |
344 | 345 | for (Map.Entry<String, List<String>> entry : request.headers().map().entrySet()) { |
@@ -712,52 +713,90 @@ public TransactionResult execute( |
712 | 713 | return Json.deserialize(rsp, TransactionResult.class); |
713 | 714 | } |
714 | 715 |
|
715 | | - // TODO: map string to result object |
716 | | - public String executeAsync( |
| 716 | + public HashMap<String, Any> executeAsyncWait( |
| 717 | + String database, String engine, String source, boolean readonly) throws HttpError, IOException, InterruptedException { |
| 718 | + return executeAsyncWait(database, engine, source, readonly, null); |
| 719 | + } |
| 720 | + |
| 721 | + public HashMap<String, Any> executeAsyncWait( |
| 722 | + String database, String engine, |
| 723 | + String source, boolean readonly, |
| 724 | + Map<String, String> inputs) throws HttpError, IOException, InterruptedException { |
| 725 | + String transactionId = null; |
| 726 | + var output = new HashMap<String, Any>(); |
| 727 | + |
| 728 | + var rsp = executeAsync(database, engine, source, readonly, inputs); |
| 729 | + |
| 730 | + try { |
| 731 | + transactionId = rsp.asMap().get("id").toString(); |
| 732 | + } catch (ClassCastException e) { |
| 733 | + transactionId = rsp.get(0).asMap().get("id").toString(); |
| 734 | + } |
| 735 | + |
| 736 | + var state = getTransaction(transactionId) |
| 737 | + .asMap() |
| 738 | + .get("transaction") |
| 739 | + .asMap() |
| 740 | + .get("state") |
| 741 | + .toString(); |
| 742 | + |
| 743 | + while (!"COMPLETED".equals(state)){ |
| 744 | + Thread.sleep(2000); |
| 745 | + |
| 746 | + state = getTransaction(transactionId) |
| 747 | + .asMap() |
| 748 | + .get("transaction") |
| 749 | + .asMap() |
| 750 | + .get("state") |
| 751 | + .toString(); |
| 752 | + } |
| 753 | + |
| 754 | + output.put("results", getTransactionResults(transactionId)); |
| 755 | + output.put("metadata", getTransactionMetadata(transactionId)); |
| 756 | + output.put("problems", getTransactionProblems(transactionId)); |
| 757 | + |
| 758 | + return output; |
| 759 | + } |
| 760 | + |
| 761 | + public Any executeAsync( |
717 | 762 | String database, String engine, String source, boolean readonly) throws HttpError, IOException, InterruptedException { |
718 | 763 | return executeAsync(database, engine, source, readonly, null); |
719 | 764 | } |
720 | 765 |
|
721 | | - // TODO: map string to result object |
722 | | - public String executeAsync( |
| 766 | + public Any executeAsync( |
723 | 767 | String database, String engine, |
724 | 768 | String source, boolean readonly, |
725 | 769 | Map<String, String> inputs) throws HttpError, IOException, InterruptedException { |
726 | 770 | var tx = new TransactionAsync(database, engine, source, readonly); |
727 | 771 | var action = DbAction.makeQueryAction(source, inputs); |
728 | 772 | var body = tx.payload(action); |
729 | 773 | var rsp = post(PATH_TRANSACTIONS, tx.queryParams(), body); |
730 | | - return rsp; |
| 774 | + return Json.deserialize(rsp); |
731 | 775 | } |
732 | 776 |
|
733 | | - // TODO: map string to result object |
734 | | - public String getTransaction(String id) throws HttpError, IOException, InterruptedException { |
| 777 | + public Any getTransaction(String id) throws HttpError, IOException, InterruptedException { |
735 | 778 | var rsp = get(String.format("%s/%s", PATH_TRANSACTIONS, id)); |
736 | | - return rsp; |
| 779 | + return Json.deserialize(rsp); |
737 | 780 | } |
738 | 781 |
|
739 | | - // TODO: map string to result object |
740 | | - public String getTransactions() throws HttpError, IOException, InterruptedException { |
| 782 | + public Any getTransactions() throws HttpError, IOException, InterruptedException { |
741 | 783 | var rsp = get(PATH_TRANSACTIONS); |
742 | | - return rsp; |
| 784 | + return Json.deserialize(rsp); |
743 | 785 | } |
744 | 786 |
|
745 | | - // TODO: map string to result object |
746 | | - public String getTransactionResults(String id) throws HttpError, IOException, InterruptedException { |
| 787 | + public Any getTransactionResults(String id) throws HttpError, IOException, InterruptedException { |
747 | 788 | var rsp = get(String.format("%s/%s/results", PATH_TRANSACTIONS, id)); |
748 | | - return rsp; |
| 789 | + return Json.deserialize(rsp); |
749 | 790 | } |
750 | 791 |
|
751 | | - // TODO: map string to result object |
752 | | - public String getTransactionMetadata(String id) throws HttpError, IOException, InterruptedException { |
| 792 | + public Any getTransactionMetadata(String id) throws HttpError, IOException, InterruptedException { |
753 | 793 | var rsp = get(String.format("%s/%s/metadata", PATH_TRANSACTIONS, id)); |
754 | | - return rsp; |
| 794 | + return Json.deserialize(rsp); |
755 | 795 | } |
756 | 796 |
|
757 | | - // TODO: map string to result object |
758 | | - public String getTransactionProblems(String id) throws HttpError, IOException, InterruptedException { |
| 797 | + public Any getTransactionProblems(String id) throws HttpError, IOException, InterruptedException { |
759 | 798 | var rsp = get(String.format("%s/%s/problems", PATH_TRANSACTIONS, id)); |
760 | | - return rsp; |
| 799 | + return Json.deserialize(rsp); |
761 | 800 | } |
762 | 801 |
|
763 | 802 | // EDBs |
|
0 commit comments