Atlas › Test

TestSortedSetManager_Paginate

Exact test identity: go.temporal.io/server/common/collection/TestSortedSetManager_Paginate

Package
go.temporal.io/server/common/collection
Suite / test hierarchy
TestSortedSetManager_Paginate
Test
TestSortedSetManager_Paginate
Introduced at
sorted_set_manager.go ×1 Frontier kind: Joint frontier
Covered ranges
5
Covered lines
27
Covered files
1

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/collection/sorted_set_manager.go 27 covered LOC · 5 ranges

Open complete file

11
12 // NewSortedSetManager returns a new SortedSetManager with the given comparison function and key function.
13 > func NewSortedSetManager[S ~[]E, E, K any](cmp func(E, K) int, key func(E) K) SortedSetManager[S, E, K] { sorted_set_manager.go
14 > return SortedSetManager[S, E, K]{cmp, key}
15 > }
16
17 // Add adds a new element to the set. If the element is already in the set, it returns the set unchanged and false.
18 > func (m SortedSetManager[S, E, K]) Add(set S, e E) (S, bool) { sorted_set_manager.go
19 > i, found := m.find(set, m.key(e))
20 > if found {
21 return set, false
22 }
23 > return slices.Insert(set, i, e), true sorted_set_manager.go
24 }
25
35 // Paginate returns up to n elements in the set that compare greater than gtKey. If there are more than n such elements,
36 // it also returns the key of the last element in the page. Otherwise, the second return value is nil.
37 > func (m SortedSetManager[S, E, K]) Paginate(set S, gtKey K, n int) (S, *K) { sorted_set_manager.go
38 > i, exists := m.find(set, gtKey)
39 > if exists {
40 > i++
41 > }
42 > var (
43 > lastKey *K
44 > page S
45 > )
46 > if i+n >= len(set) {
47 > page = set[i:]
48 > } else {
49 > tmp := m.key(set[i+n-1])
50 > lastKey = &tmp
51 > page = set[i : i+n]
52 > }
53 > return page, lastKey
54 }
55
63 }
64
65 > func (m SortedSetManager[S, E, K]) find(set S, key K) (int, bool) { sorted_set_manager.go
66 > return slices.BinarySearchFunc(set, key, m.cmp)
67 > }