mirror of
https://github.com/chylex/Discord-History-Tracker.git
synced 2025-04-19 13:15:44 +02:00
Compare commits
6 Commits
50d5bdba97
...
5741fad528
Author | SHA1 | Date | |
---|---|---|---|
5741fad528 | |||
e7c52fd1d5 | |||
fb9b31175c | |||
f8e56ae367 | |||
7ed31fbc8b | |||
e92aeb5a1b |
@ -13,6 +13,7 @@
|
|||||||
<entry key="Desktop/Main/Controls/MessageFilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/Controls/MessageFilterPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||||
<entry key="Desktop/Main/Controls/ServerConfigurationPanel.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/Controls/ServerConfigurationPanel.axaml" value="Desktop/Desktop.csproj" />
|
||||||
<entry key="Desktop/Main/Controls/StatusBar.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/Controls/StatusBar.axaml" value="Desktop/Desktop.csproj" />
|
||||||
|
<entry key="Desktop/Main/Dialogs/NewDatabaseSettingsDialog.axaml" value="Desktop/Desktop.csproj" />
|
||||||
<entry key="Desktop/Main/MainWindow.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/MainWindow.axaml" value="Desktop/Desktop.csproj" />
|
||||||
<entry key="Desktop/Main/Pages/AdvancedPage.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/Pages/AdvancedPage.axaml" value="Desktop/Desktop.csproj" />
|
||||||
<entry key="Desktop/Main/Pages/DatabasePage.axaml" value="Desktop/Desktop.csproj" />
|
<entry key="Desktop/Main/Pages/DatabasePage.axaml" value="Desktop/Desktop.csproj" />
|
||||||
@ -25,4 +26,4 @@
|
|||||||
</map>
|
</map>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -54,6 +54,22 @@
|
|||||||
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
<Setter Property="HorizontalAlignment" Value="Stretch" />
|
||||||
</Style>
|
</Style>
|
||||||
|
|
||||||
|
<Style Selector="Panel.buttons">
|
||||||
|
<Setter Property="Margin" Value="0 20 0 0" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons > WrapPanel.right">
|
||||||
|
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons Button">
|
||||||
|
<Setter Property="MinWidth" Value="80" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons > WrapPanel.left Button">
|
||||||
|
<Setter Property="Margin" Value="0 0 8 0" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="Panel.buttons > WrapPanel.right Button">
|
||||||
|
<Setter Property="Margin" Value="8 0 0 0" />
|
||||||
|
</Style>
|
||||||
|
|
||||||
</Application.Styles>
|
</Application.Styles>
|
||||||
|
|
||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
|
@ -15,24 +15,6 @@
|
|||||||
<namespace:CheckBoxDialogModel />
|
<namespace:CheckBoxDialogModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
|
||||||
<Style Selector="Panel.buttons">
|
|
||||||
<Setter Property="Margin" Value="0 20 0 0" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons > WrapPanel.right">
|
|
||||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons Button">
|
|
||||||
<Setter Property="MinWidth" Value="80" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons > WrapPanel.left Button">
|
|
||||||
<Setter Property="Margin" Value="0 0 8 0" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons > WrapPanel.right Button">
|
|
||||||
<Setter Property="Margin" Value="8 0 0 0" />
|
|
||||||
</Style>
|
|
||||||
</Window.Styles>
|
|
||||||
|
|
||||||
<StackPanel Margin="20">
|
<StackPanel Margin="20">
|
||||||
<ScrollViewer MaxHeight="400">
|
<ScrollViewer MaxHeight="400">
|
||||||
<ItemsRepeater ItemsSource="{Binding Items}">
|
<ItemsRepeater ItemsSource="{Binding Items}">
|
||||||
|
@ -20,24 +20,18 @@
|
|||||||
<Setter Property="FontSize" Value="15" />
|
<Setter Property="FontSize" Value="15" />
|
||||||
<Setter Property="TextWrapping" Value="Wrap" />
|
<Setter Property="TextWrapping" Value="Wrap" />
|
||||||
</Style>
|
</Style>
|
||||||
<Style Selector="WrapPanel">
|
|
||||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
|
||||||
<Setter Property="Margin" Value="0 8 0 0" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Button">
|
|
||||||
<Setter Property="Margin" Value="8 0 0 0" />
|
|
||||||
<Setter Property="MinWidth" Value="80" />
|
|
||||||
</Style>
|
|
||||||
</Window.Styles>
|
</Window.Styles>
|
||||||
|
|
||||||
<StackPanel Margin="20">
|
<StackPanel Margin="20">
|
||||||
<TextBlock Text="{Binding Message}" />
|
<TextBlock Text="{Binding Message}" />
|
||||||
<WrapPanel>
|
<Panel Classes="buttons">
|
||||||
<Button Click="ClickOk" IsVisible="{Binding IsOkVisible}">OK</Button>
|
<WrapPanel Classes="right">
|
||||||
<Button Click="ClickYes" IsVisible="{Binding IsYesVisible}">Yes</Button>
|
<Button Click="ClickOk" IsVisible="{Binding IsOkVisible}">OK</Button>
|
||||||
<Button Click="ClickNo" IsVisible="{Binding IsNoVisible}">No</Button>
|
<Button Click="ClickYes" IsVisible="{Binding IsYesVisible}">Yes</Button>
|
||||||
<Button Click="ClickCancel" IsVisible="{Binding IsCancelVisible}">Cancel</Button>
|
<Button Click="ClickNo" IsVisible="{Binding IsNoVisible}">No</Button>
|
||||||
</WrapPanel>
|
<Button Click="ClickCancel" IsVisible="{Binding IsCancelVisible}">Cancel</Button>
|
||||||
|
</WrapPanel>
|
||||||
|
</Panel>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
</Window>
|
</Window>
|
||||||
|
@ -15,19 +15,6 @@
|
|||||||
<namespace:TextBoxDialogModel />
|
<namespace:TextBoxDialogModel />
|
||||||
</Window.DataContext>
|
</Window.DataContext>
|
||||||
|
|
||||||
<Window.Styles>
|
|
||||||
<Style Selector="Panel.buttons">
|
|
||||||
<Setter Property="Margin" Value="0 20 0 0" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons > WrapPanel.right">
|
|
||||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
|
||||||
</Style>
|
|
||||||
<Style Selector="Panel.buttons Button">
|
|
||||||
<Setter Property="MinWidth" Value="80" />
|
|
||||||
<Setter Property="Margin" Value="8 0 0 0" />
|
|
||||||
</Style>
|
|
||||||
</Window.Styles>
|
|
||||||
|
|
||||||
<StackPanel Margin="20">
|
<StackPanel Margin="20">
|
||||||
<ScrollViewer MaxHeight="400">
|
<ScrollViewer MaxHeight="400">
|
||||||
<StackPanel Spacing="10">
|
<StackPanel Spacing="10">
|
||||||
|
57
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialog.axaml
Normal file
57
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialog.axaml
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<Window xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:namespace="clr-namespace:DHT.Desktop.Main.Dialogs"
|
||||||
|
mc:Ignorable="d" d:DesignWidth="450"
|
||||||
|
x:Class="DHT.Desktop.Main.Dialogs.NewDatabaseSettingsDialog"
|
||||||
|
x:DataType="namespace:NewDatabaseSettingsDialogModel"
|
||||||
|
Title="New Database Settings"
|
||||||
|
Icon="avares://DiscordHistoryTracker/Resources/icon.ico"
|
||||||
|
Closing="OnClosing"
|
||||||
|
Width="450" SizeToContent="Height" CanResize="False"
|
||||||
|
WindowStartupLocation="CenterOwner">
|
||||||
|
|
||||||
|
<Window.DataContext>
|
||||||
|
<namespace:NewDatabaseSettingsDialogModel />
|
||||||
|
</Window.DataContext>
|
||||||
|
|
||||||
|
<Window.Styles>
|
||||||
|
<Style Selector="Label > StackPanel">
|
||||||
|
<Setter Property="Margin" Value="0 4 0 0" />
|
||||||
|
<Setter Property="Orientation" Value="Vertical" />
|
||||||
|
<Setter Property="Spacing" Value="5" />
|
||||||
|
</Style>
|
||||||
|
<Style Selector="TextBlock.dim">
|
||||||
|
<Setter Property="Foreground" Value="DimGray" />
|
||||||
|
<Setter Property="FontSize" Value="13" />
|
||||||
|
</Style>
|
||||||
|
</Window.Styles>
|
||||||
|
|
||||||
|
<StackPanel Orientation="Vertical" Margin="20">
|
||||||
|
<StackPanel Orientation="Vertical" Spacing="15">
|
||||||
|
<CheckBox IsChecked="{Binding SeparateFileForDownloads}">
|
||||||
|
<Label>
|
||||||
|
<StackPanel>
|
||||||
|
<TextBlock>Use a Separate Database File for Downloads</TextBlock>
|
||||||
|
<TextBlock Classes="dim">Downloaded files will be stored separately from messages. This cannot be changed later.</TextBlock>
|
||||||
|
</StackPanel>
|
||||||
|
</Label>
|
||||||
|
</CheckBox>
|
||||||
|
<CheckBox IsChecked="{Binding DownloadsAutoStart}">
|
||||||
|
<Label>
|
||||||
|
<StackPanel>
|
||||||
|
<TextBlock>Download Files Automatically</TextBlock>
|
||||||
|
<TextBlock Classes="dim">Enables automatic downloads of files hosted on Discord. This can be changed later in the Downloads tab.</TextBlock>
|
||||||
|
</StackPanel>
|
||||||
|
</Label>
|
||||||
|
</CheckBox>
|
||||||
|
</StackPanel>
|
||||||
|
<Panel Classes="buttons">
|
||||||
|
<WrapPanel Classes="right">
|
||||||
|
<Button Click="ClickOk">OK</Button>
|
||||||
|
</WrapPanel>
|
||||||
|
</Panel>
|
||||||
|
</StackPanel>
|
||||||
|
|
||||||
|
</Window>
|
22
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialog.axaml.cs
Normal file
22
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialog.axaml.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.Main.Dialogs;
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
|
||||||
|
public sealed partial class NewDatabaseSettingsDialog : Window {
|
||||||
|
public NewDatabaseSettingsDialog() {
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnClosing(object? sender, WindowClosingEventArgs e) {
|
||||||
|
if (!e.IsProgrammatic) {
|
||||||
|
e.Cancel = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ClickOk(object? sender, RoutedEventArgs e) {
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
11
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialogModel.cs
Normal file
11
app/Desktop/Main/Dialogs/NewDatabaseSettingsDialogModel.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
|
||||||
|
namespace DHT.Desktop.Main.Dialogs;
|
||||||
|
|
||||||
|
sealed partial class NewDatabaseSettingsDialogModel : ObservableObject {
|
||||||
|
[ObservableProperty]
|
||||||
|
private bool separateFileForDownloads = true;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
private bool downloadsAutoStart = true;
|
||||||
|
}
|
@ -16,11 +16,5 @@
|
|||||||
<Expander Header="Internal Server Configuration" IsExpanded="True">
|
<Expander Header="Internal Server Configuration" IsExpanded="True">
|
||||||
<controls:ServerConfigurationPanel DataContext="{Binding ServerConfigurationModel}" />
|
<controls:ServerConfigurationPanel DataContext="{Binding ServerConfigurationModel}" />
|
||||||
</Expander>
|
</Expander>
|
||||||
<Expander Header="Database Tools" IsExpanded="True">
|
|
||||||
<StackPanel Orientation="Vertical" Spacing="10">
|
|
||||||
<TextBlock TextWrapping="Wrap">Recreates the database to remove any traces of deleted data, which frees up space and prevents forensic recovery.</TextBlock>
|
|
||||||
<Button Command="{Binding VacuumDatabase}">Vacuum Database</Button>
|
|
||||||
</StackPanel>
|
|
||||||
</Expander>
|
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
|
||||||
using DHT.Desktop.Main.Controls;
|
using DHT.Desktop.Main.Controls;
|
||||||
using DHT.Server;
|
using DHT.Server;
|
||||||
|
|
||||||
@ -11,26 +8,14 @@ namespace DHT.Desktop.Main.Pages;
|
|||||||
sealed class AdvancedPageModel : IDisposable {
|
sealed class AdvancedPageModel : IDisposable {
|
||||||
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
public ServerConfigurationPanelModel ServerConfigurationModel { get; }
|
||||||
|
|
||||||
private readonly Window window;
|
|
||||||
private readonly State state;
|
|
||||||
|
|
||||||
[Obsolete("Designer")]
|
[Obsolete("Designer")]
|
||||||
public AdvancedPageModel() : this(null!, State.Dummy) {}
|
public AdvancedPageModel() : this(null!, State.Dummy) {}
|
||||||
|
|
||||||
public AdvancedPageModel(Window window, State state) {
|
public AdvancedPageModel(Window window, State state) {
|
||||||
this.window = window;
|
|
||||||
this.state = state;
|
|
||||||
|
|
||||||
ServerConfigurationModel = new ServerConfigurationPanelModel(window, state);
|
ServerConfigurationModel = new ServerConfigurationPanelModel(window, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
ServerConfigurationModel.Dispose();
|
ServerConfigurationModel.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task VacuumDatabase() {
|
|
||||||
const string Title = "Vacuum Database";
|
|
||||||
await ProgressDialog.ShowIndeterminate(window, Title, "Vacuuming database...", _ => state.Db.Vacuum());
|
|
||||||
await Dialog.ShowOk(window, Title, "Done.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,12 @@
|
|||||||
<Button Command="{Binding MergeWithDatabase}">Merge with Database(s)...</Button>
|
<Button Command="{Binding MergeWithDatabase}">Merge with Database(s)...</Button>
|
||||||
<Button Command="{Binding ImportLegacyArchive}">Import Legacy Archive(s)...</Button>
|
<Button Command="{Binding ImportLegacyArchive}">Import Legacy Archive(s)...</Button>
|
||||||
</WrapPanel>
|
</WrapPanel>
|
||||||
|
<Expander Header="Advanced Tools" Margin="0 15 0 0">
|
||||||
|
<StackPanel Orientation="Vertical" Spacing="10">
|
||||||
|
<TextBlock TextWrapping="Wrap">Recreate the database to free up space after deleting data.</TextBlock>
|
||||||
|
<Button Command="{Binding VacuumDatabase}">Vacuum Database</Button>
|
||||||
|
</StackPanel>
|
||||||
|
</Expander>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
@ -19,6 +19,7 @@ using DHT.Server.Data;
|
|||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Database.Import;
|
using DHT.Server.Database.Import;
|
||||||
using DHT.Server.Database.Sqlite.Schema;
|
using DHT.Server.Database.Sqlite.Schema;
|
||||||
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Pages;
|
namespace DHT.Desktop.Main.Pages;
|
||||||
@ -54,11 +55,11 @@ sealed class DatabasePageModel {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case PlatformID.Unix:
|
case PlatformID.Unix:
|
||||||
Process.Start("xdg-open", new string[] { folder });
|
Process.Start("xdg-open", [ folder ]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PlatformID.MacOSX:
|
case PlatformID.MacOSX:
|
||||||
Process.Start("open", new string[] { folder });
|
Process.Start("open", [ folder ]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -97,14 +98,11 @@ sealed class DatabasePageModel {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class SchemaUpgradeCallbacks : ISchemaUpgradeCallbacks {
|
private sealed class SchemaUpgradeCallbacks(ProgressDialog dialog, int total) : ISchemaUpgradeCallbacks {
|
||||||
private readonly ProgressDialog dialog;
|
|
||||||
private readonly int total;
|
|
||||||
private bool? decision;
|
private bool? decision;
|
||||||
|
|
||||||
public SchemaUpgradeCallbacks(ProgressDialog dialog, int total) {
|
public Task<InitialDatabaseSettings?> GetInitialDatabaseSettings() {
|
||||||
this.total = total;
|
return Task.FromResult<InitialDatabaseSettings?>(null);
|
||||||
this.dialog = dialog;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> CanUpgrade() {
|
public async Task<bool> CanUpgrade() {
|
||||||
@ -263,4 +261,10 @@ sealed class DatabasePageModel {
|
|||||||
|
|
||||||
return message.ToString();
|
return message.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task VacuumDatabase() {
|
||||||
|
const string Title = "Vacuum Database";
|
||||||
|
await ProgressDialog.ShowIndeterminate(window, Title, "Vacuuming database...", _ => Db.Vacuum());
|
||||||
|
await Dialog.ShowOk(window, Title, "Done.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,48 +165,46 @@ sealed partial class DownloadsPageModel : ObservableObject, IAsyncDisposable {
|
|||||||
downloadStatisticsTask.Post(cancellationToken => state.Db.Downloads.GetStatistics(currentDownloadFilter ?? new DownloadItemFilter(), cancellationToken));
|
downloadStatisticsTask.Post(cancellationToken => state.Db.Downloads.GetStatistics(currentDownloadFilter ?? new DownloadItemFilter(), cancellationToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
private const string DeleteOrphanedDownloadsTitle = "Delete Orphaned Downloads";
|
|
||||||
|
|
||||||
public async Task OnClickDeleteOrphanedDownloads() {
|
public async Task OnClickDeleteOrphanedDownloads() {
|
||||||
await ProgressDialog.Show(window, DeleteOrphanedDownloadsTitle, DeleteOrphanedDownloads);
|
const string Title = "Delete Orphaned Downloads";
|
||||||
}
|
|
||||||
|
|
||||||
private async Task DeleteOrphanedDownloads(ProgressDialog dialog, IProgressCallback callback) {
|
|
||||||
await callback.UpdateIndeterminate("Searching for orphaned downloads...");
|
|
||||||
|
|
||||||
HashSet<string> reachableNormalizedUrls = [];
|
await ProgressDialog.Show(window, Title, async (_, callback) => {
|
||||||
HashSet<string> orphanedNormalizedUrls = [];
|
await callback.UpdateIndeterminate("Searching for orphaned downloads...");
|
||||||
|
|
||||||
await foreach (Download download in state.Db.Downloads.FindAllDownloadableUrls()) {
|
HashSet<string> reachableNormalizedUrls = [];
|
||||||
reachableNormalizedUrls.Add(download.NormalizedUrl);
|
HashSet<string> orphanedNormalizedUrls = [];
|
||||||
}
|
|
||||||
|
await foreach (Download download in state.Db.Downloads.FindAllDownloadableUrls()) {
|
||||||
await foreach (Download download in state.Db.Downloads.Get()) {
|
reachableNormalizedUrls.Add(download.NormalizedUrl);
|
||||||
string normalizedUrl = download.NormalizedUrl;
|
|
||||||
if (!reachableNormalizedUrls.Contains(normalizedUrl)) {
|
|
||||||
orphanedNormalizedUrls.Add(normalizedUrl);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
await foreach (Download download in state.Db.Downloads.Get()) {
|
||||||
if (orphanedNormalizedUrls.Count == 0) {
|
string normalizedUrl = download.NormalizedUrl;
|
||||||
await Dialog.ShowOk(window, DeleteOrphanedDownloadsTitle, "No orphaned downloads found.");
|
if (!reachableNormalizedUrls.Contains(normalizedUrl)) {
|
||||||
return;
|
orphanedNormalizedUrls.Add(normalizedUrl);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (await Dialog.ShowYesNo(window, DeleteOrphanedDownloadsTitle, orphanedNormalizedUrls.Count + " orphaned download(s) will be removed from this database. This action cannot be undone. Proceed?") != DialogResult.YesNo.Yes) {
|
|
||||||
return;
|
if (orphanedNormalizedUrls.Count == 0) {
|
||||||
}
|
await Dialog.ShowOk(window, Title, "No orphaned downloads found.");
|
||||||
|
return;
|
||||||
await callback.UpdateIndeterminate("Deleting orphaned downloads...");
|
}
|
||||||
await state.Db.Downloads.Remove(orphanedNormalizedUrls);
|
|
||||||
RecomputeDownloadStatistics();
|
if (await Dialog.ShowYesNo(window, Title, orphanedNormalizedUrls.Count + " orphaned download(s) will be removed from this database. This action cannot be undone. Proceed?") != DialogResult.YesNo.Yes) {
|
||||||
|
return;
|
||||||
if (await Dialog.ShowYesNo(window, DeleteOrphanedDownloadsTitle, "Orphaned downloads deleted. Vacuum database now to reclaim space?") != DialogResult.YesNo.Yes) {
|
}
|
||||||
return;
|
|
||||||
}
|
await callback.UpdateIndeterminate("Deleting orphaned downloads...");
|
||||||
|
await state.Db.Downloads.Remove(orphanedNormalizedUrls);
|
||||||
await callback.UpdateIndeterminate("Vacuuming database...");
|
RecomputeDownloadStatistics();
|
||||||
await state.Db.Vacuum();
|
|
||||||
|
if (await Dialog.ShowYesNo(window, Title, "Orphaned downloads deleted. Vacuum database now to reclaim space?") != DialogResult.YesNo.Yes) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await callback.UpdateIndeterminate("Vacuuming database...");
|
||||||
|
await state.Db.Vacuum();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateStatistics(DownloadStatusStatistics statusStatistics) {
|
private void UpdateStatistics(DownloadStatusStatistics statusStatistics) {
|
||||||
|
@ -9,9 +9,10 @@ using CommunityToolkit.Mvvm.ComponentModel;
|
|||||||
using DHT.Desktop.Common;
|
using DHT.Desktop.Common;
|
||||||
using DHT.Desktop.Dialogs.Message;
|
using DHT.Desktop.Dialogs.Message;
|
||||||
using DHT.Desktop.Dialogs.Progress;
|
using DHT.Desktop.Dialogs.Progress;
|
||||||
using DHT.Server.Data.Settings;
|
using DHT.Desktop.Main.Dialogs;
|
||||||
using DHT.Server.Database;
|
using DHT.Server.Database;
|
||||||
using DHT.Server.Database.Sqlite.Schema;
|
using DHT.Server.Database.Sqlite.Schema;
|
||||||
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
namespace DHT.Desktop.Main.Screens;
|
namespace DHT.Desktop.Main.Screens;
|
||||||
@ -52,21 +53,22 @@ sealed partial class WelcomeScreenModel : ObservableObject {
|
|||||||
public async Task OpenOrCreateDatabaseFromPath(string path) {
|
public async Task OpenOrCreateDatabaseFromPath(string path) {
|
||||||
dbFilePath = path;
|
dbFilePath = path;
|
||||||
|
|
||||||
bool isNew = !File.Exists(path);
|
|
||||||
|
|
||||||
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, new SchemaUpgradeCallbacks(window));
|
IDatabaseFile? db = await DatabaseGui.TryOpenOrCreateDatabaseFromPath(path, window, new SchemaUpgradeCallbacks(window));
|
||||||
if (db == null) {
|
if (db != null) {
|
||||||
return;
|
DatabaseSelected?.Invoke(this, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNew && await Dialog.ShowYesNo(window, "Automatic Downloads", "Do you want to automatically download files hosted on Discord? You can change this later in the Downloads tab.") == DialogResult.YesNo.Yes) {
|
|
||||||
await db.Settings.Set(SettingsKey.DownloadsAutoStart, value: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
DatabaseSelected?.Invoke(this, db);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class SchemaUpgradeCallbacks(Window window) : ISchemaUpgradeCallbacks {
|
private sealed class SchemaUpgradeCallbacks(Window window) : ISchemaUpgradeCallbacks {
|
||||||
|
public async Task<InitialDatabaseSettings?> GetInitialDatabaseSettings() {
|
||||||
|
var model = new NewDatabaseSettingsDialogModel();
|
||||||
|
|
||||||
|
var dialog = new NewDatabaseSettingsDialog { DataContext = model };
|
||||||
|
await dialog.ShowDialog(window);
|
||||||
|
|
||||||
|
return new InitialDatabaseSettings(model.SeparateFileForDownloads, model.DownloadsAutoStart);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> CanUpgrade() {
|
public async Task<bool> CanUpgrade() {
|
||||||
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
|
return DialogResult.YesNo.Yes == await DatabaseGui.ShowCanUpgradeDatabaseDialog(window);
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis;
|
|||||||
namespace DHT.Server.Data.Settings;
|
namespace DHT.Server.Data.Settings;
|
||||||
|
|
||||||
public static class SettingsKey {
|
public static class SettingsKey {
|
||||||
|
public static Bool SeparateFileForDownloads { get; } = new ("separate_file_for_downloads");
|
||||||
public static Bool DownloadsAutoStart { get; } = new ("downloads_auto_start");
|
public static Bool DownloadsAutoStart { get; } = new ("downloads_auto_start");
|
||||||
public static Bool DownloadsLimitSize { get; } = new ("downloads_limit_size");
|
public static Bool DownloadsLimitSize { get; } = new ("downloads_limit_size");
|
||||||
public static UnsignedLong DownloadsMaximumSize { get; } = new ("downloads_maximum_size");
|
public static UnsignedLong DownloadsMaximumSize { get; } = new ("downloads_maximum_size");
|
||||||
|
@ -18,6 +18,8 @@ namespace DHT.Server.Database.Sqlite.Repositories;
|
|||||||
sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRepository(Log), IDownloadRepository {
|
sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRepository(Log), IDownloadRepository {
|
||||||
private static readonly Log Log = Log.ForType<SqliteDownloadRepository>();
|
private static readonly Log Log = Log.ForType<SqliteDownloadRepository>();
|
||||||
|
|
||||||
|
public const string Schema = "downloads";
|
||||||
|
|
||||||
internal sealed class NewDownloadCollector : IAsyncDisposable {
|
internal sealed class NewDownloadCollector : IAsyncDisposable {
|
||||||
private readonly SqliteDownloadRepository repository;
|
private readonly SqliteDownloadRepository repository;
|
||||||
private bool hasChanged = false;
|
private bool hasChanged = false;
|
||||||
@ -71,7 +73,25 @@ sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRep
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static SqliteBlob BlobReference(ISqliteConnection conn, long rowid, bool readOnly) {
|
||||||
|
string schema = conn.HasAttachedDatabase(Schema) ? Schema : "main";
|
||||||
|
return new SqliteBlob(conn.InnerConnection, databaseName: schema, tableName: "download_blobs", columnName: "blob", rowid, readOnly);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task AddDownload(Data.Download item, Stream? stream) {
|
public async Task AddDownload(Data.Download item, Stream? stream) {
|
||||||
|
ulong? actualSize;
|
||||||
|
|
||||||
|
if (stream is not null) {
|
||||||
|
actualSize = (ulong) stream.Length;
|
||||||
|
|
||||||
|
if (actualSize != item.Size) {
|
||||||
|
Log.Warn("Download size differs from its metadata - metadata size: " + item.Size + " B, actual size: " + actualSize + " B, url: " + item.NormalizedUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
actualSize = item.Size;
|
||||||
|
}
|
||||||
|
|
||||||
await using (var conn = await pool.Take()) {
|
await using (var conn = await pool.Take()) {
|
||||||
await conn.BeginTransactionAsync();
|
await conn.BeginTransactionAsync();
|
||||||
|
|
||||||
@ -87,7 +107,7 @@ sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRep
|
|||||||
metadataCmd.Set(":download_url", item.DownloadUrl);
|
metadataCmd.Set(":download_url", item.DownloadUrl);
|
||||||
metadataCmd.Set(":status", (int) item.Status);
|
metadataCmd.Set(":status", (int) item.Status);
|
||||||
metadataCmd.Set(":type", item.Type);
|
metadataCmd.Set(":type", item.Type);
|
||||||
metadataCmd.Set(":size", item.Size);
|
metadataCmd.Set(":size", actualSize);
|
||||||
await metadataCmd.ExecuteNonQueryAsync();
|
await metadataCmd.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
if (stream == null) {
|
if (stream == null) {
|
||||||
@ -107,10 +127,10 @@ sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRep
|
|||||||
);
|
);
|
||||||
|
|
||||||
upsertBlobCmd.AddAndSet(":normalized_url", SqliteType.Text, item.NormalizedUrl);
|
upsertBlobCmd.AddAndSet(":normalized_url", SqliteType.Text, item.NormalizedUrl);
|
||||||
upsertBlobCmd.AddAndSet(":blob_length", SqliteType.Integer, item.Size);
|
upsertBlobCmd.AddAndSet(":blob_length", SqliteType.Integer, actualSize);
|
||||||
long rowid = await upsertBlobCmd.ExecuteLongScalarAsync();
|
long rowid = await upsertBlobCmd.ExecuteLongScalarAsync();
|
||||||
|
|
||||||
await using var blob = new SqliteBlob(conn.InnerConnection, "download_blobs", "blob", rowid);
|
await using var blob = BlobReference(conn, rowid, readOnly: false);
|
||||||
await stream.CopyToAsync(blob);
|
await stream.CopyToAsync(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,7 +234,7 @@ sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRep
|
|||||||
rowid = reader.GetInt64(0);
|
rowid = reader.GetInt64(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
await using (var blob = new SqliteBlob(conn.InnerConnection, "download_blobs", "blob", rowid, readOnly: true)) {
|
await using (var blob = BlobReference(conn, rowid, readOnly: true)) {
|
||||||
await dataProcessor(blob);
|
await dataProcessor(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,7 +269,7 @@ sealed class SqliteDownloadRepository(SqliteConnectionPool pool) : BaseSqliteRep
|
|||||||
rowid = reader.GetInt64(2);
|
rowid = reader.GetInt64(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
await using (var blob = new SqliteBlob(conn.InnerConnection, "download_blobs", "blob", rowid, readOnly: true)) {
|
await using (var blob = BlobReference(conn, rowid, readOnly: true)) {
|
||||||
await dataProcessor(new Data.Download(normalizedUrl, downloadUrl, DownloadStatus.Success, type, (ulong) blob.Length), blob, cancellationToken);
|
await dataProcessor(new Data.Download(normalizedUrl, downloadUrl, DownloadStatus.Success, type, (ulong) blob.Length), blob, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,10 +43,14 @@ sealed class SqliteSettingsRepository(SqliteConnectionPool pool) : ISettingsRepo
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<T?> Get<T>(SettingsKey<T> key, T? defaultValue) {
|
public async Task<T?> Get<T>(SettingsKey<T> key, T? defaultValue) {
|
||||||
|
await using var conn = await pool.Take();
|
||||||
|
return await Get(conn, key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<T?> Get<T>(ISqliteConnection conn, SettingsKey<T> key, T? defaultValue) {
|
||||||
string? value;
|
string? value;
|
||||||
|
|
||||||
await using (var conn = await pool.Take()) {
|
await using (var cmd = conn.Command("SELECT value FROM metadata WHERE key = :key")) {
|
||||||
await using var cmd = conn.Command("SELECT value FROM metadata WHERE key = :key");
|
|
||||||
cmd.AddAndSet(":key", SqliteType.Text, key.Key);
|
cmd.AddAndSet(":key", SqliteType.Text, key.Key);
|
||||||
|
|
||||||
await using var reader = await cmd.ExecuteReaderAsync();
|
await using var reader = await cmd.ExecuteReaderAsync();
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
namespace DHT.Server.Database.Sqlite.Schema;
|
namespace DHT.Server.Database.Sqlite.Schema;
|
||||||
|
|
||||||
public interface ISchemaUpgradeCallbacks {
|
public interface ISchemaUpgradeCallbacks {
|
||||||
|
Task<InitialDatabaseSettings?> GetInitialDatabaseSettings();
|
||||||
Task<bool> CanUpgrade();
|
Task<bool> CanUpgrade();
|
||||||
Task Start(int versionSteps, Func<IProgressReporter, Task> doUpgrade);
|
Task Start(int versionSteps, Func<IProgressReporter, Task> doUpgrade);
|
||||||
|
|
||||||
|
@ -1,42 +1,46 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DHT.Server.Data.Settings;
|
||||||
using DHT.Server.Database.Repositories;
|
using DHT.Server.Database.Repositories;
|
||||||
using DHT.Server.Database.Sqlite.Repositories;
|
using DHT.Server.Database.Sqlite.Repositories;
|
||||||
using DHT.Server.Database.Sqlite.Schema;
|
using DHT.Server.Database.Sqlite.Schema;
|
||||||
using DHT.Server.Database.Sqlite.Utils;
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
using Microsoft.Data.Sqlite;
|
using DHT.Utils.Logging;
|
||||||
|
|
||||||
namespace DHT.Server.Database.Sqlite;
|
namespace DHT.Server.Database.Sqlite;
|
||||||
|
|
||||||
public sealed class SqliteDatabaseFile : IDatabaseFile {
|
public sealed class SqliteDatabaseFile : IDatabaseFile {
|
||||||
|
private static readonly Log Log = Log.ForType<SqliteDatabaseFile>();
|
||||||
|
|
||||||
private const int DefaultPoolSize = 5;
|
private const int DefaultPoolSize = 5;
|
||||||
|
|
||||||
public static async Task<SqliteDatabaseFile?> OpenOrCreate(string path, ISchemaUpgradeCallbacks schemaUpgradeCallbacks) {
|
public static async Task<SqliteDatabaseFile?> OpenOrCreate(string path, ISchemaUpgradeCallbacks schemaUpgradeCallbacks) {
|
||||||
var connectionString = new SqliteConnectionStringBuilder {
|
var connectionString = new SqliteConnectionStringFactory(path);
|
||||||
DataSource = path,
|
var attachedDatabaseCollector = new AttachedDatabaseCollector(path);
|
||||||
Mode = SqliteOpenMode.ReadWriteCreate,
|
|
||||||
};
|
|
||||||
|
|
||||||
var pool = await SqliteConnectionPool.Create(connectionString, DefaultPoolSize);
|
|
||||||
bool wasOpened;
|
bool wasOpened;
|
||||||
|
await using (var conn = await CustomSqliteConnection.OpenUnpooled(connectionString)) {
|
||||||
try {
|
wasOpened = await new SqliteSchema(conn).Setup(attachedDatabaseCollector, schemaUpgradeCallbacks);
|
||||||
await using var conn = await pool.Take();
|
|
||||||
wasOpened = await new SqliteSchema(conn).Setup(schemaUpgradeCallbacks);
|
|
||||||
} catch (Exception) {
|
|
||||||
await pool.DisposeAsync();
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wasOpened) {
|
if (wasOpened) {
|
||||||
|
var pool = await SqliteConnectionPool.Create(connectionString, DefaultPoolSize, attachedDatabaseCollector);
|
||||||
return new SqliteDatabaseFile(path, pool);
|
return new SqliteDatabaseFile(path, pool);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await pool.DisposeAsync();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sealed class AttachedDatabaseCollector(string path) : ISqliteAttachedDatabaseCollector {
|
||||||
|
public async IAsyncEnumerable<AttachedDatabase> GetAttachedDatabases(ISqliteConnection conn) {
|
||||||
|
bool useSeparateFileForDownloads = await SqliteSettingsRepository.Get(conn, SettingsKey.SeparateFileForDownloads, defaultValue: false);
|
||||||
|
if (useSeparateFileForDownloads) {
|
||||||
|
yield return new AttachedDatabase(path + "_downloads", SqliteDownloadRepository.Schema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string Path { get; }
|
public string Path { get; }
|
||||||
|
|
||||||
public ISettingsRepository Settings => settings;
|
public ISettingsRepository Settings => settings;
|
||||||
@ -78,6 +82,21 @@ public sealed class SqliteDatabaseFile : IDatabaseFile {
|
|||||||
|
|
||||||
public async Task Vacuum() {
|
public async Task Vacuum() {
|
||||||
await using var conn = await pool.Take();
|
await using var conn = await pool.Take();
|
||||||
|
|
||||||
|
Perf perf = Log.Start();
|
||||||
await conn.ExecuteAsync("VACUUM");
|
await conn.ExecuteAsync("VACUUM");
|
||||||
|
perf.Step("Vacuum main schema");
|
||||||
|
|
||||||
|
await VacuumAttachedDatabase(conn, perf, SqliteDownloadRepository.Schema);
|
||||||
|
|
||||||
|
perf.End();
|
||||||
|
return;
|
||||||
|
|
||||||
|
static async Task VacuumAttachedDatabase(ISqliteConnection conn, Perf perf, string schema) {
|
||||||
|
if (conn.HasAttachedDatabase(schema)) {
|
||||||
|
await conn.ExecuteAsync("VACUUM " + schema);
|
||||||
|
perf.Step("Vacuum " + schema + " schema");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,32 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using DHT.Server.Data.Settings;
|
||||||
using DHT.Server.Database.Exceptions;
|
using DHT.Server.Database.Exceptions;
|
||||||
|
using DHT.Server.Database.Sqlite.Repositories;
|
||||||
using DHT.Server.Database.Sqlite.Schema;
|
using DHT.Server.Database.Sqlite.Schema;
|
||||||
using DHT.Server.Database.Sqlite.Utils;
|
using DHT.Server.Database.Sqlite.Utils;
|
||||||
using DHT.Utils.Logging;
|
using DHT.Utils.Logging;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
namespace DHT.Server.Database.Sqlite;
|
namespace DHT.Server.Database.Sqlite;
|
||||||
|
|
||||||
sealed class SqliteSchema {
|
sealed class SqliteSchema(CustomSqliteConnection conn) {
|
||||||
internal const int Version = 10;
|
internal const int Version = 10;
|
||||||
|
|
||||||
private static readonly Log Log = Log.ForType<SqliteSchema>();
|
private static readonly Log Log = Log.ForType<SqliteSchema>();
|
||||||
|
|
||||||
private readonly ISqliteConnection conn;
|
public async Task<bool> Setup(ISqliteAttachedDatabaseCollector attachedDatabaseCollector, ISchemaUpgradeCallbacks callbacks) {
|
||||||
|
|
||||||
public SqliteSchema(ISqliteConnection conn) {
|
|
||||||
this.conn = conn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> Setup(ISchemaUpgradeCallbacks callbacks) {
|
|
||||||
await conn.ExecuteAsync("CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT)");
|
await conn.ExecuteAsync("CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT)");
|
||||||
|
|
||||||
string? dbVersionStr = await conn.ExecuteReaderAsync("SELECT value FROM metadata WHERE key = 'version'", static reader => reader?.GetString(0));
|
string? dbVersionStr = await conn.ExecuteReaderAsync("SELECT value FROM metadata WHERE key = 'version'", static reader => reader?.GetString(0));
|
||||||
if (dbVersionStr == null) {
|
if (dbVersionStr == null) {
|
||||||
|
if (await callbacks.GetInitialDatabaseSettings() is {} initialSettings) {
|
||||||
|
await ApplyInitialSettings(initialSettings);
|
||||||
|
await AttachAdditionalDatabasesIfNecessary(attachedDatabaseCollector);
|
||||||
|
}
|
||||||
|
|
||||||
await InitializeSchemas();
|
await InitializeSchemas();
|
||||||
}
|
}
|
||||||
else if (!int.TryParse(dbVersionStr, out int dbVersion) || dbVersion < 1) {
|
else if (!int.TryParse(dbVersionStr, out int dbVersion) || dbVersion < 1) {
|
||||||
@ -37,12 +41,44 @@ sealed class SqliteSchema {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await AttachAdditionalDatabasesIfNecessary(attachedDatabaseCollector);
|
||||||
await callbacks.Start(Version - dbVersion, async reporter => await UpgradeSchemas(dbVersion, reporter));
|
await callbacks.Start(Version - dbVersion, async reporter => await UpgradeSchemas(dbVersion, reporter));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task ApplyInitialSettings(InitialDatabaseSettings initialSettings) {
|
||||||
|
await using var cmd = conn.Command("INSERT INTO metadata (key, value) VALUES (:key, :value)");
|
||||||
|
cmd.Add("key", SqliteType.Text);
|
||||||
|
cmd.Add("value", SqliteType.Text);
|
||||||
|
|
||||||
|
static async Task ApplySetting<T>(SqliteCommand cmd, SettingsKey<T> key, T value) {
|
||||||
|
cmd.Set("key", key.Key);
|
||||||
|
cmd.Set("value", key.ToString(value));
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (initialSettings.SeparateFileForDownloads) {
|
||||||
|
await ApplySetting(cmd, SettingsKey.SeparateFileForDownloads, value: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (initialSettings.DownloadsAutoStart) {
|
||||||
|
await ApplySetting(cmd, SettingsKey.DownloadsAutoStart, value: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "WithExpressionModifiesAllMembers")]
|
||||||
|
private async Task AttachAdditionalDatabasesIfNecessary(ISqliteAttachedDatabaseCollector attachedDatabaseCollector) {
|
||||||
|
await foreach (var attachedDatabase in attachedDatabaseCollector.GetAttachedDatabases(conn)) {
|
||||||
|
if (!File.Exists(attachedDatabase.Path)) {
|
||||||
|
await using CustomSqliteConnection _ = await CustomSqliteConnection.OpenUnpooled(conn.ConnectionStringFactory with { Path = attachedDatabase.Path });
|
||||||
|
}
|
||||||
|
|
||||||
|
await conn.AttachDatabase(attachedDatabase);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task InitializeSchemas() {
|
private async Task InitializeSchemas() {
|
||||||
await conn.ExecuteAsync("""
|
await conn.ExecuteAsync("""
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
@ -142,23 +178,25 @@ sealed class SqliteSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task CreateDownloadTables(ISqliteConnection conn) {
|
internal static async Task CreateDownloadTables(ISqliteConnection conn) {
|
||||||
await conn.ExecuteAsync("""
|
string schema = conn.HasAttachedDatabase(SqliteDownloadRepository.Schema) ? SqliteDownloadRepository.Schema : "main";
|
||||||
CREATE TABLE download_metadata (
|
|
||||||
normalized_url TEXT NOT NULL PRIMARY KEY,
|
|
||||||
download_url TEXT NOT NULL,
|
|
||||||
status INTEGER NOT NULL,
|
|
||||||
type TEXT,
|
|
||||||
size INTEGER
|
|
||||||
)
|
|
||||||
""");
|
|
||||||
|
|
||||||
await conn.ExecuteAsync("""
|
await conn.ExecuteAsync($"""
|
||||||
CREATE TABLE download_blobs (
|
CREATE TABLE {schema}.download_metadata (
|
||||||
normalized_url TEXT NOT NULL PRIMARY KEY,
|
normalized_url TEXT NOT NULL PRIMARY KEY,
|
||||||
blob BLOB NOT NULL,
|
download_url TEXT NOT NULL,
|
||||||
FOREIGN KEY (normalized_url) REFERENCES download_metadata (normalized_url) ON UPDATE CASCADE ON DELETE CASCADE
|
status INTEGER NOT NULL,
|
||||||
)
|
type TEXT,
|
||||||
""");
|
size INTEGER
|
||||||
|
)
|
||||||
|
""");
|
||||||
|
|
||||||
|
await conn.ExecuteAsync($"""
|
||||||
|
CREATE TABLE {schema}.download_blobs (
|
||||||
|
normalized_url TEXT NOT NULL PRIMARY KEY,
|
||||||
|
blob BLOB NOT NULL,
|
||||||
|
FOREIGN KEY (normalized_url) REFERENCES download_metadata (normalized_url) ON UPDATE CASCADE ON DELETE CASCADE
|
||||||
|
)
|
||||||
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task CreateMessageAttachmentsTable(ISqliteConnection conn) {
|
internal static async Task CreateMessageAttachmentsTable(ISqliteConnection conn) {
|
||||||
|
3
app/Server/Database/Sqlite/Utils/AttachedDatabase.cs
Normal file
3
app/Server/Database/Sqlite/Utils/AttachedDatabase.cs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
sealed record AttachedDatabase(string Path, string Schema);
|
91
app/Server/Database/Sqlite/Utils/CustomSqliteConnection.cs
Normal file
91
app/Server/Database/Sqlite/Utils/CustomSqliteConnection.cs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
class CustomSqliteConnection : ISqliteConnection {
|
||||||
|
internal static async Task<CustomSqliteConnection> OpenUnpooled(SqliteConnectionStringFactory connectionStringFactory) {
|
||||||
|
SqliteConnection conn = new SqliteConnection(connectionStringFactory.Create());
|
||||||
|
await conn.OpenAsync();
|
||||||
|
|
||||||
|
CustomSqliteConnection custom = new CustomSqliteConnection(connectionStringFactory, conn);
|
||||||
|
await custom.Setup(CancellationToken.None);
|
||||||
|
|
||||||
|
return custom;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal SqliteConnectionStringFactory ConnectionStringFactory { get; }
|
||||||
|
public SqliteConnection InnerConnection { get; }
|
||||||
|
|
||||||
|
private ImmutableHashSet<string> attachedDatabases = [];
|
||||||
|
private DbTransaction? activeTransaction;
|
||||||
|
|
||||||
|
protected CustomSqliteConnection(SqliteConnectionStringFactory connectionStringFactory, SqliteConnection innerConnection) {
|
||||||
|
this.ConnectionStringFactory = connectionStringFactory;
|
||||||
|
this.InnerConnection = innerConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal async Task Setup(CancellationToken cancellationToken) {
|
||||||
|
await this.ExecuteAsync("PRAGMA journal_mode=WAL", cancellationToken);
|
||||||
|
await this.ExecuteAsync("PRAGMA foreign_keys=ON", cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SuppressMessage("ReSharper", "WithExpressionModifiesAllMembers")]
|
||||||
|
internal async Task AttachDatabase(AttachedDatabase attachedDatabase) {
|
||||||
|
await this.ExecuteAsync("ATTACH DATABASE '" + attachedDatabase.Path.Replace("'", "''") + "' AS " + attachedDatabase.Schema);
|
||||||
|
ImmutableInterlocked.Update(ref attachedDatabases, static (set, schema) => set.Add(schema), attachedDatabase.Schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasAttachedDatabase(string schema) {
|
||||||
|
return attachedDatabases.Contains(schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task BeginTransactionAsync() {
|
||||||
|
if (activeTransaction != null) {
|
||||||
|
throw new InvalidOperationException("A transaction is already active.");
|
||||||
|
}
|
||||||
|
|
||||||
|
activeTransaction = await InnerConnection.BeginTransactionAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CommitTransactionAsync() {
|
||||||
|
if (activeTransaction == null) {
|
||||||
|
throw new InvalidOperationException("No active transaction to commit.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await activeTransaction.CommitAsync();
|
||||||
|
await activeTransaction.DisposeAsync();
|
||||||
|
activeTransaction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task RollbackTransactionAsync() {
|
||||||
|
if (activeTransaction == null) {
|
||||||
|
throw new InvalidOperationException("No active transaction to rollback.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await activeTransaction.RollbackAsync();
|
||||||
|
await activeTransaction.DisposeAsync();
|
||||||
|
activeTransaction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AssignActiveTransaction(SqliteCommand command) {
|
||||||
|
command.Transaction = (SqliteTransaction?) activeTransaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async ValueTask DisposeAsync() {
|
||||||
|
if (activeTransaction != null) {
|
||||||
|
await RollbackTransactionAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
await DisposeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private protected virtual async ValueTask DisposeConnection() {
|
||||||
|
await InnerConnection.DisposeAsync();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
interface ISqliteAttachedDatabaseCollector {
|
||||||
|
IAsyncEnumerable<AttachedDatabase> GetAttachedDatabases(ISqliteConnection conn);
|
||||||
|
}
|
@ -7,6 +7,8 @@ namespace DHT.Server.Database.Sqlite.Utils;
|
|||||||
interface ISqliteConnection : IAsyncDisposable {
|
interface ISqliteConnection : IAsyncDisposable {
|
||||||
SqliteConnection InnerConnection { get; }
|
SqliteConnection InnerConnection { get; }
|
||||||
|
|
||||||
|
bool HasAttachedDatabase(string schema);
|
||||||
|
|
||||||
Task BeginTransactionAsync();
|
Task BeginTransactionAsync();
|
||||||
Task CommitTransactionAsync();
|
Task CommitTransactionAsync();
|
||||||
Task RollbackTransactionAsync();
|
Task RollbackTransactionAsync();
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
public sealed record InitialDatabaseSettings(bool SeparateFileForDownloads, bool DownloadsAutoStart);
|
@ -1,6 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using DHT.Utils.Collections;
|
using DHT.Utils.Collections;
|
||||||
@ -9,16 +9,13 @@ using Microsoft.Data.Sqlite;
|
|||||||
namespace DHT.Server.Database.Sqlite.Utils;
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
sealed class SqliteConnectionPool : IAsyncDisposable {
|
sealed class SqliteConnectionPool : IAsyncDisposable {
|
||||||
public static async Task<SqliteConnectionPool> Create(SqliteConnectionStringBuilder connectionStringBuilder, int poolSize) {
|
public static async Task<SqliteConnectionPool> Create(SqliteConnectionStringFactory connectionStringFactory, int poolSize, ISqliteAttachedDatabaseCollector attachedDatabaseCollector) {
|
||||||
var pool = new SqliteConnectionPool(poolSize);
|
SqliteConnectionPool pool = new SqliteConnectionPool(poolSize, attachedDatabaseCollector);
|
||||||
await pool.InitializePooledConnections(connectionStringBuilder);
|
await pool.InitializePooledConnections(connectionStringFactory);
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetConnectionString(SqliteConnectionStringBuilder connectionStringBuilder) {
|
private readonly ISqliteAttachedDatabaseCollector attachedDatabaseCollector;
|
||||||
connectionStringBuilder.Pooling = false;
|
|
||||||
return connectionStringBuilder.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly int poolSize;
|
private readonly int poolSize;
|
||||||
private readonly List<PooledConnection> all;
|
private readonly List<PooledConnection> all;
|
||||||
@ -27,24 +24,35 @@ sealed class SqliteConnectionPool : IAsyncDisposable {
|
|||||||
private readonly CancellationTokenSource disposalTokenSource = new ();
|
private readonly CancellationTokenSource disposalTokenSource = new ();
|
||||||
private readonly CancellationToken disposalToken;
|
private readonly CancellationToken disposalToken;
|
||||||
|
|
||||||
private SqliteConnectionPool(int poolSize) {
|
private SqliteConnectionPool(int poolSize, ISqliteAttachedDatabaseCollector attachedDatabaseCollector) {
|
||||||
|
this.attachedDatabaseCollector = attachedDatabaseCollector;
|
||||||
|
|
||||||
this.poolSize = poolSize;
|
this.poolSize = poolSize;
|
||||||
this.all = new List<PooledConnection>(poolSize);
|
this.all = new List<PooledConnection>(poolSize);
|
||||||
this.free = new ConcurrentPool<PooledConnection>(poolSize);
|
this.free = new ConcurrentPool<PooledConnection>(poolSize);
|
||||||
|
|
||||||
this.disposalToken = disposalTokenSource.Token;
|
this.disposalToken = disposalTokenSource.Token;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task InitializePooledConnections(SqliteConnectionStringBuilder connectionStringBuilder) {
|
private async Task InitializePooledConnections(SqliteConnectionStringFactory connectionStringFactory) {
|
||||||
string connectionString = GetConnectionString(connectionStringBuilder);
|
string connectionString = connectionStringFactory.Create();
|
||||||
|
|
||||||
|
List<AttachedDatabase> attachedDatabases = [];
|
||||||
|
|
||||||
for (int i = 0; i < poolSize; i++) {
|
for (int i = 0; i < poolSize; i++) {
|
||||||
var conn = new SqliteConnection(connectionString);
|
SqliteConnection innerConnection = new SqliteConnection(connectionString);
|
||||||
conn.Open();
|
await innerConnection.OpenAsync(disposalToken);
|
||||||
|
|
||||||
var pooledConnection = new PooledConnection(this, conn);
|
PooledConnection pooledConnection = new PooledConnection(this, connectionStringFactory, innerConnection);
|
||||||
|
await pooledConnection.Setup(disposalToken);
|
||||||
|
|
||||||
await pooledConnection.ExecuteAsync("PRAGMA journal_mode=WAL", disposalToken);
|
if (i == 0) {
|
||||||
await pooledConnection.ExecuteAsync("PRAGMA foreign_keys=ON", disposalToken);
|
attachedDatabases = await attachedDatabaseCollector.GetAttachedDatabases(pooledConnection).ToListAsync(disposalToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var attachedDatabase in attachedDatabases) {
|
||||||
|
await pooledConnection.AttachDatabase(attachedDatabase);
|
||||||
|
}
|
||||||
|
|
||||||
all.Add(pooledConnection);
|
all.Add(pooledConnection);
|
||||||
await free.Push(pooledConnection, disposalToken);
|
await free.Push(pooledConnection, disposalToken);
|
||||||
@ -74,48 +82,8 @@ sealed class SqliteConnectionPool : IAsyncDisposable {
|
|||||||
disposalTokenSource.Dispose();
|
disposalTokenSource.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class PooledConnection(SqliteConnectionPool pool, SqliteConnection conn) : ISqliteConnection {
|
private sealed class PooledConnection(SqliteConnectionPool pool, SqliteConnectionStringFactory connectionStringFactory, SqliteConnection innerConnection) : CustomSqliteConnection(connectionStringFactory, innerConnection) {
|
||||||
public SqliteConnection InnerConnection { get; } = conn;
|
private protected override async ValueTask DisposeConnection() {
|
||||||
|
|
||||||
private DbTransaction? activeTransaction;
|
|
||||||
|
|
||||||
public async Task BeginTransactionAsync() {
|
|
||||||
if (activeTransaction != null) {
|
|
||||||
throw new InvalidOperationException("A transaction is already active.");
|
|
||||||
}
|
|
||||||
|
|
||||||
activeTransaction = await InnerConnection.BeginTransactionAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task CommitTransactionAsync() {
|
|
||||||
if (activeTransaction == null) {
|
|
||||||
throw new InvalidOperationException("No active transaction to commit.");
|
|
||||||
}
|
|
||||||
|
|
||||||
await activeTransaction.CommitAsync();
|
|
||||||
await activeTransaction.DisposeAsync();
|
|
||||||
activeTransaction = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task RollbackTransactionAsync() {
|
|
||||||
if (activeTransaction == null) {
|
|
||||||
throw new InvalidOperationException("No active transaction to rollback.");
|
|
||||||
}
|
|
||||||
|
|
||||||
await activeTransaction.RollbackAsync();
|
|
||||||
await activeTransaction.DisposeAsync();
|
|
||||||
activeTransaction = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AssignActiveTransaction(SqliteCommand command) {
|
|
||||||
command.Transaction = (SqliteTransaction?) activeTransaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask DisposeAsync() {
|
|
||||||
if (activeTransaction != null) {
|
|
||||||
await RollbackTransactionAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
await pool.Return(this);
|
await pool.Return(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
|
||||||
|
namespace DHT.Server.Database.Sqlite.Utils;
|
||||||
|
|
||||||
|
sealed record SqliteConnectionStringFactory(string Path) {
|
||||||
|
public string Create() {
|
||||||
|
var builder = new SqliteConnectionStringBuilder {
|
||||||
|
DataSource = Path,
|
||||||
|
Mode = SqliteOpenMode.ReadWriteCreate,
|
||||||
|
Pooling = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
return builder.ToString();
|
||||||
|
}
|
||||||
|
}
|
@ -55,7 +55,10 @@ public sealed class DelayedThrottledTask<T> : IDisposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public void Dispose() {
|
||||||
|
try {
|
||||||
|
cancellationTokenSource.Cancel();
|
||||||
|
} catch (ObjectDisposedException) {}
|
||||||
|
|
||||||
taskChannel.Writer.Complete();
|
taskChannel.Writer.Complete();
|
||||||
cancellationTokenSource.Cancel();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user