Permalink
Browse files

workspace: Rename GoPackageList to Packages.

This is a minor refactor to use a simpler struct field name,
with the goal of improving readability.
  • Loading branch information...
dmitshur committed Dec 15, 2018
1 parent c2ed958 commit 7f138b4c2e3ac1fd1ece09ab844716c76143293c
Showing with 65 additions and 66 deletions.
  1. +12 −12 cmd/Go-Package-Store/update.go
  2. +53 −54 workspace/workspace.go
@@ -56,9 +56,9 @@ func (u updateWorker) Start() {

func (u updateWorker) run() {
for ur := range u.updateRequests {
c.pipeline.GoPackageList.Lock()
rp, ok := c.pipeline.GoPackageList.ByRoot[ur.Root]
c.pipeline.GoPackageList.Unlock()
c.pipeline.Packages.Lock()
rp, ok := c.pipeline.Packages.ByRoot[ur.Root]
c.pipeline.Packages.Unlock()
if !ok {
ur.ResponseChan <- fmt.Errorf("root %q not found", ur.Root)
continue
@@ -69,30 +69,30 @@ func (u updateWorker) run() {
}

// Mark repo as updating.
c.pipeline.GoPackageList.Lock()
c.pipeline.GoPackageList.ByRoot[ur.Root].UpdateState = workspace.Updating
c.pipeline.GoPackageList.Unlock()
c.pipeline.Packages.Lock()
c.pipeline.Packages.ByRoot[ur.Root].UpdateState = workspace.Updating
c.pipeline.Packages.Unlock()

updateError := u.updater.Update(rp.Repo)

if updateError == nil {
c.pipeline.GoPackageList.Lock()
for i, rp := range c.pipeline.GoPackageList.Active {
c.pipeline.Packages.Lock()
for i, rp := range c.pipeline.Packages.Active {
if rp.Repo.Root == ur.Root {
// Remove from active.
copy(c.pipeline.GoPackageList.Active[i:], c.pipeline.GoPackageList.Active[i+1:])
c.pipeline.GoPackageList.Active = c.pipeline.GoPackageList.Active[:len(c.pipeline.GoPackageList.Active)-1]
copy(c.pipeline.Packages.Active[i:], c.pipeline.Packages.Active[i+1:])
c.pipeline.Packages.Active = c.pipeline.Packages.Active[:len(c.pipeline.Packages.Active)-1]

// Mark repo as updated.
rp.UpdateState = workspace.Updated

// Append to history.
c.pipeline.GoPackageList.History = append(c.pipeline.GoPackageList.History, rp)
c.pipeline.Packages.History = append(c.pipeline.Packages.History, rp)

break
}
}
c.pipeline.GoPackageList.Unlock()
c.pipeline.Packages.Unlock()
}

ur.ResponseChan <- updateError
@@ -16,39 +16,6 @@ import (
"golang.org/x/tools/go/vcs"
)

// GoPackageList is a list of Go packages.
// It's implemented as two slices and map that are kept in sync, with a mutex.
type GoPackageList struct {
sync.Mutex
Active []*RepoPresentation // Active repo presentations, latest at the end.
History []*RepoPresentation // Historical repo presentations, latest at the end.
ByRoot map[string]*RepoPresentation // Map key is repoRoot.
}

// RepoPresentation represents a repository update presentation.
type RepoPresentation struct {
Repo *gps.Repo
Presentation *presenter.Presentation

UpdateState UpdateState
}

// UpdateState represents the state of an update.
//
// TODO: Dedup.
type UpdateState uint8

const (
// Available represents an available update.
Available UpdateState = iota

// Updating represents an update in progress.
Updating

// Updated represents a completed update.
Updated
)

// Pipeline for processing a Go workspace, where each repo has local and remote components.
type Pipeline struct {
wd string // Working directory. Used to resolve relative import paths.
@@ -73,15 +40,47 @@ type Pipeline struct {
reposMu sync.Mutex
repos map[string]*gps.Repo // Map key is the import path corresponding to the root of the repository.

newObserver chan observerRequest
observers map[chan *RepoPresentation]struct{}
GoPackageList *GoPackageList
newObserver chan observerRequest
observers map[chan *RepoPresentation]struct{}

// Packages is the working list of Go packages.
// It's implemented as two slices and a map kept in sync, protected by a mutex.
Packages struct {
sync.Mutex
Active []*RepoPresentation // Active repo presentations, latest at the end.
History []*RepoPresentation // Historical repo presentations, latest at the end.
ByRoot map[string]*RepoPresentation // Map key is repoRoot.
}
}

type observerRequest struct {
Response chan chan *RepoPresentation
}

// RepoPresentation represents a repository update presentation.
type RepoPresentation struct {
Repo *gps.Repo
Presentation *presenter.Presentation

UpdateState UpdateState
}

// UpdateState represents the state of an update.
//
// TODO: Dedup.
type UpdateState uint8

const (
// Available represents an available update.
Available UpdateState = iota

// Updating represents an update in progress.
Updating

// Updated represents a completed update.
Updated
)

// NewPipeline creates a Pipeline with working directory wd.
// Working directory is used to resolve relative import paths.
//
@@ -104,10 +103,10 @@ func NewPipeline(wd string) *Pipeline {

repos: make(map[string]*gps.Repo),

newObserver: make(chan observerRequest),
observers: make(map[chan *RepoPresentation]struct{}),
GoPackageList: &GoPackageList{ByRoot: make(map[string]*RepoPresentation)},
newObserver: make(chan observerRequest),
observers: make(map[chan *RepoPresentation]struct{}),
}
p.Packages.ByRoot = make(map[string]*RepoPresentation)

// It is a lot of work to
// find all Go packages in one's GOPATH workspace (or vendor.json file),
@@ -329,15 +328,15 @@ Outer:
}

// Append repoPresentation to current list.
p.GoPackageList.Lock()
p.Packages.Lock()
switch repoPresentation.UpdateState {
case Available, Updating:
p.GoPackageList.Active = append(p.GoPackageList.Active, repoPresentation)
p.Packages.Active = append(p.Packages.Active, repoPresentation)
case Updated:
p.GoPackageList.History = append(p.GoPackageList.History, repoPresentation)
p.Packages.History = append(p.Packages.History, repoPresentation)
}
p.GoPackageList.ByRoot[repoPresentation.Repo.Root] = repoPresentation
p.GoPackageList.Unlock()
p.Packages.ByRoot[repoPresentation.Repo.Root] = repoPresentation
p.Packages.Unlock()

// Send new repoPresentation to all existing observers.
for ch := range p.observers {
@@ -346,15 +345,15 @@ Outer:
}
// New observer request.
case req := <-p.newObserver:
p.GoPackageList.Lock()
ch := make(chan *RepoPresentation, len(p.GoPackageList.Active)+len(p.GoPackageList.History))
for _, repoPresentation := range p.GoPackageList.Active {
p.Packages.Lock()
ch := make(chan *RepoPresentation, len(p.Packages.Active)+len(p.Packages.History))
for _, repoPresentation := range p.Packages.Active {
ch <- repoPresentation
}
for _, repoPresentation := range p.GoPackageList.History {
for _, repoPresentation := range p.Packages.History {
ch <- repoPresentation
}
p.GoPackageList.Unlock()
p.Packages.Unlock()

p.observers[ch] = struct{}{}

@@ -370,15 +369,15 @@ Outer:

// Respond to new observer requests directly.
for req := range p.newObserver {
p.GoPackageList.Lock()
ch := make(chan *RepoPresentation, len(p.GoPackageList.Active)+len(p.GoPackageList.History))
for _, repoPresentation := range p.GoPackageList.Active {
p.Packages.Lock()
ch := make(chan *RepoPresentation, len(p.Packages.Active)+len(p.Packages.History))
for _, repoPresentation := range p.Packages.Active {
ch <- repoPresentation
}
for _, repoPresentation := range p.GoPackageList.History {
for _, repoPresentation := range p.Packages.History {
ch <- repoPresentation
}
p.GoPackageList.Unlock()
p.Packages.Unlock()

close(ch)

0 comments on commit 7f138b4

Please sign in to comment.