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
 
SampleBlockPtr DoCreateFromId (sampleFormat srcformat, SampleBlockID id) override
 
SampleBlock::DeletionCallback GetSampleBlockDeletionCallback () const
 
- 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)
 
SampleBlockPtr CreateFromId (sampleFormat srcformat, SampleBlockID id)
 
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
 
SampleBlock::DeletionCallback mSampleBlockDeletionCallback
 
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
 
virtual SampleBlockPtr DoCreateFromId (sampleFormat srcformat, SampleBlockID id)=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 146 of file SqliteSampleBlock.cpp.

Member Typedef Documentation

◆ AllBlocksMap

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

Definition at line 192 of file SqliteSampleBlock.cpp.

Constructor & Destructor Documentation

◆ SqliteSampleBlockFactory()

SqliteSampleBlockFactory::SqliteSampleBlockFactory ( AudacityProject project)
explicit

Definition at line 197 of file SqliteSampleBlock.cpp.

198 : mProject{ project }
199 , mppConnection{ ConnectionPtr::Get(project).shared_from_this() }
200{
202 .Subscribe([this](UndoRedoMessage message){
203 switch (message.type) {
204 case UndoRedoMessage::BeginPurge:
205 return OnBeginPurge(message.begin, message.end);
206 case UndoRedoMessage::EndPurge:
207 return OnEndPurge();
208 default:
209 return;
210 }
211 });
212}
const auto project
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, project, 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 216 of file SqliteSampleBlock.cpp.

218{
219 auto sb = std::make_shared<SqliteSampleBlock>(shared_from_this());
220 sb->SetSamples(src, numsamples, srcformat);
221 // block id has now been assigned
222 mAllBlocks[ sb->GetBlockID() ] = sb;
223 return sb;
224}

References mAllBlocks.

◆ DoCreateFromId()

SampleBlockPtr SqliteSampleBlockFactory::DoCreateFromId ( sampleFormat  srcformat,
SampleBlockID  id 
)
overridevirtual

Implements SampleBlockFactory.

Definition at line 277 of file SqliteSampleBlock.cpp.

279{
280 if (id <= 0)
281 return DoCreateSilent(-id, floatSample);
282
283 // First see if this block id was previously loaded
284 auto& wb = mAllBlocks[id];
285
286 if (auto block = wb.lock())
287 return block;
288
289 // First sight of this id
290 auto ssb = std::make_shared<SqliteSampleBlock>(shared_from_this());
291 wb = ssb;
292 ssb->mSampleFormat = srcformat;
293 // This may throw database errors
294 // It initializes the rest of the fields
295 ssb->Load(static_cast<SampleBlockID>(id));
296
297 return ssb;
298}
long long SampleBlockID
Definition: CloudSyncDTO.h:26
int id
SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat) override

References DoCreateSilent(), floatSample, id, and mAllBlocks.

Referenced by DoCreateFromXML().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DoCreateFromXML()

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

Implements SampleBlockFactory.

Definition at line 259 of file SqliteSampleBlock.cpp.

261{
262 // loop through attrs, which is a null-terminated list of attribute-value pairs
263 for (auto pair : attrs)
264 {
265 auto attr = pair.first;
266 auto value = pair.second;
267
268 long long nValue;
269
270 if (attr == "blockid" && value.TryGet(nValue))
271 return DoCreateFromId(srcformat, nValue);
272 }
273
274 return nullptr;
275}
SampleBlockPtr DoCreateFromId(sampleFormat srcformat, SampleBlockID id) override

References DoCreateFromId().

Here is the call graph for this function:

◆ DoCreateSilent()

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

Implements SampleBlockFactory.

Definition at line 241 of file SqliteSampleBlock.cpp.

243{
244 auto id = -static_cast< SampleBlockID >(numsamples);
245 auto &result = sSilentBlocks[ id ];
246 if ( !result ) {
247 result = std::make_shared<SqliteSampleBlock>(nullptr);
248 result->mBlockID = id;
249
250 // Ignore the supplied sample format
251 result->SetSizes(numsamples, floatSample);
252 result->mValid = true;
253 }
254
255 return result;
256}
static std::map< SampleBlockID, std::shared_ptr< SqliteSampleBlock > > sSilentBlocks

