Audacity 3.2.0
Public Types | Public Member Functions | Private Types | Private Attributes | List of all members
audacity::cloud::audiocom::sync::BlockHasher::Workers Class Referencefinal
Collaboration diagram for audacity::cloud::audiocom::sync::BlockHasher::Workers:
[legend]

Public Types

using SampleData = std::vector< std::remove_pointer_t< samplePtr > >
 

Public Member Functions

 Workers (BlockHashCache &cache, const std::vector< LockedBlock > blocks, std::function< void()> onComplete)
 
bool IsReady () const
 
std::pair< std::string, bool > ComputeHash (SampleData &sampleData, const LockedBlock &block) const
 
void NotifyReady ()
 
std::vector< std::pair< int64_t, std::string > > TakeResult ()
 

Private Types

using Result = std::unordered_map< int64_t, std::pair< std::string, bool > >
 

Private Attributes

const size_t mThreadsCount
 
BlockHashCachemCache
 
std::vector< std::future< Result > > mResults
 
std::future< void > mWaiter
 
std::function< void()> mOnComplete
 

Detailed Description

Definition at line 25 of file BlockHasher.cpp.

Member Typedef Documentation

◆ Result

using audacity::cloud::audiocom::sync::BlockHasher::Workers::Result = std::unordered_map<int64_t, std::pair<std::string, bool> >
private

Definition at line 150 of file BlockHasher.cpp.

◆ SampleData

Definition at line 28 of file BlockHasher.cpp.

Constructor & Destructor Documentation

◆ Workers()

audacity::cloud::audiocom::sync::BlockHasher::Workers::Workers ( BlockHashCache cache,
const std::vector< LockedBlock blocks,
std::function< void()>  onComplete 
)
inlineexplicit

Definition at line 30 of file BlockHasher.cpp.

33 : mThreadsCount { std::max(1u, std::thread::hardware_concurrency() / 2) }
34 , mCache { cache }
35 , mOnComplete { std::move(onComplete) }
36 {
37 mResults.reserve(mThreadsCount);
38
39 const auto blocksCount = blocks.size();
40 // Try to add no more that 1 extra block per thread
41 const size_t blockPerThread = blocks.size() / mThreadsCount + 1;
42
43 for (size_t i = 0; i < mThreadsCount; ++i)
44 {
45 const size_t startIndex = i;
46
47 if (startIndex >= blocks.size())
48 break;
49
50 std::vector<LockedBlock> threadBlocks;
51 threadBlocks.reserve(blockPerThread);
52
53 for (size_t j = startIndex; j < blocksCount; j += mThreadsCount)
54 threadBlocks.emplace_back(blocks[j]);
55
56 mResults.emplace_back(std::async(
57 std::launch::async,
58 [this, threadBlocks = std::move(threadBlocks)]()
59 {
60 Result result;
61 SampleData sampleData;
62
63 for (const auto& block : threadBlocks)
64 result.emplace(block.Id, ComputeHash(sampleData, block));
65
66 return result;
67 }));
68 }
69
70 mWaiter = std::async(
71 std::launch::async,
72 [this]
73 {
74 for (auto& fut : mResults)
75 fut.wait();
76
78 });
79 }
std::vector< std::future< Result > > mResults
std::pair< std::string, bool > ComputeHash(SampleData &sampleData, const LockedBlock &block) const
Definition: BlockHasher.cpp:93
std::unordered_map< int64_t, std::pair< std::string, bool > > Result
std::vector< std::remove_pointer_t< samplePtr > > SampleData
Definition: BlockHasher.cpp:28

References ComputeHash(), mResults, mThreadsCount, mWaiter, and NotifyReady().

Here is the call graph for this function:

Member Function Documentation

◆ ComputeHash()

std::pair< std::string, bool > audacity::cloud::audiocom::sync::BlockHasher::Workers::ComputeHash ( SampleData sampleData,
const LockedBlock block 
) const
inline

Definition at line 93 of file BlockHasher.cpp.

