Debian Codegraph @

gopls/internal/cache/filemap.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package cache
6
7 import (
8 "iter"
9 "path/filepath"
10
11 "golang.org/x/tools/gopls/internal/file"
12 "golang.org/x/tools/gopls/internal/protocol"
13 "golang.org/x/tools/gopls/internal/util/persistent"
14 )
15
16 // A fileMap maps files in the snapshot, with some additional bookkeeping:
17 // It keeps track of overlays as well as directories containing any observed
18 // file.
19 type fileMap struct {
20 files *persistent.Map[protocol.DocumentURI, file.Handle]
21 overlays *persistent.Map[protocol.DocumentURI, *overlay] // the subset of files that are overlays
22 dirs *persistent.Set[string] // all dirs containing files; if nil, dirs have not been initialized
23 }
24
25 func newFileMap() *fileMap {
26 return &fileMap{
27 files: new(persistent.Map[protocol.DocumentURI, file.Handle]),
28 overlays: new(persistent.Map[protocol.DocumentURI, *overlay]),
29 dirs: new(persistent.Set[string]),
30 }
31 }
32
33 // clone creates a copy of the fileMap, incorporating the changes specified by
34 // the changes map.
35 func (m *fileMap) clone(changes map[protocol.DocumentURI]file.Handle) *fileMap {
36 m2 := &fileMap{
37 files: m.files.Clone(),
38 overlays: m.overlays.Clone(),
39 }
40 if m.dirs != nil {
41 m2.dirs = m.dirs.Clone()
42 }
43
44 // Handle file changes.
45 //
46 // Note, we can't simply delete the file unconditionally and let it be
47 // re-read by the snapshot, as (1) the snapshot must always observe all
48 // overlays, and (2) deleting a file forces directories to be reevaluated, as
49 // it may be the last file in a directory. We want to avoid that work in the
50 // common case where a file has simply changed.
51 //
52 // For that reason, we also do this in two passes, processing deletions
53 // first, as a set before a deletion would result in pointless work.
54 for uri, fh := range changes {
55 if !fileExists(fh) {
56 m2.delete(uri)
57 }
58 }
59 for uri, fh := range changes {
60 if fileExists(fh) {
61 m2.set(uri, fh)
62 }
63 }
64 return m2
65 }
66
67 func (m *fileMap) destroy() {
68 m.files.Destroy()
69 m.overlays.Destroy()
70 if m.dirs != nil {
71 m.dirs.Destroy()
72 }
73 }
74
75 // get returns the file handle mapped by the given key, or (nil, false) if the
76 // key is not present.
77 func (m *fileMap) get(key protocol.DocumentURI) (file.Handle, bool) {
78 return m.files.Get(key)
79 }
80
81 // all returns the sequence of (uri, fh) entries in the map.
82 func (m *fileMap) all() iter.Seq2[protocol.DocumentURI, file.Handle] {
83 return m.files.All()
84 }
85
86 // set stores the given file handle for key, updating overlays and directories
87 // accordingly.
88 func (m *fileMap) set(key protocol.DocumentURI, fh file.Handle) {
89 m.files.Set(key, fh, nil)
90
91 // update overlays
92 if o, ok := fh.(*overlay); ok {
93 m.overlays.Set(key, o, nil)
94 } else {
95 // Setting a non-overlay must delete the corresponding overlay, to preserve
96 // the accuracy of the overlay set.
97 m.overlays.Delete(key)
98 }
99
100 // update dirs, if they have been computed
101 if m.dirs != nil {
102 m.addDirs(key)
103 }
104 }
105
106 // addDirs adds all directories containing u to the dirs set.
107 func (m *fileMap) addDirs(u protocol.DocumentURI) {
108 dir := u.DirPath()
109 for dir != "" && !m.dirs.Contains(dir) {
110 m.dirs.Add(dir)
111 dir = filepath.Dir(dir)
112 }
113 }
114
115 // delete removes a file from the map, and updates overlays and dirs
116 // accordingly.
117 func (m *fileMap) delete(key protocol.DocumentURI) {
118 m.files.Delete(key)
119 m.overlays.Delete(key)
120
121 // Deleting a file may cause the set of dirs to shrink; therefore we must
122 // re-evaluate the dir set.
123 //
124 // Do this lazily, to avoid work if there are multiple deletions in a row.
125 if m.dirs != nil {
126 m.dirs.Destroy()
127 m.dirs = nil
128 }
129 }
130
131 // getOverlays returns a new unordered array of overlay files.
132 func (m *fileMap) getOverlays() []*overlay {
133 var overlays []*overlay
134 for _, o := range m.overlays.All() {
135 overlays = append(overlays, o)
136 }
137 return overlays
138 }
139
140 // getDirs reports returns the set of dirs observed by the fileMap.
141 //
142 // This operation mutates the fileMap.
143 // The result must not be mutated by the caller.
144 func (m *fileMap) getDirs() *persistent.Set[string] {
145 if m.dirs == nil {
146 m.dirs = new(persistent.Set[string])
147 for uri := range m.files.All() {
148 m.addDirs(uri)
149 }
150 }
151 return m.dirs
152 }