gps: source cache: add ok bool to singleSourceCache.getAllVersions() in order to avoid depending on len(0) vs nil distinction

This commit is contained in:
Jordan Krage 2017-09-23 10:18:32 -05:00
Родитель ca3d62d6aa
Коммит 31ede573e7
7 изменённых файлов: 34 добавлений и 32 удалений

Просмотреть файл

@ -494,8 +494,10 @@ func (sg *sourceGateway) listVersions(ctx context.Context) ([]PairedVersion, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
if pvs, ok := sg.cache.getAllVersions(); ok {
return sg.cache.getAllVersions(), nil return pvs, nil
}
return nil, nil
} }
func (sg *sourceGateway) revisionPresentIn(ctx context.Context, r Revision) (bool, error) { func (sg *sourceGateway) revisionPresentIn(ctx context.Context, r Revision) (bool, error) {

Просмотреть файл

@ -43,7 +43,7 @@ type singleSourceCache interface {
getVersionsFor(Revision) ([]UnpairedVersion, bool) getVersionsFor(Revision) ([]UnpairedVersion, bool)
// Gets all the version pairs currently known to the cache. // Gets all the version pairs currently known to the cache.
getAllVersions() []PairedVersion getAllVersions() ([]PairedVersion, bool)
// Get the revision corresponding to the given unpaired version. // Get the revision corresponding to the given unpaired version.
getRevisionFor(UnpairedVersion) (Revision, bool) getRevisionFor(UnpairedVersion) (Revision, bool)
@ -169,17 +169,17 @@ func (c *singleSourceCacheMemory) getVersionsFor(r Revision) ([]UnpairedVersion,
return versionList, has return versionList, has
} }
func (c *singleSourceCacheMemory) getAllVersions() []PairedVersion { func (c *singleSourceCacheMemory) getAllVersions() ([]PairedVersion, bool) {
c.mut.Lock() c.mut.Lock()
vList := c.vList vList := c.vList
c.mut.Unlock() c.mut.Unlock()
if vList == nil { if vList == nil {
return nil return nil, false
} }
cp := make([]PairedVersion, len(vList)) cp := make([]PairedVersion, len(vList))
copy(cp, vList) copy(cp, vList)
return cp return cp, true
} }
func (c *singleSourceCacheMemory) getRevisionFor(uv UnpairedVersion) (Revision, bool) { func (c *singleSourceCacheMemory) getRevisionFor(uv UnpairedVersion) (Revision, bool) {

Просмотреть файл

@ -351,8 +351,7 @@ func (s *singleSourceCacheBolt) getVersionsFor(rev Revision) (uvs []UnpairedVers
return return
} }
func (s *singleSourceCacheBolt) getAllVersions() []PairedVersion { func (s *singleSourceCacheBolt) getAllVersions() (pvs []PairedVersion, ok bool) {
var pvs []PairedVersion
err := s.viewSourceBucket(func(src *bolt.Bucket) error { err := s.viewSourceBucket(func(src *bolt.Bucket) error {
versions := cacheFindLatestValid(src, cacheVersion, s.epoch) versions := cacheFindLatestValid(src, cacheVersion, s.epoch)
if versions == nil { if versions == nil {
@ -369,14 +368,15 @@ func (s *singleSourceCacheBolt) getAllVersions() []PairedVersion {
return err return err
} }
pvs = append(pvs, uv.Pair(Revision(v))) pvs = append(pvs, uv.Pair(Revision(v)))
ok = true
return nil return nil
}) })
}) })
if err != nil { if err != nil {
s.logger.Println(errors.Wrap(err, "failed to get all cached versions")) s.logger.Println(errors.Wrap(err, "failed to get all cached versions"))
return nil return nil, false
} }
return pvs return
} }
func (s *singleSourceCacheBolt) getRevisionFor(uv UnpairedVersion) (rev Revision, ok bool) { func (s *singleSourceCacheBolt) getRevisionFor(uv UnpairedVersion) (rev Revision, ok bool) {

Просмотреть файл

@ -119,8 +119,8 @@ func TestBoltCacheTimeout(t *testing.T) {
} }
comparePackageTree(t, ptree, got) comparePackageTree(t, ptree, got)
gotV := c.getAllVersions() gotV, ok := c.getAllVersions()
if len(gotV) != len(pvs) { if !ok || len(gotV) != len(pvs) {
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs) t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
} else { } else {
SortPairedForDowngrade(gotV) SortPairedForDowngrade(gotV)
@ -161,8 +161,8 @@ func TestBoltCacheTimeout(t *testing.T) {
} }
comparePackageTree(t, ptree, gotPtree) comparePackageTree(t, ptree, gotPtree)
pvs := c.getAllVersions() pvs, ok := c.getAllVersions()
if len(pvs) > 0 { if ok || len(pvs) > 0 {
t.Errorf("expected no cached versions, but got:\n\t%#v", pvs) t.Errorf("expected no cached versions, but got:\n\t%#v", pvs)
} }
} }
@ -194,8 +194,8 @@ func TestBoltCacheTimeout(t *testing.T) {
} }
comparePackageTree(t, ptree, got) comparePackageTree(t, ptree, got)
gotV := c.getAllVersions() gotV, ok := c.getAllVersions()
if len(gotV) != len(pvs) { if !ok || len(gotV) != len(pvs) {
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs) t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, pvs)
} else { } else {
SortPairedForDowngrade(gotV) SortPairedForDowngrade(gotV)
@ -282,8 +282,8 @@ func TestBoltCacheTimeout(t *testing.T) {
} }
comparePackageTree(t, newPtree, got) comparePackageTree(t, newPtree, got)
gotV := c.getAllVersions() gotV, ok := c.getAllVersions()
if len(gotV) != len(newPVS) { if !ok || len(gotV) != len(newPVS) {
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, newPVS) t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", gotV, newPVS)
} else { } else {
SortPairedForDowngrade(gotV) SortPairedForDowngrade(gotV)

Просмотреть файл

@ -77,19 +77,19 @@ func (c *multiCache) getVersionsFor(rev Revision) ([]UnpairedVersion, bool) {
return c.disk.getVersionsFor(rev) return c.disk.getVersionsFor(rev)
} }
func (c *multiCache) getAllVersions() []PairedVersion { func (c *multiCache) getAllVersions() ([]PairedVersion, bool) {
pvs := c.mem.getAllVersions() pvs, ok := c.mem.getAllVersions()
if pvs != nil { if ok {
return pvs return pvs, true
} }
pvs = c.disk.getAllVersions() pvs, ok = c.disk.getAllVersions()
if pvs != nil { if ok {
c.mem.setVersionMap(pvs) c.mem.setVersionMap(pvs)
return pvs return pvs, true
} }
return nil return nil, false
} }
func (c *multiCache) getRevisionFor(uv UnpairedVersion) (Revision, bool) { func (c *multiCache) getRevisionFor(uv UnpairedVersion) (Revision, bool) {

Просмотреть файл

@ -305,8 +305,8 @@ func (test singleSourceCacheTest) run(t *testing.T) {
} }
t.Run("getAllVersions", func(t *testing.T) { t.Run("getAllVersions", func(t *testing.T) {
got := c.getAllVersions() got, ok := c.getAllVersions()
if len(got) != len(versions) { if !ok || len(got) != len(versions) {
t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", got, versions) t.Errorf("unexpected versions:\n\t(GOT): %#v\n\t(WNT): %#v", got, versions)
} else { } else {
SortPairedForDowngrade(got) SortPairedForDowngrade(got)
@ -566,8 +566,8 @@ func (discardCache) getVersionsFor(Revision) ([]UnpairedVersion, bool) {
return nil, false return nil, false
} }
func (discardCache) getAllVersions() []PairedVersion { func (discardCache) getAllVersions() ([]PairedVersion, bool) {
return nil return nil, false
} }
func (discardCache) getRevisionFor(UnpairedVersion) (Revision, bool) { func (discardCache) getRevisionFor(UnpairedVersion) (Revision, bool) {

Просмотреть файл

@ -60,8 +60,8 @@ func testSourceGateway(t *testing.T) {
t.Fatalf("error on cloning git repo: %s", err) t.Fatalf("error on cloning git repo: %s", err)
} }
cvlist := sg.cache.getAllVersions() cvlist, ok := sg.cache.getAllVersions()
if len(cvlist) != 4 { if !ok || len(cvlist) != 4 {
t.Fatalf("repo setup should've cached four versions, got %v: %s", len(cvlist), cvlist) t.Fatalf("repo setup should've cached four versions, got %v: %s", len(cvlist), cvlist)
} }