Skip to content

Commit

Permalink
Use map[T]bool for sets instead of k8s.io/apimachinery/pkg/util/sets
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 3, 2024
1 parent a85edc9 commit 45cb280
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
40 changes: 30 additions & 10 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"testing"

"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
Expand Down Expand Up @@ -117,35 +116,56 @@ func (a *realActionLog) validate(db *statedb.DB, t *testing.T) {
defer a.Unlock()

// Collapse the log down to objects that are alive at the end.
alive := map[statedb.Table[fuzzObj]]sets.Set[uint64]{}
alive := map[statedb.Table[fuzzObj]]map[uint64]bool{}
for _, e := range a.log {
aliveThis, ok := alive[e.table]
if !ok {
aliveThis = sets.New[uint64]()
aliveThis = map[uint64]bool{}
alive[e.table] = aliveThis
}
switch e.act {
case actInsert:
aliveThis.Insert(e.id)
aliveThis[e.id] = true
case actDelete:
aliveThis.Delete(e.id)
delete(aliveThis, e.id)
case actDeleteAll:
aliveThis.Clear()
clear(aliveThis)
}
}

for table, expected := range alive {
txn := db.ReadTxn()
iter, _ := table.All(txn)
actual := sets.New[uint64]()
actual := map[uint64]bool{}
for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() {
actual.Insert(obj.id)
actual[obj.id] = true
}
require.True(t, expected.Equal(actual), "validate failed, mismatching ids: %v",
actual.SymmetricDifference(expected))
require.Equal(t, expected, actual, "validate failed, mismatching ids: %v",
setSymmetricDifference(actual, expected))
}
}

func setSymmetricDifference[T comparable, M map[T]bool](s1, s2 M) M {
counts := make(map[T]int, len(s1)+len(s2))
for k1, v1 := range s1 {
if v1 {
counts[k1] = 1
}
}
for k2, v2 := range s2 {
if v2 {
counts[k2]++
}
}
result := M{}
for k, count := range counts {
if count == 1 {
result[k] = true
}
}
return result
}

type nopActionLog struct {
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
go.uber.org/goleak v1.3.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/time v0.5.0
k8s.io/apimachinery v0.29.1
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,3 @@ gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/apimachinery v0.29.1 h1:KY4/E6km/wLBguvCZv8cKTeOwwOBqFNjwJIdMkMbbRc=
k8s.io/apimachinery v0.29.1/go.mod h1:6HVkd1FwxIagpYrHSwJlQqZI3G9LfYWRPAkUvLnXTKU=
12 changes: 0 additions & 12 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package statedb
import (
"bytes"
"fmt"

"k8s.io/apimachinery/pkg/util/sets"
)

// Collect creates a slice of objects out of the iterator.
Expand All @@ -20,16 +18,6 @@ func Collect[Obj any](iter Iterator[Obj]) []Obj {
return objs
}

// CollectSet creates a set of objects out of the iterator.
// The iterator is consumed in the process.
func CollectSet[Obj comparable](iter Iterator[Obj]) sets.Set[Obj] {
objs := sets.New[Obj]()
for obj, _, ok := iter.Next(); ok; obj, _, ok = iter.Next() {
objs.Insert(obj)
}
return objs
}

// ProcessEach invokes the given function for each object provided by the iterator.
func ProcessEach[Obj any, It Iterator[Obj]](iter It, fn func(Obj, Revision) error) (err error) {
for obj, rev, ok := iter.Next(); ok; obj, rev, ok = iter.Next() {
Expand Down
12 changes: 5 additions & 7 deletions reconciler/example/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"path"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/hive/cell"
"github.com/cilium/statedb"
"github.com/cilium/statedb/reconciler"
Expand Down Expand Up @@ -46,19 +44,19 @@ func (ops *MemoOps) Delete(ctx context.Context, txn statedb.ReadTxn, memo *Memo)

// Prune unexpected memos.
func (ops *MemoOps) Prune(ctx context.Context, txn statedb.ReadTxn, iter statedb.Iterator[*Memo]) error {
expected := sets.New[string]()
expected := map[string]bool{}
for memo, _, ok := iter.Next(); ok; memo, _, ok = iter.Next() {
expected.Insert(memo.Name)
expected[memo.Name] = true
}

// Find unexpected files
unexpected := sets.New[string]()
unexpected := map[string]bool{}
if entries, err := os.ReadDir(ops.directory); err != nil {
return err
} else {
for _, entry := range entries {
if !expected.Has(entry.Name()) {
unexpected.Insert(entry.Name())
if !expected[entry.Name()] {
unexpected[entry.Name()] = true
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/util/sets"

"github.com/cilium/statedb/internal"
iradix "github.com/hashicorp/go-immutable-radix/v2"

Expand Down Expand Up @@ -73,13 +71,13 @@ func NewTable[Obj any](
}

// Validate that indexes have unique ids.
indexNames := sets.New[string]()
indexNames.Insert(primaryIndexer.indexName())
indexNames := map[string]bool{}
indexNames[primaryIndexer.indexName()] = true
for _, indexer := range secondaryIndexers {
if indexNames.Has(indexer.indexName()) {
if indexNames[indexer.indexName()] {
return nil, tableError(tableName, fmt.Errorf("index %q: %w", indexer.indexName(), ErrDuplicateIndex))
}
indexNames.Insert(indexer.indexName())
indexNames[indexer.indexName()] = true
}
for name := range indexNames {
if strings.HasPrefix(name, reservedIndexPrefix) {
Expand Down

0 comments on commit 45cb280

Please sign in to comment.