Skip to content

Commit

Permalink
update to ensure sets are kept in sorted order
Browse files Browse the repository at this point in the history
  • Loading branch information
saiskee committed May 10, 2024
1 parent 229a218 commit 14fb83b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/v0.38.7/efficient-union.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
changelog:
- type: NON_USER_FACING
description: >
"Makes the `Union` method in v2 sets more efficient. If the input set is sorted by resource ID, we can take advantage of this to
do a more efficient Union while keeping the Unioned set in sorted order.
54 changes: 54 additions & 0 deletions contrib/tests/set_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,59 @@ var _ = Describe("PaintSetV2", func() {
Expect(unionSetBA.Has(paintA) && unionSetBA.Has(paintB)).To(BeTrue())
})

Context("Sorted Order Preservation after Union", func() {
var (
setA, setB, unionSet sets_v2.ResourceSet[*v1.Paint]
paint1, paint2, paint3 *v1.Paint
)

BeforeEach(func() {
setA = sets_v2.NewResourceSet[*v1.Paint]()
setB = sets_v2.NewResourceSet[*v1.Paint]()
paint1 = &v1.Paint{
ObjectMeta: metav1.ObjectMeta{Name: "C", Namespace: "1"},
}
paint2 = &v1.Paint{
ObjectMeta: metav1.ObjectMeta{Name: "A", Namespace: "3"},
}
paint3 = &v1.Paint{
ObjectMeta: metav1.ObjectMeta{Name: "B", Namespace: "2"},
}
setA.Insert(paint1)
setB.Insert(paint2, paint3)
unionSet = setA.Union(setB)
})

It("should maintain sorted order in Iter after union", func() {
expectedOrder := []*v1.Paint{paint2, paint3, paint1} // Expected sorted by namespace
var actualOrder []*v1.Paint
unionSet.Iter(func(_ int, p *v1.Paint) bool {
actualOrder = append(actualOrder, p)
return true
})
Expect(actualOrder).To(Equal(expectedOrder))
})

It("should maintain sorted order in Filter after union", func() {
var filteredOrder []*v1.Paint
filterFunc := func(p *v1.Paint) bool {
return true // Select all
}
unionSet.Filter(filterFunc)(func(_ int, p *v1.Paint) bool {
filteredOrder = append(filteredOrder, p)
return true
})
Expect(filteredOrder).To(Equal([]*v1.Paint{paint2, paint3, paint1})) // Should match expected sorted order
})

It("should maintain sorted order in FilterOutAndCreateList after union", func() {
filterOutFunc := func(p *v1.Paint) bool {
return false // Do not filter out any items
}
filteredList := unionSet.FilterOutAndCreateList(filterOutFunc)
Expect(filteredList).To(Equal([]*v1.Paint{paint2, paint3, paint1})) // Should match expected sorted order
})
})

})
})

0 comments on commit 14fb83b

Please sign in to comment.