From 74da63276ed73a78d66af45f2dca56a48f2f836a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 16 Nov 2024 01:52:43 -0600 Subject: rename Signed-off-by: Jeff Carr --- droplets.go | 106 ++++++++++++++++++++++++++++++++++++++++ scanIterator.go | 106 ---------------------------------------- work/google.golang.org/protobuf | 1 - 3 files changed, 106 insertions(+), 107 deletions(-) create mode 100644 droplets.go delete mode 100644 scanIterator.go delete mode 160000 work/google.golang.org/protobuf diff --git a/droplets.go b/droplets.go new file mode 100644 index 0000000..8408cef --- /dev/null +++ b/droplets.go @@ -0,0 +1,106 @@ +package virtbuf + +import ( + "fmt" + "os" + + "go.wit.com/log" +) + +type DropletIterator struct { + droplets []*Droplet + index int +} + +// NewDropletIterator initializes a new iterator. +func NewDropletIterator(droplets []*Droplet) *DropletIterator { + return &DropletIterator{droplets: droplets} +} + +// Scan moves to the next element and returns false if there are no more droplets. +func (it *DropletIterator) Scan() bool { + if it.index >= len(it.droplets) { + return false + } + it.index++ + return true +} + +// Droplet returns the current droplet. +func (it *DropletIterator) Droplet() *Droplet { + if it.droplets[it.index-1] == nil { + for i, d := range it.droplets { + fmt.Println("i =", i, d) + } + fmt.Println("len =", len(it.droplets)) + fmt.Println("droplet == nil", it.index, it.index-1) + os.Exit(-1) + } + return it.droplets[it.index-1] +} + +// Use Scan() in a loop, similar to a while loop +// +// for iterator.Scan() { +// d := iterator.Droplet() +// fmt.Println("Droplet UUID:", d.Uuid) +// } +func (c *Cluster) GetDropletIterator() *DropletIterator { + dropletPointers := c.SelectDropletPointers() + + iterator := NewDropletIterator(dropletPointers) + + return iterator +} + +func (c *Cluster) DropletsAll() *DropletIterator { + dropletPointers := c.SelectDropletAll() + + iterator := NewDropletIterator(dropletPointers) + + return iterator +} + +// SelectDropletPointers safely returns a slice of pointers to Droplet records. +func (c *Cluster) SelectDropletAll() []*Droplet { + c.RLock() + defer c.RUnlock() + + // Create a new slice to hold pointers to each Droplet + // dropletPointers := make([]*Droplet, len(c.E.Droplets)) + var dropletPointers []*Droplet + if c.d == nil { + log.Info("SelectDropletsAll() c.d == nil") + // os.Exit(-1) + } + for _, d := range c.d.Droplets { + if d == nil { + continue + } + if d.Archive != nil { + continue + } + dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration + } + + return dropletPointers +} + +// SelectDropletPointers safely returns a slice of pointers to Droplet records. +func (c *Cluster) SelectDropletPointers() []*Droplet { + c.RLock() + defer c.RUnlock() + + // Create a new slice to hold pointers to each Droplet + // dropletPointers := make([]*Droplet, len(c.E.Droplets)) + dropletPointers := make([]*Droplet, 1) + if c.d == nil { + log.Info("c.d == nil") + os.Exit(-1) + } + for _, d := range c.d.Droplets { + dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration + } + + return dropletPointers +} diff --git a/scanIterator.go b/scanIterator.go deleted file mode 100644 index 8408cef..0000000 --- a/scanIterator.go +++ /dev/null @@ -1,106 +0,0 @@ -package virtbuf - -import ( - "fmt" - "os" - - "go.wit.com/log" -) - -type DropletIterator struct { - droplets []*Droplet - index int -} - -// NewDropletIterator initializes a new iterator. -func NewDropletIterator(droplets []*Droplet) *DropletIterator { - return &DropletIterator{droplets: droplets} -} - -// Scan moves to the next element and returns false if there are no more droplets. -func (it *DropletIterator) Scan() bool { - if it.index >= len(it.droplets) { - return false - } - it.index++ - return true -} - -// Droplet returns the current droplet. -func (it *DropletIterator) Droplet() *Droplet { - if it.droplets[it.index-1] == nil { - for i, d := range it.droplets { - fmt.Println("i =", i, d) - } - fmt.Println("len =", len(it.droplets)) - fmt.Println("droplet == nil", it.index, it.index-1) - os.Exit(-1) - } - return it.droplets[it.index-1] -} - -// Use Scan() in a loop, similar to a while loop -// -// for iterator.Scan() { -// d := iterator.Droplet() -// fmt.Println("Droplet UUID:", d.Uuid) -// } -func (c *Cluster) GetDropletIterator() *DropletIterator { - dropletPointers := c.SelectDropletPointers() - - iterator := NewDropletIterator(dropletPointers) - - return iterator -} - -func (c *Cluster) DropletsAll() *DropletIterator { - dropletPointers := c.SelectDropletAll() - - iterator := NewDropletIterator(dropletPointers) - - return iterator -} - -// SelectDropletPointers safely returns a slice of pointers to Droplet records. -func (c *Cluster) SelectDropletAll() []*Droplet { - c.RLock() - defer c.RUnlock() - - // Create a new slice to hold pointers to each Droplet - // dropletPointers := make([]*Droplet, len(c.E.Droplets)) - var dropletPointers []*Droplet - if c.d == nil { - log.Info("SelectDropletsAll() c.d == nil") - // os.Exit(-1) - } - for _, d := range c.d.Droplets { - if d == nil { - continue - } - if d.Archive != nil { - continue - } - dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration - } - - return dropletPointers -} - -// SelectDropletPointers safely returns a slice of pointers to Droplet records. -func (c *Cluster) SelectDropletPointers() []*Droplet { - c.RLock() - defer c.RUnlock() - - // Create a new slice to hold pointers to each Droplet - // dropletPointers := make([]*Droplet, len(c.E.Droplets)) - dropletPointers := make([]*Droplet, 1) - if c.d == nil { - log.Info("c.d == nil") - os.Exit(-1) - } - for _, d := range c.d.Droplets { - dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration - } - - return dropletPointers -} diff --git a/work/google.golang.org/protobuf b/work/google.golang.org/protobuf deleted file mode 160000 index 29947bb..0000000 --- a/work/google.golang.org/protobuf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 29947bbd165cd2034df6d15ae06feb83af40c572 -- cgit v1.2.3