Package Age() Update() Append()
This commit is contained in:
parent
d6e0482465
commit
b833bceb39
50
packages.go
50
packages.go
|
@ -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) }
|
||||
|
|
Loading…
Reference in New Issue