diff --git a/src/ProjectFileIO.cpp b/src/ProjectFileIO.cpp index 36454a265..0c9fc32e9 100644 --- a/src/ProjectFileIO.cpp +++ b/src/ProjectFileIO.cpp @@ -1539,10 +1539,8 @@ bool ProjectFileIO::ImportProject(const FilePath &fileName) } wxString project; - BlockIDs blockids; - // Decode it while capturing the associated sample blockids - project = ProjectSerializer::Decode(buffer, blockids); + project = ProjectSerializer::Decode(buffer); if (project.size() == 0) { SetError(XO("Unable to decode project document")); @@ -1788,7 +1786,6 @@ bool ProjectFileIO::LoadProject(const FilePath &fileName) return false; } - BlockIDs blockids; wxString project; wxMemoryBuffer buffer; bool usedAutosave = true; @@ -1821,8 +1818,7 @@ bool ProjectFileIO::LoadProject(const FilePath &fileName) } else { - // Decode it while capturing the associated sample blockids - project = ProjectSerializer::Decode(buffer, blockids); + project = ProjectSerializer::Decode(buffer); if (project.empty()) { SetError(XO("Unable to decode project document")); @@ -1830,15 +1826,6 @@ bool ProjectFileIO::LoadProject(const FilePath &fileName) return false; } - // Check for orphans blocks...sets mRecovered if any were deleted - if (blockids.size() > 0) - { - if (!DeleteBlocks(blockids, true)) - { - return false; - } - } - XMLFileReader xmlFile; // Load 'er up @@ -1852,6 +1839,19 @@ bool ProjectFileIO::LoadProject(const FilePath &fileName) return false; } + // Check for orphans blocks...sets mRecovered if any were deleted + + auto blockids = WaveTrackFactory::Get( mProject ) + .GetSampleBlockFactory() + ->GetActiveBlockIDs(); + if (blockids.size() > 0) + { + if (!DeleteBlocks(blockids, true)) + { + return false; + } + } + // Remember if we used autosave or not if (usedAutosave) { diff --git a/src/ProjectSerializer.cpp b/src/ProjectSerializer.cpp index adfcec24c..6bb5025c5 100644 --- a/src/ProjectSerializer.cpp +++ b/src/ProjectSerializer.cpp @@ -383,7 +383,7 @@ bool ProjectSerializer::DictChanged() const } // See ProjectFileIO::LoadProject() for explanation of the blockids arg -wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer, BlockIDs &blockids) +wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer) { wxMemoryInputStream in(buffer.GetData(), buffer.GetDataLen()); @@ -549,16 +549,6 @@ wxString ProjectSerializer::Decode(const wxMemoryBuffer &buffer, BlockIDs &block { id = ReadUShort( in ); long long val = ReadLongLong( in ); - - // Look for and save the "blockid" values to support orphan - // block checking. This should be removed once serialization - // and related blocks become part of the same transaction. - const wxString &name = Lookup(id); - if (name.IsSameAs(wxT("blockid"))) - { - blockids.insert(val); - } - out.WriteAttr(Lookup(id), val); } break; diff --git a/src/ProjectSerializer.h b/src/ProjectSerializer.h index 5e561216f..1b023ea69 100644 --- a/src/ProjectSerializer.h +++ b/src/ProjectSerializer.h @@ -22,9 +22,6 @@ // From SampleBlock.h using SampleBlockID = long long; -// From ProjectFileiIO.h -using BlockIDs = std::unordered_set; - /// /// ProjectSerializer /// @@ -69,7 +66,7 @@ public: bool DictChanged() const; // Returns empty string if decoding fails - static wxString Decode(const wxMemoryBuffer &buffer, BlockIDs &blockids); + static wxString Decode(const wxMemoryBuffer &buffer); private: void WriteName(const wxString & name); diff --git a/src/SampleBlock.h b/src/SampleBlock.h index 0ff536609..a5bfc601c 100644 --- a/src/SampleBlock.h +++ b/src/SampleBlock.h @@ -13,6 +13,7 @@ SampleBlock.h #include #include +#include class AudacityProject; class ProjectFileIO; @@ -133,6 +134,10 @@ public: sampleFormat srcformat, const wxChar **attrs); + using SampleBlockIDs = std::unordered_set; + /*! @return ids of all sample blocks created by this factory and still extant */ + virtual SampleBlockIDs GetActiveBlockIDs() = 0; + protected: // The override should throw more informative exceptions on error than the // default InconsistencyException thrown by Create diff --git a/src/SqliteSampleBlock.cpp b/src/SqliteSampleBlock.cpp index 568ee5fb5..766759c6d 100644 --- a/src/SqliteSampleBlock.cpp +++ b/src/SqliteSampleBlock.cpp @@ -129,6 +129,8 @@ public: ~SqliteSampleBlockFactory() override; + SampleBlockIDs GetActiveBlockIDs() override; + SampleBlockPtr DoGet(SampleBlockID sbid) override; SampleBlockPtr DoCreate(samplePtr src, @@ -171,6 +173,21 @@ SampleBlockPtr SqliteSampleBlockFactory::DoCreate( return sb; } +auto SqliteSampleBlockFactory::GetActiveBlockIDs() -> SampleBlockIDs +{ + SampleBlockIDs result; + for (auto end = mAllBlocks.end(), it = mAllBlocks.begin(); it != end;) { + if (it->second.expired()) + // Tighten up the map + it = mAllBlocks.erase(it); + else { + result.insert( it->first ); + ++it; + } + } + return result; +} + SampleBlockPtr SqliteSampleBlockFactory::DoCreateSilent( size_t numsamples, sampleFormat srcformat ) {