Skip to content

Commit 5d5dede

Browse files
rma-rripkenMikeNeilson
authored andcommitted
Wrap uses of jOOQ stream() with try-with-resources. (#1570)
(cherry picked from commit fde517f)
1 parent d599d46 commit 5d5dede

7 files changed

Lines changed: 88 additions & 52 deletions

File tree

cwms-data-api/src/main/java/cwms/cda/data/dao/LocationGroupDao.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Map;
3939
import java.util.Optional;
4040
import java.util.Set;
41+
import java.util.stream.Stream;
42+
4143
import org.geojson.Feature;
4244
import org.geojson.FeatureCollection;
4345
import org.jetbrains.annotations.NotNull;
@@ -442,10 +444,12 @@ public FeatureCollection buildFeatureCollectionForLocationGroup(String locationO
442444
.and(al.UNIT_SYSTEM.eq(units))))
443445
.orderBy(groupAssignView.ATTRIBUTE);
444446

445-
List<Feature> features =
446-
select.stream()
447-
.map(this::buildFeatureFromAvLocRecordWithLocGroup)
448-
.collect(toList());
447+
List<Feature> features;
448+
try (Stream<Record> recordStream = select.stream()) {
449+
features = recordStream
450+
.map(this::buildFeatureFromAvLocRecordWithLocGroup)
451+
.collect(toList());
452+
}
449453
FeatureCollection collection = new FeatureCollection();
450454
collection.setFeatures(features);
451455

cwms-data-api/src/main/java/cwms/cda/data/dao/LocationLevelsDaoImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
import java.util.Set;
7878
import com.google.common.flogger.FluentLogger;
7979
import java.util.regex.Pattern;
80+
import java.util.stream.Stream;
81+
8082
import mil.army.usace.hec.metadata.Interval;
8183
import mil.army.usace.hec.metadata.IntervalFactory;
8284
import mil.army.usace.hec.metadata.constants.NumericalConstants;
@@ -275,7 +277,16 @@ public LocationLevels getLocationLevels(String cursor, int pageSize,
275277
result.forEach(row -> addToAliasMaps(aliasMap, row));
276278
result.forEach(row -> parseLevels(row, builderMap, unit, aliasMap, includeAliases));
277279
} else {
278-
query.stream().forEach(r -> parseLevels(r, builderMap, unit, new LinkedHashMap<>(), includeAliases));
280+
final int[] count = {0};
281+
try (Stream<Record> recordStream = query.stream()) {
282+
recordStream.forEach(r -> {
283+
parseLevels(r, builderMap, unit, Collections.emptyMap(), false);
284+
count[0]++;
285+
}
286+
);
287+
}
288+
289+
logger.atFine().log("Fetched %d levels", count[0]);
279290
}
280291

281292
List<LocationLevel> levels = new java.util.ArrayList<>();

cwms-data-api/src/main/java/cwms/cda/data/dao/LocationsDaoImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,12 @@ public FeatureCollection buildFeatureCollection(String names, String units, Stri
397397
selectQuery = selectQuery.and(AV_LOC.LOCATION_ID.in(identifiers));
398398
}
399399

400-
List<Feature> features = selectQuery.fetchSize(DEFAULT_SMALL_FETCH_SIZE).stream()
401-
.map(LocationsDaoImpl::buildFeatureFromAvLocRecord)
402-
.collect(toList());
400+
List<Feature> features;
401+
try (Stream<Record> recordStream = selectQuery.fetchSize(DEFAULT_SMALL_FETCH_SIZE).stream()) {
402+
features = recordStream
403+
.map(LocationsDaoImpl::buildFeatureFromAvLocRecord)
404+
.collect(toList());
405+
}
403406
FeatureCollection collection = new FeatureCollection();
404407
collection.setFeatures(features);
405408

