Audacity 3.2.0
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
SqliteSampleBlockFactory Class Referencefinal

Implementation of SampleBlockFactory using Sqlite database. More...

Inheritance diagram for SqliteSampleBlockFactory:
[legend]
Collaboration diagram for SqliteSampleBlockFactory:
[legend]

Public Member Functions

 SqliteSampleBlockFactory (AudacityProject &project)
 
 ~SqliteSampleBlockFactory () override
 
SampleBlockIDs GetActiveBlockIDs () override
 
SampleBlockPtr DoCreate (constSamplePtr src, size_t numsamples, sampleFormat srcformat) override
 
SampleBlockPtr DoCreateSilent (size_t numsamples, sampleFormat srcformat) override
 
SampleBlockPtr DoCreateFromXML (sampleFormat srcformat, const AttributesList &attrs) override
 
- Public Member Functions inherited from SampleBlockFactory
virtual ~SampleBlockFactory ()
 
SampleBlockPtr Create (constSamplePtr src, size_t numsamples, sampleFormat srcformat)
 
SampleBlockPtr CreateSilent (size_t numsamples, sampleFormat srcformat)
 
SampleBlockPtr CreateFromXML (sampleFormat srcformat, const AttributesList &attrs)
 
virtual SampleBlockIDs GetActiveBlockIDs ()=0
 
- Public Member Functions inherited from Observer::Publisher< SampleBlockCreateMessage >
 Publisher (ExceptionPolicy *pPolicy=nullptr, Alloc a={})
 Constructor supporting type-erased custom allocation/deletion. More...
 
 Publisher (Publisher &&)=default
 
Publisheroperator= (Publisher &&)=default
 
Subscription Subscribe (Callback callback)
 Connect a callback to the Publisher; later-connected are called earlier. More...
 
Subscription Subscribe (Object &obj, Return(Object::*callback)(Args...))
 Overload of Subscribe takes an object and pointer-to-member-function. More...
 

Private Types

using AllBlocksMap = std::map< SampleBlockID, std::weak_ptr< SqliteSampleBlock > >
 

Private Member Functions

void OnBeginPurge (size_t begin, size_t end)
 
void OnEndPurge ()
 

Private Attributes

friend SqliteSampleBlock
 
AudacityProjectmProject
 
Observer::Subscription mUndoSubscription
 
std::optional< SampleBlock::DeletionCallback::ScopemScope
 
const std::shared_ptr< ConnectionPtrmppConnection
 
AllBlocksMap mAllBlocks
 

Additional Inherited Members

- Public Types inherited from SampleBlockFactory
using SampleBlockIDs = std::unordered_set< SampleBlockID >
 
- Public Types inherited from Observer::Publisher< SampleBlockCreateMessage >
using message_type = SampleBlockCreateMessage
 
using CallbackReturn = std::conditional_t< true, void, bool >
 
using Callback = std::function< CallbackReturn(const SampleBlockCreateMessage &) >
 Type of functions that can be connected to the Publisher. More...
 
- Static Public Member Functions inherited from SampleBlockFactory
static SampleBlockFactoryPtr New (AudacityProject &project)
 
- Static Public Attributes inherited from Observer::Publisher< SampleBlockCreateMessage >
static constexpr bool notifies_all
 
virtual SampleBlockPtr DoCreate (constSamplePtr src, size_t numsamples, sampleFormat srcformat)=0
 
virtual SampleBlockPtr DoCreateSilent (size_t numsamples, sampleFormat srcformat)=0
 
virtual SampleBlockPtr DoCreateFromXML (sampleFormat srcformat, const AttributesList &attrs)=0
 
- Protected Member Functions inherited from Observer::Publisher< SampleBlockCreateMessage >
CallbackReturn Publish (const SampleBlockCreateMessage &message)
 Send a message to connected callbacks. More...
 

Detailed Description

Implementation of SampleBlockFactory using Sqlite database.

Definition at line 135 of file SqliteSampleBlock.cpp.

Member Typedef Documentation

◆ AllBlocksMap

using SqliteSampleBlockFactory::AllBlocksMap = std::map< SampleBlockID, std::weak_ptr< SqliteSampleBlock > >
private

Definition at line 173 of file SqliteSampleBlock.cpp.

Constructor & Destructor Documentation

◆ SqliteSampleBlockFactory()

SqliteSampleBlockFactory::SqliteSampleBlockFactory ( AudacityProject project)
explicit

