Audacity  3.0.3
Public Member Functions | Private Types | 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 wxChar **attrs) override
 
BlockDeletionCallback SetBlockDeletionCallback (BlockDeletionCallback callback) override
 Install a callback, returning the previously installed callback. More...
 
- 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 wxChar **attrs)
 

Private Types

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

Private Attributes

friend SqliteSampleBlock
 
const std::shared_ptr< ConnectionPtrmppConnection
 
AllBlocksMap mAllBlocks
 
BlockDeletionCallback mCallback
 

Additional Inherited Members

- Public Types inherited from SampleBlockFactory
using SampleBlockIDs = std::unordered_set< SampleBlockID >
 
using BlockDeletionCallback = std::function< void(const SampleBlock &) >
 Type of function that is informed when a block is about to be deleted. More...
 
- Static Public Member Functions inherited from SampleBlockFactory
static SampleBlockFactoryFactory RegisterFactoryFactory (SampleBlockFactoryFactory newFactory)
 
static SampleBlockFactoryPtr New (AudacityProject &project)
 

Detailed Description

Implementation of SampleBlockFactory using Sqlite database.

Definition at line 132 of file SqliteSampleBlock.cpp.

Member Typedef Documentation

◆ AllBlocksMap

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

Definition at line 167 of file SqliteSampleBlock.cpp.

Constructor & Destructor Documentation

◆ SqliteSampleBlockFactory()

SqliteSampleBlockFactory::SqliteSampleBlockFactory ( AudacityProject project)
explicit

Definition at line 174 of file SqliteSampleBlock.cpp.

175  : mppConnection{ ConnectionPtr::Get(project).shared_from_this() }
176 {
177 
178 }

References ConnectionPtr::Get().

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

184 {
185  auto sb = std::make_shared<SqliteSampleBlock>(shared_from_this());
186  sb->SetSamples(src, numsamples, srcformat);
187  // block id has now been assigned
188  mAllBlocks[ sb->GetBlockID() ] = sb;
189  return sb;
190 }

References mAllBlocks.

◆ DoCreateFromXML()

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

Implements SampleBlockFactory.

Definition at line 225 of file SqliteSampleBlock.cpp.

227 {
228  std::shared_ptr<SampleBlock> sb;
229 
230  int found = 0;
231 
232  // loop through attrs, which is a null-terminated list of attribute-value pairs
233  while(*attrs)
234  {
235  const wxChar *attr = *attrs++;
236  const wxChar *value = *attrs++;
237 
238  if (!value)
239  {
240  break;
241  }
242 
243  const wxString strValue = value; // promote string, we need this for all
244  double dblValue;
245  long long nValue;
246 
247  if (wxStrcmp(attr, wxT("blockid")) == 0 &&
248  XMLValueChecker::IsGoodInt(strValue) && strValue.ToLongLong(&nValue))
249  {
250  if (nValue <= 0) {
251  sb = DoCreateSilent( -nValue, floatSample );
252  }
253  else {
254  // First see if this block id was previously loaded
255  auto &wb = mAllBlocks[ nValue ];
256  auto pb = wb.lock();
257  if (pb)
258  // Reuse the block
259  sb = pb;
260  else {
261  // First sight of this id
262  auto ssb =
263  std::make_shared<SqliteSampleBlock>(shared_from_this());
264  wb = ssb;
265  sb = ssb;
266  ssb->mSampleFormat = srcformat;
267  // This may throw database errors
268  // It initializes the rest of the fields
269  ssb->Load((SampleBlockID) nValue);
270  }
271  }
272  found++;
273  }
274  }
275 
276  // Were all attributes found?
277  if (found != 1)
278  {
279  return nullptr;
280  }
281 
282  return sb;
283 }

References DoCreateSilent(), floatSample, XMLValueChecker::IsGoodInt(), 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 207 of file SqliteSampleBlock.cpp.

209 {
210  auto id = -static_cast< SampleBlockID >(numsamples);
211  auto &result = sSilentBlocks[ id ];
212  if ( !result ) {
213  result = std::make_shared<SqliteSampleBlock>(nullptr);
214  result->mBlockID = id;
215 
216  // Ignore the supplied sample format
217  result->SetSizes(numsamples, floatSample);
218  result->mValid = true;
219  }
220 
221  return result;
222 }

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

193 {
194  SampleBlockIDs result;
195  for (auto end = mAllBlocks.end(), it = mAllBlocks.begin(); it != end;) {
196  if (it->second.expired())
197  // Tighten up the map
198  it = mAllBlocks.erase(it);
199  else {
200  result.insert( it->first );
201  ++it;
202  }
203  }
204  return result;
205 }

◆ SetBlockDeletionCallback()

auto SqliteSampleBlockFactory::SetBlockDeletionCallback ( BlockDeletionCallback  callback)
overridevirtual

Install a callback, returning the previously installed callback.

Implements SampleBlockFactory.

Definition at line 285 of file SqliteSampleBlock.cpp.

287 {
288  auto result = mCallback;
289  mCallback = std::move( callback );
290  return result;
291 }

Member Data Documentation

◆ mAllBlocks

AllBlocksMap SqliteSampleBlockFactory::mAllBlocks
private

Definition at line 169 of file SqliteSampleBlock.cpp.

Referenced by DoCreate(), and DoCreateFromXML().

◆ mCallback

BlockDeletionCallback SqliteSampleBlockFactory::mCallback
private

Definition at line 171 of file SqliteSampleBlock.cpp.

◆ mppConnection

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

Definition at line 161 of file SqliteSampleBlock.cpp.

◆ SqliteSampleBlock

friend SqliteSampleBlockFactory::SqliteSampleBlock
private

Definition at line 159 of file SqliteSampleBlock.cpp.


The documentation for this class was generated from the following file:
XMLValueChecker::IsGoodInt
static bool IsGoodInt(const wxString &strInt)
Check that the supplied string can be converted to a long (32bit) integer.
Definition: XMLTagHandler.cpp:157
SqliteSampleBlockFactory::mppConnection
const std::shared_ptr< ConnectionPtr > mppConnection
Definition: SqliteSampleBlock.cpp:161
SampleBlockID
long long SampleBlockID
Definition: ProjectFileIO.h:39
floatSample
@ floatSample
Definition: SampleFormat.h:34
sSilentBlocks
static std::map< SampleBlockID, std::shared_ptr< SqliteSampleBlock > > sSilentBlocks
Definition: SqliteSampleBlock.cpp:129
SampleBlockFactory::SampleBlockIDs
std::unordered_set< SampleBlockID > SampleBlockIDs
Definition: SampleBlock.h:134
SqliteSampleBlockFactory::mAllBlocks
AllBlocksMap mAllBlocks
Definition: SqliteSampleBlock.cpp:169
id
int id
Definition: WaveTrackControls.cpp:577
ConnectionPtr::Get
static ConnectionPtr & Get(AudacityProject &project)
Definition: DBConnection.cpp:728
SqliteSampleBlockFactory::mCallback
BlockDeletionCallback mCallback
Definition: SqliteSampleBlock.cpp:171
SqliteSampleBlockFactory::DoCreateSilent
SampleBlockPtr DoCreateSilent(size_t numsamples, sampleFormat srcformat) override
Definition: SqliteSampleBlock.cpp:207