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), issueKeys(ctx, snap, name)...)
148 keys = append(keys, pendingKeys(ctx, snap, name)...)
149 keys = append(keys, ciKeys(ctx, snap, name)...)
150 keys = append(keys, settingsKeys(ctx, snap, name)...)
151 for len(keys) > deleteChunk {
152 var b kv.Batch
153 for _, k := range keys[:deleteChunk] {
154 b.Delete(k)
155 }
156 if _, err := s.db.Apply(ctx, &b); err != nil {
157 return fmt.Errorf("delete merge requests of %q: %w", name, err)
158 }
159 keys = keys[deleteChunk:]
160 }
161 var b kv.Batch
162 if repoOK {
163 b.RequireVersion(repoKey(name), repoItem.Version)
164 } else {
165 b.RequireAbsent(repoKey(name))
166 }
167 for _, k := range keys {
168 b.Delete(k)
169 }
170 b.Delete(repoKey(name))
171 _, err := s.db.Apply(ctx, &b)
172 if errors.Is(err, kv.ErrConflict) {
173 continue
174 }
175 if err != nil {
176 return fmt.Errorf("delete repository %q: %w", name, err)
177 }
178 return nil
179 }
180 return fmt.Errorf("delete repository %q: %w", name, domain.ErrConflict)
181}
182
183func encodeRepo(r domain.Repo) ([]byte, error) {
184 if r.Name.IsZero() {
185 return nil, fmt.Errorf("%w: empty name", domain.ErrInvalidName)
186 }
187 if !utf8.ValidString(r.Description) {
188 return nil, errors.New("repository description is not valid UTF-8")
189 }
190 vis, err := encodeVisibility(r.Visibility)
191 if err != nil {
192 return nil, err
193 }
194 settings := r.Settings
195 if settings.IsZero() {
196 // Settings are always stored explicitly, so that a later change of
197 // the defaults never alters a stored repository.
198 settings = domain.DefaultRepoSettings()
199 }
200 pbSettings, err := encodeSettings(settings)
201 if err != nil {
202 return nil, err
203 }
204 pb := &storagepb.Repo{
205 Name: r.Name.String(),
206 Description: r.Description,
207 Topics: r.Topics,
208 Visibility: vis,
209 CreatedAt: encodeTime(r.CreatedAt),
210 PushedAt: encodeTime(r.PushedAt),
211 Settings: pbSettings,
212 }
213 return mustMarshal(pb), nil
214}
215
216func decodeRepo(val []byte) (domain.Repo, error) {
217 var pb storagepb.Repo
218 if err := proto.Unmarshal(val, &pb); err != nil {
219 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
220 }
221 name, err := domain.ParseRepoName(pb.GetName())
222 if err != nil {
223 return domain.Repo{}, fmt.Errorf("decode repository: %w", err)
224 }
225 return domain.Repo{
226 Name: name,
227 Description: pb.GetDescription(),
228 Topics: slices.Clone(pb.GetTopics()),
229 Visibility: decodeVisibility(pb.GetVisibility()),
230 CreatedAt: decodeTime(pb.GetCreatedAt()),
231 PushedAt: decodeTime(pb.GetPushedAt()),
232 Settings: decodeSettings(pb.GetSettings()),
233 }, nil
234}
235
236func encodeVisibility(v domain.Visibility) (storagepb.Visibility, error) {
237 switch v {
238 case domain.VisibilityPublic:
239 return storagepb.Visibility_VISIBILITY_PUBLIC, nil
240 case domain.VisibilityPrivate:
241 return storagepb.Visibility_VISIBILITY_PRIVATE, nil
242 }
243 return storagepb.Visibility_VISIBILITY_UNSPECIFIED, fmt.Errorf("%w: %q", domain.ErrInvalidVisibility, v)
244}
245
246// decodeVisibility maps anything but public to private, including the
247// unspecified value and values from a newer schema: the safe side.
248func decodeVisibility(v storagepb.Visibility) domain.Visibility {
249 if v == storagepb.Visibility_VISIBILITY_PUBLIC {
250 return domain.VisibilityPublic
251 }
252 return domain.VisibilityPrivate
253}
254
255func encodeTime(t time.Time) *timestamppb.Timestamp {
256 if t.IsZero() {
257 return nil
258 }
259 return timestamppb.New(t)
260}
261
262func decodeTime(ts *timestamppb.Timestamp) time.Time {
263 if ts == nil {
264 return time.Time{}
265 }
266 return ts.AsTime()
267}