oras.land/oras-go/v2@v2.5.1-0.20240520045656-aef90e4d04c4/internal/resolver/memory.go (about)

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