diff options
| author | Jeff Carr <[email protected]> | 2024-11-15 17:25:11 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-11-15 17:25:11 -0600 |
| commit | 5ef3669e7d35d6b281e2c4b5972fa9958c30f74c (patch) | |
| tree | 1553423b3495b218e9bd5536f98e04bc0e9c0993 /packages.go | |
| parent | eacbb1fb7af28656b269b8bdc287f67a88e4f233 (diff) | |
stub in sort function
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'packages.go')
| -rw-r--r-- | packages.go | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/packages.go b/packages.go new file mode 100644 index 0000000..1436c45 --- /dev/null +++ b/packages.go @@ -0,0 +1,131 @@ +package zoopb + +// this is becoming a standard format +// todo: autogenerate this from the .proto file? + +import ( + "fmt" + "os" + "sort" + sync "sync" +) + +// bad global lock until I figure out some other plan +var lock sync.RWMutex + +type PackageIterator struct { + sync.RWMutex + + packs []*Package + index int +} + +// NewPackageIterator initializes a new iterator. +func NewPackageIterator(packs []*Package) *PackageIterator { + return &PackageIterator{packs: packs} +} + +// Scan moves to the next element and returns false if there are no more packs. +func (it *PackageIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Package returns the current repo. +func (it *PackageIterator) Package() *Package { + if it.packs[it.index-1] == nil { + for i, d := range it.packs { + fmt.Println("i =", i, d) + } + fmt.Println("len =", len(it.packs)) + fmt.Println("repo == nil", it.index, it.index-1) + os.Exit(-1) + } + return it.packs[it.index-1] +} + +// Use Scan() in a loop, similar to a while loop +// +// for iterator.Scan() { +// d := iterator.Package() +// fmt.Println("Package UUID:", d.Uuid) +// } + +func (r *Packages) All() *PackageIterator { + repoPointers := r.selectAllPackages() + + iterator := NewPackageIterator(repoPointers) + return iterator +} + +func (r *Packages) SortByName() *PackageIterator { + packs := r.selectAllPackages() + + sort.Sort(ByName(packs)) + + iterator := NewPackageIterator(packs) + return iterator +} + +// should this be a pointer? what really happens here? +func (r *Packages) Append(newP *Package) { + lock.Lock() + defer lock.Unlock() + + r.Packages = append(r.Packages, newP) +} + +type ByName []*Package + +func (a ByName) Len() int { return len(a) } +func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name } +func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// safely returns a slice of pointers to the Package protobufs +func (r *Packages) selectAllPackages() []*Package { + lock.RLock() + defer lock.RUnlock() + + // Create a new slice to hold pointers to each Package + var allPacks []*Package + allPacks = make([]*Package, len(r.Packages)) + for i, p := range r.Packages { + allPacks[i] = p // Copy pointers for safe iteration + } + + return allPacks +} + +/* +func (r *Packages) UnmergedPackageRepos() *PackageRepoIterator { + repoPointers := r.selectUnmergedPackageRepos() + + sort.Sort(ByName(repoPointers)) + + iterator := NewPackageRepoIterator(repoPointers) + + return iterator +} +*/ + +/* +// this sort doesn't really work. I think it forgets to sort the last two +// todo: sort this out. literally +// SelectPackagePointers safely returns a slice of pointers to Package records. +func (r *Packages) selectUnmergedPackages() []*PackageRow { + r.RLock() + defer r.RUnlock() + + // Create a new slice to hold pointers to each Package + // repoPointers := make([]*Package, len(c.E.Packages)) + var repoPointers []*PackageRow + for _, repo := range me.allrepos { + repoPointers = append(repoPointers, repo) // Copy pointers for safe iteration + } + + return repoPointers +} +*/ |
