Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: modernize memory store and copy #89

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/OrasProject.Oras/Content/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class Extensions
/// <param name="node"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static async Task<IList<Descriptor>> SuccessorsAsync(this IFetchable fetcher, Descriptor node, CancellationToken cancellationToken)
public static async Task<IEnumerable<Descriptor>> GetSuccessorsAsync(this IFetchable fetcher, Descriptor node, CancellationToken cancellationToken)
{
switch (node.MediaType)
{
Expand Down Expand Up @@ -61,7 +61,7 @@ public static async Task<IList<Descriptor>> SuccessorsAsync(this IFetchable fetc
return index.Manifests;
}
}
return new List<Descriptor>();
return Array.Empty<Descriptor>();
}

/// <summary>
Expand Down
36 changes: 36 additions & 0 deletions src/OrasProject.Oras/Content/IPredecessorFinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Oci;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace OrasProject.Oras.Content;

/// <summary>
/// IPredecessorFinder finds out the nodes directly pointing to a given node of a
/// directed acyclic graph.
/// In other words, returns the "parents" of the current descriptor.
/// IPredecessorFinder is an extension of Storage.
/// </summary>
public interface IPredecessorFinder
{
/// <summary>
/// returns the nodes directly pointing to the current node.
/// </summary>
/// <param name="node"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<IEnumerable<Descriptor>> GetPredecessorsAsync(Descriptor node, CancellationToken cancellationToken = default);
}
65 changes: 65 additions & 0 deletions src/OrasProject.Oras/Content/MemoryGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Oci;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace OrasProject.Oras.Content;

internal class MemoryGraph : IPredecessorFinder
{
private readonly ConcurrentDictionary<BasicDescriptor, ConcurrentDictionary<BasicDescriptor, Descriptor>> _predecessors = new();

/// <summary>
/// Returns the nodes directly pointing to the current node.
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public Task<IEnumerable<Descriptor>> GetPredecessorsAsync(Descriptor node, CancellationToken cancellationToken = default)
{
var key = node.BasicDescriptor;
if (_predecessors.TryGetValue(key, out var predecessors))
{
return Task.FromResult<IEnumerable<Descriptor>>(predecessors.Values);
}
return Task.FromResult<IEnumerable<Descriptor>>(Array.Empty<Descriptor>());
}

internal async Task IndexAsync(IFetchable fetcher, Descriptor node, CancellationToken cancellationToken)
{
var successors = await fetcher.GetSuccessorsAsync(node, cancellationToken).ConfigureAwait(false);
Index(node, successors);
}

/// <summary>
/// Index indexes predecessors for each direct successor of the given node.
/// There is no data consistency issue as long as deletion is not implemented
/// for the underlying storage.
/// </summary>
/// <param name="node"></param>
/// <param name="successors"></param>
private void Index(Descriptor node, IEnumerable<Descriptor> successors)
{
var predecessorKey = node.BasicDescriptor;
foreach (var successor in successors)
{
var successorKey = successor.BasicDescriptor;
var predecessors = _predecessors.GetOrAdd(successorKey, _ => new ConcurrentDictionary<BasicDescriptor, Descriptor>());
predecessors.TryAdd(predecessorKey, node);
}
}
}
55 changes: 55 additions & 0 deletions src/OrasProject.Oras/Content/MemoryStorage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Oci;
using System.Collections.Concurrent;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace OrasProject.Oras.Content;

internal class MemoryStorage : IStorage
{
private readonly ConcurrentDictionary<BasicDescriptor, byte[]> _content = new();

public Task<bool> ExistsAsync(Descriptor target, CancellationToken _ = default)
{
return Task.FromResult(_content.ContainsKey(target.BasicDescriptor));
}

public Task<Stream> FetchAsync(Descriptor target, CancellationToken _ = default)
{
if (!_content.TryGetValue(target.BasicDescriptor, out var content))
{
throw new NotFoundException($"{target.Digest}: {target.MediaType}");
}
return Task.FromResult<Stream>(new MemoryStream(content));
}

public async Task PushAsync(Descriptor expected, Stream contentStream, CancellationToken cancellationToken = default)
{
var key = expected.BasicDescriptor;
if (_content.ContainsKey(key))
{
throw new AlreadyExistsException($"{expected.Digest}: {expected.MediaType}");
}

var content = await contentStream.ReadAllAsync(expected, cancellationToken).ConfigureAwait(false);
if (!_content.TryAdd(key, content))
{
throw new AlreadyExistsException($"{key.Digest}: {key.MediaType}");

Check warning on line 52 in src/OrasProject.Oras/Content/MemoryStorage.cs

View check run for this annotation

Codecov / codecov/patch

src/OrasProject.Oras/Content/MemoryStorage.cs#L51-L52

Added lines #L51 - L52 were not covered by tests
}
}
}
97 changes: 97 additions & 0 deletions src/OrasProject.Oras/Content/MemoryStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Oci;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace OrasProject.Oras.Content;

