BlueGit

timfa bluegit

Files

bluegit / internal / server / gitcache.go

gitcache.go

gitcache.go 6.6 KiB Download Raw
1package server
2
3import (
4 "context"
5 "regexp"
6 "strconv"
7
8 "gitlab.com/Timfa/bluegit/internal/gitrepo"
9 "gitlab.com/Timfa/bluegit/internal/readcache"
10 "gitlab.com/Timfa/bluegit/internal/store"
11)
12
13// De leescache zit tussen de pagina's en git. Toegang is al gecontroleerd
14// voordat een handler hier komt (guard/repoView); de cache weet niets van
15// rechten en geeft alleen terug wat git voor dezelfde repo zou geven.
16// Sleutels op een commit-SHA zijn onveranderlijk. Alles op een ref-naam
17// staat in de refs-ruimte en verdwijnt bij elke ref-wijziging (AfterPush).
18
19// ReadCache is de actieve cache, of nil.
20func (s *Server) ReadCache() *readcache.Cache {
21 s.cacheMu.RLock()
22 defer s.cacheMu.RUnlock()
23 return s.rcache
24}
25
26// SetReadCache zet of wist de cache.
27func (s *Server) SetReadCache(c *readcache.Cache) {
28 s.cacheMu.Lock()
29 s.rcache = c
30 s.cacheMu.Unlock()
31}
32
33var fullSHA = regexp.MustCompile(`^[0-9a-f]{40}([0-9a-f]{24})?$`)
34
35func cachedRead[T any](s *Server, repoID int64, refs bool, key string, fn func() (T, error)) (T, error) {
36 c := s.ReadCache()
37 var v T
38 if c != nil && c.GetJSON(repoID, refs, key, &v) {
39 return v, nil
40 }
41 gen := c.Gen(repoID)
42 v, err := fn()
43 if err == nil && c != nil {
44 c.PutJSONAt(repoID, refs, key, v, gen)
45 }
46 return v, err
47}
48
49func (s *Server) gdir(repo store.Repo) string {
50 return gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
51}
52
53func (s *Server) cRefs(ctx context.Context, repo store.Repo) (map[string]string, error) {
54 return cachedRead(s, repo.ID, true, "refs", func() (map[string]string, error) {
55 return gitrepo.Refs(ctx, s.Git, s.gdir(repo))
56 })
57}
58
59// revKey kiest de cachesleutel voor rev: de commit-SHA als die bekend is,
60// anders de naam in de refs-ruimte.
61func (s *Server) revKey(ctx context.Context, repo store.Repo, rev string) (refs bool, key string) {
62 if fullSHA.MatchString(rev) {
63 return false, rev
64 }
65 if s.ReadCache() == nil {
66 return true, rev
67 }
68 if m, err := s.cRefs(ctx, repo); err == nil {
69 for _, full := range []string{"refs/heads/" + rev, "refs/tags/" + rev, rev} {
70 if sha, ok := m[full]; ok && fullSHA.MatchString(sha) {
71 return false, sha
72 }
73 }
74 }
75 return true, rev
76}
77
78func (s *Server) cBranches(ctx context.Context, repo store.Repo) ([]string, error) {
79 return cachedRead(s, repo.ID, true, "branches", func() ([]string, error) {
80 return gitrepo.Branches(ctx, s.Git, s.gdir(repo))
81 })
82}
83
84func (s *Server) cBranchTips(ctx context.Context, repo store.Repo) ([]gitrepo.BranchTip, error) {
85 return cachedRead(s, repo.ID, true, "tips", func() ([]gitrepo.BranchTip, error) {
86 return gitrepo.BranchTips(ctx, s.Git, s.gdir(repo))
87 })
88}
89
90func (s *Server) cTags(ctx context.Context, repo store.Repo) ([]string, error) {
91 return cachedRead(s, repo.ID, true, "tags", func() ([]string, error) {
92 return gitrepo.Tags(ctx, s.Git, s.gdir(repo))
93 })
94}
95
96func (s *Server) cListTags(ctx context.Context, repo store.Repo) ([]gitrepo.TagInfo, error) {
97 return cachedRead(s, repo.ID, true, "listtags", func() ([]gitrepo.TagInfo, error) {
98 return gitrepo.ListTags(ctx, s.Git, s.gdir(repo))
99 })
100}
101
102func (s *Server) cAheadBehind(ctx context.Context, repo store.Repo, base, branch string) (int, int, error) {
103 _, kb := s.revKey(ctx, repo, base)
104 _, kr := s.revKey(ctx, repo, branch)
105 refs := !fullSHA.MatchString(kb) || !fullSHA.MatchString(kr)
106 v, err := cachedRead(s, repo.ID, refs, "ab:"+kb+":"+kr, func() ([2]int, error) {
107 a, b, err := gitrepo.AheadBehind(ctx, s.Git, s.gdir(repo), base, branch)
108 return [2]int{a, b}, err
109 })
110 return v[0], v[1], err
111}
112
113type statsVal struct{ Commits, Branches, Tags int }
114
115func (s *Server) cStats(ctx context.Context, repo store.Repo, rev string) (int, int, int, error) {
116 // branches en tags hangen van refs af; dus altijd in de refs-ruimte
117 v, err := cachedRead(s, repo.ID, true, "stats:"+rev, func() (statsVal, error) {
118 c, b, t, err := gitrepo.Stats(ctx, s.Git, s.gdir(repo), rev)
119 return statsVal{c, b, t}, err
120 })
121 return v.Commits, v.Branches, v.Tags, err
122}
123
124func (s *Server) cLastCommit(ctx context.Context, repo store.Repo, rev, path string) (gitrepo.Commit, error) {
125 refs, k := s.revKey(ctx, repo, rev)
126 return cachedRead(s, repo.ID, refs, "last:"+k+":"+path, func() (gitrepo.Commit, error) {
127 return gitrepo.LastCommit(ctx, s.Git, s.gdir(repo), rev, path)
128 })
129}
130
131func (s *Server) cTree(ctx context.Context, repo store.Repo, rev, rel string) ([]gitrepo.Entry, error) {
132 refs, k := s.revKey(ctx, repo, rev)
133 return cachedRead(s, repo.ID, refs, "tree:"+k+":"+rel, func() ([]gitrepo.Entry, error) {
134 return gitrepo.Tree(ctx, s.Git, s.gdir(repo), rev, rel)
135 })
136}
137
138func (s *Server) cTreeAll(ctx context.Context, repo store.Repo, rev string) ([]gitrepo.Entry, error) {
139 refs, k := s.revKey(ctx, repo, rev)
140 return cachedRead(s, repo.ID, refs, "treeall:"+k, func() ([]gitrepo.Entry, error) {
141 return gitrepo.TreeAll(ctx, s.Git, s.gdir(repo), rev)
142 })
143}
144
145func (s *Server) cTouch(ctx context.Context, repo store.Repo, rev string) (map[string]gitrepo.Commit, error) {
146 refs, k := s.revKey(ctx, repo, rev)
147 return cachedRead(s, repo.ID, refs, "touch:"+k, func() (map[string]gitrepo.Commit, error) {
148 return gitrepo.Touch(ctx, s.Git, s.gdir(repo), rev)
149 })
150}
151
152type blobVal struct {
153 Text string
154 Binary bool
155}
156
157// cDoc is voor kleine documenten zoals de README; grote bestanden lezen
158// we gewoon uit git.
159func (s *Server) cDoc(ctx context.Context, repo store.Repo, rev, path string, max int) (string, bool, error) {
160 refs, k := s.revKey(ctx, repo, rev)
161 v, err := cachedRead(s, repo.ID, refs, "doc:"+strconv.Itoa(max)+":"+k+":"+path, func() (blobVal, error) {
162 t, b, err := gitrepo.Blob(ctx, s.Git, s.gdir(repo), rev, path, max)
163 return blobVal{t, b}, err
164 })
165 return v.Text, v.Binary, err
166}
167
168// refreshCache is de write-through na elke ref-wijziging: oude ref-data
169// weg, daarna de overzichtspagina van de standaardbranch opnieuw vullen.
170// refsChanged vergeet de ref-data meteen (dus nooit verouderd) en vult
171// op de achtergrond opnieuw.
172func (s *Server) refsChanged(repo store.Repo) {
173 c := s.ReadCache()
174 if c == nil {
175 return
176 }
177 c.DropRefs(repo.ID)
178 go s.refreshCache(repo)
179}
180
181func (s *Server) refreshCache(repo store.Repo) {
182 if s.ReadCache() == nil {
183 return
184 }
185 ctx := context.Background()
186 _, _ = s.cRefs(ctx, repo)
187 _, _ = s.cBranches(ctx, repo)
188 _, _ = s.cBranchTips(ctx, repo)
189 _, _ = s.cTags(ctx, repo)
190 _, _ = s.cListTags(ctx, repo)
191 rev := repo.DefaultBranch
192 if rev == "" {
193 return
194 }
195 _, _, _, _ = s.cStats(ctx, repo, rev)
196 _, _ = s.cLastCommit(ctx, repo, rev, "")
197 _, _ = s.cTouch(ctx, repo, rev)
198 if entries, err := s.cTree(ctx, repo, rev, ""); err == nil {
199 if readme, _ := findDocs(entries); readme != "" {
200 _, _, _ = s.cDoc(ctx, repo, rev, readme, 200_000)
201 }
202 }
203 _ = s.repoLanguage(ctx, repo, rev)
204}