cuelabs.dev/go/oci/ociregistry@v0.0.0-20240906074133-82eb438dd565/ocimem/lister.go (about)

     1  // Copyright 2023 CUE Labs AG
     2  //
     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  package ocimem
    16  
    17  import (
    18  	"context"
    19  	"slices"
    20  	"strings"
    21  
    22  	"cuelabs.dev/go/oci/ociregistry"
    23  )
    24  
    25  func (r *Registry) Repositories(ctx context.Context, startAfter string) ociregistry.Seq[string] {
    26  	r.mu.Lock()
    27  	defer r.mu.Unlock()
    28  	return mapKeysIter(r.repos, strings.Compare, startAfter)
    29  }
    30  
    31  func (r *Registry) Tags(ctx context.Context, repoName string, startAfter string) ociregistry.Seq[string] {
    32  	r.mu.Lock()
    33  	defer r.mu.Unlock()
    34  	repo, err := r.repo(repoName)
    35  	if err != nil {
    36  		return ociregistry.ErrorSeq[string](err)
    37  	}
    38  	return mapKeysIter(repo.tags, strings.Compare, startAfter)
    39  }
    40  
    41  func (r *Registry) Referrers(ctx context.Context, repoName string, digest ociregistry.Digest, artifactType string) ociregistry.Seq[ociregistry.Descriptor] {
    42  	r.mu.Lock()
    43  	defer r.mu.Unlock()
    44  	repo, err := r.repo(repoName)
    45  	if err != nil {
    46  		return ociregistry.ErrorSeq[ociregistry.Descriptor](err)
    47  	}
    48  	var referrers []ociregistry.Descriptor
    49  	for _, b := range repo.manifests {
    50  		if b.subject != digest {
    51  			continue
    52  		}
    53  		// TODO filter by artifact type
    54  		referrers = append(referrers, b.descriptor())
    55  	}
    56  	slices.SortFunc(referrers, compareDescriptor)
    57  	return ociregistry.SliceSeq(referrers)
    58  }
    59  
    60  func mapKeysIter[K comparable, V any](m map[K]V, cmp func(K, K) int, startAfter K) ociregistry.Seq[K] {
    61  	ks := make([]K, 0, len(m))
    62  	for k := range m {
    63  		if cmp(startAfter, k) < 0 {
    64  			ks = append(ks, k)
    65  		}
    66  	}
    67  	slices.SortFunc(ks, cmp)
    68  	return ociregistry.SliceSeq(ks)
    69  }
    70  
    71  func compareDescriptor(d0, d1 ociregistry.Descriptor) int {
    72  	return strings.Compare(string(d0.Digest), string(d1.Digest))
    73  }