From bedf62977fb24c3f36a1c60b07395093db9fb0d5 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 10 Sep 2023 19:08:13 +0000 Subject: [PATCH] cluster: move Machines to a dedicated file Signed-off-by: Alejandro Mery --- pkg/cluster/machines.go | 69 +++++++++++++++++++++++++++++++++++++++++ pkg/cluster/zones.go | 65 -------------------------------------- 2 files changed, 69 insertions(+), 65 deletions(-) create mode 100644 pkg/cluster/machines.go diff --git a/pkg/cluster/machines.go b/pkg/cluster/machines.go new file mode 100644 index 0000000..e49f2d5 --- /dev/null +++ b/pkg/cluster/machines.go @@ -0,0 +1,69 @@ +package cluster + +import "sort" + +var ( + _ MachineIterator = Machines(nil) + _ sort.Interface = Machines(nil) +) + +// A MachineIterator is a set of Machines we can iterate on +type MachineIterator interface { + ForEachMachine(func(*Machine) bool) +} + +// Machines is a list of Machine objects +type Machines []*Machine + +// ForEachMachine calls a function for each Machine in the list +// until instructed to terminate the loop +func (m Machines) ForEachMachine(fn func(*Machine) bool) { + for _, p := range m { + if fn(p) { + return + } + } +} + +// Len returns the number of machines in the list +func (m Machines) Len() int { + return len(m) +} + +// Less implements sort.Interface to sort the list +func (m Machines) Less(i, j int) bool { + a, b := m[i], m[j] + za, zb := a.Zone(), b.Zone() + + switch { + case za == zb: + return a.ID < b.ID + default: + return za < zb + } +} + +// Swap implements sort.Interface to sort the list +func (m Machines) Swap(i, j int) { + m[i], m[j] = m[j], m[i] +} + +// FilterMachines produces a subset of the machines offered by the given +// iterator fulfilling a condition +func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) { + var out []*Machine + + if cond == nil { + // unconditional + cond = func(*Machine) bool { return true } + } + + m.ForEachMachine(func(p *Machine) bool { + if cond(p) { + out = append(out, p) + } + return false + }) + + return out, len(out) +} diff --git a/pkg/cluster/zones.go b/pkg/cluster/zones.go index 26d9bb8..000c575 100644 --- a/pkg/cluster/zones.go +++ b/pkg/cluster/zones.go @@ -2,7 +2,6 @@ package cluster import ( "io/fs" - "sort" "darvaza.org/resolver" "darvaza.org/slog" @@ -10,80 +9,16 @@ import ( ) var ( - _ MachineIterator = Machines(nil) - _ sort.Interface = Machines(nil) - _ MachineIterator = (*Zone)(nil) _ MachineIterator = (*Zones)(nil) _ ZoneIterator = (*Zones)(nil) ) -// A MachineIterator is a set of Machines we can iterate on -type MachineIterator interface { - ForEachMachine(func(*Machine) bool) -} - // A ZoneIterator is a set of Zones we can iterate on type ZoneIterator interface { ForEachZone(func(*Zone) bool) } -// Machines is a list of Machine objects -type Machines []*Machine - -// ForEachMachine calls a function for each Machine in the list -// until instructed to terminate the loop -func (m Machines) ForEachMachine(fn func(*Machine) bool) { - for _, p := range m { - if fn(p) { - return - } - } -} - -// Len returns the number of machines in the list -func (m Machines) Len() int { - return len(m) -} - -// Less implements sort.Interface to sort the list -func (m Machines) Less(i, j int) bool { - a, b := m[i], m[j] - za, zb := a.Zone(), b.Zone() - - switch { - case za == zb: - return a.ID < b.ID - default: - return za < zb - } -} - -// Swap implements sort.Interface to sort the list -func (m Machines) Swap(i, j int) { - m[i], m[j] = m[j], m[i] -} - -// FilterMachines produces a subset of the machines offered by the given -// iterator fulfilling a condition -func FilterMachines(m MachineIterator, cond func(*Machine) bool) (Machines, int) { - var out []*Machine - - if cond == nil { - // unconditional - cond = func(*Machine) bool { return true } - } - - m.ForEachMachine(func(p *Machine) bool { - if cond(p) { - out = append(out, p) - } - return false - }) - - return out, len(out) -} - // Zone represents one zone in a cluster type Zone struct { zones *Zones