summaryrefslogtreecommitdiff
path: root/src/DefaultPieceStorage.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/DefaultPieceStorage.cc')
-rw-r--r--src/DefaultPieceStorage.cc471
1 files changed, 227 insertions, 244 deletions
diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc
index 5214ab0e..a152c368 100644
--- a/src/DefaultPieceStorage.cc
+++ b/src/DefaultPieceStorage.cc
@@ -70,63 +70,64 @@
#include "RequestGroup.h"
#include "SimpleRandomizer.h"
#ifdef ENABLE_BITTORRENT
-# include "bittorrent_helper.h"
+#include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
namespace aria2 {
-DefaultPieceStorage::DefaultPieceStorage
-(const std::shared_ptr<DownloadContext>& downloadContext, const Option* option)
- : downloadContext_(downloadContext),
- bitfieldMan_(make_unique<BitfieldMan>
- (downloadContext->getPieceLength(),
- downloadContext->getTotalLength())),
- diskWriterFactory_(std::make_shared<DefaultDiskWriterFactory>()),
- endGame_(false),
- endGamePieceNum_(END_GAME_PIECE_NUM),
- option_(option),
- pieceStatMan_(std::make_shared<PieceStatMan>
- (downloadContext->getNumPieces(), true)),
- pieceSelector_(make_unique<RarestPieceSelector>(pieceStatMan_)),
- wrDiskCache_(nullptr)
+DefaultPieceStorage::DefaultPieceStorage(
+ const std::shared_ptr<DownloadContext>& downloadContext,
+ const Option* option)
+ : downloadContext_(downloadContext),
+ bitfieldMan_(make_unique<BitfieldMan>(downloadContext->getPieceLength(),
+ downloadContext->getTotalLength())),
+ diskWriterFactory_(std::make_shared<DefaultDiskWriterFactory>()),
+ endGame_(false),
+ endGamePieceNum_(END_GAME_PIECE_NUM),
+ option_(option),
+ pieceStatMan_(std::make_shared<PieceStatMan>(
+ downloadContext->getNumPieces(), true)),
+ pieceSelector_(make_unique<RarestPieceSelector>(pieceStatMan_)),
+ wrDiskCache_(nullptr)
{
const std::string& pieceSelectorOpt =
- option_->get(PREF_STREAM_PIECE_SELECTOR);
- if(pieceSelectorOpt.empty() || pieceSelectorOpt == A2_V_DEFAULT) {
- streamPieceSelector_ = make_unique<DefaultStreamPieceSelector>
- (bitfieldMan_.get());
- } else if(pieceSelectorOpt == V_INORDER) {
- streamPieceSelector_ = make_unique<InorderStreamPieceSelector>
- (bitfieldMan_.get());
- } else if(pieceSelectorOpt == A2_V_RANDOM) {
- streamPieceSelector_ = make_unique<RandomStreamPieceSelector>
- (bitfieldMan_.get());
- } else if(pieceSelectorOpt == A2_V_GEOM) {
- streamPieceSelector_ = make_unique<GeomStreamPieceSelector>
- (bitfieldMan_.get(), 1.5);
+ option_->get(PREF_STREAM_PIECE_SELECTOR);
+ if (pieceSelectorOpt.empty() || pieceSelectorOpt == A2_V_DEFAULT) {
+ streamPieceSelector_ =
+ make_unique<DefaultStreamPieceSelector>(bitfieldMan_.get());
+ }
+ else if (pieceSelectorOpt == V_INORDER) {
+ streamPieceSelector_ =
+ make_unique<InorderStreamPieceSelector>(bitfieldMan_.get());
+ }
+ else if (pieceSelectorOpt == A2_V_RANDOM) {
+ streamPieceSelector_ =
+ make_unique<RandomStreamPieceSelector>(bitfieldMan_.get());
+ }
+ else if (pieceSelectorOpt == A2_V_GEOM) {
+ streamPieceSelector_ =
+ make_unique<GeomStreamPieceSelector>(bitfieldMan_.get(), 1.5);
}
}
-DefaultPieceStorage::~DefaultPieceStorage()
-{}
+DefaultPieceStorage::~DefaultPieceStorage() {}
-std::shared_ptr<Piece> DefaultPieceStorage::checkOutPiece
-(size_t index, cuid_t cuid)
+std::shared_ptr<Piece> DefaultPieceStorage::checkOutPiece(size_t index,
+ cuid_t cuid)
{
bitfieldMan_->setUseBit(index);
std::shared_ptr<Piece> piece = findUsedPiece(index);
- if(!piece) {
- piece = std::make_shared<Piece>
- (index, bitfieldMan_->getBlockLength(index));
+ if (!piece) {
+ piece = std::make_shared<Piece>(index, bitfieldMan_->getBlockLength(index));
piece->setHashType(downloadContext_->getPieceHashType());
addUsedPiece(piece);
}
piece->addUser(cuid);
RequestGroup* group = downloadContext_->getOwnerRequestGroup();
- if((!group || !group->inMemoryDownload()) &&
- wrDiskCache_ && !piece->getWrDiskCacheEntry()) {
+ if ((!group || !group->inMemoryDownload()) && wrDiskCache_ &&
+ !piece->getWrDiskCacheEntry()) {
// So, we rely on the fact that diskAdaptor_ is not reinitialized
// in the session.
piece->initWrCache(wrDiskCache_, diskAdaptor_);
@@ -141,12 +142,12 @@ std::shared_ptr<Piece> DefaultPieceStorage::checkOutPiece
std::shared_ptr<Piece> DefaultPieceStorage::getPiece(size_t index)
{
std::shared_ptr<Piece> piece;
- if(index <= bitfieldMan_->getMaxIndex()) {
+ if (index <= bitfieldMan_->getMaxIndex()) {
piece = findUsedPiece(index);
- if(!piece) {
- piece = std::make_shared<Piece>(index,
- bitfieldMan_->getBlockLength(index));
- if(hasPiece(index)) {
+ if (!piece) {
+ piece =
+ std::make_shared<Piece>(index, bitfieldMan_->getBlockLength(index));
+ if (hasPiece(index)) {
piece->setAllBlock();
}
}
@@ -167,10 +168,11 @@ std::shared_ptr<Piece> DefaultPieceStorage::findUsedPiece(size_t index) const
p->setIndex(index);
auto i = usedPieces_.find(p);
- if(i == usedPieces_.end()) {
+ if (i == usedPieces_.end()) {
p.reset();
return p;
- } else {
+ }
+ else {
return *i;
}
}
@@ -183,55 +185,56 @@ bool DefaultPieceStorage::hasMissingPiece(const std::shared_ptr<Peer>& peer)
peer->getBitfieldLength());
}
-void DefaultPieceStorage::getMissingPiece
-(std::vector<std::shared_ptr<Piece> >& pieces,
- size_t minMissingBlocks,
- const unsigned char* bitfield,
- size_t length,
- cuid_t cuid)
+void DefaultPieceStorage::getMissingPiece(
+ std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
+ const unsigned char* bitfield, size_t length, cuid_t cuid)
{
const size_t mislen = bitfieldMan_->getBitfieldLength();
auto misbitfield = make_unique<unsigned char[]>(mislen);
size_t blocks = bitfieldMan_->countBlock();
size_t misBlock = 0;
- if(isEndGame()) {
- bool r = bitfieldMan_->getAllMissingIndexes
- (misbitfield.get(), mislen, bitfield, length);
- if(!r) {
+ if (isEndGame()) {
+ bool r = bitfieldMan_->getAllMissingIndexes(misbitfield.get(), mislen,
+ bitfield, length);
+ if (!r) {
return;
}
std::vector<size_t> indexes;
- for(size_t i = 0; i < blocks; ++i) {
- if(bitfield::test(misbitfield, blocks, i)) {
+ for (size_t i = 0; i < blocks; ++i) {
+ if (bitfield::test(misbitfield, blocks, i)) {
indexes.push_back(i);
}
}
std::shuffle(indexes.begin(), indexes.end(),
*SimpleRandomizer::getInstance());
- for(std::vector<size_t>::const_iterator i = indexes.begin(),
- eoi = indexes.end(); i != eoi && misBlock < minMissingBlocks; ++i) {
+ for (std::vector<size_t>::const_iterator i = indexes.begin(),
+ eoi = indexes.end();
+ i != eoi && misBlock < minMissingBlocks; ++i) {
std::shared_ptr<Piece> piece = checkOutPiece(*i, cuid);
- if(piece->getUsedBySegment()) {
+ if (piece->getUsedBySegment()) {
// We don't share piece downloaded via HTTP/FTP
piece->removeUser(cuid);
- } else {
+ }
+ else {
pieces.push_back(piece);
misBlock += piece->countMissingBlock();
}
}
- } else {
- bool r = bitfieldMan_->getAllMissingUnusedIndexes
- (misbitfield.get(), mislen, bitfield, length);
- if(!r) {
+ }
+ else {
+ bool r = bitfieldMan_->getAllMissingUnusedIndexes(misbitfield.get(), mislen,
+ bitfield, length);
+ if (!r) {
return;
}
- while(misBlock < minMissingBlocks) {
+ while (misBlock < minMissingBlocks) {
size_t index;
- if(pieceSelector_->select(index, misbitfield.get(), blocks)) {
+ if (pieceSelector_->select(index, misbitfield.get(), blocks)) {
pieces.push_back(checkOutPiece(index, cuid));
bitfield::flipBit(misbitfield.get(), blocks, index);
misBlock += pieces.back()->countMissingBlock();
- } else {
+ }
+ else {
break;
}
}
@@ -248,132 +251,119 @@ void unsetExcludedIndexes(BitfieldMan& bitfield,
}
} // namespace
-void DefaultPieceStorage::createFastIndexBitfield
-(BitfieldMan& bitfield, const std::shared_ptr<Peer>& peer)
+void DefaultPieceStorage::createFastIndexBitfield(
+ BitfieldMan& bitfield, const std::shared_ptr<Peer>& peer)
{
const auto& is = peer->getPeerAllowedIndexSet();
- for(const auto& i: is) {
- if(!bitfieldMan_->isBitSet(i) && peer->hasPiece(i)) {
+ for (const auto& i : is) {
+ if (!bitfieldMan_->isBitSet(i) && peer->hasPiece(i)) {
bitfield.setBit(i);
}
}
}
-void DefaultPieceStorage::getMissingPiece
-(std::vector<std::shared_ptr<Piece> >& pieces,
- size_t minMissingBlocks,
- const std::shared_ptr<Peer>& peer,
- cuid_t cuid)
+void DefaultPieceStorage::getMissingPiece(
+ std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
+ const std::shared_ptr<Peer>& peer, cuid_t cuid)
{
- getMissingPiece(pieces, minMissingBlocks,
- peer->getBitfield(), peer->getBitfieldLength(),
- cuid);
+ getMissingPiece(pieces, minMissingBlocks, peer->getBitfield(),
+ peer->getBitfieldLength(), cuid);
}
-
-void DefaultPieceStorage::getMissingPiece
-(std::vector<std::shared_ptr<Piece> >& pieces,
- size_t minMissingBlocks,
- const std::shared_ptr<Peer>& peer,
- const std::vector<size_t>& excludedIndexes,
- cuid_t cuid)
+void DefaultPieceStorage::getMissingPiece(
+ std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
+ const std::shared_ptr<Peer>& peer,
+ const std::vector<size_t>& excludedIndexes, cuid_t cuid)
{
BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
bitfieldMan_->getTotalLength());
tempBitfield.setBitfield(peer->getBitfield(), peer->getBitfieldLength());
unsetExcludedIndexes(tempBitfield, excludedIndexes);
- getMissingPiece(pieces, minMissingBlocks,
- tempBitfield.getBitfield(), tempBitfield.getBitfieldLength(),
- cuid);
+ getMissingPiece(pieces, minMissingBlocks, tempBitfield.getBitfield(),
+ tempBitfield.getBitfieldLength(), cuid);
}
-void DefaultPieceStorage::getMissingFastPiece
-(std::vector<std::shared_ptr<Piece> >& pieces,
- size_t minMissingBlocks,
- const std::shared_ptr<Peer>& peer,
- cuid_t cuid)
+void DefaultPieceStorage::getMissingFastPiece(
+ std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
+ const std::shared_ptr<Peer>& peer, cuid_t cuid)
{
- if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
+ if (peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
bitfieldMan_->getTotalLength());
createFastIndexBitfield(tempBitfield, peer);
- getMissingPiece(pieces, minMissingBlocks,
- tempBitfield.getBitfield(),
- tempBitfield.getBitfieldLength(),
- cuid);
+ getMissingPiece(pieces, minMissingBlocks, tempBitfield.getBitfield(),
+ tempBitfield.getBitfieldLength(), cuid);
}
}
-void DefaultPieceStorage::getMissingFastPiece
-(std::vector<std::shared_ptr<Piece> >& pieces,
- size_t minMissingBlocks,
- const std::shared_ptr<Peer>& peer,
- const std::vector<size_t>& excludedIndexes,
- cuid_t cuid)
+void DefaultPieceStorage::getMissingFastPiece(
+ std::vector<std::shared_ptr<Piece>>& pieces, size_t minMissingBlocks,
+ const std::shared_ptr<Peer>& peer,
+ const std::vector<size_t>& excludedIndexes, cuid_t cuid)
{
- if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
+ if (peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
BitfieldMan tempBitfield(bitfieldMan_->getBlockLength(),
bitfieldMan_->getTotalLength());
createFastIndexBitfield(tempBitfield, peer);
unsetExcludedIndexes(tempBitfield, excludedIndexes);
- getMissingPiece(pieces, minMissingBlocks,
- tempBitfield.getBitfield(),
- tempBitfield.getBitfieldLength(),
- cuid);
+ getMissingPiece(pieces, minMissingBlocks, tempBitfield.getBitfield(),
+ tempBitfield.getBitfieldLength(), cuid);
}
}
std::shared_ptr<Piece>
-DefaultPieceStorage::getMissingPiece
-(const std::shared_ptr<Peer>& peer,
- cuid_t cuid)
+DefaultPieceStorage::getMissingPiece(const std::shared_ptr<Peer>& peer,
+ cuid_t cuid)
{
- std::vector<std::shared_ptr<Piece> > pieces;
+ std::vector<std::shared_ptr<Piece>> pieces;
getMissingPiece(pieces, 1, peer, cuid);
- if(pieces.empty()) {
+ if (pieces.empty()) {
return nullptr;
- } else {
+ }
+ else {
return pieces.front();
}
}
-std::shared_ptr<Piece> DefaultPieceStorage::getMissingPiece
-(const std::shared_ptr<Peer>& peer,
- const std::vector<size_t>& excludedIndexes,
- cuid_t cuid)
+std::shared_ptr<Piece>
+DefaultPieceStorage::getMissingPiece(const std::shared_ptr<Peer>& peer,
+ const std::vector<size_t>& excludedIndexes,
+ cuid_t cuid)
{
- std::vector<std::shared_ptr<Piece> > pieces;
+ std::vector<std::shared_ptr<Piece>> pieces;
getMissingPiece(pieces, 1, peer, excludedIndexes, cuid);
- if(pieces.empty()) {
+ if (pieces.empty()) {
return nullptr;
- } else {
+ }
+ else {
return pieces.front();
}
}
-std::shared_ptr<Piece> DefaultPieceStorage::getMissingFastPiece
-(const std::shared_ptr<Peer>& peer,
- cuid_t cuid)
+std::shared_ptr<Piece>
+DefaultPieceStorage::getMissingFastPiece(const std::shared_ptr<Peer>& peer,
+ cuid_t cuid)
{
- std::vector<std::shared_ptr<Piece> > pieces;
+ std::vector<std::shared_ptr<Piece>> pieces;
getMissingFastPiece(pieces, 1, peer, cuid);
- if(pieces.empty()) {
+ if (pieces.empty()) {
return nullptr;
- } else {
+ }
+ else {
return pieces.front();
}
}
-std::shared_ptr<Piece> DefaultPieceStorage::getMissingFastPiece
-(const std::shared_ptr<Peer>& peer,
- const std::vector<size_t>& excludedIndexes,
- cuid_t cuid)
+std::shared_ptr<Piece> DefaultPieceStorage::getMissingFastPiece(
+ const std::shared_ptr<Peer>& peer,
+ const std::vector<size_t>& excludedIndexes, cuid_t cuid)
{
- std::vector<std::shared_ptr<Piece> > pieces;
+ std::vector<std::shared_ptr<Piece>> pieces;
getMissingFastPiece(pieces, 1, peer, excludedIndexes, cuid);
- if(pieces.empty()) {
+ if (pieces.empty()) {
return nullptr;
- } else {
+ }
+ else {
return pieces.front();
}
}
@@ -386,35 +376,35 @@ bool DefaultPieceStorage::hasMissingUnusedPiece()
return bitfieldMan_->getFirstMissingUnusedIndex(index);
}
-std::shared_ptr<Piece> DefaultPieceStorage::getMissingPiece
-(size_t minSplitSize,
- const unsigned char* ignoreBitfield,
- size_t length,
- cuid_t cuid)
+std::shared_ptr<Piece>
+DefaultPieceStorage::getMissingPiece(size_t minSplitSize,
+ const unsigned char* ignoreBitfield,
+ size_t length, cuid_t cuid)
{
size_t index;
- if(streamPieceSelector_->select
- (index, minSplitSize, ignoreBitfield, length)) {
+ if (streamPieceSelector_->select(index, minSplitSize, ignoreBitfield,
+ length)) {
return checkOutPiece(index, cuid);
- } else {
+ }
+ else {
return nullptr;
}
}
-std::shared_ptr<Piece> DefaultPieceStorage::getMissingPiece
-(size_t index,
- cuid_t cuid)
+std::shared_ptr<Piece> DefaultPieceStorage::getMissingPiece(size_t index,
+ cuid_t cuid)
{
- if(hasPiece(index) || isPieceUsed(index)) {
+ if (hasPiece(index) || isPieceUsed(index)) {
return nullptr;
- } else {
+ }
+ else {
return checkOutPiece(index, cuid);
}
}
void DefaultPieceStorage::deleteUsedPiece(const std::shared_ptr<Piece>& piece)
{
- if(!piece) {
+ if (!piece) {
return;
}
usedPieces_.erase(piece);
@@ -459,32 +449,33 @@ void DefaultPieceStorage::deleteUsedPiece(const std::shared_ptr<Piece>& piece)
void DefaultPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
{
- if(!piece) {
+ if (!piece) {
return;
}
deleteUsedPiece(piece);
// if(!isEndGame()) {
// reduceUsedPieces(100);
// }
- if(allDownloadFinished()) {
+ if (allDownloadFinished()) {
return;
}
bitfieldMan_->setBit(piece->getIndex());
bitfieldMan_->unsetUseBit(piece->getIndex());
addPieceStats(piece->getIndex());
- if(downloadFinished()) {
+ if (downloadFinished()) {
downloadContext_->resetDownloadStopTime();
- if(isSelectiveDownloadingMode()) {
+ if (isSelectiveDownloadingMode()) {
A2_LOG_NOTICE(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
// following line was commented out in order to stop sending request
// message after user-specified files were downloaded.
- //finishSelectiveDownloadingMode();
- } else {
+ // finishSelectiveDownloadingMode();
+ }
+ else {
A2_LOG_INFO(MSG_DOWNLOAD_COMPLETED);
}
#ifdef ENABLE_BITTORRENT
- if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
- if(!bittorrent::getTorrentAttrs(downloadContext_)->metadata.empty()) {
+ if (downloadContext_->hasAttribute(CTX_ATTR_BT)) {
+ if (!bittorrent::getTorrentAttrs(downloadContext_)->metadata.empty()) {
#ifdef __MINGW32__
// On Windows, if aria2 opens files with GENERIC_WRITE access
// right, some programs cannot open them aria2 is seeding. To
@@ -500,8 +491,8 @@ void DefaultPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
util::executeHookByOptName(group, option_,
PREF_ON_BT_DOWNLOAD_COMPLETE);
- SingletonHolder<Notifier>::instance()->
- notifyDownloadEvent(EVENT_ON_BT_DOWNLOAD_COMPLETE, group);
+ SingletonHolder<Notifier>::instance()->notifyDownloadEvent(
+ EVENT_ON_BT_DOWNLOAD_COMPLETE, group);
group->enableSeedOnly();
}
@@ -516,18 +507,18 @@ bool DefaultPieceStorage::isSelectiveDownloadingMode()
}
// not unittested
-void DefaultPieceStorage::cancelPiece
-(const std::shared_ptr<Piece>& piece, cuid_t cuid)
+void DefaultPieceStorage::cancelPiece(const std::shared_ptr<Piece>& piece,
+ cuid_t cuid)
{
- if(!piece) {
+ if (!piece) {
return;
}
piece->removeUser(cuid);
- if(!piece->getUsed()) {
+ if (!piece->getUsed()) {
bitfieldMan_->unsetUseBit(piece->getIndex());
}
- if(!isEndGame()) {
- if(piece->getCompletedLength() == 0) {
+ if (!isEndGame()) {
+ if (piece->getCompletedLength() == 0) {
deleteUsedPiece(piece);
}
}
@@ -556,9 +547,9 @@ int64_t DefaultPieceStorage::getFilteredTotalLength()
int64_t DefaultPieceStorage::getCompletedLength()
{
int64_t completedLength =
- bitfieldMan_->getCompletedLength()+getInFlightPieceCompletedLength();
+ bitfieldMan_->getCompletedLength() + getInFlightPieceCompletedLength();
int64_t totalLength = getTotalLength();
- if(completedLength > totalLength) {
+ if (completedLength > totalLength) {
completedLength = totalLength;
}
return completedLength;
@@ -566,14 +557,14 @@ int64_t DefaultPieceStorage::getCompletedLength()
int64_t DefaultPieceStorage::getFilteredCompletedLength()
{
- return bitfieldMan_->getFilteredCompletedLength()+
- getInFlightPieceFilteredCompletedLength();
+ return bitfieldMan_->getFilteredCompletedLength() +
+ getInFlightPieceFilteredCompletedLength();
}
int64_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
{
int64_t len = 0;
- for(auto & elem : usedPieces_) {
+ for (auto& elem : usedPieces_) {
len += elem->getCompletedLength();
}
return len;
@@ -582,8 +573,8 @@ int64_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
int64_t DefaultPieceStorage::getInFlightPieceFilteredCompletedLength() const
{
int64_t len = 0;
- for(auto & elem : usedPieces_) {
- if(bitfieldMan_->isFilterBitSet(elem->getIndex())) {
+ for (auto& elem : usedPieces_) {
+ if (bitfieldMan_->isFilterBitSet(elem->getIndex())) {
len += elem->getCompletedLength();
}
}
@@ -593,20 +584,20 @@ int64_t DefaultPieceStorage::getInFlightPieceFilteredCompletedLength() const
// not unittested
void DefaultPieceStorage::setupFileFilter()
{
- const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
- downloadContext_->getFileEntries();
+ const std::vector<std::shared_ptr<FileEntry>>& fileEntries =
+ downloadContext_->getFileEntries();
bool allSelected = true;
- for(auto & e : fileEntries) {
- if(!e->isRequested()) {
+ for (auto& e : fileEntries) {
+ if (!e->isRequested()) {
allSelected = false;
break;
}
}
- if(allSelected) {
+ if (allSelected) {
return;
}
- for(auto & e: fileEntries) {
- if(e->isRequested()) {
+ for (auto& e : fileEntries) {
+ if (e->isRequested()) {
bitfieldMan_->addFilter(e->getOffset(), e->getLength());
}
}
@@ -614,10 +605,7 @@ void DefaultPieceStorage::setupFileFilter()
}
// not unittested
-void DefaultPieceStorage::clearFileFilter()
-{
- bitfieldMan_->clearFilter();
-}
+void DefaultPieceStorage::clearFileFilter() { bitfieldMan_->clearFilter(); }
// not unittested
bool DefaultPieceStorage::downloadFinished()
@@ -636,18 +624,19 @@ bool DefaultPieceStorage::allDownloadFinished()
// not unittested
void DefaultPieceStorage::initStorage()
{
- if(downloadContext_->getFileEntries().size() == 1) {
+ if (downloadContext_->getFileEntries().size() == 1) {
A2_LOG_DEBUG("Instantiating DirectDiskAdaptor");
auto directDiskAdaptor = make_unique<DirectDiskAdaptor>();
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
- directDiskAdaptor->setFileEntries
- (downloadContext_->getFileEntries().begin(),
- downloadContext_->getFileEntries().end());
+ directDiskAdaptor->setFileEntries(
+ downloadContext_->getFileEntries().begin(),
+ downloadContext_->getFileEntries().end());
- directDiskAdaptor->setDiskWriter
- (diskWriterFactory_->newDiskWriter(directDiskAdaptor->getFilePath()));
+ directDiskAdaptor->setDiskWriter(
+ diskWriterFactory_->newDiskWriter(directDiskAdaptor->getFilePath()));
diskAdaptor_ = std::move(directDiskAdaptor);
- } else {
+ }
+ else {
A2_LOG_DEBUG("Instantiating MultiDiskAdaptor");
auto multiDiskAdaptor = make_unique<MultiDiskAdaptor>();
multiDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
@@ -655,9 +644,10 @@ void DefaultPieceStorage::initStorage()
multiDiskAdaptor->setPieceLength(downloadContext_->getPieceLength());
diskAdaptor_ = std::move(multiDiskAdaptor);
}
- if(option_->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
+ if (option_->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
diskAdaptor_->setFileAllocationMethod(DiskAdaptor::FILE_ALLOC_FALLOC);
- } else if(option_->get(PREF_FILE_ALLOCATION) == V_TRUNC) {
+ }
+ else if (option_->get(PREF_FILE_ALLOCATION) == V_TRUNC) {
diskAdaptor_->setFileAllocationMethod(DiskAdaptor::FILE_ALLOC_TRUNC);
}
}
@@ -679,26 +669,24 @@ const unsigned char* DefaultPieceStorage::getBitfield()
return bitfieldMan_->getBitfield();
}
-std::shared_ptr<DiskAdaptor> DefaultPieceStorage::getDiskAdaptor() {
+std::shared_ptr<DiskAdaptor> DefaultPieceStorage::getDiskAdaptor()
+{
return diskAdaptor_;
}
-WrDiskCache* DefaultPieceStorage::getWrDiskCache()
-{
- return wrDiskCache_;
-}
+WrDiskCache* DefaultPieceStorage::getWrDiskCache() { return wrDiskCache_; }
void DefaultPieceStorage::flushWrDiskCacheEntry()
{
- if(!wrDiskCache_) {
+ if (!wrDiskCache_) {
return;
}
// UsedPieceSet is sorted by piece index. It means we can flush
// cache by non-decreasing offset, which is good to reduce disk seek
// unless the file is heavily fragmented.
- for(auto & piece : usedPieces_) {
+ for (auto& piece : usedPieces_) {
auto ce = piece->getWrDiskCacheEntry();
- if(ce) {
+ if (ce) {
piece->flushWrCache(wrDiskCache_);
piece->releaseWrCache(wrDiskCache_);
}
@@ -716,60 +704,59 @@ void DefaultPieceStorage::advertisePiece(cuid_t cuid, size_t index)
haves_.push_front(entry);
}
-void
-DefaultPieceStorage::getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
- cuid_t myCuid,
- const Timer& lastCheckTime)
+void DefaultPieceStorage::getAdvertisedPieceIndexes(
+ std::vector<size_t>& indexes, cuid_t myCuid, const Timer& lastCheckTime)
{
- for(std::deque<HaveEntry>::const_iterator itr = haves_.begin(),
- eoi = haves_.end(); itr != eoi; ++itr) {
+ for (std::deque<HaveEntry>::const_iterator itr = haves_.begin(),
+ eoi = haves_.end();
+ itr != eoi; ++itr) {
const HaveEntry& have = *itr;
- if(lastCheckTime > have.getRegisteredTime()) {
+ if (lastCheckTime > have.getRegisteredTime()) {
break;
}
indexes.push_back(have.getIndex());
}
}
-void
-DefaultPieceStorage::removeAdvertisedPiece(const std::chrono::seconds& elapsed)
+void DefaultPieceStorage::removeAdvertisedPiece(
+ const std::chrono::seconds& elapsed)
{
- auto itr = std::find_if(std::begin(haves_), std::end(haves_),
- [&elapsed](const HaveEntry& have) {
- return have.getRegisteredTime().difference(global::wallclock()) >= elapsed;
- });
+ auto itr = std::find_if(
+ std::begin(haves_), std::end(haves_), [&elapsed](const HaveEntry& have) {
+ return have.getRegisteredTime().difference(global::wallclock()) >=
+ elapsed;
+ });
- if(itr != std::end(haves_)) {
+ if (itr != std::end(haves_)) {
A2_LOG_DEBUG(fmt(MSG_REMOVED_HAVE_ENTRY,
static_cast<unsigned long>(std::end(haves_) - itr)));
haves_.erase(itr, std::end(haves_));
}
}
-void DefaultPieceStorage::markAllPiecesDone()
-{
- bitfieldMan_->setAllBit();
-}
+void DefaultPieceStorage::markAllPiecesDone() { bitfieldMan_->setAllBit(); }
void DefaultPieceStorage::markPiecesDone(int64_t length)
{
- if(length == bitfieldMan_->getTotalLength()) {
+ if (length == bitfieldMan_->getTotalLength()) {
bitfieldMan_->setAllBit();
- } else if(length == 0) {
+ }
+ else if (length == 0) {
// TODO this would go to markAllPiecesUndone()
bitfieldMan_->clearAllBit();
usedPieces_.clear();
- } else {
- size_t numPiece = length/bitfieldMan_->getBlockLength();
- if(numPiece > 0) {
- bitfieldMan_->setBitRange(0, numPiece-1);
+ }
+ else {
+ size_t numPiece = length / bitfieldMan_->getBlockLength();
+ if (numPiece > 0) {
+ bitfieldMan_->setBitRange(0, numPiece - 1);
}
- size_t r = (length%bitfieldMan_->getBlockLength())/Piece::BLOCK_LENGTH;
- if(r > 0) {
+ size_t r = (length % bitfieldMan_->getBlockLength()) / Piece::BLOCK_LENGTH;
+ if (r > 0) {
auto p = std::make_shared<Piece>(numPiece,
bitfieldMan_->getBlockLength(numPiece));
- for(size_t i = 0; i < r; ++i) {
+ for (size_t i = 0; i < r; ++i) {
p->completeBlock(i);
}
@@ -785,25 +772,22 @@ void DefaultPieceStorage::markPieceMissing(size_t index)
bitfieldMan_->unsetBit(index);
}
-void DefaultPieceStorage::addInFlightPiece
-(const std::vector<std::shared_ptr<Piece> >& pieces)
+void DefaultPieceStorage::addInFlightPiece(
+ const std::vector<std::shared_ptr<Piece>>& pieces)
{
usedPieces_.insert(pieces.begin(), pieces.end());
}
-size_t DefaultPieceStorage::countInFlightPiece()
-{
- return usedPieces_.size();
-}
+size_t DefaultPieceStorage::countInFlightPiece() { return usedPieces_.size(); }
-void DefaultPieceStorage::getInFlightPieces
-(std::vector<std::shared_ptr<Piece> >& pieces)
+void DefaultPieceStorage::getInFlightPieces(
+ std::vector<std::shared_ptr<Piece>>& pieces)
{
pieces.insert(pieces.end(), usedPieces_.begin(), usedPieces_.end());
}
-void DefaultPieceStorage::setDiskWriterFactory
-(const std::shared_ptr<DiskWriterFactory>& diskWriterFactory)
+void DefaultPieceStorage::setDiskWriterFactory(
+ const std::shared_ptr<DiskWriterFactory>& diskWriterFactory)
{
diskWriterFactory_ = diskWriterFactory;
}
@@ -824,8 +808,7 @@ void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
size_t newBitfieldLength,
const unsigned char* oldBitfield)
{
- pieceStatMan_->updatePieceStats(newBitfield, newBitfieldLength,
- oldBitfield);
+ pieceStatMan_->updatePieceStats(newBitfield, newBitfieldLength, oldBitfield);
}
void DefaultPieceStorage::addPieceStats(size_t index)
@@ -835,8 +818,8 @@ void DefaultPieceStorage::addPieceStats(size_t index)
size_t DefaultPieceStorage::getNextUsedIndex(size_t index)
{
- for(size_t i = index+1; i < bitfieldMan_->countBlock(); ++i) {
- if(bitfieldMan_->isUseBitSet(i) || bitfieldMan_->isBitSet(i)) {
+ for (size_t i = index + 1; i < bitfieldMan_->countBlock(); ++i) {
+ if (bitfieldMan_->isUseBitSet(i) || bitfieldMan_->isBitSet(i)) {
return i;
}
}
@@ -848,8 +831,8 @@ void DefaultPieceStorage::onDownloadIncomplete()
streamPieceSelector_->onBitfieldInit();
}
-void DefaultPieceStorage::setPieceSelector
-(std::unique_ptr<PieceSelector> pieceSelector)
+void DefaultPieceStorage::setPieceSelector(
+ std::unique_ptr<PieceSelector> pieceSelector)
{
pieceSelector_ = std::move(pieceSelector);
}