Definition at line 178 of file SqliteSampleBlock.cpp.

179 : mProject{ project }
180 , mppConnection{ ConnectionPtr::Get(project).shared_from_this() }
181{
183 .Subscribe([this](UndoRedoMessage message){
184 switch (message.type) {
185 case UndoRedoMessage::BeginPurge:
186 return OnBeginPurge(message.begin, message.end);
187 case UndoRedoMessage::EndPurge:
188 return OnEndPurge();
189 default:
190 return;
191 }
192 });
193}
static ConnectionPtr & Get(AudacityProject &project)
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
const std::shared_ptr< ConnectionPtr > mppConnection
Observer::Subscription mUndoSubscription
static UndoManager & Get(AudacityProject &project)
Definition: UndoManager.cpp:71
Type of message published by UndoManager.
Definition: UndoManager.h:55
enum UndoRedoMessage::Type type

References UndoManager::Get(), mUndoSubscription, Observer::Publisher< Message, NotifyAll >::Subscribe(), and UndoRedoMessage::type.

Here is the call graph for this function:

◆ ~SqliteSampleBlockFactory()

SqliteSampleBlockFactory::~SqliteSampleBlockFactory ( )
overridedefault

Member Function Documentation

◆ DoCreate()

SampleBlockPtr SqliteSampleBlockFactory::DoCreate ( constSamplePtr  src,
size_t  numsamples,
sampleFormat  srcformat 
)
overridevirtual

Implements SampleBlockFactory.

Definition at line 197 of file SqliteSampleBlock.cpp.

199{
200 auto sb = std::make_shared<SqliteSampleBlock>(shared_from_this());
201 sb->SetSamples(src, numsamples, srcformat);
202 // block id has now been assigned
203 mAllBlocks[ sb->GetBlockID() ] = sb;
204 return sb;
205}

References mAllBlocks.

◆ DoCreateFromXML()

SampleBlockPtr SqliteSampleBlockFactory::DoCreateFromXML ( sampleFormat  srcformat,
const AttributesList attrs 
)
overridevirtual

Implements SampleBlockFactory.

Definition at line 240 of file SqliteSampleBlock.cpp.

242{
243 std::shared_ptr<SampleBlock> sb;
244
245 int found = 0;
246
247 // loop through attrs, which is a null-terminated list of attribute-value pairs
248 for (auto pair : attrs)
249 {
250 auto attr = pair.first;
251 auto value = pair.second;
252
253 long long nValue;
254
255 if (attr == "blockid" && value.TryGet(nValue))
256 {
257 if (nValue <= 0) {
258 sb = DoCreateSilent( -nValue, floatSample );
259 }
260 else {
261 // First see if this block id was previously loaded
262 auto &wb = mAllBlocks[ nValue ];
263 auto pb = wb.lock();
264 if (pb)
265 // Reuse the block
266 sb = pb;
267 else {
268 // First sight of this id
269 auto ssb =
270 std::make_shared<SqliteSampleBlock>(shared_from_this());
271 wb = ssb;
272 sb = ssb;
273 ssb->mSampleFormat = srcformat;
274 // This may throw database errors
275 // It initializes the rest of the fields
276 ssb->Load((SampleBlockID) nValue);
277 }
278 }
279 found++;
280 }
281 }
282
283 // Were all attributes found?
284 if (found != 1)
285 {
286 return nullptr;
287 }
288
289 return sb;
290}
long long SampleBlockID
Definition: ProjectFileIO.h:42
SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat) override

References DoCreateSilent(), floatSample, and mAllBlocks.

Here is the call graph for this function:

◆ DoCreateSilent()

SampleBlockPtr SqliteSampleBlockFactory::DoCreateSilent ( size_t  numsamples,
sampleFormat  srcformat 
)
overridevirtual

Implements SampleBlockFactory.

Definition at line 222 of file SqliteSampleBlock.cpp.

224{
225 auto id = -static_cast< SampleBlockID >(numsamples);
226 auto &result = sSilentBlocks[ id ];
227 if ( !result ) {
228 result = std::make_shared<SqliteSampleBlock>(nullptr);
229 result->mBlockID = id;
230
231 // Ignore the supplied sample format
232 result->SetSizes(numsamples, floatSample);
233 result->mValid = true;
234 }
235
236 return result;
237}
static std::map< SampleBlockID, std::shared_ptr< SqliteSampleBlock > > sSilentBlocks
int id