public class MemoryStore : ITarget, IPredecessorFinder
{
private readonly MemoryStorage _storage = new();
private readonly MemoryTagStore _tagResolver = new();
private readonly MemoryGraph _graph = new();

/// <summary>
/// ExistsAsync returns checks if the described content exists.
/// </summary>
/// <param name="target"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> ExistsAsync(Descriptor target, CancellationToken cancellationToken = default)
=> await _storage.ExistsAsync(target, cancellationToken).ConfigureAwait(false);

/// <summary>
/// FetchAsync fetches the content identified by the descriptor.
/// </summary>
/// <param name="target"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Stream> FetchAsync(Descriptor target, CancellationToken cancellationToken = default)
=> await _storage.FetchAsync(target, cancellationToken).ConfigureAwait(false);

/// <summary>
/// PushAsync pushes the content, matching the expected descriptor.
/// </summary>
/// <param name="expected"></param>
/// <param name="contentStream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task PushAsync(Descriptor expected, Stream contentStream, CancellationToken cancellationToken = default)
{
await _storage.PushAsync(expected, contentStream, cancellationToken).ConfigureAwait(false);
await _graph.IndexAsync(_storage, expected, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// ResolveAsync resolves a reference to a descriptor.
/// </summary>
/// <param name="reference"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<Descriptor> ResolveAsync(string reference, CancellationToken cancellationToken = default)
=> await _tagResolver.ResolveAsync(reference, cancellationToken).ConfigureAwait(false);

/// <summary>
/// TagAsync tags a descriptor with a reference string.
/// It throws NotFoundException if the tagged content does not exist.
/// </summary>
/// <param name="descriptor"></param>
/// <param name="reference"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotFoundException"></exception>
public async Task TagAsync(Descriptor descriptor, string reference, CancellationToken cancellationToken = default)
{
if (!await _storage.ExistsAsync(descriptor, cancellationToken).ConfigureAwait(false))
{
throw new NotFoundException($"{descriptor.Digest}: {descriptor.MediaType}");

Check warning on line 82 in src/OrasProject.Oras/Content/MemoryStore.cs

View check run for this annotation

Codecov / codecov/patch

src/OrasProject.Oras/Content/MemoryStore.cs#L81-L82

Added lines #L81 - L82 were not covered by tests
}
await _tagResolver.TagAsync(descriptor, reference, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// PredecessorsAsync returns the nodes directly pointing to the current node.
/// Predecessors returns null without error if the node does not exists in the
/// store.
/// </summary>
/// <param name="node"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<IEnumerable<Descriptor>> GetPredecessorsAsync(Descriptor node, CancellationToken cancellationToken = default)
=> await _graph.GetPredecessorsAsync(node, cancellationToken).ConfigureAwait(false);
}
40 changes: 40 additions & 0 deletions src/OrasProject.Oras/Content/MemoryTagStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The ORAS Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using OrasProject.Oras.Exceptions;
using OrasProject.Oras.Oci;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace OrasProject.Oras.Content;

internal class MemoryTagStore : ITagStore
{
private readonly ConcurrentDictionary<string, Descriptor> _index = new();

public Task<Descriptor> ResolveAsync(string reference, CancellationToken _ = default)
{
if (!_index.TryGetValue(reference, out var content))
{
throw new NotFoundException();

Check warning on line 30 in src/OrasProject.Oras/Content/MemoryTagStore.cs

View check run for this annotation

Codecov / codecov/patch

src/OrasProject.Oras/Content/MemoryTagStore.cs#L29-L30

Added lines #L29 - L30 were not covered by tests
}
return Task.FromResult(content);
}

public Task TagAsync(Descriptor descriptor, string reference, CancellationToken _ = default)
{
_index.AddOrUpdate(reference, descriptor, (_, _) => descriptor);
return Task.CompletedTask;
}
}
81 changes: 0 additions & 81 deletions src/OrasProject.Oras/Copy.cs

This file was deleted.

Loading
Loading