2020import org .elasticsearch .xcontent .XContentBuilder ;
2121
2222import java .io .IOException ;
23- import java .util .Collections ;
23+ import java .util .ArrayList ;
2424import java .util .Comparator ;
2525import java .util .Iterator ;
26+ import java .util .List ;
2627import java .util .Objects ;
2728import java .util .Set ;
28- import java .util .SortedSet ;
29- import java .util .TreeSet ;
3029import java .util .function .Consumer ;
3130
32- import static java .util .Collections .unmodifiableSortedSet ;
31+ import static java .util .Collections .unmodifiableList ;
3332
3433/**
3534 * Represents snapshots marked as to be deleted and pending deletion.
5150 */
5251public class SnapshotDeletionsPending extends AbstractNamedDiffable <Custom > implements Custom {
5352
54- public static final SnapshotDeletionsPending EMPTY = new SnapshotDeletionsPending (Collections . emptySortedSet ());
53+ public static final SnapshotDeletionsPending EMPTY = new SnapshotDeletionsPending (List . of ());
5554 public static final String TYPE = "snapshot_deletions_pending" ;
5655
5756 /**
@@ -70,19 +69,19 @@ public class SnapshotDeletionsPending extends AbstractNamedDiffable<Custom> impl
7069 /**
7170 * A list of snapshots to delete, sorted by creation time
7271 */
73- private final SortedSet <Entry > entries ;
72+ private final List <Entry > entries ;
7473
75- private SnapshotDeletionsPending (SortedSet <Entry > entries ) {
76- this .entries = unmodifiableSortedSet (Objects .requireNonNull (entries ));
74+ private SnapshotDeletionsPending (List <Entry > entries ) {
75+ this .entries = unmodifiableList (Objects .requireNonNull (entries ));
7776 }
7877
7978 public SnapshotDeletionsPending (StreamInput in ) throws IOException {
80- this (new TreeSet <>( in .readSet (Entry ::new ) ));
79+ this (in .readList (Entry ::new ));
8180 }
8281
8382 @ Override
8483 public void writeTo (StreamOutput out ) throws IOException {
85- out .writeCollection (entries );
84+ out .writeList (entries );
8685 }
8786
8887 @ Override
@@ -98,7 +97,7 @@ public boolean contains(SnapshotId snapshotId) {
9897 return entries .stream ().anyMatch (entry -> Objects .equals (entry .getSnapshotId (), snapshotId ));
9998 }
10099
101- public SortedSet <Entry > entries () {
100+ public List <Entry > entries () {
102101 return entries ;
103102 }
104103
@@ -122,9 +121,13 @@ public SnapshotDeletionsPending withRemovedSnapshots(Set<SnapshotId> snapshotIds
122121 return this ;
123122 }
124123 boolean changed = false ;
125- final SortedSet <Entry > updatedEntries = new TreeSet <>(entries );
126- if (updatedEntries .removeIf (entry -> snapshotIds .contains (entry .getSnapshotId ()))) {
127- changed = true ;
124+ final List <Entry > updatedEntries = new ArrayList <>();
125+ for (Entry entry : entries ) {
126+ if (snapshotIds .contains (entry .snapshotId )) {
127+ changed = true ;
128+ continue ;
129+ }
130+ updatedEntries .add (entry );
128131 }
129132 if (changed == false ) {
130133 return this ;
@@ -240,18 +243,17 @@ public int compareTo(final Entry other) {
240243
241244 public static final class Builder {
242245
243- private final SortedSet <Entry > entries = new TreeSet <>() ;
246+ private final List <Entry > entries ;
244247 private final Consumer <Entry > consumer ;
245248
246249 public Builder (SnapshotDeletionsPending snapshotDeletionsPending , Consumer <Entry > onLimitExceeded ) {
247- entries . addAll (snapshotDeletionsPending .entries );
250+ this . entries = new ArrayList <> (snapshotDeletionsPending .entries );
248251 this .consumer = onLimitExceeded ;
249252 }
250253
251254 private void ensureLimit (final int maxPendingDeletions ) {
252255 while (entries .size () >= maxPendingDeletions ) {
253- final Entry removed = entries .last ();
254- entries .remove (removed );
256+ final Entry removed = entries .remove (0 );
255257 if (consumer != null ) {
256258 consumer .accept (removed );
257259 }
0 commit comments