smiklosovic commented on code in PR #3733:
URL: https://github.com/apache/cassandra/pull/3733#discussion_r1883623506
##########
src/java/org/apache/cassandra/tools/nodetool/Status.java:
##########
@@ -170,24 +215,134 @@ private void addNode(String endpoint, Float owns,
HostStatWithPort hostStat, int
strOwns = owns != null && hasEffectiveOwns ? new
DecimalFormat("##0.0%").format(owns) : "?";
hostID = hostIDMap.get(endpoint);
- try
- {
+ try {
rack = epSnitchInfo.getRack(endpoint);
- }
- catch (UnknownHostException e)
- {
+ } catch (UnknownHostException e) {
throw new RuntimeException(e);
}
- epDns = hostStat.ipOrDns(printPort);
if (isTokenPerNode)
- {
- tableBuilder.add(statusAndState, epDns, load, strOwns, hostID,
hostStat.token, rack);
- }
+ return List.of(addressAndPort, statusAndState, epDns, load,
strOwns, hostID, hostStat.token, rack);
else
- {
- tableBuilder.add(statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ return List.of(addressAndPort, statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ }
+
+ // To check for descending order
+ private Boolean desc() {
+ return sortOrder == null ? null : sortOrder == SortOrder.desc;
+ }
+
+ // Sort function to sort the data
+ private Map<String, List<Object>> sort(Map<String, List<Object>> map) {
+ switch (sortBy) {
+ case none:
+ return map;
+ case ip:
+ return sortByIp(map, 0, desc() != null ? desc() : false);
+ case load:
+ return sortByLoad(map, 3, desc() != null ? desc() : true);
+ case id:
+ return sortById(map, isTokenPerNode ? 5 : 6, desc() != null ?
desc() : false);
+ case rack:
+ return sortByRack(map, 7, desc() != null ? desc() : false);
+ case owns:
+ return sortByOwns(map, isTokenPerNode ? 4 : 5, desc() != null
? desc() : true);
+ case state:
+ return sortByState(map, 1, desc() != null ? desc() : true);
+ default:
+ throw new IllegalArgumentException("Sorting by " + sortBy + "
is not supported.");
}
}
-}
+ // Helper Function to Sort by Ip
+ private LinkedHashMap<String, List<Object>> sortByIp(Map<String,
List<Object>> data, int index, boolean descending) {
+ return data.entrySet()
+ .stream()
+ .sorted((e1, e2) -> {
+ InetAddressAndPort addr1 = (InetAddressAndPort)
e1.getValue().get(index);
+ InetAddressAndPort addr2 = (InetAddressAndPort)
e2.getValue().get(index);
+ return descending ? addr2.compareTo(addr1) :
addr1.compareTo(addr2);
+ })
+ .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1,
e2) -> e1, LinkedHashMap::new));
+ }
+
+ // Helper Function to Sort by Load
+ private LinkedHashMap<String, List<Object>> sortByLoad(Map<String,
List<Object>> data, int index, boolean descending) {
+ return data.entrySet()
+ .stream()
+ .sorted((e1, e2) -> {
+ String str1 = (String) e1.getValue().get(index);
+ String str2 = (String) e2.getValue().get(index);
+ // Check if str1 or str2 contains a '?' and set a value
for it.
+ boolean containsQuestionMark1 = str1.contains("?");
+ boolean containsQuestionMark2 = str2.contains("?");
+ if (containsQuestionMark1 && containsQuestionMark2) {
+ // If both contain '?', return 0 (they are considered
equal).
+ return 0;
+ }
+
+ if (containsQuestionMark1) {
+ // If str1 contains '?', ensure it's last (or first
depending on descending).
+ return descending ? 1 : -1;
+ }
+
+ if (containsQuestionMark2) {
Review Comment:
you dont need braces around if if that `if` contains just one line
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]