Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1023 from jmank88/source_cache
Browse files Browse the repository at this point in the history
gps: source cache: interface tweaks and implementation optimizations
  • Loading branch information
sdboyer committed Aug 17, 2017
2 parents 2097d86 + 184949a commit ee6fcfc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions internal/gps/maybe_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 15 additions & 16 deletions internal/gps/source_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/source_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit ee6fcfc

Please sign in to comment.