| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package cas |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"sync" |
| 24 |
|
| 25 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 26 |
contentpkg "oras.land/oras-go/v2/content" |
| 27 |
"oras.land/oras-go/v2/errdef" |
| 28 |
"oras.land/oras-go/v2/internal/descriptor" |
| 29 |
) |
| 30 |
|
| 31 |
// Memory is a memory based CAS. |
| 32 |
type Memory struct { |
| 33 |
content sync.Map // map[descriptor.Descriptor][]byte |
| 34 |
} |
| 35 |
|
| 36 |
// NewMemory creates a new Memory CAS. |
| 37 |
func NewMemory() *Memory { |
| 38 |
return &Memory{} |
| 39 |
} |
| 40 |
|
| 41 |
// Fetch fetches the content identified by the descriptor. |
| 42 |
func (m *Memory) Fetch(_ context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { |
| 43 |
key := descriptor.FromOCI(target) |
| 44 |
content, exists := m.content.Load(key) |
| 45 |
if !exists { |
| 46 |
return nil, fmt.Errorf("%s: %s: %w", key.Digest, key.MediaType, errdef.ErrNotFound) |
| 47 |
} |
| 48 |
return io.NopCloser(bytes.NewReader(content.([]byte))), nil |
| 49 |
} |
| 50 |
|
| 51 |
// Push pushes the content, matching the expected descriptor. |
| 52 |
func (m *Memory) Push(_ context.Context, expected ocispec.Descriptor, content io.Reader) error { |
| 53 |
key := descriptor.FromOCI(expected) |
| 54 |
|
| 55 |
// check if the content exists in advance to avoid reading from the content. |
| 56 |
if _, exists := m.content.Load(key); exists { |
| 57 |
return fmt.Errorf("%s: %s: %w", key.Digest, key.MediaType, errdef.ErrAlreadyExists) |
| 58 |
} |
| 59 |
|
| 60 |
// read and try to store the content. |
| 61 |
value, err := contentpkg.ReadAll(content, expected) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
if _, exists := m.content.LoadOrStore(key, value); exists { |
| 66 |
return fmt.Errorf("%s: %s: %w", key.Digest, key.MediaType, errdef.ErrAlreadyExists) |
| 67 |
} |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
// Exists returns true if the described content exists. |
| 72 |
func (m *Memory) Exists(_ context.Context, target ocispec.Descriptor) (bool, error) { |
| 73 |
key := descriptor.FromOCI(target) |
| 74 |
_, exists := m.content.Load(key) |
| 75 |
return exists, nil |
| 76 |
} |
| 77 |
|
| 78 |
// Map dumps the memory into a built-in map structure. |
| 79 |
// Like other operations, calling Map() is go-routine safe. However, it does not |
| 80 |
// necessarily correspond to any consistent snapshot of the storage contents. |
| 81 |
func (m *Memory) Map() map[descriptor.Descriptor][]byte { |
| 82 |
res := make(map[descriptor.Descriptor][]byte) |
| 83 |
m.content.Range(func(key, value any) bool { |
| 84 |
res[key.(descriptor.Descriptor)] = value.([]byte) |
| 85 |
return true |
| 86 |
}) |
| 87 |
return res |
| 88 |
} |