107 lines
2.4 KiB
Go
107 lines
2.4 KiB
Go
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
|
|
}
|