cwms-data-api/src/main/java/cwms/cda/data/dao/PoolDao.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.List;
1111
import com.google.common.flogger.FluentLogger;
1212
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
14+
1315
import org.jetbrains.annotations.NotNull;
1416
import org.jooq.Condition;
1517
import org.jooq.DSLContext;
@@ -36,14 +38,16 @@ public List<Pool> catalogPools(String projectIdMask, String poolNameMask,
3638
List<String> types = getTypes(includeExplicit, includeImplicit);
3739
Condition condition = getCondition(projectIdMask, poolNameMask, bottomLevelMask, topLevelMask, officeIdMask, types);
3840

39-
return dsl.select(DSL.asterisk()).from(view)
40-
.where(condition)
41-
.orderBy(view.DEFINITION_TYPE,
42-
DSL.upper(view.OFFICE_ID), DSL.upper(view.PROJECT_ID), view.ATTRIBUTE, DSL.upper(view.POOL_NAME))
43-
.stream()
44-
.map(r -> toPool(r, true))
45-
.collect(toList());
46-
}
41+
try (Stream<Record> recordStream = dsl.select(DSL.asterisk()).from(view)
42+
.where(condition)
43+
.orderBy(view.DEFINITION_TYPE,
44+
DSL.upper(view.OFFICE_ID), DSL.upper(view.PROJECT_ID), view.ATTRIBUTE, DSL.upper(view.POOL_NAME))
45+
.stream()) {
46+
return recordStream
47+
.map(r -> toPool(r, true))
48+
.collect(toList());
49+
}
50+
}
4751

