Skip to content

Commit

Permalink
add hybrid store
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Jun 30, 2023
1 parent 184fa65 commit 506d471
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
20 changes: 10 additions & 10 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/fileref"
"oras.land/oras/cmd/oras/internal/option"
"oras.land/oras/internal/cache"
ostore "oras.land/oras/internal/store"
)

type pushOptions struct {
Expand Down Expand Up @@ -141,8 +141,8 @@ func runPush(ctx context.Context, opts pushOptions) error {
return err
}
defer store.Close()
memStore := memory.New()
proxy := cache.New(store, memStore)
manifestStore := memory.New()
hybrid := ostore.NewReadOnlyHybrid(store, manifestStore, manifestStore)
if opts.manifestConfigRef != "" {
path, cfgMediaType, err := fileref.Parse(opts.manifestConfigRef, oras.MediaTypeUnknownConfig)
if err != nil {
Expand All @@ -164,11 +164,11 @@ func runPush(ctx context.Context, opts pushOptions) error {
return err
}
pack := func() (ocispec.Descriptor, error) {
root, err := oras.Pack(ctx, memStore, opts.artifactType, descs, packOpts)
root, err := oras.Pack(ctx, manifestStore, opts.artifactType, descs, packOpts)
if err != nil {
return ocispec.Descriptor{}, err
}
if err = memStore.Tag(ctx, root, root.Digest.String()); err != nil {
if err = manifestStore.Tag(ctx, root, root.Digest.String()); err != nil {
return ocispec.Descriptor{}, err
}
return root, nil
Expand All @@ -181,12 +181,12 @@ func runPush(ctx context.Context, opts pushOptions) error {
}
copyOptions := oras.DefaultCopyOptions
copyOptions.Concurrency = opts.concurrency
updateDisplayOption(&copyOptions.CopyGraphOptions, proxy, opts.Verbose)
updateDisplayOption(&copyOptions.CopyGraphOptions, hybrid, opts.Verbose)
copy := func(root ocispec.Descriptor) error {
if tag := opts.Reference; tag == "" {
err = oras.CopyGraph(ctx, proxy, dst, root, copyOptions.CopyGraphOptions)
err = oras.CopyGraph(ctx, hybrid, dst, root, copyOptions.CopyGraphOptions)
} else {
_, err = oras.Copy(ctx, proxy, root.Digest.String(), dst, tag, copyOptions)
_, err = oras.Copy(ctx, hybrid, root.Digest.String(), dst, tag, copyOptions)
}
return err
}
Expand All @@ -199,7 +199,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
fmt.Println("Pushed", opts.AnnotatedReference())

if len(opts.extraRefs) != 0 {
contentBytes, err := content.FetchAll(ctx, proxy, root)
contentBytes, err := content.FetchAll(ctx, hybrid, root)
if err != nil {
return err
}
Expand All @@ -213,7 +213,7 @@ func runPush(ctx context.Context, opts pushOptions) error {
fmt.Println("Digest:", root.Digest)

// Export manifest
return opts.ExportManifest(ctx, proxy, root)
return opts.ExportManifest(ctx, manifestStore, root)
}

func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, verbose bool) {
Expand Down
61 changes: 61 additions & 0 deletions internal/store/hybrid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
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 cache

import (
"context"
"io"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)

type hybrid struct {
oras.ReadOnlyTarget
cache []oras.Target
}

// NewReadOnlyHybrid generates a new hybrid storage.
func NewReadOnlyHybrid(provider oras.ReadOnlyTarget, cache ...oras.Target) oras.ReadOnlyTarget {
return &hybrid{
ReadOnlyTarget: provider,
cache: cache,
}
}

// Fetch fetches the content from cache first, then from the provider.
func (h *hybrid) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) {
for _, c := range h.cache {
rc, err := c.Fetch(ctx, target)
if err == nil {
// Fetch from cache
return rc, nil
}
}
return h.ReadOnlyTarget.Fetch(ctx, target)
}

// Resolve resolves the content from cache first, then from the provider.
func (h *hybrid) Resolve(ctx context.Context, ref string) (ocispec.Descriptor, error) {
for _, c := range h.cache {
desc, err := c.Resolve(ctx, ref)
if err == nil {
// Resolve from cache
return desc, nil
}
}
return h.ReadOnlyTarget.Resolve(ctx, ref)
}

0 comments on commit 506d471

Please sign in to comment.