Skip to content

Commit

Permalink
reintroduce chunk store interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Dec 10, 2024
1 parent a554cfb commit c233872
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Etherna.BeeNet.Hashing.Pipeline
{
internal sealed class ChunkStoreWriterPipelineStage(
ChunkStoreBase chunkStore,
IChunkStore chunkStore,
IPostageStamper postageStamper,
IHasherPipelineStage? nextStage)
: IHasherPipelineStage
Expand Down
2 changes: 1 addition & 1 deletion src/BeeNet.Util/Hashing/Pipeline/HasherPipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IHasherPipeline BuildNewHasherPipeline(
chunkConcurrency);

public static IHasherPipeline BuildNewHasherPipeline(
ChunkStoreBase chunkStore,
IChunkStore chunkStore,
IPostageStamper postageStamper,
RedundancyLevel redundancyLevel,
bool isEncrypted,
Expand Down
2 changes: 1 addition & 1 deletion src/BeeNet.Util/Manifest/ReferencedMantarayManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Etherna.BeeNet.Manifest
{
public class ReferencedMantarayManifest(
ReadOnlyChunkStoreBase chunkStore,
IReadOnlyChunkStore chunkStore,
SwarmHash rootHash,
bool useChunksCache = true)
: IReadOnlyMantarayManifest
Expand Down
4 changes: 2 additions & 2 deletions src/BeeNet.Util/Manifest/ReferencedMantarayNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Etherna.BeeNet.Manifest
public class ReferencedMantarayNode : IReadOnlyMantarayNode
{
// Fields.
private readonly ReadOnlyChunkStoreBase chunkStore;
private readonly IReadOnlyChunkStore chunkStore;
private readonly bool useChunksCache;

private SwarmHash? _entryHash;
Expand All @@ -36,7 +36,7 @@ public class ReferencedMantarayNode : IReadOnlyMantarayNode

// Constructor.
public ReferencedMantarayNode(
ReadOnlyChunkStoreBase chunkStore,
IReadOnlyChunkStore chunkStore,
SwarmHash chunkHash,
Dictionary<string, string>? metadata,
NodeType nodeTypeFlags,
Expand Down
8 changes: 4 additions & 4 deletions src/BeeNet.Util/Services/ChunkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<UploadEvaluationResult> EvaluateDirectoryUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null)
IChunkStore? chunkStore = null)
{
// Checks.
if (indexFilename?.Contains(SwarmAddress.Separator, StringComparison.InvariantCulture) == true)
Expand Down Expand Up @@ -146,7 +146,7 @@ public async Task<UploadEvaluationResult> EvaluateSingleFileUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null)
IChunkStore? chunkStore = null)
{
using var stream = new MemoryStream(data);
return await EvaluateSingleFileUploadAsync(
Expand All @@ -170,7 +170,7 @@ public async Task<UploadEvaluationResult> EvaluateSingleFileUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null)
IChunkStore? chunkStore = null)
{
chunkStore ??= new FakeChunkStore();

Expand Down Expand Up @@ -240,7 +240,7 @@ public string[] GetAllChunkFilesInDirectory(string chunkStoreDirectory) =>

public async Task<IReadOnlyDictionary<string, string>> GetFileMetadataFromChunksAsync(
SwarmAddress address,
ReadOnlyChunkStoreBase chunkStore)
IReadOnlyChunkStore chunkStore)
{
var rootManifest = new ReferencedMantarayManifest(
chunkStore,
Expand Down
8 changes: 4 additions & 4 deletions src/BeeNet.Util/Services/IChunkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Task<UploadEvaluationResult> EvaluateDirectoryUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null);
IChunkStore? chunkStore = null);

/// <summary>
/// Evaluate the result uploading a single file
Expand All @@ -70,7 +70,7 @@ Task<UploadEvaluationResult> EvaluateSingleFileUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null);
IChunkStore? chunkStore = null);

