summaryrefslogtreecommitdiff
path: root/scanIterator.go
blob: 8408cefa4b4e33b0567497d9e4372e68b121c967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
}