25
25
import io .trino .gateway .ha .persistence .dao .SelectorsDao ;
26
26
import jakarta .annotation .Nullable ;
27
27
28
- import java .util .ArrayList ;
29
28
import java .util .List ;
29
+ import java .util .Optional ;
30
30
31
31
import static com .google .common .collect .ImmutableList .toImmutableList ;
32
32
@@ -90,13 +90,10 @@ public ResourceGroupsDetail updateResourceGroup(ResourceGroupsDetail resourceGro
90
90
@ Nullable String routingGroupDatabase )
91
91
{
92
92
ResourceGroupsDao dao = getResourceGroupsDao (routingGroupDatabase );
93
- ResourceGroups model = dao .findFirstById (resourceGroup .getResourceGroupId ());
94
- if (model == null ) {
95
- dao .create (resourceGroup );
96
- }
97
- else {
98
- dao .update (resourceGroup );
99
- }
93
+ Optional .ofNullable (dao .findFirstById (resourceGroup .getResourceGroupId ()))
94
+ .ifPresentOrElse (
95
+ _ -> dao .update (resourceGroup ),
96
+ () -> dao .create (resourceGroup ));
100
97
return resourceGroup ;
101
98
}
102
99
@@ -153,13 +150,10 @@ public SelectorsDetail updateSelector(SelectorsDetail selector, SelectorsDetail
153
150
@ Nullable String routingGroupDatabase )
154
151
{
155
152
SelectorsDao dao = getSelectorsDao (routingGroupDatabase );
156
- Selectors model = dao .findFirst (selector );
157
- if (model == null ) {
158
- dao .insert (updatedSelector );
159
- }
160
- else {
161
- dao .update (selector , updatedSelector );
162
- }
153
+ Optional .ofNullable (dao .findFirst (selector ))
154
+ .ifPresentOrElse (
155
+ _ -> dao .update (selector , updatedSelector ),
156
+ () -> dao .insert (updatedSelector ));
163
157
return updatedSelector ;
164
158
}
165
159
@@ -213,14 +207,10 @@ public GlobalPropertiesDetail updateGlobalProperty(GlobalPropertiesDetail global
213
207
@ Nullable String routingGroupDatabase )
214
208
{
215
209
ResourceGroupsGlobalPropertiesDao dao = getDao (routingGroupDatabase );
216
- ResourceGroupsGlobalProperties model = dao .findFirstByName (globalProperty .getName ());
217
-
218
- if (model == null ) {
219
- dao .insert (globalProperty .getName (), globalProperty .getValue ());
220
- }
221
- else {
222
- dao .update (globalProperty .getName (), globalProperty .getValue ());
223
- }
210
+ Optional .ofNullable (dao .findFirstByName (globalProperty .getName ()))
211
+ .ifPresentOrElse (
212
+ _ -> dao .update (globalProperty .getName (), globalProperty .getValue ()),
213
+ () -> dao .insert (globalProperty .getName (), globalProperty .getValue ()));
224
214
return globalProperty ;
225
215
}
226
216
@@ -263,8 +253,9 @@ public List<ExactSelectorsDetail> readExactMatchSourceSelector()
263
253
public ExactSelectorsDetail getExactMatchSourceSelector (
264
254
ExactSelectorsDetail exactSelectorDetail )
265
255
{
266
- ExactMatchSourceSelectors exactSelector = exactMatchSourceSelectorsDao .findFirst (exactSelectorDetail );
267
- return upcastExactSelectors (exactSelector );
256
+ return Optional .ofNullable (exactMatchSourceSelectorsDao .findFirst (exactSelectorDetail ))
257
+ .map (HaResourceGroupsManager ::upcastExactSelectors )
258
+ .orElse (null );
268
259
}
269
260
270
261
private SelectorsDao getSelectorsDao (@ Nullable String routingGroupDatabase )
@@ -284,15 +275,14 @@ private ResourceGroupsGlobalPropertiesDao getDao(@Nullable String routingGroupDa
284
275
285
276
private static List <GlobalPropertiesDetail > upcast (List <ResourceGroupsGlobalProperties > globalPropertiesList )
286
277
{
287
- List <GlobalPropertiesDetail > globalProperties = new ArrayList <>();
288
- for (ResourceGroupsGlobalProperties dao : globalPropertiesList ) {
289
- GlobalPropertiesDetail globalPropertiesDetail = new GlobalPropertiesDetail ();
290
- globalPropertiesDetail .setName (dao .name ());
291
- globalPropertiesDetail .setValue (dao .value ());
292
-
293
- globalProperties .add (globalPropertiesDetail );
294
- }
295
- return globalProperties ;
278
+ return globalPropertiesList .stream ()
279
+ .map (dao -> {
280
+ GlobalPropertiesDetail detail = new GlobalPropertiesDetail ();
281
+ detail .setName (dao .name ());
282
+ detail .setValue (dao .value ());
283
+ return detail ;
284
+ })
285
+ .collect (toImmutableList ());
296
286
}
297
287
298
288
private static ExactSelectorsDetail upcastExactSelectors (ExactMatchSourceSelectors exactMatchSourceSelector )
0 commit comments