-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Use map.putIfAbsent() or map.computeIfAbsent() as appropriate instead of containsKey() + put() #7764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use map.putIfAbsent() or map.computeIfAbsent() as appropriate instead of containsKey() + put() #7764
Changes from 9 commits
37ebf6e
50b80a0
b8e4473
b54c36e
2b6cf49
a3f6019
0d90c3b
2eb5c81
71a6ce0
0cee976
e3ebf0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,87 +204,87 @@ public Firehose connect(InputRowParser inputRowParser, File temporaryDirectory) | |
| segmentIds | ||
| ); | ||
|
|
||
| try { | ||
| final List<TimelineObjectHolder<String, DataSegment>> timeLineSegments = getTimeline(); | ||
|
|
||
| // Download all segments locally. | ||
| // Note: this requires enough local storage space to fit all of the segments, even though | ||
| // IngestSegmentFirehose iterates over the segments in series. We may want to change this | ||
| // to download files lazily, perhaps sharing code with PrefetchableTextFilesFirehoseFactory. | ||
| final SegmentLoader segmentLoader = segmentLoaderFactory.manufacturate(temporaryDirectory); | ||
| Map<DataSegment, File> segmentFileMap = Maps.newLinkedHashMap(); | ||
| for (TimelineObjectHolder<String, DataSegment> holder : timeLineSegments) { | ||
| for (PartitionChunk<DataSegment> chunk : holder.getObject()) { | ||
| final DataSegment segment = chunk.getObject(); | ||
| if (!segmentFileMap.containsKey(segment)) { | ||
| segmentFileMap.put(segment, segmentLoader.getSegmentFiles(segment)); | ||
| final List<TimelineObjectHolder<String, DataSegment>> timeLineSegments = getTimeline(); | ||
|
|
||
| // Download all segments locally. | ||
| // Note: this requires enough local storage space to fit all of the segments, even though | ||
| // IngestSegmentFirehose iterates over the segments in series. We may want to change this | ||
| // to download files lazily, perhaps sharing code with PrefetchableTextFilesFirehoseFactory. | ||
| final SegmentLoader segmentLoader = segmentLoaderFactory.manufacturate(temporaryDirectory); | ||
| Map<DataSegment, File> segmentFileMap = Maps.newLinkedHashMap(); | ||
| for (TimelineObjectHolder<String, DataSegment> holder : timeLineSegments) { | ||
| for (PartitionChunk<DataSegment> chunk : holder.getObject()) { | ||
| final DataSegment segment = chunk.getObject(); | ||
|
|
||
| segmentFileMap.computeIfAbsent(segment, k -> { | ||
| try { | ||
| return segmentLoader.getSegmentFiles(segment); | ||
| } | ||
| } | ||
| } | ||
| catch (SegmentLoadingException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| final List<String> dims; | ||
| if (dimensions != null) { | ||
| dims = dimensions; | ||
| } else if (inputRowParser.getParseSpec().getDimensionsSpec().hasCustomDimensions()) { | ||
| dims = inputRowParser.getParseSpec().getDimensionsSpec().getDimensionNames(); | ||
| } else { | ||
| dims = getUniqueDimensions( | ||
| timeLineSegments, | ||
| inputRowParser.getParseSpec().getDimensionsSpec().getDimensionExclusions() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| final List<String> metricsList = metrics == null ? getUniqueMetrics(timeLineSegments) : metrics; | ||
| final List<String> dims; | ||
| if (dimensions != null) { | ||
| dims = dimensions; | ||
| } else if (inputRowParser.getParseSpec().getDimensionsSpec().hasCustomDimensions()) { | ||
| dims = inputRowParser.getParseSpec().getDimensionsSpec().getDimensionNames(); | ||
| } else { | ||
| dims = getUniqueDimensions( | ||
| timeLineSegments, | ||
| inputRowParser.getParseSpec().getDimensionsSpec().getDimensionExclusions() | ||
| ); | ||
| } | ||
|
|
||
| final List<WindowedStorageAdapter> adapters = Lists.newArrayList( | ||
| Iterables.concat( | ||
| Iterables.transform( | ||
| timeLineSegments, | ||
| new Function<TimelineObjectHolder<String, DataSegment>, Iterable<WindowedStorageAdapter>>() | ||
| { | ||
| final List<String> metricsList = metrics == null ? getUniqueMetrics(timeLineSegments) : metrics; | ||
|
|
||
| final List<WindowedStorageAdapter> adapters = Lists.newArrayList( | ||
| Iterables.concat( | ||
| Iterables.transform( | ||
| timeLineSegments, | ||
| new Function<TimelineObjectHolder<String, DataSegment>, Iterable<WindowedStorageAdapter>>() { | ||
| @Override | ||
| public Iterable<WindowedStorageAdapter> apply(final TimelineObjectHolder<String, DataSegment> holder) | ||
| { | ||
| return | ||
| Iterables.transform( | ||
| holder.getObject(), | ||
| new Function<PartitionChunk<DataSegment>, WindowedStorageAdapter>() { | ||
| @Override | ||
| public Iterable<WindowedStorageAdapter> apply(final TimelineObjectHolder<String, DataSegment> holder) | ||
| public WindowedStorageAdapter apply(final PartitionChunk<DataSegment> input) | ||
| { | ||
| return | ||
| Iterables.transform( | ||
| holder.getObject(), | ||
| new Function<PartitionChunk<DataSegment>, WindowedStorageAdapter>() | ||
| { | ||
| @Override | ||
| public WindowedStorageAdapter apply(final PartitionChunk<DataSegment> input) | ||
| { | ||
| final DataSegment segment = input.getObject(); | ||
| try { | ||
| return new WindowedStorageAdapter( | ||
| new QueryableIndexStorageAdapter( | ||
| indexIO.loadIndex( | ||
| Preconditions.checkNotNull( | ||
| segmentFileMap.get(segment), | ||
| "File for segment %s", segment.getId() | ||
| ) | ||
| ) | ||
| ), | ||
| holder.getInterval() | ||
| ); | ||
| } | ||
| catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| final DataSegment segment = input.getObject(); | ||
| try { | ||
| return new WindowedStorageAdapter( | ||
| new QueryableIndexStorageAdapter( | ||
| indexIO.loadIndex( | ||
| Preconditions.checkNotNull( | ||
| segmentFileMap.get(segment), | ||
| "File for segment %s", segment.getId() | ||
| ) | ||
| ) | ||
| ), | ||
| holder.getInterval() | ||
| ); | ||
| } | ||
| catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| ); | ||
| ); | ||
| } | ||
| } | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| final TransformSpec transformSpec = TransformSpec.fromInputRowParser(inputRowParser); | ||
| return new IngestSegmentFirehose(adapters, transformSpec, dims, metricsList, dimFilter); | ||
| } | ||
| catch (SegmentLoadingException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outer try/catch block removed. |
||
| final TransformSpec transformSpec = TransformSpec.fromInputRowParser(inputRowParser); | ||
| return new IngestSegmentFirehose(adapters, transformSpec, dims, metricsList, dimFilter); | ||
| } | ||
|
|
||
| private long jitter(long input) | ||
|
|
@@ -508,13 +508,14 @@ static List<String> getUniqueMetrics(List<TimelineObjectHolder<String, DataSegme | |
| // segments to olders. | ||
|
|
||
| // timelineSegments are sorted in order of interval | ||
| int index = 0; | ||
| int[] index = {0}; | ||
| for (TimelineObjectHolder<String, DataSegment> timelineHolder : Lists.reverse(timelineSegments)) { | ||
| for (PartitionChunk<DataSegment> chunk : timelineHolder.getObject()) { | ||
| for (String metric : chunk.getObject().getMetrics()) { | ||
| if (!uniqueMetrics.containsKey(metric)) { | ||
| uniqueMetrics.put(metric, index++); | ||
| uniqueMetrics.computeIfAbsent(metric, k -> { | ||
| return index[0]++; | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using index variable as int as is, the compiler complains |
||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
segmentLoader.getSegmentFiles(segment) throws SegmentLoadingException, which needs to be caught and re thrown as RuntimeException. This makes the outer try catch redundant (compilation error) as the SegmentLoadingException has already been caught.