in src/main/java/com/amazonaws/services/kinesis/scaling/StreamScalingUtils.java [293:347]
public static Map<String, ShardHashInfo> getOpenShards(KinesisClient kinesisClient, String streamName,
SortOrder sortOrder, String lastShardId) throws Exception {
Collection<String> openShardNames = new ArrayList<>();
Map<String, ShardHashInfo> shardMap = new LinkedHashMap<>();
// load all the open shards on the Stream and sort if required
for (Shard shard : listShards(kinesisClient, streamName, lastShardId)) {
openShardNames.add(shard.shardId());
shardMap.put(shard.shardId(), new ShardHashInfo(streamName, shard));
// remove this Shard's parents from the set of active shards - they
// are now closed and cannot be modified or written to
if (shard.parentShardId() != null) {
openShardNames.remove(shard.parentShardId());
shardMap.remove(shard.parentShardId());
}
if (shard.adjacentParentShardId() != null) {
openShardNames.remove(shard.adjacentParentShardId());
shardMap.remove(shard.adjacentParentShardId());
}
}
// create a List of Open shards for sorting
List<Shard> sortShards = new ArrayList<>();
for (String s : openShardNames) {
// paranoid null check in case we get a null map entry
if (s != null) {
sortShards.add(shardMap.get(s).getShard());
}
}
if (sortOrder.equals(SortOrder.ASCENDING)) {
// sort the list into lowest start hash order
Collections.sort(sortShards, new Comparator<Shard>() {
public int compare(Shard o1, Shard o2) {
return compareShardsByStartHash(o1, o2);
}
});
} else if (sortOrder.equals(SortOrder.DESCENDING)) {
// sort the list into highest start hash order
Collections.sort(sortShards, new Comparator<Shard>() {
public int compare(Shard o1, Shard o2) {
return compareShardsByStartHash(o1, o2) * -1;
}
});
} // else we were supplied a NONE sort order so no sorting
// build the Shard map into the correct order
shardMap.clear();
for (Shard s : sortShards) {
shardMap.put(s.shardId(), new ShardHashInfo(streamName, s));
}
return shardMap;
}