From 2e3e4f98d33d8794a1c8a6aa13e6c9fa92fbcadb Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 27 Nov 2024 13:57:17 -0600 Subject: start full refactor to have repo be here Signed-off-by: Jeff Carr --- repo.sort.go | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 repo.sort.go (limited to 'repo.sort.go') diff --git a/repo.sort.go b/repo.sort.go new file mode 100644 index 0000000..6495676 --- /dev/null +++ b/repo.sort.go @@ -0,0 +1,150 @@ +package gitpb + +// 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 repolock sync.RWMutex + +type RepoIterator struct { + sync.RWMutex + + packs []*Repo + index int +} + +// NewRepoIterator initializes a new iterator. +func NewRepoIterator(packs []*Repo) *RepoIterator { + return &RepoIterator{packs: packs} +} + +// Scan moves to the next element and returns false if there are no more packs. +func (it *RepoIterator) Scan() bool { + if it.index >= len(it.packs) { + return false + } + it.index++ + return true +} + +// Repo returns the current repo. +func (it *RepoIterator) Repo() *Repo { + 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.Repo() +// fmt.Println("Repo UUID:", d.Uuid) +// } + +func (r *Repos) All() *RepoIterator { + repoPointers := r.selectAllRepo() + + iterator := NewRepoIterator(repoPointers) + return iterator +} + +func (r *Repos) SortByName() *RepoIterator { + packs := r.selectAllRepo() + + sort.Sort(RepoByName(packs)) + + iterator := NewRepoIterator(packs) + return iterator +} + +// enforces no duplicate package names +func (all *Repos) Append(newP *Repo) bool { + repolock.Lock() + defer repolock.Unlock() + + for _, p := range all.Repos { + if p.GoPath == newP.GoPath { + return false + } + } + + all.Repos = append(all.Repos, newP) + return true +} + +/* +// returns time.Duration since last Update() +func (r *Repo) LastPull() time.Duration { + t := time.Since(r.LastPull.AsTime()) + return t +} +*/ + +// find a package by gopath +func (all *Repos) FindByPath(gopath string) *Repo { + repolock.RLock() + defer repolock.RUnlock() + + for _, p := range all.Repos { + if p.GoPath == gopath { + return p + } + } + + return nil +} + +func (all *Repos) Len() int { + repolock.RLock() + defer repolock.RUnlock() + + return len(all.Repos) +} + +type RepoByName []*Repo + +func (a RepoByName) Len() int { return len(a) } +func (a RepoByName) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath } +func (a RepoByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +// safely returns a slice of pointers to the Repo protobufs +func (all *Repos) selectAllRepo() []*Repo { + repolock.RLock() + defer repolock.RUnlock() + + // Create a new slice to hold pointers to each Repo + var aRepos []*Repo + aRepos = make([]*Repo, len(all.Repos)) + for i, p := range all.Repos { + aRepos[i] = p // Copy pointers for safe iteration + } + + return aRepos +} + +func (all *Repos) DeleteByPath(gopath string) *Repo { + repolock.Lock() + defer repolock.Unlock() + + for i, _ := range all.Repos { + if all.Repos[i].GoPath == gopath { + all.Repos[i] = all.Repos[len(all.Repos)-1] + all.Repos = all.Repos[:len(all.Repos)-1] + return nil + } + } + return nil +} -- cgit v1.2.3