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
 
void OnSampleBlockDtor (const SampleBlock &)
 
- 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
 
std::function< void()> 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 193 of file SqliteSampleBlock.cpp.

Constructor & Destructor Documentation

◆ SqliteSampleBlockFactory()

SqliteSampleBlockFactory::SqliteSampleBlockFactory ( AudacityProject project)
explicit

Definition at line 198 of file SqliteSampleBlock.cpp.

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

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

References mAllBlocks.

◆ DoCreateFromId()

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

Implements SampleBlockFactory.

Definition at line 278 of file SqliteSampleBlock.cpp.

280{
281 if (id <= 0)
282 return DoCreateSilent(-id, floatSample);
283
284 // First see if this block id was previously loaded
285 auto& wb = mAllBlocks[id];
286
287 if (auto block = wb.lock())
288 return block;
289
290 // First sight of this id
291 auto ssb = std::make_shared<SqliteSampleBlock>(shared_from_this());
292 wb = ssb;
293 ssb->mSampleFormat = srcformat;
294 // This may throw database errors
295 // It initializes the rest of the fields
296 ssb->Load(static_cast<SampleBlockID>(id));
297
298 return ssb;
299}
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 260 of file SqliteSampleBlock.cpp.

262{
263 // loop through attrs, which is a null-terminated list of attribute-value pairs
264 for (auto pair : attrs)
265 {
266 auto attr = pair.first;
267 auto value = pair.second;
268
269 long long nValue;
270
271 if (attr == "blockid" && value.TryGet(nValue))
272 return DoCreateFromId(srcformat, nValue);
273 }
274
275 return nullptr;
276}
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 242 of file SqliteSampleBlock.cpp.

244{
245 auto id = -static_cast< SampleBlockID >(numsamples);
246 auto &result = sSilentBlocks[ id ];
247 if ( !result ) {
248 result = std::make_shared<SqliteSampleBlock>(nullptr);
249 result->mBlockID = id;
250
251 // Ignore the supplied sample format
252 result->SetSizes(numsamples, floatSample);
253 result->mValid = true;
254 }
255
256 return result;
257}
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 227 of file SqliteSampleBlock.cpp.

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

References details::end().

Here is the call graph for this function:

◆ OnBeginPurge()

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

Definition at line 1100 of file SqliteSampleBlock.cpp.

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

1130{
1132}

References mSampleBlockDeletionCallback.

◆ OnSampleBlockDtor()

void SqliteSampleBlockFactory::OnSampleBlockDtor ( const SampleBlock )
inline

Definition at line 172 of file SqliteSampleBlock.cpp.

References mSampleBlockDeletionCallback.

Member Data Documentation

◆ mAllBlocks

AllBlocksMap SqliteSampleBlockFactory::mAllBlocks
private

Definition at line 195 of file SqliteSampleBlock.cpp.

Referenced by DoCreate(), and DoCreateFromId().

◆ mppConnection

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

Definition at line 187 of file SqliteSampleBlock.cpp.

◆ mProject

AudacityProject& SqliteSampleBlockFactory::mProject
private

Definition at line 184 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge().

◆ mSampleBlockDeletionCallback

std::function<void()> SqliteSampleBlockFactory::mSampleBlockDeletionCallback
private

Definition at line 186 of file SqliteSampleBlock.cpp.

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

◆ mUndoSubscription

Observer::Subscription SqliteSampleBlockFactory::mUndoSubscription
private

Definition at line 185 of file SqliteSampleBlock.cpp.

Referenced by SqliteSampleBlockFactory().

◆ SqliteSampleBlock

friend SqliteSampleBlockFactory::SqliteSampleBlock
private

Definition at line 182 of file SqliteSampleBlock.cpp.


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