/
store.go
139 lines (129 loc) · 4.16 KB
/
store.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2020 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufmodulestorage
import (
"context"
"fmt"
"io/ioutil"
"github.com/bufbuild/buf/internal/buf/bufcore/bufmodule"
modulev1 "github.com/bufbuild/buf/internal/gen/proto/go/buf/module/v1"
"github.com/bufbuild/buf/internal/pkg/normalpath"
"github.com/bufbuild/buf/internal/pkg/protoencoding"
"github.com/bufbuild/buf/internal/pkg/storage"
"github.com/klauspost/compress/zstd"
"go.uber.org/multierr"
)
const (
moduleFileName = "module.bin.zst"
formatVersion = "v1"
)
type store struct {
readWriteBucket storage.ReadWriteBucket
}
func newStore(readWriteBucket storage.ReadWriteBucket) *store {
// Add version prefix
readWriteBucket = storage.MapReadWriteBucket(readWriteBucket, storage.MapOnPrefix(formatVersion))
return &store{
readWriteBucket: readWriteBucket,
}
}
func (s *store) Get(ctx context.Context, key Key) (_ bufmodule.Module, retErr error) {
if err := normalpath.ValidatePathComponents(key...); err != nil {
return nil, fmt.Errorf("invalid key: %w", err)
}
modulePath := normalpath.Join(append(key, moduleFileName)...)
readObjectCloser, err := s.readWriteBucket.Get(ctx, modulePath)
if err != nil {
// This correctly returns an error that fufills storage.ErrNotExist per the documentation
// if the module is not in the reader.
return nil, err
}
defer func() {
retErr = multierr.Append(retErr, readObjectCloser.Close())
}()
zstdDecoder, err := zstd.NewReader(readObjectCloser)
if err != nil {
return nil, err
}
defer zstdDecoder.Close()
data, err := ioutil.ReadAll(zstdDecoder)
if err != nil {
return nil, err
}
protoModule := &modulev1.Module{}
if err := protoencoding.NewWireUnmarshaler(nil).Unmarshal(data, protoModule); err != nil {
return nil, err
}
return bufmodule.NewModuleForProto(ctx, protoModule)
}
func (s *store) Put(ctx context.Context, key Key, module bufmodule.Module) (retErr error) {
if err := normalpath.ValidatePathComponents(key...); err != nil {
return fmt.Errorf("invalid key: %w", err)
}
modulePath := normalpath.Join(append(key, moduleFileName)...)
// Check if there is already a module at the path
if _, err := s.readWriteBucket.Stat(ctx, modulePath); !storage.IsNotExist(err) {
return fmt.Errorf("module already exists at path %q", modulePath)
}
protoModule, err := bufmodule.ModuleToProtoModule(ctx, module)
if err != nil {
return err
}
data, err := protoencoding.NewWireMarshaler().Marshal(protoModule)
if err != nil {
return err
}
writeObjectCloser, err := s.readWriteBucket.Put(ctx, modulePath)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, writeObjectCloser.Close())
}()
zstdEncoder, err := zstd.NewWriter(writeObjectCloser)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, zstdEncoder.Close())
}()
if _, err := zstdEncoder.Write(data); err != nil {
return err
}
return nil
}
func (s *store) Delete(ctx context.Context, key Key) error {
if err := normalpath.ValidatePathComponents(key...); err != nil {
return fmt.Errorf("invalid key: %w", err)
}
modulePath := normalpath.Join(append(key, moduleFileName)...)
return s.readWriteBucket.Delete(ctx, modulePath)
}
func (s *store) AllKeys(ctx context.Context) ([]Key, error) {
paths, err := storage.AllPaths(ctx, s.readWriteBucket, "")
if err != nil {
return nil, err
}
var keys []Key
for _, path := range paths {
if normalpath.Base(path) != moduleFileName {
return nil, fmt.Errorf("unexpected path in bucket: %q", path)
}
// Trim module file name
dir := normalpath.Dir(path)
components := normalpath.Components(dir)
keys = append(keys, Key(components))
}
return keys, nil
}