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
67// Update replaces the stored repository. It returns domain.ErrNotFound if
68// there is none and domain.ErrConflict if another write changed it since it
69// was read.
70//
71//hexcheck:ignore suspicious-business-logic-in-adapter port method name only
72func (s *Store) Update(ctx context.Context, repo domain.Repo) error {
73 val, err := encodeRepo(repo)
74 if err != nil {
75 return err
76 }
77 key := repoKey(repo.Name)
78 item, ok := s.db.Get(ctx, key)
79 if !ok {
80 return domain.ErrNotFound
81 }
82 var b kv.Batch
83 b.RequireVersion(key, item.Version)
84 b.Put(key, val)
85 if _, err := s.db.Apply(ctx, &b); err != nil {
86 if errors.Is(err, kv.ErrConflict) {
87 return domain.ErrConflict
88 }
89 return fmt.Errorf("update repository %q: %w", repo.Name, err)
90 }
91 return nil
92}
93
94// guardRepoKey makes the batch depend on the repository: it requires the
95// repository key to be at the version just read and rewrites it, which gives it
96// a new version. Every write that adds a key under the repository (a merge
97// request, a comment, a read position, a CI run, job or check) goes through it,
98// so that
99//
100// - such a write fails with domain.ErrNotFound once the repository is deleted,
101// and leaves no key that a repository created again under the name would
102// inherit;
103// - Store.Delete, which requires the version of the repository key it listed
104// the keys at, notices a write that got in after the listing and lists again.
105//
106// It returns domain.ErrNotFound when the repository does not exist.
107func guardRepoKey(ctx context.Context, db *kv.DB, b *kv.Batch, repo domain.RepoName) error {
108 item, ok := db.Get(ctx, repoKey(repo))
109 if !ok {
110 return domain.ErrNotFound
111 }
112 b.RequireVersion(repoKey(repo), item.Version)
113 b.Put(repoKey(repo), item.Value)
114 return nil
115}
116
117// deleteChunk is how many merge request keys one batch deletes before the
118// last one, which also removes the repository.
119const deleteChunk = 1000
120
121// deleteTries bounds the listings of Delete: a listing is repeated when a merge
122// request write got in after it.
123const deleteTries = 32
124
125// Delete removes the repository and everything keyed to it: its merge
126// requests, their comments and read positions, and its number sequence, and
127// its CI runs, jobs, checks, run numbers and artifacts. The
128// Git data is moved to the trash by the caller; the merge requests are not
129// kept anywhere, like the repository metadata, so a repository created again
130// under the same name starts clean, with its numbers starting at 1. A missing
131// repository is not an error.
132//
133// The merge request keys go first, in chunks, and the repository key goes with
134// the last chunk: a delete that fails half way leaves the repository, which
135// the caller can delete again. The last batch requires the repository key to
136// be at the version the keys were listed at. Every write of a merge request key
137// rewrites the repository key (see guardRepoKey), so a write that
138// got in after the listing makes the batch fail and the keys are listed again,
139// and a write after the batch finds no repository: no key outlives the
140// repository to attach itself to a repository created again.
141//
142//hexcheck:ignore suspicious-business-logic-in-adapter chunked delete under a version precondition: a storage concern, no policy
143func (s *Store) Delete(ctx context.Context, name domain.RepoName) error {
144 for range deleteTries {
145 snap := s.db.Snapshot(ctx)
146 repoItem, repoOK := snap.Get(ctx, repoKey(name))
147 keys := append(mergeRequestKeys(ctx, snap, name), ciKeys(ctx, snap, name)...)
148 keys = append(keys, settingsKeys(ctx, snap, name)...)
149 for len(keys) > deleteChunk {
150 var b kv.Batch
151 for _, k := range keys[:deleteChunk] {
152 b.Delete(k)
153 }
154 if _, err := s.db.Apply(ctx, &b); err != nil {
155 return fmt.Errorf("delete merge requests of %q: %w", name, err)
156 }
157 keys = keys[deleteChunk:]
158 }
159 var b kv.Batch
160 if repoOK {
161 b.RequireVersion(repoKey(name), repoItem.Version)
162 } else {
163 b.RequireAbsent(repoKey(name))
164 }
165 for _, k := range keys {
166 b.Delete(k)
167 }
168 b.Delete(repoKey(name))
169 _, err := s.db.Apply(ctx, &b)
170 if errors.Is(err, kv.ErrConflict) {
171 continue
172 }
173 if err != nil {
174 return fmt.Errorf("delete repository %q: %w", name, err)
175 }
176 return nil
177 }
178 return fmt.Errorf("delete repository %q: %w", name, domain.ErrConflict)
179}
180
181func encodeRepo(r domain.Repo) ([]byte, error) {
182 if r.Name.IsZero() {
183 return nil, fmt.Errorf("%w: empty name", domain.ErrInvalidName)
184 }
185 if !utf8.ValidString(r.Description) {
186 return nil, errors.New("repository description is not valid UTF-8")
187 }
188 vis, err := encodeVisibility(r.Visibility)
189 if err != nil {
190 return nil, err
191 }
192 settings := r.Settings
193 if settings.IsZero() {
194 // Settings are always stored explicitly, so that a later change of
195 // the defaults never alters a stored repository.
196 settings = domain.DefaultRepoSettings()
197 }
198 pbSettings, err := encodeSettings(settings)
199 if err != nil {
200 return nil, err
201 }
202 pb := &storagepb.Repo{
203 Name: r.Name.String(),
204 Description: r.Description,
205 Topics: r.Topics,
206 Visibility: vis,
207 CreatedAt: encodeTime(r.CreatedAt),
208 PushedAt: encodeTime(r.PushedAt),
209 Settings: pbSettings,
210 }
211 return mustMarshal(pb), nil
212}
213
214func decodeRepo(val []byte) (domain.Repo, error) {
215 var pb storagepb.Repo
216 if err := proto.Unmarshal(val, &pb); err != nil {
217 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
218 }
219 name, err := domain.ParseRepoName(pb.GetName())
220 if err != nil {
221 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
222 }
223 return domain.Repo{
224 Name: name,
225 Description: pb.GetDescription(),
226 Topics: slices.Clone(pb.GetTopics()),
227 Visibility: decodeVisibility(pb.GetVisibility()),
228 CreatedAt: decodeTime(pb.GetCreatedAt()),
229 PushedAt: decodeTime(pb.GetPushedAt()),
230 Settings: decodeSettings(pb.GetSettings()),
231 }, nil
232}
233
234func encodeVisibility(v domain.Visibility) (storagepb.Visibility, error) {
235 switch v {
236 case domain.VisibilityPublic:
237 return storagepb.Visibility_VISIBILITY_PUBLIC, nil
238 case domain.VisibilityPrivate:
239 return storagepb.Visibility_VISIBILITY_PRIVATE, nil
240 }
241 return storagepb.Visibility_VISIBILITY_UNSPECIFIED, fmt.Errorf("%w: %q", domain.ErrInvalidVisibility, v)
242}
243
244// decodeVisibility maps anything but public to private, including the
245// unspecified value and values from a newer schema: the safe side.
246func decodeVisibility(v storagepb.Visibility) domain.Visibility {
247 if v == storagepb.Visibility_VISIBILITY_PUBLIC {
248 return domain.VisibilityPublic
249 }
250 return domain.VisibilityPrivate
251}
252
253func encodeTime(t time.Time) *timestamppb.Timestamp {
254 if t.IsZero() {
255 return nil
256 }
257 return timestamppb.New(t)
258}
259
260func decodeTime(ts *timestamppb.Timestamp) time.Time {
261 if ts == nil {
262 return time.Time{}
263 }
264 return ts.AsTime()
265}