Skip to content

Commit

Permalink
Refactor cache-handling logic in chunk store API
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Jan 7, 2025
1 parent 5853ade commit 5e20162
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task FeedAsync(HasherPipelineFeedArgs args)
// Store chunk.
var chunk = SwarmChunk.BuildFromSpanAndData(args.Hash.Value, args.Data.Span);
chunk.PostageStamp = stamp;
await chunkStore.AddAsync(chunk, true).ConfigureAwait(false);
await chunkStore.AddAsync(chunk).ConfigureAwait(false);

if (nextStage is not null)
await nextStage.FeedAsync(args).ConfigureAwait(false);
Expand Down
3 changes: 1 addition & 2 deletions src/BeeNet.Util/Manifest/ReferencedMantarayNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public async Task DecodeFromChunkAsync(SwarmHash manifestHash)

var chunk = await chunkStore.GetAsync(
Hash,
!useChunksCache,
!useChunksCache).ConfigureAwait(false);
useChunksCache).ConfigureAwait(false);

var data = chunk.Data.ToArray();
var readIndex = 0;
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 @@ -73,7 +73,7 @@ private async Task GetJoinedChunkDataHelperAsync(
CancellationToken cancellationToken)
{
// Read and decrypt chunk data.
var chunk = await chunkStore.GetAsync(chunkReference.Hash, true, true).ConfigureAwait(false);
var chunk = await chunkStore.GetAsync(chunkReference.Hash).ConfigureAwait(false);
var dataArray = chunk.Data.ToArray();
chunkReference.EncryptionKey?.EncryptDecrypt(dataArray);

Expand Down
6 changes: 3 additions & 3 deletions src/BeeNet.Util/Stores/ChunkStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ public abstract class ChunkStoreBase(
/// Add a chunk in the store
/// </summary>
/// <param name="chunk">The chuck to add</param>
/// <param name="bypassCacheWriting">Bypass cache update</param>
/// <param name="cacheChunk">Add chunk in cache</param>
/// <returns>True if chunk has been added, false if already existing</returns>
public async Task<bool> AddAsync(
SwarmChunk chunk,
bool bypassCacheWriting)
bool cacheChunk = false)
{
ArgumentNullException.ThrowIfNull(chunk, nameof(chunk));

var result = await SaveChunkAsync(chunk).ConfigureAwait(false);

if (!bypassCacheWriting)
if (cacheChunk)
ChunksCache[chunk.Hash] = chunk;

return result;
Expand Down
2 changes: 1 addition & 1 deletion src/BeeNet.Util/Stores/IChunkStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public interface IChunkStore : IReadOnlyChunkStore
{
Task<bool> AddAsync(
SwarmChunk chunk,
bool bypassCacheWriting);
bool cacheChunk = false);
}
}
6 changes: 2 additions & 4 deletions src/BeeNet.Util/Stores/IReadOnlyChunkStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ public interface IReadOnlyChunkStore
{
Task<SwarmChunk> GetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting);
bool cacheChunk = false);

Task<SwarmChunk?> TryGetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting);
bool cacheChunk = false);
}
}
16 changes: 7 additions & 9 deletions src/BeeNet.Util/Stores/ReadOnlyChunkStoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,33 @@ public abstract class ReadOnlyChunkStoreBase(
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null)
: IReadOnlyChunkStore
{
// Fields.
protected readonly IDictionary<SwarmHash, SwarmChunk> ChunksCache =
// Properties.
protected IDictionary<SwarmHash, SwarmChunk> ChunksCache { get; } =
chunksCache ?? new Dictionary<SwarmHash, SwarmChunk>();

// Methods.
public async Task<SwarmChunk> GetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting)
bool cacheChunk = false)
{
if (!bypassCacheReading && ChunksCache.TryGetValue(hash, out var chunk))
if (ChunksCache.TryGetValue(hash, out var chunk))
return chunk;

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

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

return chunk;
}

public async Task<SwarmChunk?> TryGetAsync(
SwarmHash hash,
bool bypassCacheReading,
bool bypassCacheWriting)
bool cacheChunk = false)
{
try
{
return await GetAsync(hash, bypassCacheReading, bypassCacheWriting).ConfigureAwait(false);
return await GetAsync(hash, cacheChunk).ConfigureAwait(false);
}
catch (BeeNetApiException)
{
Expand Down

0 comments on commit 5e20162

Please sign in to comment.