From 3516cf9f0b67af3ae58308d5f7433048fdbff5aa Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 20 Oct 2024 23:02:49 +0200 Subject: [PATCH] chunks fixes --- src/BeeTurbo.Domain/Models/Chunk.cs | 8 +++++--- src/BeeTurbo.Persistence/BeehiveDbContext.cs | 16 +++++++--------- src/BeeTurbo.Persistence/ModelMaps/ChunkMap.cs | 8 +++++++- src/BeeTurbo/Program.cs | 8 +++++++- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/BeeTurbo.Domain/Models/Chunk.cs b/src/BeeTurbo.Domain/Models/Chunk.cs index 936bba6..a1f29bd 100644 --- a/src/BeeTurbo.Domain/Models/Chunk.cs +++ b/src/BeeTurbo.Domain/Models/Chunk.cs @@ -13,7 +13,7 @@ // If not, see . using Etherna.BeeNet.Models; -using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Etherna.BeeTurbo.Domain.Models { @@ -23,7 +23,7 @@ public class Chunk : EntityModelBase public Chunk(SwarmHash hash, byte[] payload) { Hash = hash; - Payload = payload.AsReadOnly(); + Payload = payload; } #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. public Chunk() { } @@ -31,6 +31,8 @@ public Chunk() { } // Properties. public virtual SwarmHash Hash { get; protected set; } - public virtual IReadOnlyCollection Payload { get; protected set; } + + [SuppressMessage("Performance", "CA1819:Properties should not return arrays")] + public virtual byte[] Payload { get; protected set; } } } \ No newline at end of file diff --git a/src/BeeTurbo.Persistence/BeehiveDbContext.cs b/src/BeeTurbo.Persistence/BeehiveDbContext.cs index eece740..cb9909e 100644 --- a/src/BeeTurbo.Persistence/BeehiveDbContext.cs +++ b/src/BeeTurbo.Persistence/BeehiveDbContext.cs @@ -45,21 +45,19 @@ public class BeehiveDbContext : DbContext, IBeehiveDbContext IndexBuilders = [ (Builders.IndexKeys.Ascending(c => c.CreationDateTime), new CreateIndexOptions()), - (Builders.IndexKeys.Ascending(c => c.Hash), new CreateIndexOptions { Unique = true }) + (Builders.IndexKeys.Ascending(c => c.Hash), new CreateIndexOptions()) ] }); public GridFSBucket ChunksBucket { get { - if (_chunksBucket == null) - _chunksBucket = new GridFSBucket(Database, new GridFSBucketOptions - { - BucketName = "chunks", - WriteConcern = WriteConcern.WMajority, - ReadPreference = ReadPreference.Secondary - }); - return _chunksBucket; + return _chunksBucket ??= new GridFSBucket(Database, new GridFSBucketOptions + { + BucketName = "chunks", + WriteConcern = WriteConcern.WMajority, + ReadPreference = ReadPreference.Secondary + }); } } diff --git a/src/BeeTurbo.Persistence/ModelMaps/ChunkMap.cs b/src/BeeTurbo.Persistence/ModelMaps/ChunkMap.cs index d5883a3..0d61be2 100644 --- a/src/BeeTurbo.Persistence/ModelMaps/ChunkMap.cs +++ b/src/BeeTurbo.Persistence/ModelMaps/ChunkMap.cs @@ -13,6 +13,7 @@ // If not, see . using Etherna.BeeTurbo.Domain.Models; +using Etherna.MongoDB.Bson.Serialization.Serializers; using Etherna.MongODM.Core; using Etherna.MongODM.Core.Serialization; @@ -23,7 +24,12 @@ internal sealed class ChunkMap : IModelMapsCollector public void Register(IDbContext dbContext) { dbContext.MapRegistry.AddModelMap( //v0.2.4 - "06aaf593-07af-4fca-99a9-bdc3718547d8"); + "06aaf593-07af-4fca-99a9-bdc3718547d8", + mm => + { + mm.AutoMap(); + mm.MapProperty(c => c.Payload).SetSerializer(ByteArraySerializer.Instance); + }); } } } \ No newline at end of file diff --git a/src/BeeTurbo/Program.cs b/src/BeeTurbo/Program.cs index 4815ec6..5908772 100644 --- a/src/BeeTurbo/Program.cs +++ b/src/BeeTurbo/Program.cs @@ -112,8 +112,8 @@ private static void ConfigureLogging() private static void ConfigureServices(WebApplicationBuilder builder, string beeUrl) { - var services = builder.Services; var config = builder.Configuration; + var services = builder.Services; // Add services. services.AddCors(); @@ -198,6 +198,12 @@ private static void ConfigureApplication(WebApplication app, string beeUrl) handler.HandleAsync(httpContext)); app.MapForwarder("/{**catch-all}", beeUrl); + + // Internal features mapping + app.Map("db/migrate", (IBeehiveDbContext dbContext) => + { + dbContext.Chunks.BuildIndexesAsync(); + }); } } } \ No newline at end of file