forked from oras-project/oras-go
/
store.go
213 lines (189 loc) · 5.46 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright The ORAS Authors.
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 oras
import (
"context"
"io"
"io/ioutil"
"time"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/remotes"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/sync/errgroup"
orascontent "oras.land/oras-go/pkg/content"
)
type hybridStore struct {
cache *orascontent.Memory
cachedMediaTypes []string
cacheOnly bool
provider content.Provider
ingester content.Ingester
}
func newHybridStoreFromPusher(pusher remotes.Pusher, cachedMediaTypes []string, cacheOnly bool) *hybridStore {
// construct an ingester from a pusher
ingester := pusherIngester{
pusher: pusher,
}
return &hybridStore{
cache: orascontent.NewMemory(),
cachedMediaTypes: cachedMediaTypes,
ingester: ingester,
cacheOnly: cacheOnly,
}
}
func (s *hybridStore) Set(desc ocispec.Descriptor, content []byte) {
s.cache.Set(desc, content)
}
func (s *hybridStore) Fetch(ctx context.Context, desc ocispec.Descriptor) (io.ReadCloser, error) {
reader, err := s.cache.Fetch(ctx, desc)
if err == nil {
return reader, err
}
if s.provider != nil {
rat, err := s.provider.ReaderAt(ctx, desc)
return ioutil.NopCloser(orascontent.NewReaderAtWrapper(rat)), err
}
return nil, err
}
func (s *hybridStore) Push(ctx context.Context, desc ocispec.Descriptor) (content.Writer, error) {
return s.Writer(ctx, content.WithDescriptor(desc))
}
// Writer begins or resumes the active writer identified by desc
func (s *hybridStore) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
var wOpts content.WriterOpts
for _, opt := range opts {
if err := opt(&wOpts); err != nil {
return nil, err
}
}
if isAllowedMediaType(wOpts.Desc.MediaType, s.cachedMediaTypes...) || s.ingester == nil {
pusher, err := s.cache.Pusher(ctx, "")
if err != nil {
return nil, err
}
cacheWriter, err := pusher.Push(ctx, wOpts.Desc)
if err != nil {
return nil, err
}
// if we cache it only, do not pass it through
if s.cacheOnly {
return cacheWriter, nil
}
ingesterWriter, err := s.ingester.Writer(ctx, opts...)
switch {
case err == nil:
return newTeeWriter(wOpts.Desc, cacheWriter, ingesterWriter), nil
case errdefs.IsAlreadyExists(err):
return cacheWriter, nil
}
return nil, err
}
return s.ingester.Writer(ctx, opts...)
}
// teeWriter tees the content to one or more content.Writer
type teeWriter struct {
writers []content.Writer
digester digest.Digester
status content.Status
}
func newTeeWriter(desc ocispec.Descriptor, writers ...content.Writer) *teeWriter {
now := time.Now()
return &teeWriter{
writers: writers,
digester: digest.Canonical.Digester(),
status: content.Status{
Total: desc.Size,
StartedAt: now,
UpdatedAt: now,
},
}
}
func (t *teeWriter) Close() error {
g := new(errgroup.Group)
for _, w := range t.writers {
w := w // closure issues, see https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return w.Close()
})
}
return g.Wait()
}
func (t *teeWriter) Write(p []byte) (n int, err error) {
g := new(errgroup.Group)
for _, w := range t.writers {
w := w // closure issues, see https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
n, err := w.Write(p[:])
if err != nil {
return err
}
if n != len(p) {
return io.ErrShortWrite
}
return nil
})
}
err = g.Wait()
n = len(p)
if err != nil {
return n, err
}
_, _ = t.digester.Hash().Write(p[:n])
t.status.Offset += int64(len(p))
t.status.UpdatedAt = time.Now()
return n, nil
}
// Digest may return empty digest or panics until committed.
func (t *teeWriter) Digest() digest.Digest {
return t.digester.Digest()
}
func (t *teeWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error {
g := new(errgroup.Group)
for _, w := range t.writers {
w := w // closure issues, see https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return w.Commit(ctx, size, expected, opts...)
})
}
return g.Wait()
}
// Status returns the current state of write
func (t *teeWriter) Status() (content.Status, error) {
return t.status, nil
}
// Truncate updates the size of the target blob
func (t *teeWriter) Truncate(size int64) error {
g := new(errgroup.Group)
for _, w := range t.writers {
w := w // closure issues, see https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return w.Truncate(size)
})
}
return g.Wait()
}
// pusherIngester simple wrapper to get an ingester from a pusher
type pusherIngester struct {
pusher remotes.Pusher
}
func (p pusherIngester) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
var wOpts content.WriterOpts
for _, opt := range opts {
if err := opt(&wOpts); err != nil {
return nil, err
}
}
return p.pusher.Push(ctx, wOpts.Desc)
}