33import java .sql .SQLException ;
44import java .time .Duration ;
55import java .util .Collection ;
6- import java .util .Collections ;
7- import java .util .concurrent .CompletableFuture ;
86import java .util .concurrent .LinkedBlockingQueue ;
97import java .util .concurrent .atomic .AtomicReference ;
108
11- import tech .ydb .core .Result ;
9+ import tech .ydb .core .Status ;
1210import tech .ydb .core .StatusCode ;
1311import tech .ydb .core .grpc .GrpcReadStream ;
1412import tech .ydb .jdbc .YdbConst ;
13+ import tech .ydb .jdbc .YdbQueryResult ;
1514import tech .ydb .jdbc .YdbResultSet ;
1615import tech .ydb .jdbc .YdbStatement ;
1716import tech .ydb .jdbc .YdbTracer ;
1817import tech .ydb .jdbc .common .YdbTypes ;
1918import tech .ydb .jdbc .exception .YdbRetryableException ;
20- import tech .ydb .jdbc .impl .YdbQueryResult ;
21- import tech .ydb .jdbc .impl .YdbStaticResultSet ;
19+ import tech .ydb .jdbc .impl .YdbQueryResultReader ;
20+ import tech .ydb .jdbc .impl .YdbQueryResultStatic ;
21+ import tech .ydb .jdbc .impl .YdbResultSetMemory ;
2222import tech .ydb .jdbc .query .QueryType ;
2323import tech .ydb .jdbc .query .YdbQuery ;
2424import tech .ydb .table .Session ;
@@ -66,7 +66,8 @@ protected Session createNewTableSession(YdbValidator validator) throws SQLExcept
6666 return validator .call ("Get session" , null , () -> tableClient .createSession (sessionTimeout ));
6767 }
6868
69- protected void closeCurrentResult () throws SQLException {
69+ @ Override
70+ public void clearState () throws SQLException {
7071 YdbQueryResult rs = currResult .get ();
7172 if (rs != null ) {
7273 rs .close ();
@@ -83,20 +84,18 @@ protected YdbQueryResult updateCurrentResult(YdbQueryResult result) throws SQLEx
8384
8485 @ Override
8586 public void ensureOpened () throws SQLException {
86- closeCurrentResult ();
8787 if (isClosed ()) {
8888 throw new SQLException (YdbConst .CLOSED_CONNECTION );
8989 }
9090 }
9191
92-
9392 @ Override
94- public YdbQueryResult executeDataQuery (YdbStatement statement , YdbQuery query , String preparedYql , Params params ,
95- long timeout , boolean keepInCache ) throws SQLException {
93+ public YdbQueryResult executeDataQuery (YdbStatement statement , YdbQuery query , String preparedYql , Params params )
94+ throws SQLException {
9695 boolean insideTx = isInsideTransaction ();
9796 while (true ) {
9897 try {
99- return executeQueryImpl (statement , query , preparedYql , params , timeout , keepInCache );
98+ return executeQueryImpl (statement , query , preparedYql , params );
10099 } catch (YdbRetryableException ex ) {
101100 if (insideTx || ex .getStatus ().getCode () != StatusCode .BAD_SESSION ) {
102101 throw ex ;
@@ -106,7 +105,7 @@ public YdbQueryResult executeDataQuery(YdbStatement statement, YdbQuery query, S
106105 }
107106
108107 protected abstract YdbQueryResult executeQueryImpl (YdbStatement statement , YdbQuery query , String preparedYql ,
109- Params params , long timeout , boolean keepInCache ) throws SQLException ;
108+ Params params ) throws SQLException ;
110109
111110 @ Override
112111 public YdbQueryResult executeSchemeQuery (YdbStatement statement , YdbQuery query ) throws SQLException {
@@ -130,7 +129,7 @@ public YdbQueryResult executeSchemeQuery(YdbStatement statement, YdbQuery query)
130129 tracer .close ();
131130 }
132131
133- return updateCurrentResult (new StaticQueryResult (query , Collections . emptyList () ));
132+ return updateCurrentResult (new YdbQueryResultStatic (query ));
134133 }
135134
136135 @ Override
@@ -152,7 +151,7 @@ public YdbQueryResult executeBulkUpsert(YdbStatement statement, YdbQuery query,
152151 tracer .close ();
153152 }
154153
155- return updateCurrentResult (new StaticQueryResult (query , Collections . emptyList () ));
154+ return updateCurrentResult (new YdbQueryResultStatic (query ));
156155 }
157156
158157 @ Override
@@ -164,9 +163,6 @@ public YdbQueryResult executeScanQuery(YdbStatement statement, YdbQuery query, S
164163 YdbContext ctx = statement .getConnection ().getCtx ();
165164 YdbValidator validator = statement .getValidator ();
166165 Duration scanQueryTimeout = ctx .getOperationProperties ().getScanQueryTimeout ();
167- ExecuteScanQuerySettings settings = ExecuteScanQuerySettings .newBuilder ()
168- .withRequestTimeout (scanQueryTimeout )
169- .build ();
170166 String msg = QueryType .SCAN_QUERY + " >>\n " + yql ;
171167
172168 YdbTracer tracer = ctx .getTracer ();
@@ -177,50 +173,49 @@ public YdbQueryResult executeScanQuery(YdbStatement statement, YdbQuery query, S
177173
178174 if (!useStreamResultSet ) {
179175 try {
176+ ExecuteScanQuerySettings settings = ExecuteScanQuerySettings .newBuilder ()
177+ .withRequestTimeout (scanQueryTimeout )
178+ .build ();
179+
180180 Collection <ResultSetReader > resultSets = new LinkedBlockingQueue <>();
181181
182182 ctx .traceQueryByFullScanDetector (query , yql );
183183 validator .execute (QueryType .SCAN_QUERY + " >>\n " + yql , tracer ,
184184 () -> session .executeScanQuery (yql , params , settings ).start (resultSets ::add )
185185 );
186186
187- YdbResultSet rs = new YdbStaticResultSet (types , statement , ProtoValueReaders .forResultSets (resultSets ));
188- return updateCurrentResult (new StaticQueryResult (query , Collections . singletonList ( rs ) ));
187+ YdbResultSet rs = new YdbResultSetMemory (types , statement , ProtoValueReaders .forResultSets (resultSets ));
188+ return updateCurrentResult (new YdbQueryResultStatic (query , rs ));
189189 } finally {
190190 session .close ();
191191 tracer .close ();
192192 }
193193 }
194194
195- StreamQueryResult lazy = validator .call (msg , null , () -> {
196- final CompletableFuture <Result <StreamQueryResult >> future = new CompletableFuture <>();
197- final GrpcReadStream <ResultSetReader > stream = session .executeScanQuery (yql , params , settings );
198- final StreamQueryResult result = new StreamQueryResult (msg , types , statement , query , stream ::cancel );
199-
200- stream .start ((rsr ) -> {
201- future .complete (Result .success (result ));
202- result .onStreamResultSet (0 , rsr );
203- }).whenComplete ((st , th ) -> {
195+ final YdbQueryResultReader reader = new YdbQueryResultReader (types , statement , query ) {
196+ @ Override
197+ public void onClose (Status status , Throwable th ) {
204198 session .close ();
205-
206199 if (th != null ) {
207- result .onStreamFinished (th );
208- future .completeExceptionally (th );
209200 tracer .trace ("<-- " + th .getMessage ());
210201 }
211- if (st != null ) {
212- validator .addStatusIssues (st );
213- result .onStreamFinished (st );
214- future .complete (st .isSuccess () ? Result .success (result ) : Result .fail (st ));
215- tracer .trace ("<-- " + st .toString ());
202+ if (status != null ) {
203+ validator .addStatusIssues (status );
204+ tracer .trace ("<-- " + status .toString ());
216205 }
217206 tracer .close ();
218- });
219207
220- return future ;
221- });
208+ super .onClose (status , th );
209+ }
210+ };
222211
223- return updateCurrentResult (lazy );
224- }
212+ ExecuteScanQuerySettings settings = ExecuteScanQuerySettings .newBuilder ()
213+ .withRequestTimeout (scanQueryTimeout )
214+ .setGrpcFlowControl (reader )
215+ .build ();
225216
217+ GrpcReadStream <ResultSetReader > stream = session .executeScanQuery (yql , params , settings );
218+ validator .execute (msg , tracer , () -> reader .load (stream ));
219+ return updateCurrentResult (reader );
220+ }
226221}
0 commit comments