2025-03-10 22:01:14 +01:00
// Copyright (C) 2025, The Duplicati Team
2024-02-28 15:45:30 +01:00
// https://duplicati.com, hello@duplicati.com
2025-05-19 20:38:34 +02:00
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
2024-02-28 15:45:30 +01:00
// Software is furnished to do so, subject to the following conditions:
2025-05-19 20:38:34 +02:00
//
// The above copyright notice and this permission notice shall be included in
2024-02-28 15:45:30 +01:00
// all copies or substantial portions of the Software.
2025-05-19 20:38:34 +02:00
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2024-02-28 15:45:30 +01:00
// DEALINGS IN THE SOFTWARE.
2013-03-27 16:06:45 +01:00
2025-04-03 15:24:57 +02:00
#nullable enable
2013-03-27 16:06:45 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2025-05-19 20:43:45 +02:00
using System.Threading.Tasks ;
2019-12-09 20:31:15 -08:00
using Duplicati.Library.Interface ;
2025-05-19 20:43:45 +02:00
using Microsoft.Data.Sqlite ;
2013-03-27 16:06:45 +01:00
2013-05-08 20:17:07 +02:00
namespace Duplicati.Library.Main.Database
2013-03-27 16:06:45 +01:00
{
2016-09-15 11:39:27 +02:00
internal class LocalDeleteDatabase : LocalDatabase
{
2018-03-12 14:07:11 +01:00
/// <summary>
/// The tag used for logging
/// </summary>
private static readonly string LOGTAG = Logging . Log . LogTagFromType < LocalDeleteDatabase >();
2024-11-01 14:50:26 +01:00
/// <summary>
/// Flag for toggling temporary tables; set to empty string if debugging
/// </summary>
private const string TEMPORARY = "TEMPORARY" ;
2025-04-03 15:24:57 +02:00
private const string REGISTER_COMMAND = @"INSERT OR IGNORE INTO ""DuplicateBlock"" (""BlockID"", ""VolumeID"") SELECT ""ID"", @VolumeId FROM ""Block"" WHERE ""Hash"" = @Hash AND ""Size"" = @Size " ;
2025-05-19 20:43:45 +02:00
private SqliteCommand m_registerDuplicateBlockCommand = null !;
2013-03-27 16:06:45 +01:00
2025-05-21 07:24:36 +02:00
public static async Task < LocalDeleteDatabase > CreateAsync ( string path , string operation , long pagecachesize , LocalDeleteDatabase ? dbnew = null )
2016-09-15 11:39:27 +02:00
{
2025-05-21 07:24:36 +02:00
dbnew ??= new LocalDeleteDatabase ();
2025-05-19 20:43:45 +02:00
2025-05-21 07:24:36 +02:00
dbnew = ( LocalDeleteDatabase ) await CreateLocalDatabaseAsync ( path , operation , true , pagecachesize , dbnew );
2025-05-19 20:43:45 +02:00
2025-05-21 07:24:36 +02:00
dbnew . m_registerDuplicateBlockCommand = await dbnew . Connection . CreateCommandAsync ( REGISTER_COMMAND );
return dbnew ;
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2025-05-21 07:24:36 +02:00
// TODO Second argumnet is optional, as the original constructor call chain will have initialized the object if the constructor is called from another constructor. If not, the constructor will instantiate a new object.
public static async Task < LocalDeleteDatabase > CreateAsync ( LocalDatabase dbparent , LocalDeleteDatabase ? dbnew = null )
2016-09-15 11:39:27 +02:00
{
2025-05-21 07:24:36 +02:00
dbnew ??= new LocalDeleteDatabase ();
2025-05-19 20:43:45 +02:00
dbnew = ( LocalDeleteDatabase ) await CreateLocalDatabaseAsync ( dbparent , dbnew );
dbnew . m_registerDuplicateBlockCommand = await dbnew . Connection . CreateCommandAsync ( REGISTER_COMMAND );
return dbnew ;
2016-09-15 11:39:27 +02:00
}
2018-10-14 20:10:32 -07:00
2016-09-15 11:39:27 +02:00
/// <summary>
/// Drops all entries related to operations listed in the table.
/// </summary>
/// <param name="toDelete">The fileset entries to delete</param>
/// <param name="transaction">The transaction to execute the commands in</param>
/// <returns>A list of filesets to delete</returns>
2025-05-19 21:51:28 +02:00
public async IAsyncEnumerable < KeyValuePair < string , long >> DropFilesetsFromTable ( DateTime [] toDelete )
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
using ( var cmd = m_connection . CreateCommand ( m_rtr ))
2016-09-15 11:39:27 +02:00
{
2019-07-31 07:09:37 -07:00
var deleted = 0 ;
2025-05-19 21:51:28 +02:00
using ( var tempTable = await TemporaryDbValueList . CreateAsync ( this , toDelete . Select ( Library . Utility . Utility . NormalizeDateTimeToEpochSeconds )))
deleted += await ( await cmd . SetCommandAndParameters ( @"
DELETE FROM ""Fileset""
WHERE ""Timestamp"" IN (@Timestamps)
" )
. ExpandInClauseParameterAsync ( "@Timestamps" , tempTable ))
. ExecuteNonQueryAsync ();
2019-07-31 07:09:37 -07:00
2016-09-15 11:39:27 +02:00
if ( deleted != toDelete . Length )
2025-03-13 23:06:32 +01:00
throw new Exception ( $"Unexpected number of deleted filesets {deleted} vs {toDelete.Length}" );
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
//Then we delete anything that is no longer being referenced
2025-05-19 21:51:28 +02:00
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""FilesetEntry""
WHERE ""FilesetID"" NOT IN (
SELECT DISTINCT ""ID""
FROM ""Fileset""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""ChangeJournalData""
WHERE ""FilesetID"" NOT IN (
SELECT DISTINCT ""ID""
FROM ""Fileset""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""FileLookup""
WHERE ""ID"" NOT IN (
SELECT DISTINCT ""FileID""
FROM ""FilesetEntry""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""Metadataset""
WHERE ""ID"" NOT IN (
SELECT DISTINCT ""MetadataID""
FROM ""FileLookup""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""Blockset""
WHERE ""ID"" NOT IN (
SELECT DISTINCT ""BlocksetID""
FROM ""FileLookup""
UNION
SELECT DISTINCT ""BlocksetID""
FROM ""Metadataset""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""BlocksetEntry""
WHERE ""BlocksetID"" NOT IN (
SELECT DISTINCT ""ID""
FROM ""Blockset""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""BlocklistHash""
WHERE ""BlocksetID"" NOT IN (
SELECT DISTINCT ""ID""
FROM ""Blockset""
)
" );
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
//We save the block info for the remote files, before we delete it
2025-05-19 21:51:28 +02:00
await cmd . ExecuteNonQueryAsync ( @"
INSERT INTO ""DeletedBlock"" (
""Hash"",
""Size"",
""VolumeID""
)
SELECT
""Hash"",
""Size"",
""VolumeID""
FROM ""Block""
WHERE ""ID"" NOT IN (
SELECT DISTINCT ""BlockID"" AS ""BlockID""
FROM ""BlocksetEntry""
UNION
SELECT DISTINCT ""ID""
FROM
""Block"",
""BlocklistHash""
WHERE ""Block"".""Hash"" = ""BlocklistHash"".""Hash""
)
" );
await cmd . ExecuteNonQueryAsync ( @"
DELETE FROM ""Block""
WHERE ""ID"" NOT IN (
SELECT DISTINCT ""BlockID""
FROM ""BlocksetEntry""
UNION
SELECT DISTINCT ""ID""
FROM
""Block"",
""BlocklistHash""
WHERE ""Block"".""Hash"" = ""BlocklistHash"".""Hash""
)
" );
2020-02-23 13:23:21 -06:00
2025-03-10 22:01:14 +01:00
//Find all remote filesets that are no longer required, and mark them as deleting
2025-05-19 21:51:28 +02:00
var updated = await cmd . SetCommandAndParameters ( @"
UPDATE ""RemoteVolume""
SET ""State"" = @NewState
WHERE
""Type"" = @CurrentType
AND ""State"" IN (@AllowedStates)
AND ""ID"" NOT IN (
SELECT ""VolumeID""
FROM ""Fileset""
)
" )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@NewState" , RemoteVolumeState . Deleting . ToString ())
. SetParameterValue ( "@CurrentType" , RemoteVolumeType . Files . ToString ())
2025-05-22 13:24:00 +02:00
. ExpandInClauseParameterAsync ( "@AllowedStates" , [
2025-05-19 21:51:28 +02:00
RemoteVolumeState . Uploaded . ToString (),
RemoteVolumeState . Verified . ToString (),
RemoteVolumeState . Temporary . ToString (),
RemoteVolumeState . Deleting . ToString ()
])
. ExecuteNonQueryAsync ();
2020-02-23 13:23:21 -06:00
2016-09-15 11:39:27 +02:00
if ( deleted != updated )
2025-03-13 23:06:32 +01:00
throw new Exception ( $"Unexpected number of remote volumes marked as deleted. Found {deleted} filesets, but {updated} volumes" );
2020-11-21 13:58:16 -08:00
2025-05-19 21:51:28 +02:00
cmd . SetCommandAndParameters ( @"
SELECT
""Name"",
""Size""
FROM ""RemoteVolume""
WHERE
""Type"" = @Type
AND ""State"" = @State
" )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@Type" , RemoteVolumeType . Files . ToString ())
. SetParameterValue ( "@State" , RemoteVolumeState . Deleting . ToString ());
2025-05-19 21:51:28 +02:00
using ( var rd = await cmd . ExecuteReaderAsync ())
while ( await rd . ReadAsync ())
yield return new KeyValuePair < string , long >(
rd . ConvertValueToString ( 0 ) ?? "" ,
rd . ConvertValueToInt64 ( 1 )
);
2016-09-15 11:39:27 +02:00
}
}
2013-03-27 16:06:45 +01:00
2019-12-12 18:38:17 -08:00
/// <summary>
/// Returns a collection of IListResultFilesets, where the Version is the backup version number
2024-07-26 00:08:45 -04:00
/// exposed to the user. This is in contrast to other cases where the Version is the ID in the
2019-12-12 18:38:17 -08:00
/// Fileset table.
/// </summary>
2025-05-19 21:03:40 +02:00
internal async IAsyncEnumerable < IListResultFileset > FilesetsWithBackupVersion ()
2019-12-09 20:31:15 -08:00
{
2025-05-19 21:03:40 +02:00
using ( var cmd = m_connection . CreateCommand ( m_rtr ))
2019-12-09 20:31:15 -08:00
{
2025-05-19 21:03:40 +02:00
// TODO check if this is still the case? (shouldn't be with new sqlite driver):
// We can also use the ROW_NUMBER() window function to generate the backup versions,
// but this requires at least SQLite 3.25, which is not available in some common
// distributions (e.g., Debian) currently.
cmd . SetCommandAndParameters ( @"
SELECT
""IsFullBackup"",
""Timestamp""
FROM ""Fileset""
ORDER BY ""Timestamp"" DESC
" );
using ( var reader = await cmd . ExecuteReaderAsync ())
2019-12-09 20:31:15 -08:00
{
2025-05-19 21:03:40 +02:00
int version = 0 ;
while ( await reader . ReadAsync ())
2019-12-09 20:31:15 -08:00
{
2025-05-19 21:03:40 +02:00
yield return new ListResultFileset (
version ++,
reader . GetInt32 ( 0 ),
ParseFromEpochSeconds ( reader . ConvertValueToInt64 ( 1 )). ToLocalTime (),
- 1L ,
- 1L
);
2019-12-09 20:31:15 -08:00
}
}
}
}
2016-09-15 11:39:27 +02:00
private struct VolumeUsage
{
2019-10-19 10:56:21 -07:00
public readonly string Name ;
public readonly long DataSize ;
public readonly long WastedSize ;
public readonly long CompressedSize ;
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public VolumeUsage ( string name , long datasize , long wastedsize , long compressedsize )
{
2025-03-14 14:34:56 +01:00
Name = name ;
DataSize = datasize ;
WastedSize = wastedsize ;
CompressedSize = compressedsize ;
2016-09-15 11:39:27 +02:00
}
}
2013-03-27 16:06:45 +01:00
2016-09-15 11:39:27 +02:00
/// <summary>
/// Returns the number of bytes stored in each volume,
/// and the number of bytes no longer needed in each volume.
/// The sizes are the uncompressed values.
/// </summary>
/// <returns>A list of tuples with name, datasize, wastedbytes.</returns>
2025-05-19 21:51:28 +02:00
private async IAsyncEnumerable < VolumeUsage > GetWastedSpaceReport ()
2016-09-15 11:39:27 +02:00
{
var tmptablename = "UsageReport-" + Library . Utility . Utility . ByteArrayAsHexString ( Guid . NewGuid (). ToByteArray ());
2024-11-01 16:45:25 +01:00
2025-05-19 21:51:28 +02:00
var usedBlocks = @"
SELECT
SUM(""Block"".""Size"") AS ""ActiveSize"",
""Block"".""VolumeID"" AS ""VolumeID"" FROM ""Block"",
""Remotevolume""
WHERE
""Block"".""VolumeID"" = ""Remotevolume"".""ID""
AND ""Block"".""ID"" NOT IN (
SELECT ""Block"".""ID""
FROM
""Block"",
""DeletedBlock""
WHERE
""Block"".""Hash"" = ""DeletedBlock"".""Hash""
AND ""Block"".""Size"" = ""DeletedBlock"".""Size""
AND ""Block"".""VolumeID"" = ""DeletedBlock"".""VolumeID""
)
GROUP BY ""Block"".""VolumeID""
" ;
var lastmodifiedFile = @"
SELECT
""Block"".""VolumeID"" AS ""VolumeID"",
""Fileset"".""Timestamp"" AS ""Sorttime""
FROM
""Fileset"",
""FilesetEntry"",
""FileLookup"",
""BlocksetEntry"",
""Block""
WHERE
""FilesetEntry"".""FileID"" = ""FileLookup"".""ID""
AND ""FileLookup"".""BlocksetID"" = ""BlocksetEntry"".""BlocksetID""
AND ""BlocksetEntry"".""BlockID"" = ""Block"".""ID""
AND ""Fileset"".""ID"" = ""FilesetEntry"".""FilesetID""
" ;
var lastmodifiedMetadata = @"
SELECT
""Block"".""VolumeID"" AS ""VolumeID"",
""Fileset"".""Timestamp"" AS ""Sorttime""
FROM
""Fileset"",
""FilesetEntry"",
""FileLookup"",
""BlocksetEntry"",
""Block"",
""Metadataset""
WHERE
""FilesetEntry"".""FileID"" = ""FileLookup"".""ID""
AND ""FileLookup"".""MetadataID"" = ""Metadataset"".""ID""
AND ""Metadataset"".""BlocksetID"" = ""BlocksetEntry"".""BlocksetID""
AND ""BlocksetEntry"".""BlockID"" = ""Block"".""ID""
AND ""Fileset"".""ID"" = ""FilesetEntry"".""FilesetID""
" ;
var scantime = @ $"
SELECT
""VolumeID"" AS ""VolumeID"",
MIN(""Sorttime"") AS ""Sorttime""
FROM (
{lastmodifiedFile}
UNION {lastmodifiedMetadata}
)
GROUP BY ""VolumeID""
" ;
var active = @ $"
SELECT
""A"".""ActiveSize"" AS ""ActiveSize"",
0 AS ""InactiveSize"",
""A"".""VolumeID"" AS ""VolumeID"",
CASE
WHEN ""B"".""Sorttime"" IS NULL
THEN 0
ELSE ""B"".""Sorttime""
END AS ""Sorttime""
FROM ({usedBlocks}) A
LEFT OUTER JOIN ({scantime}) B
ON ""B"".""VolumeID"" = ""A"".""VolumeID""
" ;
var inactive = @"
SELECT
0 AS ""ActiveSize"",
SUM(""Size"") AS ""InactiveSize"",
""VolumeID"" AS ""VolumeID"",
0 AS ""SortScantime""
FROM ""DeletedBlock""
GROUP BY ""VolumeID""
" ;
var empty = @"
SELECT
0 AS ""ActiveSize"",
0 AS ""InactiveSize"",
""Remotevolume"".""ID"" AS ""VolumeID"",
0 AS ""SortScantime""
FROM ""Remotevolume""
WHERE
""Remotevolume"".""Type"" = @Type
AND ""Remotevolume"".""State"" IN (@AllowedStates)
AND ""Remotevolume"".""ID"" NOT IN (
SELECT ""VolumeID""
FROM ""Block""
)
" ;
var combined = $"{active} UNION {inactive} UNION {empty}" ;
var collected = @ $"
SELECT
""VolumeID"" AS ""VolumeID"",
SUM(""ActiveSize"") AS ""ActiveSize"",
SUM(""InactiveSize"") AS ""InactiveSize"",
MAX(""Sorttime"") AS ""Sorttime""
FROM ({combined})
GROUP BY ""VolumeID""
" ;
var createtable = $"{@$" CREATE { TEMPORARY } TABLE "" { tmptablename } "" AS "}{collected}" ;
2024-11-01 16:45:25 +01:00
2025-05-19 21:51:28 +02:00
using ( var cmd = m_connection . CreateCommand ( m_rtr ))
2016-09-15 11:39:27 +02:00
{
try
{
2025-05-19 21:51:28 +02:00
await cmd . SetCommandAndParameters ( createtable )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@Type" , RemoteVolumeType . Blocks . ToString ())
2025-05-22 13:24:00 +02:00
. ExpandInClauseParameterAsync ( "@AllowedStates" , [
2025-05-19 21:51:28 +02:00
RemoteVolumeState . Uploaded . ToString (),
RemoteVolumeState . Verified . ToString ()
])
. ExecuteNonQueryAsync ();
cmd . SetCommandAndParameters ( $@"
SELECT
""A"".""Name"",
""B"".""ActiveSize"",
""B"".""InactiveSize"",
""A"".""Size""
FROM
""Remotevolume"" A,
""{tmptablename}"" B
WHERE ""A"".""ID"" = ""B"".""VolumeID""
ORDER BY ""B"".""Sorttime"" ASC
" );
using ( var rd = await cmd . ExecuteReaderAsync ())
while ( await rd . ReadAsync ())
yield return new VolumeUsage (
rd . ConvertValueToString ( 0 ) ?? "" ,
rd . ConvertValueToInt64 ( 1 , 0 ) + rd . ConvertValueToInt64 ( 2 , 0 ),
rd . ConvertValueToInt64 ( 2 , 0 ),
rd . ConvertValueToInt64 ( 3 , 0 )
);
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
finally
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
try
{
await cmd . ExecuteNonQueryAsync ( $@"DROP TABLE IF EXISTS ""{tmptablename}"" " );
}
2016-09-15 11:39:27 +02:00
catch { }
}
}
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public interface ICompactReport
{
IEnumerable < string > DeleteableVolumes { get ; }
IEnumerable < string > CompactableVolumes { get ; }
bool ShouldReclaim { get ; }
bool ShouldCompact { get ; }
2024-11-01 16:45:25 +01:00
void ReportCompactData ();
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
private class CompactReport : ICompactReport
{
2018-05-23 21:18:01 -07:00
private readonly IEnumerable < VolumeUsage > m_report ;
private readonly IEnumerable < VolumeUsage > m_cleandelete ;
private readonly IEnumerable < VolumeUsage > m_wastevolumes ;
private readonly IEnumerable < VolumeUsage > m_smallvolumes ;
private readonly long m_deletablevolumes ;
private readonly long m_wastedspace ;
private readonly long m_smallspace ;
private readonly long m_fullsize ;
private readonly long m_smallvolumecount ;
2024-11-01 16:45:25 +01:00
2018-05-23 21:18:01 -07:00
private readonly long m_wastethreshold ;
private readonly long m_volsize ;
private readonly long m_maxsmallfilecount ;
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public CompactReport ( long volsize , long wastethreshold , long smallfilesize , long maxsmallfilecount , IEnumerable < VolumeUsage > report )
{
m_report = report ;
2024-11-01 16:45:25 +01:00
2015-09-16 21:25:32 +02:00
m_cleandelete = ( from n in m_report where n . DataSize <= n . WastedSize select n ). ToArray ();
2016-09-15 11:39:27 +02:00
m_wastevolumes = from n in m_report where (((( n . WastedSize / ( float ) n . DataSize ) * 100 ) >= wastethreshold ) || ((( n . WastedSize / ( float ) volsize ) * 100 ) >= wastethreshold )) && ! m_cleandelete . Contains ( n ) select n ;
m_smallvolumes = from n in m_report where n . CompressedSize <= smallfilesize && ! m_cleandelete . Contains ( n ) select n ;
2013-03-27 16:06:45 +01:00
2016-09-15 11:39:27 +02:00
m_wastethreshold = wastethreshold ;
m_volsize = volsize ;
2013-08-24 21:16:44 +02:00
m_maxsmallfilecount = maxsmallfilecount ;
2013-03-27 16:06:45 +01:00
2016-09-15 11:39:27 +02:00
m_deletablevolumes = m_cleandelete . Count ();
m_fullsize = report . Select ( x => x . DataSize ). Sum ();
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
m_wastedspace = m_wastevolumes . Select ( x => x . WastedSize ). Sum ();
m_smallspace = m_smallvolumes . Select ( x => x . CompressedSize ). Sum ();
2013-08-24 21:16:44 +02:00
m_smallvolumecount = m_smallvolumes . Count ();
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2018-03-12 14:07:11 +01:00
public void ReportCompactData ()
2013-08-22 20:52:54 +02:00
{
2013-08-25 07:50:24 +02:00
var wastepercentage = (( m_wastedspace / ( float ) m_fullsize ) * 100 );
2018-03-12 14:07:11 +01:00
Logging . Log . WriteVerboseMessage ( LOGTAG , "FullyDeletableCount" , "Found {0} fully deletable volume(s)" , m_deletablevolumes );
Logging . Log . WriteVerboseMessage ( LOGTAG , "SmallVolumeCount" , "Found {0} small volumes(s) with a total size of {1}" , m_smallvolumes . Count (), Library . Utility . Utility . FormatSizeString ( m_smallspace ));
Logging . Log . WriteVerboseMessage ( LOGTAG , "WastedSpaceVolumes" , "Found {0} volume(s) with a total of {1:F2}% wasted space ({2} of {3})" , m_wastevolumes . Count (), wastepercentage , Library . Utility . Utility . FormatSizeString ( m_wastedspace ), Library . Utility . Utility . FormatSizeString ( m_fullsize ));
2016-09-15 11:39:27 +02:00
if ( m_deletablevolumes > 0 )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "CompactReason" , "Compacting because there are {0} fully deletable volume(s)" , m_deletablevolumes );
2016-09-15 11:39:27 +02:00
else if ( wastepercentage >= m_wastethreshold && m_wastevolumes . Count () >= 2 )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "CompactReason" , "Compacting because there is {0:F2}% wasted space and the limit is {1}%" , wastepercentage , m_wastethreshold );
2016-09-15 11:39:27 +02:00
else if ( m_smallspace > m_volsize )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "CompactReason" , "Compacting because there are {0} in small volumes and the volume size is {1}" , Library . Utility . Utility . FormatSizeString ( m_smallspace ), Library . Utility . Utility . FormatSizeString ( m_volsize ));
2013-08-24 21:16:44 +02:00
else if ( m_smallvolumecount > m_maxsmallfilecount )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "CompactReason" , "Compacting because there are {0} small volumes and the maximum is {1}" , m_smallvolumecount , m_maxsmallfilecount );
2016-09-15 11:39:27 +02:00
else
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "CompactReason" , "Compacting not required" );
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public bool ShouldReclaim
{
2024-11-01 16:45:25 +01:00
get
2016-09-15 11:39:27 +02:00
{
return m_deletablevolumes > 0 ;
}
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public bool ShouldCompact
{
2024-11-01 16:45:25 +01:00
get
2016-09-15 11:39:27 +02:00
{
return ((( m_wastedspace / ( float ) m_fullsize ) * 100 ) >= m_wastethreshold && m_wastevolumes . Count () >= 2 ) || m_smallspace > m_volsize || m_smallvolumecount > m_maxsmallfilecount ;
}
}
2013-04-07 09:10:04 +02:00
2024-11-01 16:45:25 +01:00
public IEnumerable < string > DeleteableVolumes
{
get { return from n in m_cleandelete select n . Name ; }
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
public IEnumerable < string > CompactableVolumes
{
get
{
2016-09-15 11:39:27 +02:00
//The order matters, we compact old volumes together first,
// as we anticipate old data will stay around, where never data
// is more likely to be discarded again
return m_wastevolumes . Union ( m_smallvolumes ). Select ( x => x . Name ). Distinct ();
2024-11-01 16:45:25 +01:00
}
2016-09-15 11:39:27 +02:00
}
}
2024-11-01 16:45:25 +01:00
2025-05-19 21:51:28 +02:00
public async Task < ICompactReport > GetCompactReport ( long volsize , long wastethreshold , long smallfilesize , long maxsmallfilecount )
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
return new CompactReport ( volsize , wastethreshold , smallfilesize , maxsmallfilecount , await GetWastedSpaceReport (). ToListAsync ());
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public interface IBlockQuery : IDisposable
{
2024-11-01 16:45:25 +01:00
/// <summary>
/// Checks if a block is in use. If volumeId is not -1, check specific volume
/// </summary>
/// <param name="hash">The hash of the block</param>
/// <param name="size">The size of the block</param>
/// <param name="volumeId">The volume ID to check, or -1 to check all volumes</param>
/// <param name="transaction">The transaction to execute the command in</param>
/// <returns>True if the block is in use</returns>
2025-05-19 21:51:28 +02:00
Task < bool > UseBlock ( string hash , long size , long volumeId );
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
private class BlockQuery : IBlockQuery
{
2025-05-19 21:51:28 +02:00
private LocalDatabase m_db = null !;
private SqliteCommand m_command = null !;
2016-12-16 17:30:39 +01:00
2025-05-19 21:51:28 +02:00
[Obsolete("Calling this constructor will throw an exception. Use CreateAsync() instead.")]
public BlockQuery ( SqliteConnection connection , SqliteTransaction ? transaction )
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
throw new NotImplementedException ( "Use CreateAsync() instead" );
}
private BlockQuery ()
{
// Private constructor to prevent instantiation without CreateAsync
}
public static async Task < BlockQuery > CreateAsync ( LocalDatabase db )
{
return new BlockQuery
{
m_db = db ,
m_command = await db . Connection . CreateCommandAsync ( @"
SELECT ""VolumeID""
FROM ""Block""
WHERE
""Hash"" = @Hash
AND ""Size"" = @Size
" )
};
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
/// <inheritdoc />
2025-05-19 21:51:28 +02:00
public async Task < bool > UseBlock ( string hash , long size , long volumeId )
2024-11-01 16:45:25 +01:00
{
2025-05-19 21:51:28 +02:00
var r = await m_command
. SetTransaction ( m_db . Transaction )
2025-04-04 14:41:58 +02:00
. SetParameterValue ( "@Hash" , hash )
. SetParameterValue ( "@Size" , size )
2025-05-19 21:51:28 +02:00
. ExecuteScalarInt64Async (- 1 );
2024-11-01 16:45:25 +01:00
if ( r == - 1 )
{
return false ;
}
else if ( volumeId == - 1 )
{
return true ;
}
else
{
// Check that the volume id matches
2025-04-01 15:54:54 +02:00
return r == volumeId ;
2024-11-01 16:45:25 +01:00
}
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
public void Dispose ()
{
if ( m_command != null )
try { m_command . Dispose (); }
2025-04-03 15:24:57 +02:00
finally { m_command = null !; }
2016-09-15 11:39:27 +02:00
}
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
/// <summary>
/// Builds a lookup table to enable faster response to block queries
/// </summary>
2025-05-21 14:51:22 +02:00
public async Task < IBlockQuery > CreateBlockQueryHelper ()
{
return await BlockQuery . CreateAsync ( this );
}
2013-03-27 16:06:45 +01:00
2024-11-01 14:50:26 +01:00
/// <summary>
/// Registers a block as moved to a new volume
/// </summary>
/// <param name="hash">The hash of the block</param>
/// <param name="size">The size of the block</param>
/// <param name="volumeID">The new volume ID</param>
2025-05-19 21:51:28 +02:00
public async Task RegisterDuplicatedBlock ( string hash , long size , long volumeID )
2016-09-15 11:39:27 +02:00
{
2024-11-01 14:50:26 +01:00
// Using INSERT OR IGNORE to avoid duplicate entries, result may be 1 or 0
2025-05-19 21:51:28 +02:00
await m_registerDuplicateBlockCommand
. SetTransaction ( m_rtr )
. SetParameterValue ( "@VolumeId" , volumeID )
. SetParameterValue ( "@Hash" , hash )
. SetParameterValue ( "@Size" , size )
. ExecuteNonQueryAsync ();
2024-11-01 14:50:26 +01:00
}
/// <summary>
/// After new volumes are uploaded, this method will update the blocks from the old volumes to point to the new volumes
/// </summary>
/// <param name="filename">The file to remove</param>
/// <param name="volumeIdsToBeRemoved">The volume IDs that will be removed</param>
/// <param name="transaction">The transaction to execute the command in</param>
2025-05-19 21:51:28 +02:00
public async Task PrepareForDelete ( string filename , IEnumerable < long > volumeIdsToBeRemoved )
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
var deletedVolume = await GetRemoteVolume ( filename );
2024-11-01 14:50:26 +01:00
if ( deletedVolume . Type != RemoteVolumeType . Blocks )
return ;
2025-05-19 21:51:28 +02:00
using ( var cmd = m_connection . CreateCommand ( m_rtr ))
2024-11-01 14:50:26 +01:00
{
var updatedBlocks = "BlocksToUpdate-" + Library . Utility . Utility . ByteArrayAsHexString ( Guid . NewGuid (). ToByteArray ());
var replacementBlocks = "ReplacementBlocks-" + Library . Utility . Utility . ByteArrayAsHexString ( Guid . NewGuid (). ToByteArray ());
try
{
2025-05-19 21:51:28 +02:00
await cmd . SetCommandAndParameters ( $@"
CREATE {TEMPORARY} TABLE ""{updatedBlocks}"" AS
SELECT ""ID""
FROM ""Block""
WHERE ""VolumeID"" = @VolumeId
" )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@VolumeId" , deletedVolume . ID )
2025-05-19 21:51:28 +02:00
. ExecuteNonQueryAsync ();
using ( var tempTable = await TemporaryDbValueList . CreateAsync ( this , volumeIdsToBeRemoved ))
await ( await cmd . SetCommandAndParameters ( $@"
CREATE {TEMPORARY} TABLE ""{replacementBlocks}"" AS
SELECT
""BlockID"",
MAX(""VolumeID"") AS ""VolumeID""
FROM ""DuplicateBlock""
WHERE
""VolumeID"" NOT IN (@VolumeIds)
AND ""BlockID"" IN (
SELECT ""ID""
FROM ""{updatedBlocks}""
)
GROUP BY ""BlockID""
" )
. ExpandInClauseParameterAsync ( "@VolumeIds" , tempTable ))
. ExecuteNonQueryAsync ();
var targetCount = await cmd . ExecuteScalarInt64Async ( $@"
SELECT COUNT(*)
FROM ""{updatedBlocks}""
" );
2025-04-02 13:49:21 +02:00
2024-11-01 14:50:26 +01:00
if ( targetCount == 0 )
return ;
2025-05-19 21:51:28 +02:00
var replacementCount = await cmd . ExecuteScalarInt64Async ( $@"
SELECT COUNT(*)
FROM ""{replacementBlocks}""
" );
var updateCount = await cmd . SetCommandAndParameters ( @ $"
UPDATE ""Block""
SET ""VolumeID"" = (
SELECT ""VolumeID""
FROM ""{replacementBlocks}""
WHERE
""{replacementBlocks}"".""BlockID"" = ""Block"".""ID""
AND ""Block"".""VolumeID"" = @VolumeId
)
WHERE ""Block"".""VolumeID"" = @VolumeId
" )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@VolumeId" , deletedVolume . ID )
2025-05-19 21:51:28 +02:00
. ExecuteNonQueryAsync ();
var deleteCount = await cmd . ExecuteNonQueryAsync ( @ $"
DELETE FROM ""DuplicateBlock""
WHERE
(
""DuplicateBlock"".""BlockID""
|| ':'
|| ""DuplicateBlock"".""VolumeID""
) IN (
SELECT
""RB"".""BlockID""
|| ':'
|| ""RB"".""VolumeID""
FROM ""{replacementBlocks}"" RB
)
" );
if ( targetCount != updateCount
|| replacementCount != deleteCount
|| updateCount != deleteCount )
{
2025-03-13 23:06:32 +01:00
throw new Exception ( $"Unexpected number of rows updated. Expected {targetCount} but got updated {updateCount}, deleted {deleteCount}, and replaced {replacementCount}" );
2025-05-19 21:51:28 +02:00
}
2024-11-01 14:50:26 +01:00
// Remove knowledge of any old blocks
2025-05-19 21:51:28 +02:00
await cmd . SetCommandAndParameters ( @ $"
DELETE FROM ""DuplicateBlock""
WHERE ""VolumeID"" = @VolumeId
" )
2025-04-02 13:49:21 +02:00
. SetParameterValue ( "@VolumeId" , deletedVolume . ID )
2025-05-19 21:51:28 +02:00
. ExecuteNonQueryAsync ();
2024-11-01 14:50:26 +01:00
}
finally
{
2025-05-19 21:51:28 +02:00
try
{
await cmd . ExecuteNonQueryAsync ( $@"DROP TABLE IF EXISTS ""{updatedBlocks}"" " );
}
2024-11-01 14:50:26 +01:00
catch { }
2025-05-19 21:51:28 +02:00
try
{
await cmd . ExecuteNonQueryAsync ( $@"DROP TABLE IF EXISTS ""{replacementBlocks}"" " );
}
2024-11-01 14:50:26 +01:00
catch { }
}
}
2016-09-15 11:39:27 +02:00
}
2024-11-01 16:45:25 +01:00
2016-09-15 11:39:27 +02:00
/// <summary>
2017-01-05 09:57:06 +01:00
/// Calculates the sequence in which files should be deleted based on their relations.
2016-09-15 11:39:27 +02:00
/// </summary>
/// <param name="deleteableVolumes">Block volumes slated for deletion.</param>
2024-11-01 14:50:26 +01:00
/// <param name="transaction">The transaction to execute the command in</param>
/// <returns>The deletable volumes.</returns>
2025-05-19 21:51:28 +02:00
public async IAsyncEnumerable < IRemoteVolume > ReOrderDeleteableVolumes ( IEnumerable < IRemoteVolume > deleteableVolumes )
2016-09-15 11:39:27 +02:00
{
2025-05-19 21:51:28 +02:00
using ( var cmd = m_connection . CreateCommand ( m_rtr ))
2016-09-15 11:39:27 +02:00
{
// Although the generated index volumes are always in pairs,
// this code handles many-to-many relations between
// index files and block volumes, should this be added later
var lookupBlock = new Dictionary < string , List < IRemoteVolume >>();
var lookupIndexfiles = new Dictionary < string , List < string >>();
2024-11-01 16:45:25 +01:00
2025-05-19 21:51:28 +02:00
cmd . SetCommandAndParameters ( @"
SELECT
""C"".""Name"",
""B"".""Name"",
""B"".""Hash"",
""B"".""Size""
FROM
""IndexBlockLink"" A,
""RemoteVolume"" B,
""RemoteVolume"" C
WHERE
""A"".""IndexVolumeID"" = ""B"".""ID""
AND ""A"".""BlockVolumeID"" = ""C"".""ID""
AND ""B"".""Hash"" IS NOT NULL
AND ""B"".""Size"" IS NOT NULL
" );
using ( var rd = await cmd . ExecuteReaderAsync ())
while ( await rd . ReadAsync ())
2016-09-15 11:39:27 +02:00
{
2025-04-03 15:24:57 +02:00
var name = rd . ConvertValueToString ( 0 ) ?? "" ;
2024-11-01 14:50:26 +01:00
if (! lookupBlock . TryGetValue ( name , out var indexfileList ))
2024-11-01 16:45:25 +01:00
{
2016-09-15 11:39:27 +02:00
indexfileList = new List < IRemoteVolume >();
lookupBlock . Add ( name , indexfileList );
}
2024-11-01 16:45:25 +01:00
2025-05-19 21:51:28 +02:00
var v = new RemoteVolume (
rd . ConvertValueToString ( 1 ),
rd . ConvertValueToString ( 2 ),
rd . ConvertValueToInt64 ( 3 )
);
2016-09-15 11:39:27 +02:00
indexfileList . Add ( v );
2013-04-08 22:24:54 +02:00
2024-11-01 14:50:26 +01:00
if (! lookupIndexfiles . TryGetValue ( v . Name , out var blockList ))
2024-11-01 16:45:25 +01:00
{
2016-09-15 11:39:27 +02:00
blockList = new List < string >();
lookupIndexfiles . Add ( v . Name , blockList );
}
blockList . Add ( name );
}
2013-04-08 22:24:54 +02:00
2024-11-01 16:45:25 +01:00
foreach ( var r in deleteableVolumes . Distinct ())
2016-09-15 11:39:27 +02:00
{
// Return the input
yield return r ;
2024-11-01 14:50:26 +01:00
if ( lookupBlock . TryGetValue ( r . Name , out var indexfileList ))
2024-11-01 16:45:25 +01:00
foreach ( var sh in indexfileList )
2016-09-15 11:39:27 +02:00
{
2024-11-01 14:50:26 +01:00
if ( lookupIndexfiles . TryGetValue ( sh . Name , out var backref ))
2016-09-15 11:39:27 +02:00
{
2025-05-19 20:43:45 +02:00
//If this is the last reference,
2016-09-15 11:39:27 +02:00
// remove the index file as well
if ( backref . Remove ( r . Name ) && backref . Count == 0 )
yield return sh ;
}
}
}
}
}
2013-03-27 16:06:45 +01:00
2016-09-15 11:39:27 +02:00
}
2013-03-27 16:06:45 +01:00
}