Denormalised block count. moved table into card. Fixed link again.

This commit is contained in:
michael-bailey 2025-03-17 21:09:22 +00:00
parent af89e58c7b
commit 99e5a0ce3d
8 changed files with 171 additions and 37 deletions

View File

@ -28,7 +28,7 @@ public class FilesController(
var handles = fileHandles.Select(e => new FileHandleModel
{
FileName = e.Name,
BlockCount = e.FileBlocks.Count,
BlockCount = e.FileBlockCount,
Id = e.Id,
})
.ToList();
@ -48,7 +48,7 @@ public class FilesController(
var model = new FileHandleModel
{
FileName = fileHandle.Name,
BlockCount = fileHandle.FileBlocks.Count,
BlockCount = fileHandle.FileBlockCount,
Id = fileHandle.Id,
};

View File

@ -10,4 +10,6 @@ public class FileHandle
public required string Name { get; set; }
public ICollection<FileBlock> FileBlocks { get; } = new List<FileBlock>();
public int FileBlockCount { get; set; } = 0;
}

View File

@ -0,0 +1,84 @@
// <auto-generated />
using System;
using FileStorageService.www.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FileStorageService.www.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20250317201703_DenormalisingBlockCount")]
partial class DenormalisingBlockCount
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
modelBuilder.Entity("FileStorageService.www.Data.FileBlock", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<int>("BlockNumber")
.HasColumnType("INTEGER");
b.Property<byte[]>("Data")
.IsRequired()
.HasMaxLength(1024)
.HasColumnType("BLOB");
b.Property<Guid>("FileHandleId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("FileHandleId");
b.ToTable("FileBlocks");
});
modelBuilder.Entity("FileStorageService.www.Data.FileHandle", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<int>("FileBlockCount")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("FileHandles");
});
modelBuilder.Entity("FileStorageService.www.Data.FileBlock", b =>
{
b.HasOne("FileStorageService.www.Data.FileHandle", "FileHandle")
.WithMany("FileBlocks")
.HasForeignKey("FileHandleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("FileHandle");
});
modelBuilder.Entity("FileStorageService.www.Data.FileHandle", b =>
{
b.Navigation("FileBlocks");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FileStorageService.www.Data.Migrations
{
/// <inheritdoc />
public partial class DenormalisingBlockCount : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "FileBlockCount",
table: "FileHandles",
type: "INTEGER",
nullable: false,
defaultValue: 0);
migrationBuilder.Sql(@"
UPDATE FileHandles
SET FileBlockCount = (
SELECT COUNT(*)
FROM FileBlocks
WHERE FileBlocks.FileHandleId = FileHandles.Id
);
");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "FileBlockCount",
table: "FileHandles");
}
}
}

View File

@ -47,6 +47,9 @@ namespace FileStorageService.www.Data.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<int>("FileBlockCount")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(64)

View File

@ -7,8 +7,8 @@ public class FileRepository(ApplicationDbContext context)
{
public static readonly int MAX_BLOCKS = 10_485_760;
private readonly Queue<(string, Stream)> creationQueue = new();
private readonly Lock countLock = new();
private readonly Queue<(string, Stream)> _creationQueue = new();
private readonly Lock _countLock = new();
public async Task<List<FileHandle>> GetAllFilesAsync()
{
@ -26,9 +26,9 @@ public class FileRepository(ApplicationDbContext context)
{
var tcs = new TaskCompletionSource<Guid?>();
lock (creationQueue)
lock (_creationQueue)
{
creationQueue.Enqueue((name, reader));
_creationQueue.Enqueue((name, reader));
}
Task.Run(async () =>
@ -37,9 +37,9 @@ public class FileRepository(ApplicationDbContext context)
Stream stream;
string name;
lock (creationQueue)
lock (_creationQueue)
{
(name, stream) = creationQueue.Dequeue();
(name, stream) = _creationQueue.Dequeue();
}
var fileHandle = new FileHandle
@ -49,8 +49,9 @@ public class FileRepository(ApplicationDbContext context)
var handle = await CreateFileBlocks(stream, fileHandle);
var currentFileCount = handle.FileBlocks.Count();
handle.FileBlockCount = currentFileCount;
lock (countLock)
lock (_countLock)
{
var count = context.FileBlocks.Count();

View File

@ -7,7 +7,7 @@
<h2>All Files</h2>
<div class="card">
<div class="card mb-4">
<div class="card-body">
<h5 class="card-title">Total Storage</h5>
<div class="progress">
@ -21,31 +21,37 @@
</div>
@if (!Model.FileHandles.Any())
{
<p>No files available</p>
}
else
{
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Block Count</th>
<th scope="col">Links</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.FileHandles)
<div class="card">
<div class="card-body">
@if (!Model.FileHandles.Any())
{
<tr>
<th scope="row" class="align-middle">@file.FileName</th>
<th scope="row" class="align-middle">@file.BlockCount</th>
<th scope="row" class="align-middle"><a
asp-controller="Files" asp-action="Index" asp-route-id="@file.Id"
class="btn btn-primary">View...</a></th>
</tr>
<p>No files available</p>
<a class="btn btn-primary" asp-area="" asp-controller="Files" asp-action="New">New File...</a>
}
</tbody>
</table>
}
else
{
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Block Count</th>
<th scope="col">Links</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.FileHandles)
{
<tr>
<th scope="row" class="align-middle">@file.FileName</th>
<th scope="row" class="align-middle">@file.BlockCount</th>
<th scope="row" class="align-middle">
<a
asp-controller="Files" asp-action="Index" asp-route-id="@file.Id"
class="btn btn-primary">View...</a></th>
</tr>
}
</tbody>
</table>
}
</div>
</div>

View File

@ -24,7 +24,7 @@
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Files" asp-action="Index">Files</a>
<a class="nav-link text-dark" asp-area="" asp-controller="Files" asp-action="">Files</a>
</li>
</ul>
</div>