References floatSample, id, and sSilentBlocks.

Referenced by DoCreateFromXML().

Here is the caller graph for this function:

◆ GetActiveBlockIDs()

auto SqliteSampleBlockFactory::GetActiveBlockIDs ( )
overridevirtual
Returns
ids of all sample blocks created by this factory and still extant

Implements SampleBlockFactory.

Definition at line 207 of file SqliteSampleBlock.cpp.

208{
209 SampleBlockIDs result;
210 for (auto end = mAllBlocks.end(), it = mAllBlocks.begin(); it != end;) {
211 if (it->second.expired())
212 // Tighten up the map
213 it = mAllBlocks.erase(it);
214 else {
215 result.insert( it->first );
216 ++it;
217 }
218 }
219 return result;
220}
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:141
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159

References PackedArray::end().

Here is the call graph for this function:

◆ OnBeginPurge()

void SqliteSampleBlockFactory::OnBeginPurge ( size_t  begin,
size_t  end 
)
private

Definition at line 1050 of file SqliteSampleBlock.cpp.

1051{
1052 // Install a callback function that updates a progress indicator
1053 using namespace BasicUI;
1054
1055 //Avoid showing dialog to the user if purge operation
1056 //does not take much time, as it will resign focus from main window
1057 //but dialog itself may not be presented to the user at all.
1058 //On MacOS 13 (bug #3975) focus isn't restored in that case.
1059 constexpr auto ProgressDialogShowDelay = std::chrono::milliseconds (200);
1060 const auto nToDelete = EstimateRemovedBlocks(mProject, begin, end);
1061 if(nToDelete == 0)
1062 return;
1063 auto purgeStartTime = std::chrono::system_clock::now();
1064 std::shared_ptr<ProgressDialog> progressDialog;
1065 mScope.emplace([=, nDeleted = 0](auto&) mutable {
1066 ++nDeleted;
1067 if(!progressDialog)
1068 {
1069 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
1070 std::chrono::system_clock::now() - purgeStartTime);
1071 if(elapsed >= ProgressDialogShowDelay)
1072 progressDialog = MakeProgress(XO("Progress"), XO("Discarding undo/redo history"), 0);
1073 }
1074 else
1075 progressDialog->Poll(nDeleted, nToDelete);
1076 });
1077}
XO("Cut/Copy/Paste")
static size_t EstimateRemovedBlocks(AudacityProject &project, size_t begin, size_t end)
Just to find a denominator for a progress indicator.
std::optional< SampleBlock::DeletionCallback::Scope > mScope
std::unique_ptr< ProgressDialog > MakeProgress(const TranslatableString &title, const TranslatableString &message, unsigned flags=(ProgressShowStop|ProgressShowCancel), const TranslatableString &remainingLabelText={})
Create and display a progress dialog.
Definition: BasicUI.h:289
auto begin(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:150

References PackedArray::begin(), PackedArray::end(), EstimateRemovedBlocks(), BasicUI::MakeProgress(), mProject, mScope, and XO().

Here is the call graph for this function:

◆ OnEndPurge()

void SqliteSampleBlockFactory::OnEndPurge ( )
private

Definition at line 1079 of file SqliteSampleBlock.cpp.

1080{
1081 mScope.reset();
1082}

References mScope.

Member Data Documentation

◆ mAllBlocks

AllBlocksMap SqliteSampleBlockFactory::mAllBlocks
private

Definition at line 175 of file SqliteSampleBlock.cpp.

Referenced by DoCreate(), and DoCreateFromXML().

◆ mppConnection

const std::shared_ptr<ConnectionPtr> SqliteSampleBlockFactory::mppConnection
private

Definition at line 167 of file SqliteSampleBlock.cpp.

◆ mProject

AudacityProject& SqliteSampleBlockFactory::mProject
private

Definition at line 164 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge().

◆ mScope

std::optional<SampleBlock::DeletionCallback::Scope> SqliteSampleBlockFactory::mScope
private

Definition at line 166 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge(), and OnEndPurge().

◆ mUndoSubscription

Observer::Subscription SqliteSampleBlockFactory::mUndoSubscription
private

Definition at line 165 of file SqliteSampleBlock.cpp.

Referenced by SqliteSampleBlockFactory().

◆ SqliteSampleBlock

friend SqliteSampleBlockFactory::SqliteSampleBlock
private

Definition at line 162 of file SqliteSampleBlock.cpp.


The documentation for this class was generated from the following file: