Skip to content

Commit

Permalink
refined the code
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Sep 18, 2023
1 parent d25b0ae commit a40961c
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 23 deletions.
14 changes: 1 addition & 13 deletions content/oci/deletableoci.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func NewDeletableStoreWithContext(ctx context.Context, root string) (*DeletableS
if err != nil {
return nil, fmt.Errorf("failed to create storage: %w", err)
}

store := &DeletableStore{
AutoSaveIndex: true,
root: rootAbs,
Expand All @@ -84,7 +83,6 @@ func NewDeletableStoreWithContext(ctx context.Context, root string) (*DeletableS
tagResolver: resolver.NewMemory(),
graph: graph.NewDeletableMemory(),
}

if err := ensureDir(filepath.Join(rootAbs, ociBlobsDir)); err != nil {
return nil, err
}
Expand All @@ -94,7 +92,6 @@ func NewDeletableStoreWithContext(ctx context.Context, root string) (*DeletableS
if err := store.loadIndexFile(ctx); err != nil {
return nil, fmt.Errorf("invalid OCI Image Index: %w", err)
}

return store, nil
}

Expand Down Expand Up @@ -160,15 +157,13 @@ func (ds *DeletableStore) Tag(ctx context.Context, desc ocispec.Descriptor, refe
if err := validateReference(reference); err != nil {
return err
}

exists, err := ds.storage.Exists(ctx, desc)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("%s: %s: %w", desc.Digest, desc.MediaType, errdef.ErrNotFound)
}

return ds.tag(ctx, desc, reference)
}

Expand Down Expand Up @@ -201,7 +196,6 @@ func (ds *DeletableStore) Resolve(ctx context.Context, reference string) (ocispe
if reference == "" {
return ocispec.Descriptor{}, errdef.ErrMissingReference
}

// attempt resolving manifest
desc, err := ds.tagResolver.Resolve(ctx, reference)
if err != nil {
Expand All @@ -211,11 +205,9 @@ func (ds *DeletableStore) Resolve(ctx context.Context, reference string) (ocispe
}
return ocispec.Descriptor{}, err
}

if reference == desc.Digest.String() {
return descriptor.Plain(desc), nil
}

return desc, nil
}

Expand Down Expand Up @@ -249,7 +241,6 @@ func (ds *DeletableStore) ensureOCILayoutFile() error {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to open OCI layout file: %w", err)
}

layout := ocispec.ImageLayout{
Version: ocispec.ImageLayoutVersion,
}
Expand All @@ -260,7 +251,6 @@ func (ds *DeletableStore) ensureOCILayoutFile() error {
return os.WriteFile(layoutFilePath, layoutJSON, 0666)
}
defer layoutFile.Close()

var layout ocispec.ImageLayout
err = json.NewDecoder(layoutFile).Decode(&layout)
if err != nil {
Expand All @@ -277,7 +267,6 @@ func (ds *DeletableStore) loadIndexFile(ctx context.Context) error {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to open index file: %w", err)
}

// write index.json if it does not exist
ds.index = &ocispec.Index{
Versioned: specs.Versioned{
Expand All @@ -288,7 +277,6 @@ func (ds *DeletableStore) loadIndexFile(ctx context.Context) error {
return ds.writeIndexFile()
}
defer indexFile.Close()

var index ocispec.Index
if err := json.NewDecoder(indexFile).Decode(&index); err != nil {
return fmt.Errorf("failed to decode index file: %w", err)
Expand Down Expand Up @@ -349,7 +337,7 @@ func (ds *DeletableStore) writeIndexFile() error {
return os.WriteFile(ds.indexPath, indexJSON, 0666)
}

// loadIndexInDeletableMemory loads index into memory.
// loadIndexInDeletableMemory loads index into the memory.
func loadIndexInDeletableMemory(ctx context.Context, index *ocispec.Index, fetcher content.Fetcher, tagger content.Tagger, graph *graph.DeletableMemory) error {
for _, desc := range index.Manifests {
if err := tagger.Tag(ctx, deleteAnnotationRefName(desc), desc.Digest.String()); err != nil {
Expand Down
1 change: 0 additions & 1 deletion content/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func (s *Storage) Delete(ctx context.Context, target ocispec.Descriptor) error {
return fmt.Errorf("%s: %s: %w", target.Digest, target.MediaType, errdef.ErrInvalidDigest)
}
targetPath := filepath.Join(s.root, path)

return os.Remove(targetPath)
}

Expand Down
4 changes: 0 additions & 4 deletions content/oci/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,26 +385,22 @@ func TestStorage_Delete(t *testing.T) {
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}

tempDir := t.TempDir()
s, err := NewStorage(tempDir)
if err != nil {
t.Fatal("New() error =", err)
}
ctx := context.Background()

if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil {
t.Fatal("Storage.Push() error =", err)
}

exists, err := s.Exists(ctx, desc)
if err != nil {
t.Fatal("Storage.Exists() error =", err)
}
if !exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, true)
}

err = s.Delete(ctx, desc)
if err != nil {
t.Fatal("Storage.Delete() error =", err)
Expand Down
5 changes: 0 additions & 5 deletions internal/graph/deletablememory.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ func (m *DeletableMemory) Index(ctx context.Context, fetcher content.Fetcher, no
func (m *DeletableMemory) IndexAll(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) error {
// track content status
tracker := status.NewTracker()

var fn syncutil.GoFunc[ocispec.Descriptor]
fn = func(ctx context.Context, region *syncutil.LimitedRegion, desc ocispec.Descriptor) error {
// skip the node if other go routine is working on it
_, committed := tracker.TryCommit(desc)
if !committed {
return nil
}

successors, err := m.index(ctx, fetcher, desc)
if err != nil {
if errors.Is(err, errdef.ErrNotFound) {
Expand All @@ -73,7 +71,6 @@ func (m *DeletableMemory) IndexAll(ctx context.Context, fetcher content.Fetcher,
}
return err
}

if len(successors) > 0 {
// traverse and index successors
return syncutil.Go(ctx, nil, fn, successors...)
Expand All @@ -92,7 +89,6 @@ func (m *DeletableMemory) IndexAll(ctx context.Context, fetcher content.Fetcher,
func (m *DeletableMemory) Predecessors(_ context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
m.lock.RLock()
defer m.lock.RUnlock()

key := descriptor.FromOCI(node)
set, exists := m.predecessors[key]
if !exists {
Expand Down Expand Up @@ -134,7 +130,6 @@ func (m *DeletableMemory) index(ctx context.Context, fetcher content.Fetcher, no
// index the successors and predecessors
successorSet := set.New[descriptor.Descriptor]()
m.successors[nodeKey] = successorSet

for _, successor := range successors {
successorKey := descriptor.FromOCI(successor)
successorSet.Add(successorKey)
Expand Down

0 comments on commit a40961c

Please sign in to comment.