Skip to content

Commit a4ae8ae

Browse files
committed
for loop map construction rather than lambda
1 parent 3fab75e commit a4ae8ae

1 file changed

Lines changed: 23 additions & 16 deletions

File tree

helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/AssignmentMetadataStore.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Map;
2626

2727
import java.util.Objects;
28-
import java.util.stream.Collectors;
2928
import org.apache.helix.BucketDataAccessor;
3029
import org.apache.helix.HelixException;
3130
import org.apache.helix.HelixProperty;
@@ -79,9 +78,11 @@ public Map<String, ResourceAssignment> getBaseline() {
7978
}
8079
}
8180
}
82-
return _globalBaseline.entrySet().stream().collect(
83-
Collectors.toMap(Map.Entry::getKey,
84-
entry -> new ResourceAssignment(entry.getValue().getRecord())));
81+
Map<String, ResourceAssignment> result = new HashMap<>(_globalBaseline.size());
82+
for (Map.Entry<String, ResourceAssignment> entry : _globalBaseline.entrySet()) {
83+
result.put(entry.getKey(), new ResourceAssignment(entry.getValue().getRecord()));
84+
}
85+
return result;
8586
}
8687

8788
/**
@@ -108,9 +109,11 @@ public Map<String, ResourceAssignment> getBestPossibleAssignment() {
108109
}
109110
}
110111
// Return defensive copy so that the in-memory assignment is not modified by callers
111-
return _bestPossibleAssignment.entrySet().stream().collect(
112-
Collectors.toMap(Map.Entry::getKey,
113-
entry -> new ResourceAssignment(entry.getValue().getRecord())));
112+
Map<String, ResourceAssignment> result = new HashMap<>(_bestPossibleAssignment);
113+
for (Map.Entry<String, ResourceAssignment> entry : _bestPossibleAssignment.entrySet()) {
114+
result.put(entry.getKey(), new ResourceAssignment(entry.getValue().getRecord()));
115+
}
116+
return result;
114117
}
115118

116119
private Map<String, ResourceAssignment> fetchAssignmentOrDefault(String path) {
@@ -147,9 +150,11 @@ private void persistAssignmentToMetadataStore(Map<String, ResourceAssignment> ne
147150
*/
148151
public synchronized void persistBaseline(Map<String, ResourceAssignment> globalBaseline) {
149152
// Create defensive copy so that the in-memory assignment is not modified after it is persisted
150-
Map<String, ResourceAssignment> baselineCopy = globalBaseline.entrySet().stream().collect(
151-
Collectors.toMap(Map.Entry::getKey,
152-
entry -> new ResourceAssignment(entry.getValue().getRecord())));
153+
Map<String, ResourceAssignment> baselineCopy = new HashMap<>(globalBaseline.size());
154+
for (Map.Entry<String, ResourceAssignment> entry : globalBaseline.entrySet()) {
155+
baselineCopy.put(entry.getKey(), new ResourceAssignment(entry.getValue().getRecord()));
156+
}
157+
153158
// write to metadata store
154159
persistAssignmentToMetadataStore(baselineCopy, _baselinePath, BASELINE_KEY);
155160
// write to memory
@@ -163,9 +168,10 @@ public synchronized void persistBaseline(Map<String, ResourceAssignment> globalB
163168
*/
164169
public synchronized void persistBestPossibleAssignment(Map<String, ResourceAssignment> bestPossibleAssignment) {
165170
// Create defensive copy so that the in-memory assignment is not modified after it is persisted
166-
Map<String, ResourceAssignment> bestPossibleAssignmentCopy = bestPossibleAssignment.entrySet().stream().collect(
167-
Collectors.toMap(Map.Entry::getKey,
168-
entry -> new ResourceAssignment(entry.getValue().getRecord())));
171+
Map<String, ResourceAssignment> bestPossibleAssignmentCopy = new HashMap<>(bestPossibleAssignment.size());
172+
for (Map.Entry<String, ResourceAssignment> entry : bestPossibleAssignment.entrySet()) {
173+
bestPossibleAssignmentCopy.put(entry.getKey(), new ResourceAssignment(entry.getValue().getRecord()));
174+
}
169175
// write to metadata store
170176
persistAssignmentToMetadataStore(bestPossibleAssignmentCopy, _bestPossiblePath, BEST_POSSIBLE_KEY);
171177
// write to memory
@@ -183,9 +189,10 @@ public synchronized void persistBestPossibleAssignment(Map<String, ResourceAssig
183189
*/
184190
public synchronized boolean asyncUpdateBestPossibleAssignmentCache(
185191
Map<String, ResourceAssignment> bestPossibleAssignment, int newVersion) {
186-
Map<String, ResourceAssignment> bestPossibleAssignmentCopy = bestPossibleAssignment.entrySet().stream().collect(
187-
Collectors.toMap(Map.Entry::getKey,
188-
entry -> new ResourceAssignment(entry.getValue().getRecord())));
192+
Map<String, ResourceAssignment> bestPossibleAssignmentCopy = new HashMap<>(bestPossibleAssignment.size());
193+
for (Map.Entry<String, ResourceAssignment> entry : bestPossibleAssignment.entrySet()) {
194+
bestPossibleAssignmentCopy.put(entry.getKey(), new ResourceAssignment(entry.getValue().getRecord()));
195+
}
189196
// Check if the version is stale by this point
190197
if (newVersion > _bestPossibleVersion) {
191198
_bestPossibleAssignment = bestPossibleAssignmentCopy;

0 commit comments

Comments
 (0)