Skip to content

Commit dabba6a

Browse files
committed
internal/modindex: new API for incremental update
Create() creates a new index, while Update() may write an updated index, if there have been any changes in the module cache since the last index was written. If there is no index it creates one. It returns true if it wrote a new index, false if there was no need to write a new index. Change-Id: Ic797796c2ab6db6bd93b2059df86249ae62ca8ec Reviewed-on: https://go-review.googlesource.com/c/tools/+/621860 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 7d196fc commit dabba6a

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

internal/modindex/dir_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestIncremental(t *testing.T) {
7676
}
7777
}
7878
}
79-
if err := IndexModCache(dir, true); err != nil {
79+
if err := Create(dir); err != nil {
8080
t.Fatal(err)
8181
}
8282
// add new stuff to the module cache
@@ -90,15 +90,17 @@ func TestIncremental(t *testing.T) {
9090
}
9191
}
9292
}
93-
if err := IndexModCache(dir, false); err != nil {
93+
if ok, err := Update(dir); err != nil {
9494
t.Fatal(err)
95+
} else if !ok {
96+
t.Error("failed to write updated index")
9597
}
9698
index2, err := ReadIndex(dir)
9799
if err != nil {
98100
t.Fatal(err)
99101
}
100102
// build a fresh index
101-
if err := IndexModCache(dir, true); err != nil {
103+
if err := Create(dir); err != nil {
102104
t.Fatal(err)
103105
}
104106
index1, err := ReadIndex(dir)
@@ -126,7 +128,7 @@ func TestIncrementalNope(t *testing.T) {
126128
}
127129
}
128130
}
129-
if err := IndexModCache(dir, true); err != nil {
131+
if err := Create(dir); err != nil {
130132
t.Fatal(err)
131133
}
132134
// add new stuff to the module cache
@@ -140,15 +142,17 @@ func TestIncrementalNope(t *testing.T) {
140142
}
141143
}
142144
}
143-
if err := IndexModCache(dir, false); err != nil {
145+
if ok, err := Update(dir); err != nil {
144146
t.Fatal(err)
147+
} else if !ok {
148+
t.Error("failed to write updated index")
145149
}
146150
index2, err := ReadIndex(dir)
147151
if err != nil {
148152
t.Fatal(err)
149153
}
150154
// build a fresh index
151-
if err := IndexModCache(dir, true); err != nil {
155+
if err := Create(dir); err != nil {
152156
t.Fatal(err)
153157
}
154158
index1, err := ReadIndex(dir)
@@ -174,7 +178,7 @@ func TestDirsSinglePath(t *testing.T) {
174178
}
175179
}
176180
// build and check the index
177-
if err := IndexModCache(dir, false); err != nil {
181+
if err := Create(dir); err != nil {
178182
t.Fatal(err)
179183
}
180184
ix, err := ReadIndex(dir)

internal/modindex/gomodindex/cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ func init() {
8282
}
8383

8484
func index(dir string) {
85-
modindex.IndexModCache(dir, true)
85+
modindex.Create(dir)
8686
}
8787

8888
func update(dir string) {
89-
modindex.IndexModCache(dir, false)
89+
modindex.Update(dir)
9090
}
9191

9292
func query(dir string) {

internal/modindex/modindex.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,51 @@ import (
2121
"golang.org/x/mod/semver"
2222
)
2323

24-
// Modindex writes an index current as of when it is called.
24+
// Create always creates a new index for the go module cache that is in cachedir.
25+
func Create(cachedir string) error {
26+
_, err := indexModCache(cachedir, true)
27+
return err
28+
}
29+
30+
// Update the index for the go module cache that is in cachedir,
31+
// If there is no existing index it will build one.
32+
// If there are changed directories since the last index, it will
33+
// write a new one and return true. Otherwise it returns false.
34+
func Update(cachedir string) (bool, error) {
35+
return indexModCache(cachedir, false)
36+
}
37+
38+
// indexModCache writes an index current as of when it is called.
2539
// If clear is true the index is constructed from all of GOMODCACHE
2640
// otherwise the index is constructed from the last previous index
27-
// and the updates to the cache.
28-
func IndexModCache(cachedir string, clear bool) error {
41+
// and the updates to the cache. It returns true if it wrote an index,
42+
// false otherwise.
43+
func indexModCache(cachedir string, clear bool) (bool, error) {
2944
cachedir, err := filepath.Abs(cachedir)
3045
if err != nil {
31-
return err
46+
return false, err
3247
}
3348
cd := Abspath(cachedir)
3449
future := time.Now().Add(24 * time.Hour) // safely in the future
35-
err = modindexTimed(future, cd, clear)
50+
ok, err := modindexTimed(future, cd, clear)
3651
if err != nil {
37-
return err
52+
return false, err
3853
}
39-
return nil
54+
return ok, nil
4055
}
4156

4257
// modindexTimed writes an index current as of onlyBefore.
4358
// If clear is true the index is constructed from all of GOMODCACHE
4459
// otherwise the index is constructed from the last previous index
4560
// and all the updates to the cache before onlyBefore.
46-
// (this is useful for testing; perhaps it should not be exported)
47-
func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) error {
61+
// It returns true if it wrote a new index, false if it wrote nothing.
62+
func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, error) {
4863
var curIndex *Index
4964
if !clear {
5065
var err error
5166
curIndex, err = ReadIndex(string(cachedir))
5267
if clear && err != nil {
53-
return err
68+
return false, err
5469
}
5570
// TODO(pjw): check that most of those directorie still exist
5671
}
@@ -63,12 +78,16 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) error {
6378
cfg.onlyAfter = curIndex.Changed
6479
}
6580
if err := cfg.buildIndex(); err != nil {
66-
return err
81+
return false, err
82+
}
83+
if len(cfg.newIndex.Entries) == 0 {
84+
// no changes, don't write a new index
85+
return false, nil
6786
}
6887
if err := cfg.writeIndex(); err != nil {
69-
return err
88+
return false, err
7089
}
71-
return nil
90+
return true, nil
7291
}
7392

7493
type work struct {
@@ -86,6 +105,9 @@ func (w *work) buildIndex() error {
86105
// so set it now.
87106
w.newIndex = &Index{Changed: time.Now(), Cachedir: w.cacheDir}
88107
dirs := findDirs(string(w.cacheDir), w.onlyAfter, w.onlyBefore)
108+
if len(dirs) == 0 {
109+
return nil
110+
}
89111
newdirs, err := byImportPath(dirs)
90112
if err != nil {
91113
return err

0 commit comments

Comments
 (0)