-
Notifications
You must be signed in to change notification settings - Fork 3
/
version.go
57 lines (50 loc) · 1.21 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package glassdb
import (
"context"
"errors"
"fmt"
"path"
"github.com/mbrt/glassdb/backend"
)
const (
dbVersion = "v0"
dbMetaPath = "glassdb"
dbVersionTag = "version"
)
func checkOrCreateDBMeta(ctx context.Context, b backend.Backend, name string) error {
err := checkDBVersion(ctx, b, name)
if err == nil {
return nil
}
if !errors.Is(err, backend.ErrNotFound) {
return err
}
err = setDBMetadata(ctx, b, name, dbVersion)
if err == nil {
return nil
}
if !errors.Is(err, backend.ErrPrecondition) {
return fmt.Errorf("creating db metadata: %w", err)
}
// We raced against another instance creating the metadata.
// Check it again.
return checkDBVersion(ctx, b, name)
}
func checkDBVersion(ctx context.Context, b backend.Backend, name string) error {
p := path.Join(name, dbMetaPath)
meta, err := b.GetMetadata(ctx, p)
if err != nil {
return err
}
if v := meta.Tags[dbVersionTag]; v != dbVersion {
return fmt.Errorf("got db version %q, expected %q", v, dbVersion)
}
return nil
}
func setDBMetadata(ctx context.Context, b backend.Backend, name, version string) error {
p := path.Join(name, dbMetaPath)
_, err := b.WriteIfNotExists(ctx, p, nil, backend.Tags{
dbVersionTag: version,
})
return err
}