94 {
95 std::string hash;
96
97 if (mCache.GetHash(block.Id, hash))
98 return { hash, false };
99
100 const auto sampleFormat = block.Format;
101 const auto sampleCount = block.Block->GetSampleCount();
102 const auto dataSize = sampleCount * SAMPLE_SIZE(sampleFormat);
103
104 sampleData.resize(dataSize);
105
106 const size_t samplesRead = block.Block->GetSamples(
107 sampleData.data(), sampleFormat, 0, sampleCount, false);
108
109 if (samplesRead != sampleCount)
110 return { {}, false };
111
112 hash = crypto::sha256(sampleData);
113
114 return { hash, true };
115 }
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
#define SAMPLE_SIZE(SampleFormat)
Definition: SampleFormat.h:52
virtual bool GetHash(int64_t blockId, std::string &hash) const =0
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
std::string sha256(const T &data)
Definition: SHA256.h:50

References audacity::cloud::audiocom::sync::LockedBlock::Block, audacity::cloud::audiocom::sync::LockedBlock::Format, audacity::cloud::audiocom::sync::BlockHashCache::GetHash(), audacity::cloud::audiocom::sync::LockedBlock::Id, mCache, SAMPLE_SIZE, and crypto::sha256().

Referenced by Workers().

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

◆ IsReady()

bool audacity::cloud::audiocom::sync::BlockHasher::Workers::IsReady ( ) const
inline

Definition at line 81 of file BlockHasher.cpp.

82 {
83 return std::all_of(
84 mResults.begin(), mResults.end(),
85 [](const auto& result)
86 {
87 return result.wait_for(std::chrono::seconds(0)) ==
88 std::future_status::ready;
89 });
90 }

References mResults.

◆ NotifyReady()

void audacity::cloud::audiocom::sync::BlockHasher::Workers::NotifyReady ( )
inline

Definition at line 117 of file BlockHasher.cpp.

118 {
119 if (mOnComplete)
120 mOnComplete();
121 }

References mOnComplete.

Referenced by Workers().

Here is the caller graph for this function:

◆ TakeResult()

std::vector< std::pair< int64_t, std::string > > audacity::cloud::audiocom::sync::BlockHasher::Workers::TakeResult ( )
inline

Definition at line 123 of file BlockHasher.cpp.

124 {
125 std::vector<std::pair<int64_t, std::string>> result;
126
127 for (auto& fut : mResults)
128 {
129 const auto& threadResult = fut.get();
130
131 for (const auto& [id, hash] : threadResult)
132 {
133 result.emplace_back(std::make_pair(id, hash.first));
134
135 if (hash.second)
136 mCache.UpdateHash(id, hash.first);
137 }
138 }
139
140 mResults.clear();
141
142 return result;
143 }
virtual void UpdateHash(int64_t blockId, const std::string &hash)=0

References mCache, mResults, and audacity::cloud::audiocom::sync::BlockHashCache::UpdateHash().

Here is the call graph for this function:

Member Data Documentation

◆ mCache

BlockHashCache& audacity::cloud::audiocom::sync::BlockHasher::Workers::mCache
private

Definition at line 148 of file BlockHasher.cpp.

Referenced by ComputeHash(), and TakeResult().

◆ mOnComplete

std::function<void()> audacity::cloud::audiocom::sync::BlockHasher::Workers::mOnComplete
private

Definition at line 154 of file BlockHasher.cpp.

Referenced by NotifyReady().

◆ mResults

std::vector<std::future<Result> > audacity::cloud::audiocom::sync::BlockHasher::Workers::mResults
private

Definition at line 151 of file BlockHasher.cpp.

Referenced by IsReady(), TakeResult(), and Workers().

◆ mThreadsCount

const size_t audacity::cloud::audiocom::sync::BlockHasher::Workers::mThreadsCount
private

Definition at line 146 of file BlockHasher.cpp.

Referenced by Workers().

◆ mWaiter

std::future<void> audacity::cloud::audiocom::sync::BlockHasher::Workers::mWaiter
private

Definition at line 152 of file BlockHasher.cpp.

Referenced by Workers().


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