| 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 resolver |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"maps" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"github.com/opencontainers/go-digest" |
| 25 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 26 |
"oras.land/oras-go/v2/errdef" |
| 27 |
"oras.land/oras-go/v2/internal/container/set" |
| 28 |
) |
| 29 |
|
| 30 |
// Memory is a memory based resolver. |
| 31 |
type Memory struct { |
| 32 |
lock sync.RWMutex |
| 33 |
index map[string]ocispec.Descriptor |
| 34 |
tags map[digest.Digest]set.Set[string] |
| 35 |
} |
| 36 |
|
| 37 |
// NewMemory creates a new Memory resolver. |
| 38 |
func NewMemory() *Memory { |
| 39 |
return &Memory{ |
| 40 |
index: make(map[string]ocispec.Descriptor), |
| 41 |
tags: make(map[digest.Digest]set.Set[string]), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// Resolve resolves a reference to a descriptor. |
| 46 |
func (m *Memory) Resolve(_ context.Context, reference string) (ocispec.Descriptor, error) { |
| 47 |
m.lock.RLock() |
| 48 |
defer m.lock.RUnlock() |
| 49 |
|
| 50 |
desc, ok := m.index[reference] |
| 51 |
if !ok { |
| 52 |
return ocispec.Descriptor{}, fmt.Errorf("%s: %w", reference, errdef.ErrNotFound) |
| 53 |
} |
| 54 |
return desc, nil |
| 55 |
} |
| 56 |
|
| 57 |
// Tag tags a descriptor with a reference string. |
| 58 |
func (m *Memory) Tag(_ context.Context, desc ocispec.Descriptor, reference string) error { |
| 59 |
m.lock.Lock() |
| 60 |
defer m.lock.Unlock() |
| 61 |
|
| 62 |
m.index[reference] = desc |
| 63 |
tagSet, ok := m.tags[desc.Digest] |
| 64 |
if !ok { |
| 65 |
tagSet = set.New[string]() |
| 66 |
m.tags[desc.Digest] = tagSet |
| 67 |
} |
| 68 |
tagSet.Add(reference) |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
// Untag removes a reference from index map. |
| 73 |
func (m *Memory) Untag(reference string) { |
| 74 |
m.lock.Lock() |
| 75 |
defer m.lock.Unlock() |
| 76 |
|
| 77 |
desc, ok := m.index[reference] |
| 78 |
if !ok { |
| 79 |
return |
| 80 |
} |
| 81 |
delete(m.index, reference) |
| 82 |
tagSet := m.tags[desc.Digest] |
| 83 |
tagSet.Delete(reference) |
| 84 |
if len(tagSet) == 0 { |
| 85 |
delete(m.tags, desc.Digest) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
// Map dumps the memory into a built-in map structure. |
| 90 |
// Like other operations, calling Map() is go-routine safe. |
| 91 |
func (m *Memory) Map() map[string]ocispec.Descriptor { |
| 92 |
m.lock.RLock() |
| 93 |
defer m.lock.RUnlock() |
| 94 |
|
| 95 |
return maps.Clone(m.index) |
| 96 |
} |
| 97 |
|
| 98 |
// TagSet returns the set of tags of the descriptor. |
| 99 |
func (m *Memory) TagSet(desc ocispec.Descriptor) set.Set[string] { |
| 100 |
m.lock.RLock() |
| 101 |
defer m.lock.RUnlock() |
| 102 |
|
| 103 |
tagSet := m.tags[desc.Digest] |
| 104 |
return maps.Clone(tagSet) |
| 105 |
} |