DefaultPieceStorage.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "DefaultPieceStorage.h"
  36. #include <numeric>
  37. #include <algorithm>
  38. #include "DownloadContext.h"
  39. #include "Piece.h"
  40. #include "Peer.h"
  41. #include "LogFactory.h"
  42. #include "Logger.h"
  43. #include "prefs.h"
  44. #include "DirectDiskAdaptor.h"
  45. #include "MultiDiskAdaptor.h"
  46. #include "DiskWriter.h"
  47. #include "BitfieldManFactory.h"
  48. #include "BitfieldMan.h"
  49. #include "message.h"
  50. #include "DefaultDiskWriterFactory.h"
  51. #include "FileEntry.h"
  52. #include "DlAbortEx.h"
  53. #include "Util.h"
  54. #include "a2functional.h"
  55. #include "Option.h"
  56. #include "StringFormat.h"
  57. #include "RarestPieceSelector.h"
  58. #include "array_fun.h"
  59. #include "PieceStatMan.h"
  60. namespace aria2 {
  61. DefaultPieceStorage::DefaultPieceStorage
  62. (const DownloadContextHandle& downloadContext, const Option* option):
  63. downloadContext(downloadContext),
  64. bitfieldMan(BitfieldManFactory::getFactoryInstance()->
  65. createBitfieldMan(downloadContext->getPieceLength(),
  66. downloadContext->getTotalLength())),
  67. _diskWriterFactory(new DefaultDiskWriterFactory()),
  68. endGamePieceNum(END_GAME_PIECE_NUM),
  69. logger(LogFactory::getInstance()),
  70. option(option),
  71. _pieceStatMan(new PieceStatMan(downloadContext->getNumPieces(), true)),
  72. _pieceSelector(new RarestPieceSelector(_pieceStatMan))
  73. {}
  74. DefaultPieceStorage::~DefaultPieceStorage() {
  75. delete bitfieldMan;
  76. }
  77. bool DefaultPieceStorage::isEndGame()
  78. {
  79. return bitfieldMan->countMissingBlock() <= endGamePieceNum;
  80. }
  81. bool DefaultPieceStorage::getMissingPieceIndex(size_t& index,
  82. const unsigned char* bitfield,
  83. size_t length)
  84. {
  85. const size_t mislen = bitfieldMan->getBitfieldLength();
  86. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  87. bool r;
  88. if(isEndGame()) {
  89. r = bitfieldMan->getAllMissingIndexes(misbitfield, mislen,
  90. bitfield, length);
  91. } else {
  92. r = bitfieldMan->getAllMissingUnusedIndexes(misbitfield, mislen,
  93. bitfield, length);
  94. }
  95. if(r) {
  96. // We assume indexes is sorted using comparator less.
  97. return _pieceSelector->select(index, misbitfield,bitfieldMan->countBlock());
  98. } else {
  99. return false;
  100. }
  101. }
  102. PieceHandle DefaultPieceStorage::checkOutPiece(size_t index)
  103. {
  104. bitfieldMan->setUseBit(index);
  105. PieceHandle piece = findUsedPiece(index);
  106. if(piece.isNull()) {
  107. piece.reset(new Piece(index, bitfieldMan->getBlockLength(index)));
  108. #ifdef ENABLE_MESSAGE_DIGEST
  109. piece->setHashAlgo(downloadContext->getPieceHashAlgo());
  110. #endif // ENABLE_MESSAGE_DIGEST
  111. addUsedPiece(piece);
  112. return piece;
  113. } else {
  114. return piece;
  115. }
  116. }
  117. /**
  118. * Newly instantiated piece is not added to usedPieces.
  119. * Because it is waste of memory and there is no chance to use them later.
  120. */
  121. PieceHandle DefaultPieceStorage::getPiece(size_t index)
  122. {
  123. PieceHandle piece;
  124. if(0 <= index && index <= bitfieldMan->getMaxIndex()) {
  125. piece = findUsedPiece(index);
  126. if(piece.isNull()) {
  127. piece.reset(new Piece(index, bitfieldMan->getBlockLength(index)));
  128. if(hasPiece(index)) {
  129. piece->setAllBlock();
  130. }
  131. }
  132. }
  133. return piece;
  134. }
  135. void DefaultPieceStorage::addUsedPiece(const PieceHandle& piece)
  136. {
  137. std::deque<SharedHandle<Piece> >::iterator i =
  138. std::lower_bound(usedPieces.begin(), usedPieces.end(), piece);
  139. usedPieces.insert(i, piece);
  140. logger->debug("usedPieces.size()=%lu",
  141. static_cast<unsigned long>(usedPieces.size()));
  142. }
  143. PieceHandle DefaultPieceStorage::findUsedPiece(size_t index) const
  144. {
  145. SharedHandle<Piece> p(new Piece());
  146. p->setIndex(index);
  147. std::deque<SharedHandle<Piece> >::const_iterator i =
  148. std::lower_bound(usedPieces.begin(), usedPieces.end(), p);
  149. if(i != usedPieces.end() && (*i) == p) {
  150. return *i;
  151. } else {
  152. p.reset(0);
  153. return p;
  154. }
  155. }
  156. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  157. (const unsigned char* bitfield, size_t length)
  158. {
  159. size_t index;
  160. if(getMissingPieceIndex(index, bitfield, length)) {
  161. return checkOutPiece(index);
  162. } else {
  163. return SharedHandle<Piece>();
  164. }
  165. }
  166. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  167. (const BitfieldMan& bitfield)
  168. {
  169. return getMissingPiece(bitfield.getBitfield(), bitfield.getBitfieldLength());
  170. }
  171. #ifdef ENABLE_BITTORRENT
  172. bool DefaultPieceStorage::hasMissingPiece(const PeerHandle& peer)
  173. {
  174. return bitfieldMan->hasMissingPiece(peer->getBitfield(),
  175. peer->getBitfieldLength());
  176. }
  177. PieceHandle DefaultPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  178. {
  179. return getMissingPiece(peer->getBitfield(), peer->getBitfieldLength());
  180. }
  181. void DefaultPieceStorage::createFastIndexBitfield
  182. (BitfieldMan& bitfield, const SharedHandle<Peer>& peer)
  183. {
  184. for(std::deque<size_t>::const_iterator itr =
  185. peer->getPeerAllowedIndexSet().begin();
  186. itr != peer->getPeerAllowedIndexSet().end(); ++itr) {
  187. if(!bitfieldMan->isBitSet(*itr) && peer->hasPiece(*itr)) {
  188. bitfield.setBit(*itr);
  189. }
  190. }
  191. }
  192. PieceHandle DefaultPieceStorage::getMissingFastPiece
  193. (const SharedHandle<Peer>& peer)
  194. {
  195. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  196. BitfieldMan tempBitfield(bitfieldMan->getBlockLength(),
  197. bitfieldMan->getTotalLength());
  198. createFastIndexBitfield(tempBitfield, peer);
  199. return getMissingPiece(tempBitfield);
  200. } else {
  201. return SharedHandle<Piece>();
  202. }
  203. }
  204. static void unsetExcludedIndexes(BitfieldMan& bitfield,
  205. const std::deque<size_t>& excludedIndexes)
  206. {
  207. for(std::deque<size_t>::const_iterator i = excludedIndexes.begin();
  208. i != excludedIndexes.end(); ++i) {
  209. bitfield.unsetBit(*i);
  210. }
  211. }
  212. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  213. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  214. {
  215. BitfieldMan tempBitfield(bitfieldMan->getBlockLength(),
  216. bitfieldMan->getTotalLength());
  217. tempBitfield.setBitfield(peer->getBitfield(), peer->getBitfieldLength());
  218. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  219. return getMissingPiece(tempBitfield);
  220. }
  221. SharedHandle<Piece> DefaultPieceStorage::getMissingFastPiece
  222. (const SharedHandle<Peer>& peer, const std::deque<size_t>& excludedIndexes)
  223. {
  224. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  225. BitfieldMan tempBitfield(bitfieldMan->getBlockLength(),
  226. bitfieldMan->getTotalLength());
  227. createFastIndexBitfield(tempBitfield, peer);
  228. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  229. return getMissingPiece(tempBitfield);
  230. } else {
  231. return SharedHandle<Piece>();
  232. }
  233. }
  234. #endif // ENABLE_BITTORRENT
  235. PieceHandle DefaultPieceStorage::getSparseMissingUnusedPiece
  236. (const unsigned char* ignoreBitfield, size_t length)
  237. {
  238. size_t index;
  239. if(bitfieldMan->getSparseMissingUnusedIndex(index, ignoreBitfield, length)) {
  240. return checkOutPiece(index);
  241. } else {
  242. return SharedHandle<Piece>();
  243. }
  244. }
  245. PieceHandle DefaultPieceStorage::getMissingPiece(size_t index)
  246. {
  247. if(hasPiece(index) || isPieceUsed(index)) {
  248. return SharedHandle<Piece>();
  249. } else {
  250. return checkOutPiece(index);
  251. }
  252. }
  253. void DefaultPieceStorage::deleteUsedPiece(const PieceHandle& piece)
  254. {
  255. if(piece.isNull()) {
  256. return;
  257. }
  258. std::deque<SharedHandle<Piece> >::iterator i =
  259. std::lower_bound(usedPieces.begin(), usedPieces.end(), piece);
  260. if(i != usedPieces.end() && (*i) == piece) {
  261. usedPieces.erase(i);
  262. }
  263. }
  264. // void DefaultPieceStorage::reduceUsedPieces(size_t upperBound)
  265. // {
  266. // size_t usedPiecesSize = usedPieces.size();
  267. // if(usedPiecesSize <= upperBound) {
  268. // return;
  269. // }
  270. // size_t delNum = usedPiecesSize-upperBound;
  271. // int fillRate = 10;
  272. // while(delNum && fillRate <= 15) {
  273. // delNum -= deleteUsedPiecesByFillRate(fillRate, delNum);
  274. // fillRate += 5;
  275. // }
  276. // }
  277. // size_t DefaultPieceStorage::deleteUsedPiecesByFillRate(int fillRate,
  278. // size_t delNum)
  279. // {
  280. // size_t deleted = 0;
  281. // for(Pieces::iterator itr = usedPieces.begin();
  282. // itr != usedPieces.end() && deleted < delNum;) {
  283. // PieceHandle& piece = *itr;
  284. // if(!bitfieldMan->isUseBitSet(piece->getIndex()) &&
  285. // piece->countCompleteBlock() <= piece->countBlock()*(fillRate/100.0)) {
  286. // logger->info(MSG_DELETING_USED_PIECE,
  287. // piece->getIndex(),
  288. // (piece->countCompleteBlock()*100)/piece->countBlock(),
  289. // fillRate);
  290. // itr = usedPieces.erase(itr);
  291. // ++deleted;
  292. // } else {
  293. // ++itr;
  294. // }
  295. // }
  296. // return deleted;
  297. // }
  298. void DefaultPieceStorage::completePiece(const PieceHandle& piece)
  299. {
  300. if(piece.isNull()) {
  301. return;
  302. }
  303. deleteUsedPiece(piece);
  304. // if(!isEndGame()) {
  305. // reduceUsedPieces(100);
  306. // }
  307. if(allDownloadFinished()) {
  308. return;
  309. }
  310. bitfieldMan->setBit(piece->getIndex());
  311. bitfieldMan->unsetUseBit(piece->getIndex());
  312. addPieceStats(piece->getIndex());
  313. if(downloadFinished()) {
  314. downloadContext->resetDownloadStopTime();
  315. diskAdaptor->onDownloadComplete();
  316. if(isSelectiveDownloadingMode()) {
  317. logger->notice(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
  318. // following line was commented out in order to stop sending request
  319. // message after user-specified files were downloaded.
  320. //finishSelectiveDownloadingMode();
  321. } else {
  322. logger->info(MSG_DOWNLOAD_COMPLETED);
  323. }
  324. }
  325. }
  326. bool DefaultPieceStorage::isSelectiveDownloadingMode()
  327. {
  328. return bitfieldMan->isFilterEnabled();
  329. }
  330. // not unittested
  331. void DefaultPieceStorage::cancelPiece(const PieceHandle& piece)
  332. {
  333. if(piece.isNull()) {
  334. return;
  335. }
  336. bitfieldMan->unsetUseBit(piece->getIndex());
  337. if(!isEndGame()) {
  338. if(piece->getCompletedLength() == 0) {
  339. deleteUsedPiece(piece);
  340. }
  341. }
  342. }
  343. bool DefaultPieceStorage::hasPiece(size_t index)
  344. {
  345. return bitfieldMan->isBitSet(index);
  346. }
  347. bool DefaultPieceStorage::isPieceUsed(size_t index)
  348. {
  349. return bitfieldMan->isUseBitSet(index);
  350. }
  351. uint64_t DefaultPieceStorage::getTotalLength()
  352. {
  353. return bitfieldMan->getTotalLength();
  354. }
  355. uint64_t DefaultPieceStorage::getFilteredTotalLength()
  356. {
  357. return bitfieldMan->getFilteredTotalLength();
  358. }
  359. uint64_t DefaultPieceStorage::getCompletedLength()
  360. {
  361. uint64_t completedLength =
  362. bitfieldMan->getCompletedLength()+getInFlightPieceCompletedLength();
  363. uint64_t totalLength = getTotalLength();
  364. if(completedLength > totalLength) {
  365. completedLength = totalLength;
  366. }
  367. return completedLength;
  368. }
  369. uint64_t DefaultPieceStorage::getFilteredCompletedLength()
  370. {
  371. return bitfieldMan->getFilteredCompletedLength()+getInFlightPieceCompletedLength();
  372. }
  373. size_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
  374. {
  375. return std::accumulate(usedPieces.begin(), usedPieces.end(), 0, adopt2nd(std::plus<size_t>(), mem_fun_sh(&Piece::getCompletedLength)));
  376. }
  377. // not unittested
  378. void DefaultPieceStorage::setupFileFilter()
  379. {
  380. const std::deque<SharedHandle<FileEntry> >& fileEntries =
  381. downloadContext->getFileEntries();
  382. bool allSelected = true;
  383. for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
  384. fileEntries.begin(); i != fileEntries.end(); ++i) {
  385. if(!(*i)->isRequested()) {
  386. allSelected = false;
  387. break;
  388. }
  389. }
  390. if(allSelected) {
  391. return;
  392. }
  393. for(std::deque<SharedHandle<FileEntry> >::const_iterator i =
  394. fileEntries.begin(); i != fileEntries.end(); ++i) {
  395. if((*i)->isRequested()) {
  396. bitfieldMan->addFilter((*i)->getOffset(), (*i)->getLength());
  397. }
  398. }
  399. bitfieldMan->enableFilter();
  400. }
  401. // not unittested
  402. void DefaultPieceStorage::clearFileFilter()
  403. {
  404. bitfieldMan->clearFilter();
  405. }
  406. // not unittested
  407. bool DefaultPieceStorage::downloadFinished()
  408. {
  409. // TODO iterate all requested FileEntry and Call bitfieldMan->isBitSetOffsetRange()
  410. return bitfieldMan->isFilteredAllBitSet();
  411. }
  412. // not unittested
  413. bool DefaultPieceStorage::allDownloadFinished()
  414. {
  415. return bitfieldMan->isAllBitSet();
  416. }
  417. // not unittested
  418. void DefaultPieceStorage::initStorage()
  419. {
  420. if(downloadContext->getFileMode() == DownloadContext::SINGLE) {
  421. logger->debug("Instantiating DirectDiskAdaptor");
  422. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  423. directDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
  424. directDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
  425. DiskWriterHandle writer =
  426. _diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
  427. if(option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  428. writer->allowDirectIO();
  429. }
  430. directDiskAdaptor->setDiskWriter(writer);
  431. this->diskAdaptor = directDiskAdaptor;
  432. } else {
  433. // file mode == DownloadContext::MULTI
  434. logger->debug("Instantiating MultiDiskAdaptor");
  435. MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
  436. multiDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
  437. if(option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  438. multiDiskAdaptor->allowDirectIO();
  439. }
  440. multiDiskAdaptor->setPieceLength(downloadContext->getPieceLength());
  441. multiDiskAdaptor->setMaxOpenFiles(option->getAsInt(PREF_BT_MAX_OPEN_FILES));
  442. this->diskAdaptor = multiDiskAdaptor;
  443. }
  444. #ifdef HAVE_POSIX_FALLOCATE
  445. if(option->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
  446. diskAdaptor->enableFallocate();
  447. }
  448. #endif // HAVE_POSIX_FALLOCATE
  449. }
  450. void DefaultPieceStorage::setBitfield(const unsigned char* bitfield,
  451. size_t bitfieldLength)
  452. {
  453. bitfieldMan->setBitfield(bitfield, bitfieldLength);
  454. addPieceStats(bitfield, bitfieldLength);
  455. }
  456. size_t DefaultPieceStorage::getBitfieldLength()
  457. {
  458. return bitfieldMan->getBitfieldLength();
  459. }
  460. const unsigned char* DefaultPieceStorage::getBitfield()
  461. {
  462. return bitfieldMan->getBitfield();
  463. }
  464. DiskAdaptorHandle DefaultPieceStorage::getDiskAdaptor() {
  465. return diskAdaptor;
  466. }
  467. size_t DefaultPieceStorage::getPieceLength(size_t index)
  468. {
  469. return bitfieldMan->getBlockLength(index);
  470. }
  471. void DefaultPieceStorage::advertisePiece(int32_t cuid, size_t index)
  472. {
  473. HaveEntry entry(cuid, index);
  474. haves.push_front(entry);
  475. }
  476. void
  477. DefaultPieceStorage::getAdvertisedPieceIndexes(std::deque<size_t>& indexes,
  478. int32_t myCuid,
  479. const Time& lastCheckTime)
  480. {
  481. for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) {
  482. const Haves::value_type& have = *itr;
  483. if(have.getCuid() == myCuid) {
  484. continue;
  485. }
  486. if(lastCheckTime.isNewer(have.getRegisteredTime())) {
  487. break;
  488. }
  489. indexes.push_back(have.getIndex());
  490. }
  491. }
  492. class FindElapsedHave
  493. {
  494. private:
  495. time_t elapsed;
  496. public:
  497. FindElapsedHave(time_t elapsed):elapsed(elapsed) {}
  498. bool operator()(const HaveEntry& have) {
  499. if(have.getRegisteredTime().elapsed(elapsed)) {
  500. return true;
  501. } else {
  502. return false;
  503. }
  504. }
  505. };
  506. void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
  507. {
  508. Haves::iterator itr =
  509. std::find_if(haves.begin(), haves.end(), FindElapsedHave(elapsed));
  510. if(itr != haves.end()) {
  511. logger->debug(MSG_REMOVED_HAVE_ENTRY, haves.end()-itr);
  512. haves.erase(itr, haves.end());
  513. }
  514. }
  515. void DefaultPieceStorage::markAllPiecesDone()
  516. {
  517. bitfieldMan->setAllBit();
  518. }
  519. void DefaultPieceStorage::markPiecesDone(uint64_t length)
  520. {
  521. if(length == bitfieldMan->getTotalLength()) {
  522. bitfieldMan->setAllBit();
  523. } else {
  524. size_t numPiece = length/bitfieldMan->getBlockLength();
  525. if(numPiece > 0) {
  526. bitfieldMan->setBitRange(0, numPiece-1);
  527. }
  528. size_t r = (length%bitfieldMan->getBlockLength())/Piece::BLOCK_LENGTH;
  529. if(r > 0) {
  530. PieceHandle p(new Piece(numPiece, bitfieldMan->getBlockLength(numPiece)));
  531. for(size_t i = 0; i < r; ++i) {
  532. p->completeBlock(i);
  533. }
  534. #ifdef ENABLE_MESSAGE_DIGEST
  535. p->setHashAlgo(downloadContext->getPieceHashAlgo());
  536. #endif // ENABLE_MESSAGE_DIGEST
  537. addUsedPiece(p);
  538. }
  539. }
  540. }
  541. void DefaultPieceStorage::markPieceMissing(size_t index)
  542. {
  543. bitfieldMan->unsetBit(index);
  544. }
  545. void DefaultPieceStorage::addInFlightPiece(const Pieces& pieces)
  546. {
  547. usedPieces.insert(usedPieces.end(), pieces.begin(), pieces.end());
  548. std::sort(usedPieces.begin(), usedPieces.end());
  549. }
  550. size_t DefaultPieceStorage::countInFlightPiece()
  551. {
  552. return usedPieces.size();
  553. }
  554. void DefaultPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  555. {
  556. pieces.insert(pieces.end(), usedPieces.begin(), usedPieces.end());
  557. }
  558. void DefaultPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  559. {
  560. _diskWriterFactory = diskWriterFactory;
  561. }
  562. void DefaultPieceStorage::addPieceStats(const unsigned char* bitfield,
  563. size_t bitfieldLength)
  564. {
  565. _pieceStatMan->addPieceStats(bitfield, bitfieldLength);
  566. }
  567. void DefaultPieceStorage::subtractPieceStats(const unsigned char* bitfield,
  568. size_t bitfieldLength)
  569. {
  570. _pieceStatMan->subtractPieceStats(bitfield, bitfieldLength);
  571. }
  572. void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
  573. size_t newBitfieldLength,
  574. const unsigned char* oldBitfield)
  575. {
  576. _pieceStatMan->updatePieceStats(newBitfield, newBitfieldLength,
  577. oldBitfield);
  578. }
  579. void DefaultPieceStorage::addPieceStats(size_t index)
  580. {
  581. _pieceStatMan->addPieceStats(index);
  582. }
  583. } // namespace aria2