From 184949a501e835079e68a82646382f6f3b9ec49e Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Thu, 17 Aug 2017 07:51:26 -0500 Subject: [PATCH] gps: source cache: interface tweaks and implementation optimizations --- internal/gps/maybe_source.go | 4 ++-- internal/gps/source.go | 2 +- internal/gps/source_cache.go | 31 +++++++++++++++---------------- internal/gps/source_cache_test.go | 2 +- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/internal/gps/maybe_source.go b/internal/gps/maybe_source.go index 9151f17b1f..31774887ba 100644 --- a/internal/gps/maybe_source.go +++ b/internal/gps/maybe_source.go @@ -112,7 +112,7 @@ func (m maybeGitSource) try(ctx context.Context, cachedir string, c singleSource return nil, 0, err } - c.storeVersionMap(vl, true) + c.setVersionMap(vl) state := sourceIsSetUp | sourceExistsUpstream | sourceHasLatestVersionList if r.CheckLocal() { @@ -173,7 +173,7 @@ func (m maybeGopkginSource) try(ctx context.Context, cachedir string, c singleSo return nil, 0, err } - c.storeVersionMap(vl, true) + c.setVersionMap(vl) state := sourceIsSetUp | sourceExistsUpstream | sourceHasLatestVersionList if r.CheckLocal() { diff --git a/internal/gps/source.go b/internal/gps/source.go index d687a1b687..c8d64f9cd7 100644 --- a/internal/gps/source.go +++ b/internal/gps/source.go @@ -514,7 +514,7 @@ func (sg *sourceGateway) require(ctx context.Context, wanted sourceState) (errSt }) if err == nil { - sg.cache.storeVersionMap(pvl, true) + sg.cache.setVersionMap(pvl) } case sourceHasLatestLocally: err = sg.suprvsr.do(ctx, sg.src.sourceType(), ctSourceFetch, func(ctx context.Context) error { diff --git a/internal/gps/source_cache.go b/internal/gps/source_cache.go index d10ec0b399..00c521b94c 100644 --- a/internal/gps/source_cache.go +++ b/internal/gps/source_cache.go @@ -34,11 +34,10 @@ type singleSourceCache interface { // Store the mappings between a set of PairedVersions' surface versions // their corresponding revisions. // - // If flush is true, the existing list of versions will be purged before - // writing. Revisions will have their pairings purged, but record of the - // revision existing will be kept, on the assumption that revisions are - // immutable and permanent. - storeVersionMap(versionList []PairedVersion, flush bool) + // The existing list of versions will be purged before writing. Revisions + // will have their pairings purged, but record of the revision existing will + // be kept, on the assumption that revisions are immutable and permanent. + setVersionMap(versionList []PairedVersion) // Get the list of unpaired versions corresponding to the given revision. getVersionsFor(Revision) ([]UnpairedVersion, bool) @@ -135,20 +134,17 @@ func (c *singleSourceCacheMemory) getPackageTree(r Revision) (pkgtree.PackageTre return ptree, has } -func (c *singleSourceCacheMemory) storeVersionMap(versionList []PairedVersion, flush bool) { +func (c *singleSourceCacheMemory) setVersionMap(versionList []PairedVersion) { c.mut.Lock() - if flush { - // TODO(sdboyer) how do we handle cache consistency here - revs that may - // be out of date vis-a-vis the ptrees or infos maps? - for r := range c.rMap { - c.rMap[r] = nil - } - - c.vMap = make(map[UnpairedVersion]Revision) + // TODO(sdboyer) how do we handle cache consistency here - revs that may + // be out of date vis-a-vis the ptrees or infos maps? + for r := range c.rMap { + c.rMap[r] = nil } - for _, v := range versionList { - pv := v.(PairedVersion) + c.vMap = make(map[UnpairedVersion]Revision) + + for _, pv := range versionList { u, r := pv.Unpair(), pv.Revision() c.vMap[u] = r c.rMap[r] = append(c.rMap[r], u) @@ -172,6 +168,9 @@ func (c *singleSourceCacheMemory) getVersionsFor(r Revision) ([]UnpairedVersion, } func (c *singleSourceCacheMemory) getAllVersions() []PairedVersion { + if len(c.vMap) == 0 { + return nil + } vlist := make([]PairedVersion, 0, len(c.vMap)) for v, r := range c.vMap { vlist = append(vlist, v.Pair(r)) diff --git a/internal/gps/source_cache_test.go b/internal/gps/source_cache_test.go index 79e69c339b..7980876997 100644 --- a/internal/gps/source_cache_test.go +++ b/internal/gps/source_cache_test.go @@ -180,7 +180,7 @@ func TestSingleSourceCache(t *testing.T) { NewVersion(ver).Pair(rev2), } SortPairedForDowngrade(versions) - c.storeVersionMap(versions, true) + c.setVersionMap(versions) t.Run("getAllVersions", func(t *testing.T) { got := c.getAllVersions()