/// <summary>
/// Evaluate the result uploading a single file
Expand All @@ -94,7 +94,7 @@ Task<UploadEvaluationResult> EvaluateSingleFileUploadAsync(
RedundancyLevel redundancyLevel = RedundancyLevel.None,
IPostageStampIssuer? postageStampIssuer = null,
int? chunkCuncorrency = null,
ChunkStoreBase? chunkStore = null);
IChunkStore? chunkStore = null);

/// <summary>
/// Get list of all chunk files in directory
Expand All @@ -112,7 +112,7 @@ string[] GetAllChunkFilesInDirectory(
/// <returns>Resource metadata</returns>
Task<IReadOnlyDictionary<string, string>> GetFileMetadataFromChunksAsync(
SwarmAddress address,
ReadOnlyChunkStoreBase chunkStore);
IReadOnlyChunkStore chunkStore);

/// <summary>
/// Get resource stream from a directory of chunks and the resource address
Expand Down
2 changes: 1 addition & 1 deletion src/BeeNet.Util/Stores/ChunkJoiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Etherna.BeeNet.Stores
{
public class ChunkJoiner(
ReadOnlyChunkStoreBase chunkStore)
IReadOnlyChunkStore chunkStore)
{
// Methods.
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/BeeNet.Util/Stores/ChunkStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Etherna.BeeNet.Stores
{
public abstract class ChunkStoreBase(
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null)
: ReadOnlyChunkStoreBase(chunksCache)
: ReadOnlyChunkStoreBase(chunksCache), IChunkStore
{
/// <summary>
/// Add a chunk in the store
Expand Down
26 changes: 26 additions & 0 deletions src/BeeNet.Util/Stores/IChunkStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2021-present Etherna SA
// This file is part of Bee.Net.
//
// Bee.Net is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Bee.Net is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Bee.Net.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;
using System.Threading.Tasks;

namespace Etherna.BeeNet.Stores
{
public interface IChunkStore : IReadOnlyChunkStore
{
Task<bool> AddAsync(
SwarmChunk chunk,
bool bypassCacheWriting);
}
}
32 changes: 32 additions & 0 deletions src/BeeNet.Util/Stores/IReadOnlyChunkStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2021-present Etherna SA
// This file is part of Bee.Net.
//
// Bee.Net is free software: you can redistribute it and/or modify it under the terms of the
// GNU Lesser General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Bee.Net is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with Bee.Net.
// If not, see <https://www.gnu.org/licenses/>.

using Etherna.BeeNet.Models;
using System.Threading.Tasks;

namespace Etherna.BeeNet.Stores
{
public interface IReadOnlyChunkStore
{
Task<SwarmChunk> GetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting);

Task<SwarmChunk?> TryGetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting);
}
}
9 changes: 5 additions & 4 deletions src/BeeNet.Util/Stores/ReadOnlyChunkStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Etherna.BeeNet.Stores
{
public abstract class ReadOnlyChunkStoreBase(
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null)
: IReadOnlyChunkStore
{
// Fields.
protected readonly IDictionary<SwarmHash, SwarmChunk> ChunksCache =
Expand All @@ -34,15 +35,15 @@ public async Task<SwarmChunk> GetAsync(
{
if (!bypassCacheReading && ChunksCache.TryGetValue(hash, out var chunk))
return chunk;

chunk = await LoadChunkAsync(hash).ConfigureAwait(false);

if (!bypassCacheWriting)
ChunksCache[hash] = chunk;

return chunk;
}

public async Task<SwarmChunk?> TryGetAsync(
SwarmHash hash,
bool bypassCacheReading,
Expand All @@ -57,7 +58,7 @@ public async Task<SwarmChunk> GetAsync(
return null;
}
}

// Protected methods.
protected abstract Task<SwarmChunk> LoadChunkAsync(SwarmHash hash);
}
Expand Down

0 comments on commit c233872

Please sign in to comment.