1818
1919import tech .ydb .auth .TokenAuthProvider ;
2020import tech .ydb .auth .iam .CloudAuthHelper ;
21+ import tech .ydb .common .transaction .TxMode ;
2122import tech .ydb .core .Result ;
2223import tech .ydb .core .Status ;
2324import tech .ydb .core .auth .StaticCredentials ;
2425import tech .ydb .core .grpc .GrpcTransport ;
2526import tech .ydb .core .grpc .GrpcTransportBuilder ;
27+ import tech .ydb .query .QuerySession ;
28+ import tech .ydb .query .QueryStream ;
29+ import tech .ydb .query .impl .QueryClientImpl ;
30+ import tech .ydb .query .settings .ExecuteQuerySettings ;
31+ import tech .ydb .query .tools .QueryReader ;
2632import tech .ydb .table .Session ;
2733import tech .ydb .table .TableClient ;
2834import tech .ydb .table .description .TableDescription ;
2935import tech .ydb .table .query .DataQuery ;
3036import tech .ydb .table .query .Params ;
31- import tech .ydb .table .settings .ExecuteDataQuerySettings ;
32- import tech .ydb .table .transaction .TxControl ;
3337import tech .ydb .topic .TopicClient ;
3438import tech .ydb .topic .read .AsyncReader ;
3539import tech .ydb .topic .settings .ReadEventHandlersSettings ;
@@ -54,6 +58,7 @@ public class YdbService {
5458 private final GrpcTransport transport ;
5559
5660 private final TableClient tableClient ;
61+ private final QueryClientImpl queryClient ;
5762 private final TopicClient topicClient ;
5863
5964 public YdbService (Environment env ) {
@@ -93,11 +98,17 @@ public YdbService(Environment env) {
9398
9499 this .transport = builder .build ();
95100 this .tableClient = TableClient .newClient (transport ).build ();
101+ this .queryClient = QueryClientImpl .newClient (transport ).build ();
96102 this .topicClient = TopicClient .newClient (transport )
97- .setCompressionExecutor (Runnable ::run ) // Prevent OOM
103+ .setCompressionExecutor (Runnable ::run )
98104 .build ();
99105 }
100106
107+ public void updatePoolSize (int maxSize ) {
108+ logger .error ("set session pool max size {}" , maxSize );
109+ queryClient .updatePoolMaxSize (maxSize );
110+ }
111+
101112 @ PreDestroy
102113 public void close () {
103114 this .topicClient .close ();
@@ -122,6 +133,7 @@ public String expandPath(String name) {
122133 return sb .toString ();
123134 }
124135
136+ @ SuppressWarnings ("null" )
125137 public Result <DataQuery > parseQuery (String query ) {
126138 Result <Session > session = tableClient .createSession (Duration .ofSeconds (5 )).join ();
127139 if (!session .isSuccess ()) {
@@ -133,6 +145,7 @@ public Result<DataQuery> parseQuery(String query) {
133145 }
134146 }
135147
148+ @ SuppressWarnings ("null" )
136149 public Result <TableDescription > describeTable (String tablePath ) {
137150 Result <Session > session = tableClient .createSession (Duration .ofSeconds (5 )).join ();
138151 if (!session .isSuccess ()) {
@@ -144,18 +157,35 @@ public Result<TableDescription> describeTable(String tablePath) {
144157 }
145158 }
146159
147- public Status executeQuery (String query , Params params , int timeoutSeconds ) {
148- Result <Session > session = tableClient .createSession (Duration .ofSeconds (5 )).join ();
160+ public Status executeYqlQuery (String query , Params params , int timeoutSeconds ) {
161+ Result <QuerySession > session = queryClient .createSession (Duration .ofSeconds (5 )).join ();
149162 if (!session .isSuccess ()) {
150163 return session .getStatus ();
151164 }
152165
153- try (Session s = session .getValue ()) {
154- ExecuteDataQuerySettings settings = new ExecuteDataQuerySettings ();
166+ try (QuerySession s = session .getValue ()) {
167+ ExecuteQuerySettings .Builder settings = ExecuteQuerySettings .newBuilder ();
168+ if (timeoutSeconds > 0 ) {
169+ settings .withRequestTimeout (Duration .ofSeconds (timeoutSeconds ));
170+ }
171+ return s .createQuery (query , TxMode .NONE , params , settings .build ()).execute ().join ().getStatus ();
172+ }
173+ }
174+
175+ @ SuppressWarnings ("null" )
176+ public Result <QueryReader > readYqlQuery (String query , Params params , int timeoutSeconds ) {
177+ Result <QuerySession > session = queryClient .createSession (Duration .ofSeconds (5 )).join ();
178+ if (!session .isSuccess ()) {
179+ return session .map (null );
180+ }
181+
182+ try (QuerySession s = session .getValue ()) {
183+ ExecuteQuerySettings .Builder settings = ExecuteQuerySettings .newBuilder ();
155184 if (timeoutSeconds > 0 ) {
156- settings .setTimeout (Duration .ofSeconds (timeoutSeconds ));
185+ settings .withRequestTimeout (Duration .ofSeconds (timeoutSeconds ));
157186 }
158- return s .executeDataQuery (query , TxControl .serializableRw (), params , settings ).join ().getStatus ();
187+ QueryStream stream = s .createQuery (query , TxMode .SNAPSHOT_RO , params , settings .build ());
188+ return QueryReader .readFrom (stream ).join ();
159189 }
160190 }
161191
0 commit comments