Package Age() Update() Append()

This commit is contained in:
Jeff Carr 2024-11-15 18:12:17 -06:00
parent d6e0482465
commit b833bceb39
1 changed files with 49 additions and 3 deletions

View File

@ -8,6 +8,9 @@ import (
"os"
"sort"
sync "sync"
"time"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
// bad global lock until I figure out some other plan
@ -85,11 +88,47 @@ func (r *Packages) Append(newP *Package) bool {
return true
}
// find a package by name
func (r *Packages) FindByName(name string) *Package {
// Update version and timestamp.
// returns (ok, changed)
func (r *Packages) Update(newP *Package) (bool, bool) {
lock.Lock()
defer lock.Unlock()
var found *Package
for _, p := range r.Packages {
if p.Name == newP.Name {
found = p
}
}
if found == nil {
// r.Append(newP) // update here?
return false, true
}
var changed bool = false
if newP.Version != found.Version {
changed = true
found.Version = newP.Version
}
now := time.Now()
found.Laststamp = timestamppb.New(now)
r.Packages = append(r.Packages, newP)
return true, changed
}
// returns time.Duration since last Update()
func (r *Package) Age(newP *Package) time.Duration {
t := time.Since(r.Laststamp.AsTime())
return t
}
// find a package by name
func (r *Packages) FindByName(name string) *Package {
lock.RLock()
defer lock.RUnlock()
for _, p := range r.Packages {
if p.Name == name {
return p
@ -99,6 +138,13 @@ func (r *Packages) FindByName(name string) *Package {
return nil
}
func (r *Packages) Len() int {
lock.RLock()
defer lock.RUnlock()
return len(r.Packages)
}
type ByName []*Package
func (a ByName) Len() int { return len(a) }