4852
@NotNull
4953
private List<String> getTypes(boolean includeExplicit, boolean includeImplicit) {
@@ -197,15 +201,18 @@ public Pools retrievePools(String cursor, int pageSize,
197201
List<String> types = getTypes(includeExplicit, includeImplicit);
198202
Condition condition = getCondition(projectIdMask, poolNameMask, bottomLevelMask, topLevelMask, officeIdMask, types);
199203

200-
List<Pool> pools = dsl.select(DSL.asterisk()).from(view)
201-
.where(condition)
202-
.orderBy(view.DEFINITION_TYPE,
203-
DSL.upper(view.OFFICE_ID), DSL.upper(view.PROJECT_ID), view.ATTRIBUTE, DSL.upper(view.POOL_NAME))
204-
.offset(offset)
205-
.limit(pageSize)
206-
.stream().map(r -> toPool(r, true)).collect(toList());
207-
208-
Pools.Builder builder = new Pools.Builder(offset, pageSize, total);
204+
List<Pool> pools;
205+
try (Stream<Record> recordStream = dsl.select(DSL.asterisk()).from(view)
206+
.where(condition)
207+
.orderBy(view.DEFINITION_TYPE,
208+
DSL.upper(view.OFFICE_ID), DSL.upper(view.PROJECT_ID), view.ATTRIBUTE, DSL.upper(view.POOL_NAME))
209+
.offset(offset)
210+
.limit(pageSize)
211+
.stream()) {
212+
pools = recordStream.map(r -> toPool(r, true)).collect(toList());
213+
}
214+
215+
Pools.Builder builder = new Pools.Builder(offset, pageSize, total);
209216
builder.addAll(pools);
210217
return builder.build();
211218
}

cwms-data-api/src/main/java/cwms/cda/data/dao/TimeSeriesIdentifierDescriptorDao.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Optional;
3737
import java.util.logging.Level;
3838
import java.util.stream.Collectors;
39+
3940
import org.jooq.Condition;
4041
import org.jooq.Configuration;
4142
import org.jooq.DSLContext;
@@ -103,23 +104,25 @@ public TimeSeriesIdentifierDescriptors getTimeSeriesIdentifiers(String cursor, i
103104
Collection<TimeSeriesIdentifierDescriptor> retval;
104105

105106
if (!includeAliases) {
106-
retval = dsl
107+
try (var record5Stream = dsl
107108
.selectDistinct(AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.DB_OFFICE_ID,
108-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.CWMS_TS_ID,
109-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET,
110-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TS_ACTIVE_FLAG,
111-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TIME_ZONE_ID)
109+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.CWMS_TS_ID,
110+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET,
111+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TS_ACTIVE_FLAG,
112+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TIME_ZONE_ID)
112113
.from(AV_CWMS_TS_ID2.AV_CWMS_TS_ID2)
113114
.where(whereCondition)
114115
.orderBy(AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.DB_OFFICE_ID, AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.CWMS_TS_ID,
115-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET,
116-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TS_ACTIVE_FLAG,
117-
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TIME_ZONE_ID)
116+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.INTERVAL_UTC_OFFSET,
117+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TS_ACTIVE_FLAG,
118+
AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.TIME_ZONE_ID)
118119
.limit(pageSize)
119120
.offset(offset)
120-
.stream()
121-
.map(this::toDescriptor)
122-
.collect(Collectors.toList());
121+
.stream()) {
122+
retval = record5Stream
123+
.map(this::toDescriptor)
124+
.collect(Collectors.toList());
125+
}
123126
} else {
124127
Table<?> innerTable = AV_CWMS_TS_ID2.AV_CWMS_TS_ID2.as("alias_table");
125128
Field<String> tsId = innerTable.field("CWMS_TS_ID", String.class);

cwms-data-api/src/main/java/cwms/cda/data/dao/TimeZoneDao.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import usace.cwms.db.jooq.codegen.tables.MV_TIME_ZONE;
99

1010
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
1112

1213
public class TimeZoneDao extends JooqDao<String> {
1314

@@ -21,11 +22,13 @@ public String getTimeZones(String format) {
2122

2223
public TimeZoneIds getTimeZones()
2324
{
24-
return new TimeZoneIds(dsl.select(MV_TIME_ZONE.MV_TIME_ZONE.TIME_ZONE_NAME)
25-
.from(MV_TIME_ZONE.MV_TIME_ZONE)
26-
.stream()
27-
.map(Record1::component1)
28-
.map(TimeZoneId::new)
29-
.collect(Collectors.toList()));
25+
try (Stream<Record1<String>> record1Stream = dsl.select(MV_TIME_ZONE.MV_TIME_ZONE.TIME_ZONE_NAME)
26+
.from(MV_TIME_ZONE.MV_TIME_ZONE)
27+
.stream()) {
28+
return new TimeZoneIds(record1Stream
29+
.map(Record1::component1)
30+
.map(TimeZoneId::new)
31+
.collect(Collectors.toList()));
32+
}
3033
}
3134
}

cwms-data-api/src/main/java/cwms/cda/data/dao/texttimeseries/StandardTextDao.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
import cwms.cda.data.dto.texttimeseries.StandardTextValue;
3131
import org.jooq.Configuration;
3232
import org.jooq.DSLContext;
33+
import org.jooq.Record3;
3334
import usace.cwms.db.jooq.codegen.packages.CWMS_TEXT_PACKAGE;
3435
import usace.cwms.db.jooq.codegen.tables.AV_STD_TEXT;
3536

3637
import java.util.List;
38+
import java.util.stream.Stream;
3739

3840
import static java.util.stream.Collectors.toList;
3941

@@ -44,19 +46,22 @@ public StandardTextDao(DSLContext dsl) {
4446

4547
public StandardTextCatalog retreiveStandardTextCatalog(String standardTextIdMask, String officeIdMask) {
4648
AV_STD_TEXT view = AV_STD_TEXT.AV_STD_TEXT;
47-
List<StandardTextValue> textValues = dsl.select(view.OFFICE_ID, view.STD_TEXT_ID, view.LONG_TEXT)
49+
List<StandardTextValue> textValues;
50+
try (Stream<Record3<String, String, String>> record3Stream = dsl.select(view.OFFICE_ID, view.STD_TEXT_ID, view.LONG_TEXT)
4851
.from(view)
4952
.where(JooqDao.caseInsensitiveLikeRegexNullTrue(view.OFFICE_ID, officeIdMask))
5053
.and(JooqDao.caseInsensitiveLikeRegexNullTrue(view.STD_TEXT_ID, standardTextIdMask))
51-
.stream()
52-
.map(r -> new StandardTextValue.Builder()
53-
.withStandardText(r.get(view.LONG_TEXT))
54-
.withId(new StandardTextId.Builder()
55-
.withId(r.get(view.STD_TEXT_ID))
56-
.withOfficeId(r.get(view.OFFICE_ID))
57-
.build())
58-
.build())
59-
.collect(toList());
54+
.stream()) {
55+
textValues = record3Stream
56+
.map(r -> new StandardTextValue.Builder()
57+
.withStandardText(r.get(view.LONG_TEXT))
58+
.withId(new StandardTextId.Builder()
59+
.withId(r.get(view.STD_TEXT_ID))
60+
.withOfficeId(r.get(view.OFFICE_ID))
61+
.build())
62+
.build())
63+
.collect(toList());
64+
}
6065
return new StandardTextCatalog.Builder()
6166
.withValues(textValues)
6267
.build();

0 commit comments

Comments
 (0)