manish-m-pillai commented on code in PR #3733:
URL: https://github.com/apache/cassandra/pull/3733#discussion_r1879570742
##########
src/java/org/apache/cassandra/tools/nodetool/Status.java:
##########
@@ -179,15 +244,112 @@ private void addNode(String endpoint, Float owns,
HostStatWithPort hostStat, int
throw new RuntimeException(e);
}
- epDns = hostStat.ipOrDns(printPort);
if (isTokenPerNode)
+ return List.of(addressAndPort, statusAndState, epDns, load,
strOwns, hostID, hostStat.token, rack);
+ else
+ return List.of(addressAndPort, statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ }
+
+ private Boolean desc()
+ {
+ if(sortOrder==sortOrder.desc)
+ {
+ return true;
+ }
+ else if(sortOrder==sortOrder.asc)
{
- tableBuilder.add(statusAndState, epDns, load, strOwns, hostID,
hostStat.token, rack);
+ return false;
}
else
{
- tableBuilder.add(statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ return null;
+ }
+ }
+
+ private Map<String, List<Object>> sort(Map<String, List<Object>> map)
+ {
+ switch (sortBy)
+ {
+ case none:
+ return map;
+ case ip:
+ return sortInternal(map, SortBy.ip, 0, desc() != null ? desc()
: false); // default order is ascending
+ case load:
+ return sortInternal(map, SortBy.load, 3, desc() != null ?
desc() : true); // default order is descending
+ case id:
+ return sortInternal(map, SortBy.id, isTokenPerNode ? 5 : 6,
desc() != null ? desc() : false); // default order is ascending
+ case rack:
+ return sortInternal(map, SortBy.rack, 7, desc() != null ?
desc() : false); // default order is ascending
+ case owns:
+ return sortInternal(map, SortBy.owns, isTokenPerNode ? 4 : 5,
desc() != null ? desc() : true); // default order is descending
+ case status:
+ return sortInternal(map, SortBy.status, 1, desc() != null ?
desc() : true); // default order is descending
+ default:
+ throw new IllegalArgumentException("Sorting by " + sortBy + "
is not supported.");
}
}
+ private LinkedHashMap<String, List<Object>> sortInternal(Map<String,
List<Object>> data, SortBy sortBy, int index, boolean descending)
+ {
+ return data.entrySet()
+ .stream()
+ .sorted((e1, e2) -> {
+ if (sortBy == SortBy.ip)
+ {
+ InetAddressAndPort addr1 = (InetAddressAndPort)
e1.getValue().get(index);
Review Comment:
I have tested the implementation by removing the resolveIp part and directly
using compareTo on InetAddressAndPort, and it worked as expected. I will
implement the updated version
##########
src/java/org/apache/cassandra/tools/nodetool/Status.java:
##########
@@ -179,15 +244,112 @@ private void addNode(String endpoint, Float owns,
HostStatWithPort hostStat, int
throw new RuntimeException(e);
}
- epDns = hostStat.ipOrDns(printPort);
if (isTokenPerNode)
+ return List.of(addressAndPort, statusAndState, epDns, load,
strOwns, hostID, hostStat.token, rack);
+ else
+ return List.of(addressAndPort, statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ }
+
+ private Boolean desc()
+ {
+ if(sortOrder==sortOrder.desc)
+ {
+ return true;
+ }
+ else if(sortOrder==sortOrder.asc)
{
- tableBuilder.add(statusAndState, epDns, load, strOwns, hostID,
hostStat.token, rack);
+ return false;
}
else
{
- tableBuilder.add(statusAndState, epDns, load,
String.valueOf(size), strOwns, hostID, rack);
+ return null;
+ }
+ }
+
+ private Map<String, List<Object>> sort(Map<String, List<Object>> map)
+ {
+ switch (sortBy)
+ {
+ case none:
+ return map;
+ case ip:
+ return sortInternal(map, SortBy.ip, 0, desc() != null ? desc()
: false); // default order is ascending
+ case load:
+ return sortInternal(map, SortBy.load, 3, desc() != null ?
desc() : true); // default order is descending
+ case id:
+ return sortInternal(map, SortBy.id, isTokenPerNode ? 5 : 6,
desc() != null ? desc() : false); // default order is ascending
+ case rack:
+ return sortInternal(map, SortBy.rack, 7, desc() != null ?
desc() : false); // default order is ascending
+ case owns:
+ return sortInternal(map, SortBy.owns, isTokenPerNode ? 4 : 5,
desc() != null ? desc() : true); // default order is descending
+ case status:
+ return sortInternal(map, SortBy.status, 1, desc() != null ?
desc() : true); // default order is descending
+ default:
+ throw new IllegalArgumentException("Sorting by " + sortBy + "
is not supported.");
}
}
+ private LinkedHashMap<String, List<Object>> sortInternal(Map<String,
List<Object>> data, SortBy sortBy, int index, boolean descending)
Review Comment:
Initially, I had separate sorting methods, but the code became complex.
@smiklosovic suggested a simpler approach with a single method for sorting,
along with other improvements. If preferred, I can update it back to using
specific methods.
--
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]