From c7f7b2cefeb192a009ea51b593389ee740a5f3aa Mon Sep 17 00:00:00 2001 From: Carl Johnsen Date: Wed, 21 May 2025 15:10:27 +0200 Subject: [PATCH] Updated BackupDatabase async interface to call the new async database methods --- .../Main/Operation/Backup/BackupDatabase.cs | 131 ++++++++---------- .../Operation/Backup/MetadataPreProcess.cs | 32 ++--- 2 files changed, 75 insertions(+), 88 deletions(-) diff --git a/Duplicati/Library/Main/Operation/Backup/BackupDatabase.cs b/Duplicati/Library/Main/Operation/Backup/BackupDatabase.cs index 319cbc26e..e28dcec6f 100644 --- a/Duplicati/Library/Main/Operation/Backup/BackupDatabase.cs +++ b/Duplicati/Library/Main/Operation/Backup/BackupDatabase.cs @@ -29,7 +29,6 @@ using Duplicati.Library.Main.Volumes; using System.Linq; using Duplicati.Library.Interface; - namespace Duplicati.Library.Main.Operation.Backup { /// @@ -57,120 +56,108 @@ namespace Duplicati.Library.Main.Operation.Backup public Task FindBlockIDAsync(string hash, long size) { - return RunOnMain(() => m_database.FindBlockID(hash, size, GetTransaction())); + return RunOnMain(async () => await m_database.FindBlockID(hash, size)); } public Task AddBlockAsync(string hash, long size, long volumeid) { - return RunOnMain(() => m_database.AddBlock(hash, size, volumeid, GetTransaction())); + return RunOnMain(async () => await m_database.AddBlock(hash, size, volumeid)); } public Task GetFileHashAsync(long fileid) { - return RunOnMain(() => m_database.GetFileHash(fileid, GetTransaction())); + return RunOnMain(async () => await m_database.GetFileHash(fileid)); } - public Task> AddMetadatasetAsync(string hash, long size, long blocksetid) + public Task<(bool, long)> AddMetadatasetAsync(string hash, long size, long blocksetid) { - return RunOnMain(() => - { - long metadataid; - var n = m_database.AddMetadataset(hash, size, blocksetid, out metadataid, GetTransaction()); - return new Tuple(n, metadataid); - }); + return RunOnMain(async () => await m_database.AddMetadataset(hash, size, blocksetid)); } - public Task> GetMetadataIDAsync(string hash, long size) + public Task<(bool, long)> GetMetadataIDAsync(string hash, long size) { - return RunOnMain(() => - { - long metadataid; - var r = m_database.GetMetadatasetID(hash, size, out metadataid, GetTransaction()); - return new Tuple(r, metadataid); - }); + return RunOnMain(async () => await m_database.GetMetadatasetID(hash, size)); } public Task AddDirectoryEntryAsync(string filename, long metadataid, DateTime lastModified) { - return RunOnMain(() => m_database.AddDirectoryEntry(filename, metadataid, lastModified, GetTransaction())); + return RunOnMain(async () => await m_database.AddDirectoryEntry(filename, metadataid, lastModified)); } public Task AddSymlinkEntryAsync(string filename, long metadataid, DateTime lastModified) { - return RunOnMain(() => m_database.AddSymlinkEntry(filename, metadataid, lastModified, GetTransaction())); + return RunOnMain(async () => await m_database.AddSymlinkEntry(filename, metadataid, lastModified)); } public Task<(long Size, string MetadataHash)?> GetMetadataHashAndSizeForFileAsync(long fileid) { - return RunOnMain(() => m_database.GetMetadataHashAndSizeForFile(fileid, GetTransaction())); + return RunOnMain(async () => await m_database.GetMetadataHashAndSizeForFile(fileid)); } - public Task> GetFileLastModifiedAsync(long prefixid, string path, long lastfilesetid, bool includeLength) + public Task<(long, DateTime, long)> GetFileLastModifiedAsync(long prefixid, string path, long lastfilesetid, bool includeLength) { - return RunOnMain(() => - { - var id = m_database.GetFileLastModified(prefixid, path, lastfilesetid, includeLength, out var lastModified, out var length, GetTransaction()); - return new Tuple(id, lastModified, length); - }); + return RunOnMain(async () => await m_database.GetFileLastModified(prefixid, path, lastfilesetid, includeLength)); } public Task GetFileEntryAsync(long prefixid, string path, long lastfilesetid) { - return RunOnMain(() => + return RunOnMain(async () => { - DateTime oldModified; - long lastFileSize; - string oldMetahash; - long oldMetasize; - - var id = m_database.GetFileEntry(prefixid, path, lastfilesetid, out oldModified, out lastFileSize, out oldMetahash, out oldMetasize, GetTransaction()); + var (id, oldModified, lastFileSize, oldMetahash, oldMetasize) = + await m_database.GetFileEntry(prefixid, path, lastfilesetid); return id < 0 ? null : - new FileEntryData() { id = id, modified = oldModified, filesize = lastFileSize, metahash = oldMetahash, metasize = oldMetasize }; + new FileEntryData() + { + id = id, + modified = oldModified, + filesize = lastFileSize, + metahash = oldMetahash, + metasize = oldMetasize + }; }); } public Task AddBlocksetAsync(string filehash, long size, int blocksize, IEnumerable hashlist, IEnumerable blocklisthashes) { - return RunOnMain(() => + return RunOnMain(async () => { - long blocksetid; + var (_, blocksetid) = await m_database.AddBlockset(filehash, size, blocksize, hashlist, blocklisthashes); - m_database.AddBlockset(filehash, size, blocksize, hashlist, blocklisthashes, out blocksetid, GetTransaction()); return blocksetid; }); } public Task GetOrCreatePathPrefix(string prefix) { - return RunOnMain(() => m_database.GetOrCreatePathPrefix(prefix, GetTransaction())); + return RunOnMain(async () => await m_database.GetOrCreatePathPrefix(prefix)); } public Task AddFileAsync(long prefixid, string filename, DateTime lastmodified, long blocksetid, long metadataid) { - return RunOnMain(() => m_database.AddFile(prefixid, filename, lastmodified, blocksetid, metadataid, GetTransaction())); + return RunOnMain(async () => await m_database.AddFile(prefixid, filename, lastmodified, blocksetid, metadataid)); } public Task AddUnmodifiedAsync(long fileid, DateTime lastModified) { - return RunOnMain(() => m_database.AddKnownFile(fileid, lastModified, GetTransaction())); + return RunOnMain(async () => await m_database.AddKnownFile(fileid, lastModified)); } public Task MoveBlockToVolumeAsync(string blockkey, long size, long sourcevolumeid, long targetvolumeid) { - return RunOnMain(() => m_database.MoveBlockToVolume(blockkey, size, sourcevolumeid, targetvolumeid, GetTransaction())); + return RunOnMain(async () => await m_database.MoveBlockToVolume(blockkey, size, sourcevolumeid, targetvolumeid)); } public Task SafeDeleteRemoteVolumeAsync(string remotename) { - return RunOnMain(() => m_database.SafeDeleteRemoteVolume(remotename, GetTransaction())); + return RunOnMain(async () => await m_database.SafeDeleteRemoteVolume(remotename)); } public Task GetBlocklistHashesAsync(string remotename) { - return RunOnMain(() => Task.FromResult(m_database.GetBlocklistHashes(remotename, GetTransaction()))); + return RunOnMain(async () => await m_database.GetBlocklistHashes(remotename)); } public Task UpdateIndexVolumeAsync(IndexVolumeWriter indexvolume, BlockVolumeWriter blockvolume) @@ -178,47 +165,47 @@ namespace Duplicati.Library.Main.Operation.Backup if (indexvolume != null) return Task.FromResult(true); - return RunOnMain(() => + return RunOnMain(async () => { - m_database.AddIndexBlockLink(indexvolume.VolumeID, blockvolume.VolumeID, GetTransaction()); + await m_database.AddIndexBlockLink(indexvolume.VolumeID, blockvolume.VolumeID); indexvolume.StartVolume(blockvolume.RemoteFilename); - foreach (var b in m_database.GetBlocks(blockvolume.VolumeID, GetTransaction())) + await foreach (var b in m_database.GetBlocks(blockvolume.VolumeID)) indexvolume.AddBlock(b.Hash, b.Size); - m_database.UpdateRemoteVolume(indexvolume.RemoteFilename, RemoteVolumeState.Uploading, -1, null, GetTransaction()); + await m_database.UpdateRemoteVolume(indexvolume.RemoteFilename, RemoteVolumeState.Uploading, -1, null); }); } public Task AppendFilesFromPreviousSetAsync() { - return RunOnMain(() => m_database.AppendFilesFromPreviousSet(GetTransaction())); + return RunOnMain(async () => await m_database.AppendFilesFromPreviousSet()); } public Task AppendFilesFromPreviousSetAsync(string[] deletedFilelist) { - return RunOnMain(() => m_database.AppendFilesFromPreviousSet(GetTransaction(), deletedFilelist)); + return RunOnMain(async () => await m_database.AppendFilesFromPreviousSet(deletedFilelist)); } public Task AppendFilesFromPreviousSetAsync(string[] deletedFilelist, long filesetid, long prevId, DateTime timestamp) { - return RunOnMain(() => m_database.AppendFilesFromPreviousSet(GetTransaction(), deletedFilelist, filesetid, prevId, timestamp)); + return RunOnMain(async () => await m_database.AppendFilesFromPreviousSet(deletedFilelist, filesetid, prevId, timestamp)); } /// - /// Populates FilesetEntry table with files from previous fileset, which aren't - /// yet part of the new fileset, and which aren't excluded by the (optional) exclusion + /// Populates FilesetEntry table with files from previous fileset, which aren't + /// yet part of the new fileset, and which aren't excluded by the (optional) exclusion /// predicate. /// /// Optional exclusion predicate (true = exclude file) public Task AppendFilesFromPreviousSetWithPredicateAsync(Func exclusionPredicate) { - return RunOnMain(() => m_database.AppendFilesFromPreviousSetWithPredicate(GetTransaction(), exclusionPredicate)); + return RunOnMain(async () => await m_database.AppendFilesFromPreviousSetWithPredicate(exclusionPredicate)); } /// - /// Populates FilesetEntry table with files from previous fileset, which aren't - /// yet part of the new fileset, and which aren't excluded by the (optional) exclusion + /// Populates FilesetEntry table with files from previous fileset, which aren't + /// yet part of the new fileset, and which aren't excluded by the (optional) exclusion /// predicate. /// /// Optional exclusion predicate (true = exclude file) @@ -227,83 +214,83 @@ namespace Duplicati.Library.Main.Operation.Backup /// If prevFileSetId == -1, used to locate previous fileset public Task AppendFilesFromPreviousSetWithPredicateAsync(Func exclusionPredicate, long fileSetId, long prevFileSetId, DateTime timestamp) { - return RunOnMain(() => m_database.AppendFilesFromPreviousSetWithPredicate(GetTransaction(), exclusionPredicate, fileSetId, prevFileSetId, timestamp)); + return RunOnMain(async () => await m_database.AppendFilesFromPreviousSetWithPredicate(exclusionPredicate, fileSetId, prevFileSetId, timestamp)); } public Task[]> GetIncompleteFilesetsAsync() { - return RunOnMain(() => m_database.GetIncompleteFilesets(GetTransaction()).OrderBy(x => x.Value).ToArray()); + return RunOnMain(async () => await m_database.GetIncompleteFilesets().OrderBy(x => x.Value).ToArrayAsync()); } public Task[]> GetFilesetTimesAsync() { - return RunOnMain(() => m_database.FilesetTimes.ToArray()); + return RunOnMain(async () => await m_database.FilesetTimes().ToArrayAsync()); } public Task CreateFilesetAsync(long volumeID, DateTime fileTime) { - return RunOnMain(() => m_database.CreateFileset(volumeID, fileTime, GetTransaction())); + return RunOnMain(async () => await m_database.CreateFileset(volumeID, fileTime)); } public Task LinkFilesetToVolumeAsync(long filesetid, long volumeid) { - return RunOnMain(() => m_database.LinkFilesetToVolume(filesetid, volumeid, GetTransaction())); + return RunOnMain(async () => await m_database.LinkFilesetToVolume(filesetid, volumeid)); } public Task WriteFilesetAsync(FilesetVolumeWriter fsw, long filesetid) { - return RunOnMain(() => m_database.WriteFileset(fsw, filesetid, GetTransaction())); + return RunOnMain(async () => await m_database.WriteFileset(fsw, filesetid)); } public Task PushTimestampChangesToPreviousVersionAsync(long filesetid) { - return RunOnMain(() => m_database.PushTimestampChangesToPreviousVersion(filesetid, GetTransaction())); + return RunOnMain(async () => await m_database.PushTimestampChangesToPreviousVersion(filesetid)); } public Task UpdateFilesetAndMarkAsFullBackupAsync(long filesetid) { - return RunOnMain(() => m_database.UpdateFullBackupStateInFileset(filesetid, true, GetTransaction())); + return RunOnMain(async () => await m_database.UpdateFullBackupStateInFileset(filesetid, true)); } // TODO: Make IAsyncEnumerable or get rid of the SingleRunner setup public Task GetMissingIndexFilesAsync() { - return RunOnMain(() => m_database.GetMissingIndexFiles(GetTransaction()).ToArray()); + return RunOnMain(async () => await m_database.GetMissingIndexFiles().ToArrayAsync()); } public Task UpdateChangeStatisticsAsync(BackupResults result) { - return RunOnMain(() => m_database.UpdateChangeStatistics(result, GetTransaction())); + return RunOnMain(async () => await m_database.UpdateChangeStatistics(result)); } public Task VerifyConsistencyAsync(int blocksize, int blockhashSize, bool verifyfilelists) { - return RunOnMain(() => m_database.VerifyConsistency(blocksize, blockhashSize, verifyfilelists, GetTransaction())); + return RunOnMain(async () => await m_database.VerifyConsistency(blocksize, blockhashSize, verifyfilelists)); } public Task RemoveRemoteVolumeAsync(string remoteFilename) { - return RunOnMain(() => m_database.RemoveRemoteVolume(remoteFilename, GetTransaction())); + return RunOnMain(async () => await m_database.RemoveRemoteVolume(remoteFilename)); } public Task GetRemoteVolumeFromFilesetIDAsync(long filesetID) { - return RunOnMain(() => m_database.GetRemoteVolumeFromFilesetID(filesetID, GetTransaction())); + return RunOnMain(async () => await m_database.GetRemoteVolumeFromFilesetID(filesetID)); } public Task CreateChangeJournalDataAsync(IEnumerable journalData) { - return RunOnMain(() => m_database.CreateChangeJournalData(journalData, GetTransaction())); + return RunOnMain(async () => await m_database.CreateChangeJournalData(journalData)); } public Task UpdateChangeJournalDataAsync(IEnumerable journalData, long lastfilesetid) { - return RunOnMain(() => m_database.UpdateChangeJournalData(journalData, lastfilesetid, GetTransaction())); + return RunOnMain(async () => await m_database.UpdateChangeJournalData(journalData, lastfilesetid)); } public Task IsBlocklistHashKnownAsync(string hash) { - return RunOnMain(() => m_database.IsBlocklistHashKnown(hash, GetTransaction())); + return RunOnMain(async () => await m_database.IsBlocklistHashKnown(hash)); } } diff --git a/Duplicati/Library/Main/Operation/Backup/MetadataPreProcess.cs b/Duplicati/Library/Main/Operation/Backup/MetadataPreProcess.cs index 2ae10faa1..f1e1a1743 100644 --- a/Duplicati/Library/Main/Operation/Backup/MetadataPreProcess.cs +++ b/Duplicati/Library/Main/Operation/Backup/MetadataPreProcess.cs @@ -1,22 +1,22 @@ // Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com -// -// 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 +// +// 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 // Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in +// +// The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. -// -// 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 +// +// 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 // DEALINGS IN THE SOFTWARE. using System; @@ -258,7 +258,7 @@ namespace Duplicati.Library.Main.Operation.Backup /// The metadata entry. /// The database connection. /// The channel to write streams to. - internal static async Task> AddMetadataToOutputAsync(string path, IMetahash meta, BackupDatabase database, IWriteChannel streamblockchannel) + internal static async Task<(bool, long)> AddMetadataToOutputAsync(string path, IMetahash meta, BackupDatabase database, IWriteChannel streamblockchannel) { StreamProcessResult res; using (var ms = new MemoryStream(meta.Blob))