Skip to content

Commit

Permalink
Add caching mechanism for chunk retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 27, 2024
1 parent 1ffa442 commit 277782a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/BeeNet.Client/BeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,11 @@ public async Task<RedistributionState> RedistributionStateAsync(CancellationToke
fees: XDaiBalance.FromWeiString(response.Fees));
}

public async Task<SwarmChunkReference> ResolveAddressToChunkReferenceAsync(SwarmAddress address)
public async Task<SwarmChunkReference> ResolveAddressToChunkReferenceAsync(
SwarmAddress address,
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null)
{
var chunkStore = new BeeClientChunkStore(this);
var chunkStore = new BeeClientChunkStore(this, chunksCache);

var rootManifest = new ReferencedMantarayManifest(
chunkStore,
Expand Down Expand Up @@ -1260,12 +1262,13 @@ await generatedClient.BzzHeadAsync(

public async Task<string?> TryGetFileNameAsync(
SwarmAddress address,
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null,
CancellationToken cancellationToken = default)
{
var chunkService = new ChunkService();
var metadata = await chunkService.GetFileMetadataFromChunksAsync(
address,
new BeeClientChunkStore(this)).ConfigureAwait(false);
new BeeClientChunkStore(this, chunksCache)).ConfigureAwait(false);
return metadata.GetValueOrDefault(ManifestEntry.FilenameKey);
}

Expand Down
20 changes: 16 additions & 4 deletions src/BeeNet.Util/Hashing/Store/BeeClientChunkStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@

using Etherna.BeeNet.Exceptions;
using Etherna.BeeNet.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Etherna.BeeNet.Hashing.Store
{
public class BeeClientChunkStore(IBeeClient beeClient)
public class BeeClientChunkStore(
IBeeClient beeClient,
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null)
: IReadOnlyChunkStore
{
public Task<SwarmChunk> GetAsync(SwarmHash hash) =>
beeClient.GetChunkAsync(hash);
public async Task<SwarmChunk> GetAsync(SwarmHash hash)
{
if (chunksCache != null && chunksCache.TryGetValue(hash, out var chunk))
return chunk;

chunk = await beeClient.GetChunkAsync(hash).ConfigureAwait(false);
if (chunksCache != null)
chunksCache[hash] = chunk;

return chunk;
}

public async Task<SwarmChunk?> TryGetAsync(SwarmHash hash)
{
try
{
return await beeClient.GetChunkAsync(hash).ConfigureAwait(false);
return await GetAsync(hash).ConfigureAwait(false);
}
catch (BeeNetApiException)
{
Expand Down
5 changes: 4 additions & 1 deletion src/BeeNet.Util/IBeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,9 @@ Task<string> RebroadcastTransactionAsync(
/// <exception cref="BeeNetDebugApiException">A server side error occurred.</exception>
Task<RedistributionState> RedistributionStateAsync(CancellationToken cancellationToken = default);

Task<SwarmChunkReference> ResolveAddressToChunkReferenceAsync(SwarmAddress address);
Task<SwarmChunkReference> ResolveAddressToChunkReferenceAsync(
SwarmAddress address,
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null);

/// <summary>Reupload a root hash to the network</summary>
/// <param name="hash">Root hash of content (can be of any type: collection, file, chunk)</param>
Expand Down Expand Up @@ -772,6 +774,7 @@ Task<string> TryConnectToPeerAsync(
/// <returns>The file name if found</returns>
Task<string?> TryGetFileNameAsync(
SwarmAddress address,
IDictionary<SwarmHash, SwarmChunk>? chunksCache = null,
CancellationToken cancellationToken = default);

/// <summary>
Expand Down

0 comments on commit 277782a

Please sign in to comment.