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

Constructor & Destructor Documentation

◆ SqliteSampleBlockFactory()

SqliteSampleBlockFactory::SqliteSampleBlockFactory ( AudacityProject project)
explicit

Definition at line 192 of file SqliteSampleBlock.cpp.

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

213{
214 auto sb = std::make_shared<SqliteSampleBlock>(shared_from_this());
215 sb->SetSamples(src, numsamples, srcformat);
216 // block id has now been assigned
217 mAllBlocks[ sb->GetBlockID() ] = sb;
218 return sb;
219}

References mAllBlocks.

◆ DoCreateFromId()

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

Implements SampleBlockFactory.

Definition at line 272 of file SqliteSampleBlock.cpp.

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

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

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

222{
223 SampleBlockIDs result;
224 for (auto end = mAllBlocks.end(), it = mAllBlocks.begin(); it != end;) {
225 if (it->second.expired())
226 // Tighten up the map
227 it = mAllBlocks.erase(it);
228 else {
229 result.insert( it->first );
230 ++it;
231 }
232 }
233 return result;
234}
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:150
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 1092 of file SqliteSampleBlock.cpp.

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

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

Here is the call graph for this function:

◆ OnEndPurge()

void SqliteSampleBlockFactory::OnEndPurge ( )
private

Definition at line 1121 of file SqliteSampleBlock.cpp.

1122{
1123 mScope.reset();
1124}

References mScope.

Member Data Documentation

◆ mAllBlocks

AllBlocksMap SqliteSampleBlockFactory::mAllBlocks
private

Definition at line 189 of file SqliteSampleBlock.cpp.

Referenced by DoCreate(), and DoCreateFromId().

◆ mppConnection

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

Definition at line 181 of file SqliteSampleBlock.cpp.

◆ mProject

AudacityProject& SqliteSampleBlockFactory::mProject
private

Definition at line 178 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge().

◆ mScope

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

Definition at line 180 of file SqliteSampleBlock.cpp.

Referenced by OnBeginPurge(), and OnEndPurge().

◆ mUndoSubscription

Observer::Subscription SqliteSampleBlockFactory::mUndoSubscription
private

Definition at line 179 of file SqliteSampleBlock.cpp.

Referenced by SqliteSampleBlockFactory().

◆ SqliteSampleBlock

friend SqliteSampleBlockFactory::SqliteSampleBlock
private

Definition at line 176 of file SqliteSampleBlock.cpp.


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