Skip to content

Commit

Permalink
Converted variadic pairs to map
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ Barrett committed Apr 11, 2023
1 parent 0ae389b commit 29bdb10
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *KeyValueConsumer) Update(data io.Reader) error {
}

// Turn the map into a slice of key, value, ... and send it to the thing storing this data
_, err := c.updater.Update(mapToSlice(kvPairs)...)
_, err := c.updater.Update(kvPairs)

return err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/agent/flags/flag_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ func (fc *FlagController) set(key keys.FlagKey, value []byte) error {

// Update bulk replaces agent flags and stores them.
// Observers will be notified of changed flags and deleted flags.
func (fc *FlagController) Update(pairs ...string) ([]string, error) {
func (fc *FlagController) Update(kvPairs map[string]string) ([]string, error) {
// Attempt to bulk replace the store with the key-values
deletedKeys, err := fc.agentFlagsStore.Update(pairs...)
deletedKeys, err := fc.agentFlagsStore.Update(kvPairs)

// Extract just the keys from the key-value pairs
var updatedKeys []string
for i := 0; i < len(pairs); i += 2 {
updatedKeys = append(updatedKeys, pairs[i])
updatedKeys := make([]string, len(kvPairs))
i := 0
for k := range kvPairs {
updatedKeys[i] = k
i++
}

// Changed keys is the union of updated keys and deleted keys
Expand Down
6 changes: 3 additions & 3 deletions pkg/agent/flags/flag_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ func TestControllerUpdate(t *testing.T) {

tests := []struct {
name string
pairs []string
kvPairs map[string]string
changedKeys []string
}{
{
name: "happy path",
pairs: []string{keys.ControlRequestInterval.String(), "125000", keys.ControlServerURL.String(), "kolide-app.com"},
kvPairs: map[string]string{keys.ControlRequestInterval.String(): "125000", keys.ControlServerURL.String(): "kolide-app.com"},
changedKeys: []string{keys.ControlRequestInterval.String(), keys.ControlServerURL.String()},
},
}
Expand All @@ -213,7 +213,7 @@ func TestControllerUpdate(t *testing.T) {

fc.RegisterChangeObserver(mockObserver, keys.ToFlagKeys(tt.changedKeys)...)

changedKeys, err := fc.Update(tt.pairs...)
changedKeys, err := fc.Update(tt.kvPairs)
require.NoError(t, err)

assert.Equal(t, tt.changedKeys, changedKeys)
Expand Down
7 changes: 1 addition & 6 deletions pkg/agent/storage/bbolt/keyvalue_store_bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,11 @@ func (s *bboltKeyValueStore) ForEach(fn func(k, v []byte) error) error {
})
}

func (s *bboltKeyValueStore) Update(pairs ...string) ([]string, error) {
func (s *bboltKeyValueStore) Update(kvPairs map[string]string) ([]string, error) {
if s == nil || s.db == nil {
return nil, NoDbError{}
}

kvPairs := make(map[string]string)
for i := 0; i < len(pairs)-1; i += 2 {
kvPairs[pairs[i]] = pairs[i+1]
}

err := s.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(s.bucketName))
if b == nil {
Expand Down
38 changes: 19 additions & 19 deletions pkg/agent/storage/ci/keyvalue_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ func Test_Updates(t *testing.T) {

tests := []struct {
name string
updates [][]string
updates []map[string]string
want []map[string]string
}{
{
name: "empty",
updates: [][]string{{}, {}},
updates: []map[string]string{{}, {}},
want: []map[string]string{},
},
{
name: "single",
updates: [][]string{{"one", "one"}, {"one", "new_one"}},
updates: []map[string]string{{"one": "one"}, {"one": "new_one"}},
want: []map[string]string{
{
"key": "one",
Expand All @@ -181,16 +181,16 @@ func Test_Updates(t *testing.T) {
},
{
name: "multiple",
updates: [][]string{
updates: []map[string]string{
{
"one", "one",
"two", "two",
"three", "three",
"one": "one",
"two": "two",
"three": "three",
},
{
"one", "new_one",
"two", "new_two",
"three", "new_three",
"one": "new_one",
"two": "new_two",
"three": "new_three",
},
},
want: []map[string]string{
Expand All @@ -210,17 +210,17 @@ func Test_Updates(t *testing.T) {
},
{
name: "delete stale keys",
updates: [][]string{
updates: []map[string]string{
{
"one", "one",
"two", "two",
"three", "three",
"four", "four",
"five", "five",
"six", "six",
"one": "one",
"two": "two",
"three": "three",
"four": "four",
"five": "five",
"six": "six",
},
{
"four", "four",
"four": "four",
},
},
want: []map[string]string{
Expand All @@ -238,7 +238,7 @@ func Test_Updates(t *testing.T) {

for _, s := range getStores(t) {
for _, update := range tt.updates {
s.Update(update...)
s.Update(update)
}

kvps, err := getKeyValueRows(s, tt.name)
Expand Down
7 changes: 1 addition & 6 deletions pkg/agent/storage/inmemory/keyvalue_store_in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,14 @@ func (s *inMemoryKeyValueStore) ForEach(fn func(k, v []byte) error) error {
return nil
}

func (s *inMemoryKeyValueStore) Update(pairs ...string) ([]string, error) {
func (s *inMemoryKeyValueStore) Update(kvPairs map[string]string) ([]string, error) {
if s == nil {
return nil, errors.New("store is nil")
}

s.mu.Lock()
defer s.mu.Unlock()

kvPairs := make(map[string]string)
for i := 0; i < len(pairs)-1; i += 2 {
kvPairs[pairs[i]] = pairs[i+1]
}

s.items = make(map[string][]byte)

for key, value := range kvPairs {
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/types/keyvalue_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ type Iterator interface {

// Updater is an interface for bulk replacing data in a key/value store.
type Updater interface {
// Update takes a sequence of alternating key-value pairs, and inserts
// Update takes a map of key-value pairs, and inserts
// these key-values into the store. Any preexisting keys in the store which
// do not exist in data will be deleted.
Update(pairs ...string) ([]string, error)
Update(kvPairs map[string]string) ([]string, error)
}

// GetterSetter is an interface that groups the Get and Set methods.
Expand Down
24 changes: 9 additions & 15 deletions pkg/agent/types/mocks/keyvalue_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 29bdb10

Please sign in to comment.