1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package kvstore
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "slices"
10 "time"
11 "unicode/utf8"
12
13 kv "bnema.dev/ermite-kv"
14 "google.golang.org/protobuf/proto"
15 "google.golang.org/protobuf/types/known/timestamppb"
16
17 storagepb "bnema.dev/ermite/gen/ermite/storage"
18 "bnema.dev/ermite/internal/domain"
19)
20
21const repoPrefix = "repo/"
22
23func repoKey(name domain.RepoName) []byte { return []byte(repoPrefix + name.String()) }
24
25// Create stores repo with an "absent" precondition: a repository with that
26// name, or a concurrent Create that commits first, yields domain.ErrAlreadyExists.
27func (s *Store) Create(ctx context.Context, repo domain.Repo) error {
28 val, err := encodeRepo(repo)
29 if err != nil {
30 return err
31 }
32 key := repoKey(repo.Name)
33 var b kv.Batch
34 b.RequireAbsent(key)
35 b.Put(key, val)
36 if _, err := s.db.Apply(ctx, &b); err != nil {
37 if errors.Is(err, kv.ErrConflict) {
38 return domain.ErrAlreadyExists
39 }
40 return fmt.Errorf("create repository %q: %w", repo.Name, err)
41 }
42 return nil
43}
44
45// Get returns the repository or domain.ErrNotFound.
46func (s *Store) Get(ctx context.Context, name domain.RepoName) (domain.Repo, error) {
47 item, ok := s.db.Get(ctx, repoKey(name))
48 if !ok {
49 return domain.Repo{}, domain.ErrNotFound
50 }
51 return decodeRepo(item.Value)
52}
53
54// List returns every repository ordered by name.
55func (s *Store) List(ctx context.Context) ([]domain.Repo, error) {
56 var repos []domain.Repo
57 for key, item := range s.db.Snapshot(ctx).Prefix(ctx, []byte(repoPrefix)) {
58 repo, err := decodeRepo(item.Value)
59 if err != nil {
60 return nil, fmt.Errorf("decode %s: %w", key, err)
61 }
62 repos = append(repos, repo)
63 }
64 return repos, nil
65}
66
67const repoMutationTries = 8
68
69// Mutate applies a retry-safe mutation under the version it read.
70func (s *Store) Mutate(ctx context.Context, name domain.RepoName, fn func(*domain.Repo) (bool, error)) (domain.Repo, error) {
71 key := repoKey(name)
72 for range repoMutationTries {
73 if err := ctx.Err(); err != nil {
74 return domain.Repo{}, err
75 }
76 item, ok := s.db.Get(ctx, key)
77 if !ok {
78 return domain.Repo{}, domain.ErrNotFound
79 }
80 repo, err := decodeRepo(item.Value)
81 if err != nil {
82 return domain.Repo{}, err
83 }
84 changed, err := fn(&repo)
85 if err != nil {
86 return domain.Repo{}, err
87 }
88 if repo.Name != name {
89 return domain.Repo{}, fmt.Errorf("mutate repository name: %w", domain.ErrInvalidName)
90 }
91 if !changed {
92 return decodeRepo(item.Value)
93 }
94 val, err := encodeRepo(repo)
95 if err != nil {
96 return domain.Repo{}, err
97 }
98 var b kv.Batch
99 b.RequireVersion(key, item.Version)
100 b.Put(key, val)
101 if _, err := s.db.Apply(ctx, &b); err != nil {
102 if errors.Is(err, kv.ErrConflict) {
103 continue
104 }
105 return domain.Repo{}, fmt.Errorf("mutate repository %q: %w", name, err)
106 }
107 return decodeRepo(val)
108 }
109 return domain.Repo{}, domain.ErrConflict
110}
111
112// guardRepoKey makes the batch depend on the repository: it requires the
113// repository key to be at the version just read and rewrites it, which gives it
114// a new version. Every write that adds a key under the repository (a merge
115// request, a comment, a read position, a CI run, job or check) goes through it,
116// so that
117//
118// - such a write fails with domain.ErrNotFound once the repository is deleted,
119// and leaves no key that a repository created again under the name would
120// inherit;
121// - Store.Delete, which requires the version of the repository key it listed
122// the keys at, notices a write that got in after the listing and lists again.
123//
124// It returns domain.ErrNotFound when the repository does not exist.
125func guardRepoKey(ctx context.Context, db *kv.DB, b *kv.Batch, repo domain.RepoName) error {
126 item, ok := db.Get(ctx, repoKey(repo))
127 if !ok {
128 return domain.ErrNotFound
129 }
130 b.RequireVersion(repoKey(repo), item.Version)
131 b.Put(repoKey(repo), item.Value)
132 return nil
133}
134
135// deleteChunk is how many merge request keys one batch deletes before the
136// last one, which also removes the repository.
137const deleteChunk = 1000
138
139// deleteTries bounds the listings of Delete: a listing is repeated when a merge
140// request write got in after it.
141const deleteTries = 32
142
143// Delete removes the repository and everything keyed to it: its merge
144// requests, their comments and read positions, and its number sequence, and
145// its CI runs, jobs, checks, run numbers and artifacts. The
146// Git data is moved to the trash by the caller; the merge requests are not
147// kept anywhere, like the repository metadata, so a repository created again
148// under the same name starts clean, with its numbers starting at 1. A missing
149// repository is not an error.
150//
151// The merge request keys go first, in chunks, and the repository key goes with
152// the last chunk: a delete that fails half way leaves the repository, which
153// the caller can delete again. The last batch requires the repository key to
154// be at the version the keys were listed at. Every write of a merge request key
155// rewrites the repository key (see guardRepoKey), so a write that
156// got in after the listing makes the batch fail and the keys are listed again,
157// and a write after the batch finds no repository: no key outlives the
158// repository to attach itself to a repository created again.
159func (s *Store) Delete(ctx context.Context, name domain.RepoName) error {
160 for range deleteTries {
161 snap := s.db.Snapshot(ctx)
162 repoItem, repoOK := snap.Get(ctx, repoKey(name))
163 keys := append(mergeRequestKeys(ctx, snap, name), issueKeys(ctx, snap, name)...)
164 keys = append(keys, pendingKeys(ctx, snap, name)...)
165 keys = append(keys, ciKeys(ctx, snap, name)...)
166 keys = append(keys, settingsKeys(ctx, snap, name)...)
167 keys = append(keys, mirrorKeys(ctx, snap, name)...)
168 for len(keys) > deleteChunk {
169 var b kv.Batch
170 for _, k := range keys[:deleteChunk] {
171 b.Delete(k)
172 }
173 if _, err := s.db.Apply(ctx, &b); err != nil {
174 return fmt.Errorf("delete merge requests of %q: %w", name, err)
175 }
176 keys = keys[deleteChunk:]
177 }
178 var b kv.Batch
179 if repoOK {
180 b.RequireVersion(repoKey(name), repoItem.Version)
181 } else {
182 b.RequireAbsent(repoKey(name))
183 }
184 for _, k := range keys {
185 b.Delete(k)
186 }
187 b.Delete(repoKey(name))
188 _, err := s.db.Apply(ctx, &b)
189 if errors.Is(err, kv.ErrConflict) {
190 continue
191 }
192 if err != nil {
193 return fmt.Errorf("delete repository %q: %w", name, err)
194 }
195 return nil
196 }
197 return fmt.Errorf("delete repository %q: %w", name, domain.ErrConflict)
198}
199
200func encodeRepo(r domain.Repo) ([]byte, error) {
201 if r.Name.IsZero() {
202 return nil, fmt.Errorf("%w: empty name", domain.ErrInvalidName)
203 }
204 if !utf8.ValidString(r.Description) {
205 return nil, errors.New("repository description is not valid UTF-8")
206 }
207 vis, err := encodeVisibility(r.Visibility)
208 if err != nil {
209 return nil, err
210 }
211 // Store defaults explicitly so future default changes do not alter stored repositories.
212 settings := r.EffectiveSettings()
213 pbSettings, err := encodeSettings(settings)
214 if err != nil {
215 return nil, err
216 }
217 pb := &storagepb.Repo{
218 Name: r.Name.String(),
219 Description: r.Description,
220 Topics: r.Topics,
221 Visibility: vis,
222 CreatedAt: encodeTime(r.CreatedAt),
223 PushedAt: encodeTime(r.PushedAt),
224 ActiveAt: encodeTime(r.ActiveAt),
225 Settings: pbSettings,
226 }
227 return mustMarshal(pb), nil
228}
229
230func decodeRepo(val []byte) (domain.Repo, error) {
231 var pb storagepb.Repo
232 if err := proto.Unmarshal(val, &pb); err != nil {
233 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
234 }
235 name, err := domain.ParseRepoName(pb.GetName())
236 if err != nil {
237 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
238 }
239 return domain.Repo{
240 Name: name,
241 Description: pb.GetDescription(),
242 Topics: slices.Clone(pb.GetTopics()),
243 Visibility: decodeVisibility(pb.GetVisibility()),
244 CreatedAt: decodeTime(pb.GetCreatedAt()),
245 PushedAt: decodeTime(pb.GetPushedAt()),
246 ActiveAt: decodeTime(pb.GetActiveAt()),
247 Settings: decodeSettings(pb.GetSettings()),
248 }, nil
249}
250
251func encodeVisibility(v domain.Visibility) (storagepb.Visibility, error) {
252 switch v {
253 case domain.VisibilityPublic:
254 return storagepb.Visibility_VISIBILITY_PUBLIC, nil
255 case domain.VisibilityPrivate:
256 return storagepb.Visibility_VISIBILITY_PRIVATE, nil
257 }
258 return storagepb.Visibility_VISIBILITY_UNSPECIFIED, fmt.Errorf("%w: %q", domain.ErrInvalidVisibility, v)
259}
260
261// decodeVisibility maps anything but public to private, including the
262// unspecified value and values from a newer schema: the safe side.
263func decodeVisibility(v storagepb.Visibility) domain.Visibility {
264 if v == storagepb.Visibility_VISIBILITY_PUBLIC {
265 return domain.VisibilityPublic
266 }
267 return domain.VisibilityPrivate
268}
269
270func encodeTime(t time.Time) *timestamppb.Timestamp {
271 if t.IsZero() {
272 return nil
273 }
274 return timestamppb.New(t)
275}
276
277func decodeTime(ts *timestamppb.Timestamp) time.Time {
278 if ts == nil {
279 return time.Time{}
280 }
281 return ts.AsTime()
282}