You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/05/24 08:15:47 UTC

[GitHub] [ignite-3] tledkov-gridgain commented on a diff in pull request #817: IGNITE-16964 SQL API: Implement async SQL API

tledkov-gridgain commented on code in PR #817:
URL: https://github.com/apache/ignite-3/pull/817#discussion_r880200648


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/api/AsyncResultSetImpl.java:
##########
@@ -0,0 +1,410 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.api;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.BitSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionStage;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.internal.sql.engine.AsyncCursor.BatchedResult;
+import org.apache.ignite.internal.sql.engine.AsyncSqlCursor;
+import org.apache.ignite.internal.sql.engine.SqlQueryType;
+import org.apache.ignite.sql.NoRowSetExpectedException;
+import org.apache.ignite.sql.ResultSetMetadata;
+import org.apache.ignite.sql.SqlRow;
+import org.apache.ignite.sql.async.AsyncResultSet;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Asynchronous result set implementation.
+ */
+public class AsyncResultSetImpl implements AsyncResultSet {
+    private static final CompletableFuture<? extends AsyncResultSet> HAS_NO_MORE_PAGE_FUTURE =
+            CompletableFuture.failedFuture(new IgniteSqlException("There are no more pages."));
+
+    private final AsyncSqlCursor<List<Object>> cur;
+
+    private final BatchedResult<List<Object>> batchPage;
+
+    private final Page page;
+
+    private final int pageSize;
+
+    private final Runnable closeRun;
+
+    private final Object mux = new Object();
+
+    private volatile CompletionStage<? extends AsyncResultSet> next;
+
+    /**
+     * Constructor.
+     *
+     * @param cur Asynchronous query cursor.
+     */
+    public AsyncResultSetImpl(AsyncSqlCursor<List<Object>> cur, BatchedResult<List<Object>> page, int pageSize, Runnable closeRun) {
+        this.cur = cur;
+        this.batchPage = page;
+        this.pageSize = pageSize;
+        this.closeRun = closeRun;
+        this.page = new Page();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable ResultSetMetadata metadata() {
+        throw new UnsupportedOperationException("Not implemented yet.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasRowSet() {
+        return cur.queryType() == SqlQueryType.QUERY || cur.queryType() == SqlQueryType.EXPLAIN;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public long affectedRows() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DDL) {
+            return -1;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Long
+                && !batchPage.hasMore() : "Invalid DML result: " + batchPage;
+
+        return (long) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean wasApplied() {
+        if (hasRowSet() || cur.queryType() == SqlQueryType.DML) {
+            return false;
+        }
+
+        assert batchPage.items().size() == 1
+                && batchPage.items().get(0).size() == 1
+                && batchPage.items().get(0).get(0) instanceof Boolean
+                && !batchPage.hasMore() : "Invalid DDL result: " + batchPage;
+
+        return (boolean) batchPage.items().get(0).get(0);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Iterable<SqlRow> currentPage() {
+        if (!hasRowSet()) {
+            throw new NoRowSetExpectedException("Query hasn't result set: [type=" + cur.queryType() + ']');
+        }
+
+        return page;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<? extends AsyncResultSet> fetchNextPage() {
+        if (next == null) {
+            synchronized (mux) {
+                if (next == null) {
+                    if (!hasMorePages()) {
+                        next = HAS_NO_MORE_PAGE_FUTURE;
+                    } else {
+                        next = cur.requestNextAsync(pageSize)
+                                .thenApply(batchRes -> new AsyncResultSetImpl(cur, batchRes, pageSize, closeRun));
+                    }
+                }
+
+            }
+        }
+
+        return next;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean hasMorePages() {
+        return batchPage.hasMore();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletionStage<Void> closeAsync() {
+        return cur.closeAsync().thenAccept((v) -> closeRun.run());
+    }
+
+    private class Page implements Iterable<SqlRow> {
+        /** {@inheritDoc} */
+        @NotNull
+        @Override
+        public Iterator<SqlRow> iterator() {
+            return batchPage.items().stream()
+                    .map(RowTuple::new)
+                    .map(SqlRow.class::cast)
+                    .iterator();
+        }
+    }
+
+    private class RowTuple implements SqlRow {

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org