Notes added by 'git notes append'

This commit is contained in:
Jeff Carr 2025-02-15 07:33:13 -06:00
parent e5728fe40b
commit 2803e210f8
1 changed files with 234 additions and 0 deletions

View File

@ -427,3 +427,237 @@ func file_machine_proto_init() {
}
// `autogen:machine.sort.pb.go`
// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.0.50 2025-02-09_00:52:14_UTC
// go install go.wit.com/apps/autogenpb@latest
//
// define which structs (messages) you want to use in the .proto file
// Then sort.pb.go and marshal.pb.go files are autogenerated
//
// autogenpb uses it and has an example .proto file with instructions
//
package zoopb
import (
"fmt"
"sort"
"sync"
)
// a simple global lock
var machineMu sync.RWMutex
func (x *Machines) fixUuid() {
if x == nil {
return
}
if x.Uuid == "b57e7fac-a8fc-4949-9d50-fa38312dec87" {
return
}
x.Uuid = "b57e7fac-a8fc-4949-9d50-fa38312dec87"
x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb"
}
func NewMachines() *Machines {
x := new(Machines)
x.Uuid = "b57e7fac-a8fc-4949-9d50-fa38312dec87"
x.Version = "v0.0.1 go.wit.com/lib/protobuf/zoopb"
return x
}
// START SORT
// DEFINE THE Machines ITERATOR.
// itializes a new iterator.
func newMachinesIterator(things []*Machines) *MachinesIterator {
return &MachinesIterator{things: things}
}
type MachinesIterator struct {
sync.RWMutex // this isn't getting used properly yet?
things []*Machines
index int
}
func (it *MachinesIterator) Scan() bool {
if it.index >= len(it.things) {
return false
}
it.index++
return true
}
// Next() returns the next thing in the array
func (it *MachinesIterator) Next() *Machines {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in MachinesIterator", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE ITERATOR
// DEFINE THE Machine ITERATOR.
// itializes a new iterator.
func newMachineIterator(things []*Machine) *MachineIterator {
return &MachineIterator{things: things}
}
type MachineIterator struct {
sync.RWMutex // this isn't getting used properly yet?
things []*Machine
index int
}
func (it *MachineIterator) Scan() bool {
if it.index >= len(it.things) {
return false
}
it.index++
return true
}
// Next() returns the next thing in the array
func (it *MachineIterator) Next() *Machine {
if it.things[it.index-1] == nil {
fmt.Println("Next() error in MachineIterator", it.index)
}
return it.things[it.index-1]
}
// END DEFINE THE ITERATOR
// sort struct by Hostname
type MachineHostname []*Machine
func (a MachineHostname) Len() int { return len(a) }
func (a MachineHostname) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname }
func (a MachineHostname) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// safely returns a slice of pointers to the FRUIT protobufs
func (x *Machines) allMachines() []*Machine {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each FRUIT
var tmp []*Machine
tmp = make([]*Machine, len(x.Machines))
for i, p := range x.Machines {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
// safely returns a slice of pointers to the Machine protobufs
func (x *Machines) selectAllMachines() []*Machine {
x.RLock()
defer x.RUnlock()
// Create a new slice to hold pointers to each Machine
var tmp []*Machine
tmp = make([]*Machine, len(x.Machines))
for i, p := range x.Machines {
tmp[i] = p // Copy pointers for safe iteration
}
return tmp
}
func (x *Machines) SortByHostname() *MachineIterator {
// copy the pointers as fast as possible.
things := x.selectAllMachines()
// todo: try slices.SortFunc() instead to see what happens
sort.Sort(MachineHostname(things))
// slices.SortFunc(things, func(a, b *Machines) bool {
// return a.Hostname < b.Hostname // Sort by ??. let the compiler work it out??
// })
return newMachineIterator(things)
}
// END SORT
func (x *Machines) Len() int {
x.RLock()
defer x.RUnlock()
return len(x.Machines)
}
// just a simple Append() shortcut (but still uses the mutex lock)
func (x *Machines) Append(y *Machine) {
x.Lock()
defer x.Unlock()
x.Machines = append(x.Machines, y)
}
func (x *Machines) All() *MachineIterator {
MachinePointers := x.selectAllMachines()
iterator := newMachineIterator(MachinePointers)
return iterator
}
func (x *Machines) Delete(y *Machine) bool {
x.Lock()
defer x.Unlock()
for i, _ := range x.Machines {
if x.Machines[i] == y {
x.Machines[i] = x.Machines[len(x.Machines)-1]
x.Machines = x.Machines[:len(x.Machines)-1]
return true
}
}
return false
}
// lookup a Machines by the Hostname
func (x *Machines) FindByHostname(s string) *Machine {
if x == nil {
return nil
}
x.RLock()
defer x.RUnlock()
for i, _ := range x.Machines {
if x.Machines[i].Hostname == s {
return x.Machines[i]
}
}
return nil
}
func (x *Machines) DeleteByHostname(s string) bool {
x.Lock()
defer x.Unlock()
for i, _ := range x.Machines {
if x.Machines[i].Hostname == s {
x.Machines[i] = x.Machines[len(x.Machines)-1]
x.Machines = x.Machines[:len(x.Machines)-1]
return true
}
}
return false
}
func (x *Machines) AppendByHostname(y *Machine) bool {
x.Lock()
defer x.Unlock()
for _, p := range x.Machines {
if p.Hostname == y.Hostname {
return false
}
}
x.Machines = append(x.Machines, y)
return true
}