References floatSample, id, and sSilentBlocks.

Referenced by DoCreateFromId().

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 226 of file SqliteSampleBlock.cpp.

227{
228 SampleBlockIDs result;
229 for (auto end = mAllBlocks.end(), it = mAllBlocks.begin(); it != end;) {
230 if (it->second.expired())
231 // Tighten up the map
232 it = mAllBlocks.erase(it);
233 else {
234 result.insert( it->first );
235 ++it;
236 }
237 }
238 return result;
239}
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:147
const char * end(const char *str) noexcept
Definition: StringUtils.h:106

References details::end().

Here is the call graph for this function:

◆ GetSampleBlockDeletionCallback()

SampleBlock::DeletionCallback SqliteSampleBlockFactory::GetSampleBlockDeletionCallback ( ) const
inline

Definition at line 172 of file SqliteSampleBlock.cpp.

173 {
175 }
SampleBlock::DeletionCallback mSampleBlockDeletionCallback

References mSampleBlockDeletionCallback.

◆ OnBeginPurge()

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

Definition at line 1102 of file SqliteSampleBlock.cpp.

1103{
1104 // Install a callback function that updates a progress indicator
1105 using namespace BasicUI;
1106
1107 //Avoid showing dialog to the user if purge operation
1108 //does not take much time, as it will resign focus from main window
1109 //but dialog itself may not be presented to the user at all.
1110 //On MacOS 13 (bug #3975) focus isn't restored in that case.
1111 constexpr auto ProgressDialogShowDelay = std::chrono::milliseconds (200);
1112 const auto nToDelete = EstimateRemovedBlocks(mProject, begin, end);
1113 if(nToDelete == 0)
1114 return;
1115 auto purgeStartTime = std::chrono::system_clock::now();
1116 std::shared_ptr<ProgressDialog> progressDialog;
1117 mSampleBlockDeletionCallback = [=, nDeleted = 0](auto&) mutable {
1118 ++nDeleted;
1119 if(!progressDialog)
1120 {
1121 auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
1122 std::chrono::system_clock::now() - purgeStartTime);
1123 if(elapsed >= ProgressDialogShowDelay)
1124 progressDialog = MakeProgress(XO("Progress"), XO("Discarding undo/redo history"), 0);
1125 }
1126 else
1127 progressDialog->Poll(nDeleted, nToDelete);
1128 };
1129}
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::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:294
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101

References details::begin(), details::end(), EstimateRemovedBlocks(), BasicUI::MakeProgress(), mProject, mSampleBlockDeletionCallback, and XO().

Here is the call graph for this function:

◆ OnEndPurge()

void SqliteSampleBlockFactory::OnEndPurge ( )
private

Definition at line 1131 of file SqliteSampleBlock.cpp.

1132{
1134}

References mSampleBlockDeletionCallback.

Member Data Documentation

◆ mAllBlocks

AllBlocksMap SqliteSampleBlockFactory::mAllBlocks
private

Definition at line 194 of file SqliteSampleBlock.cpp.

Referenced by DoCreate(), and DoCreateFromId().

◆ mppConnection

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

Definition at line 186 of file SqliteSampleBlock.cpp.

◆ mProject

AudacityProject& SqliteSampleBlockFactory::mProject
private

Definition at line 183 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge().

◆ mSampleBlockDeletionCallback

SampleBlock::DeletionCallback SqliteSampleBlockFactory::mSampleBlockDeletionCallback
private

Definition at line 185 of file SqliteSampleBlock.cpp.

Referenced by GetSampleBlockDeletionCallback(), OnBeginPurge(), and OnEndPurge().

◆ mUndoSubscription

Observer::Subscription SqliteSampleBlockFactory::mUndoSubscription
private

Definition at line 184 of file SqliteSampleBlock.cpp.

Referenced by SqliteSampleBlockFactory().

◆ SqliteSampleBlock

friend SqliteSampleBlockFactory::SqliteSampleBlock
private

Definition at line 181 of file SqliteSampleBlock.cpp.


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