DefaultPieceStorage.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 SharedHandle<DownloadContext>& 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. bool DefaultPieceStorage::hasMissingUnusedPiece()
  236. {
  237. size_t index;
  238. return bitfieldMan->getFirstMissingUnusedIndex(index);
  239. }
  240. PieceHandle DefaultPieceStorage::getSparseMissingUnusedPiece
  241. (const unsigned char* ignoreBitfield, size_t length)
  242. {
  243. size_t index;
  244. if(bitfieldMan->getSparseMissingUnusedIndex(index, ignoreBitfield, length)) {
  245. return checkOutPiece(index);
  246. } else {
  247. return SharedHandle<Piece>();
  248. }
  249. }
  250. PieceHandle DefaultPieceStorage::getMissingPiece(size_t index)
  251. {
  252. if(hasPiece(index) || isPieceUsed(index)) {
  253. return SharedHandle<Piece>();
  254. } else {
  255. return checkOutPiece(index);
  256. }
  257. }
  258. void DefaultPieceStorage::deleteUsedPiece(const PieceHandle& piece)
  259. {
  260. if(piece.isNull()) {
  261. return;
  262. }
  263. std::deque<SharedHandle<Piece> >::iterator i =
  264. std::lower_bound(usedPieces.begin(), usedPieces.end(), piece);
  265. if(i != usedPieces.end() && (*i) == piece) {
  266. usedPieces.erase(i);
  267. }
  268. }
  269. // void DefaultPieceStorage::reduceUsedPieces(size_t upperBound)
  270. // {
  271. // size_t usedPiecesSize = usedPieces.size();
  272. // if(usedPiecesSize <= upperBound) {
  273. // return;
  274. // }
  275. // size_t delNum = usedPiecesSize-upperBound;
  276. // int fillRate = 10;
  277. // while(delNum && fillRate <= 15) {
  278. // delNum -= deleteUsedPiecesByFillRate(fillRate, delNum);
  279. // fillRate += 5;
  280. // }
  281. // }
  282. // size_t DefaultPieceStorage::deleteUsedPiecesByFillRate(int fillRate,
  283. // size_t delNum)
  284. // {
  285. // size_t deleted = 0;
  286. // for(Pieces::iterator itr = usedPieces.begin();
  287. // itr != usedPieces.end() && deleted < delNum;) {
  288. // PieceHandle& piece = *itr;
  289. // if(!bitfieldMan->isUseBitSet(piece->getIndex()) &&
  290. // piece->countCompleteBlock() <= piece->countBlock()*(fillRate/100.0)) {
  291. // logger->info(MSG_DELETING_USED_PIECE,
  292. // piece->getIndex(),
  293. // (piece->countCompleteBlock()*100)/piece->countBlock(),
  294. // fillRate);
  295. // itr = usedPieces.erase(itr);
  296. // ++deleted;
  297. // } else {
  298. // ++itr;
  299. // }
  300. // }
  301. // return deleted;
  302. // }
  303. void DefaultPieceStorage::completePiece(const PieceHandle& piece)
  304. {
  305. if(piece.isNull()) {
  306. return;
  307. }
  308. deleteUsedPiece(piece);
  309. // if(!isEndGame()) {
  310. // reduceUsedPieces(100);
  311. // }
  312. if(allDownloadFinished()) {
  313. return;
  314. }
  315. bitfieldMan->setBit(piece->getIndex());
  316. bitfieldMan->unsetUseBit(piece->getIndex());
  317. addPieceStats(piece->getIndex());
  318. if(downloadFinished()) {
  319. downloadContext->resetDownloadStopTime();
  320. if(isSelectiveDownloadingMode()) {
  321. logger->notice(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
  322. // following line was commented out in order to stop sending request
  323. // message after user-specified files were downloaded.
  324. //finishSelectiveDownloadingMode();
  325. } else {
  326. logger->info(MSG_DOWNLOAD_COMPLETED);
  327. }
  328. }
  329. }
  330. bool DefaultPieceStorage::isSelectiveDownloadingMode()
  331. {
  332. return bitfieldMan->isFilterEnabled();
  333. }
  334. // not unittested
  335. void DefaultPieceStorage::cancelPiece(const PieceHandle& piece)
  336. {
  337. if(piece.isNull()) {
  338. return;
  339. }
  340. bitfieldMan->unsetUseBit(piece->getIndex());
  341. if(!isEndGame()) {
  342. if(piece->getCompletedLength() == 0) {
  343. deleteUsedPiece(piece);
  344. }
  345. }
  346. }
  347. bool DefaultPieceStorage::hasPiece(size_t index)
  348. {
  349. return bitfieldMan->isBitSet(index);
  350. }
  351. bool DefaultPieceStorage::isPieceUsed(size_t index)
  352. {
  353. return bitfieldMan->isUseBitSet(index);
  354. }
  355. uint64_t DefaultPieceStorage::getTotalLength()
  356. {
  357. return bitfieldMan->getTotalLength();
  358. }
  359. uint64_t DefaultPieceStorage::getFilteredTotalLength()
  360. {
  361. return bitfieldMan->getFilteredTotalLength();
  362. }
  363. uint64_t DefaultPieceStorage::getCompletedLength()
  364. {
  365. uint64_t completedLength =
  366. bitfieldMan->getCompletedLength()+getInFlightPieceCompletedLength();
  367. uint64_t totalLength = getTotalLength();
  368. if(completedLength > totalLength) {
  369. completedLength = totalLength;
  370. }
  371. return completedLength;
  372. }
  373. uint64_t DefaultPieceStorage::getFilteredCompletedLength()
  374. {
  375. return bitfieldMan->getFilteredCompletedLength()+getInFlightPieceCompletedLength();
  376. }
  377. size_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
  378. {
  379. return std::accumulate(usedPieces.begin(), usedPieces.end(), 0, adopt2nd(std::plus<size_t>(), mem_fun_sh(&Piece::getCompletedLength)));
  380. }
  381. // not unittested
  382. void DefaultPieceStorage::setupFileFilter()
  383. {
  384. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  385. downloadContext->getFileEntries();
  386. bool allSelected = true;
  387. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  388. fileEntries.begin(); i != fileEntries.end(); ++i) {
  389. if(!(*i)->isRequested()) {
  390. allSelected = false;
  391. break;
  392. }
  393. }
  394. if(allSelected) {
  395. return;
  396. }
  397. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  398. fileEntries.begin(); i != fileEntries.end(); ++i) {
  399. if((*i)->isRequested()) {
  400. bitfieldMan->addFilter((*i)->getOffset(), (*i)->getLength());
  401. }
  402. }
  403. bitfieldMan->enableFilter();
  404. }
  405. // not unittested
  406. void DefaultPieceStorage::clearFileFilter()
  407. {
  408. bitfieldMan->clearFilter();
  409. }
  410. // not unittested
  411. bool DefaultPieceStorage::downloadFinished()
  412. {
  413. // TODO iterate all requested FileEntry and Call bitfieldMan->isBitSetOffsetRange()
  414. return bitfieldMan->isFilteredAllBitSet();
  415. }
  416. // not unittested
  417. bool DefaultPieceStorage::allDownloadFinished()
  418. {
  419. return bitfieldMan->isAllBitSet();
  420. }
  421. // not unittested
  422. void DefaultPieceStorage::initStorage()
  423. {
  424. if(downloadContext->getFileEntries().size() == 1) {
  425. logger->debug("Instantiating DirectDiskAdaptor");
  426. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  427. directDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
  428. directDiskAdaptor->setFileEntries(downloadContext->getFileEntries().begin(),
  429. downloadContext->getFileEntries().end());
  430. DiskWriterHandle writer =
  431. _diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
  432. if(option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  433. writer->allowDirectIO();
  434. }
  435. directDiskAdaptor->setDiskWriter(writer);
  436. this->diskAdaptor = directDiskAdaptor;
  437. } else {
  438. logger->debug("Instantiating MultiDiskAdaptor");
  439. MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
  440. multiDiskAdaptor->setFileEntries(downloadContext->getFileEntries().begin(),
  441. downloadContext->getFileEntries().end());
  442. if(option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  443. multiDiskAdaptor->allowDirectIO();
  444. }
  445. multiDiskAdaptor->setPieceLength(downloadContext->getPieceLength());
  446. multiDiskAdaptor->setMaxOpenFiles(option->getAsInt(PREF_BT_MAX_OPEN_FILES));
  447. this->diskAdaptor = multiDiskAdaptor;
  448. }
  449. #ifdef HAVE_POSIX_FALLOCATE
  450. if(option->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
  451. diskAdaptor->enableFallocate();
  452. }
  453. #endif // HAVE_POSIX_FALLOCATE
  454. }
  455. void DefaultPieceStorage::setBitfield(const unsigned char* bitfield,
  456. size_t bitfieldLength)
  457. {
  458. bitfieldMan->setBitfield(bitfield, bitfieldLength);
  459. addPieceStats(bitfield, bitfieldLength);
  460. }
  461. size_t DefaultPieceStorage::getBitfieldLength()
  462. {
  463. return bitfieldMan->getBitfieldLength();
  464. }
  465. const unsigned char* DefaultPieceStorage::getBitfield()
  466. {
  467. return bitfieldMan->getBitfield();
  468. }
  469. DiskAdaptorHandle DefaultPieceStorage::getDiskAdaptor() {
  470. return diskAdaptor;
  471. }
  472. size_t DefaultPieceStorage::getPieceLength(size_t index)
  473. {
  474. return bitfieldMan->getBlockLength(index);
  475. }
  476. void DefaultPieceStorage::advertisePiece(int32_t cuid, size_t index)
  477. {
  478. HaveEntry entry(cuid, index);
  479. haves.push_front(entry);
  480. }
  481. void
  482. DefaultPieceStorage::getAdvertisedPieceIndexes(std::deque<size_t>& indexes,
  483. int32_t myCuid,
  484. const Time& lastCheckTime)
  485. {
  486. for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) {
  487. const Haves::value_type& have = *itr;
  488. if(have.getCuid() == myCuid) {
  489. continue;
  490. }
  491. if(lastCheckTime.isNewer(have.getRegisteredTime())) {
  492. break;
  493. }
  494. indexes.push_back(have.getIndex());
  495. }
  496. }
  497. class FindElapsedHave
  498. {
  499. private:
  500. time_t elapsed;
  501. public:
  502. FindElapsedHave(time_t elapsed):elapsed(elapsed) {}
  503. bool operator()(const HaveEntry& have) {
  504. if(have.getRegisteredTime().elapsed(elapsed)) {
  505. return true;
  506. } else {
  507. return false;
  508. }
  509. }
  510. };
  511. void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
  512. {
  513. Haves::iterator itr =
  514. std::find_if(haves.begin(), haves.end(), FindElapsedHave(elapsed));
  515. if(itr != haves.end()) {
  516. logger->debug(MSG_REMOVED_HAVE_ENTRY, haves.end()-itr);
  517. haves.erase(itr, haves.end());
  518. }
  519. }
  520. void DefaultPieceStorage::markAllPiecesDone()
  521. {
  522. bitfieldMan->setAllBit();
  523. }
  524. void DefaultPieceStorage::markPiecesDone(uint64_t length)
  525. {
  526. if(length == bitfieldMan->getTotalLength()) {
  527. bitfieldMan->setAllBit();
  528. } else {
  529. size_t numPiece = length/bitfieldMan->getBlockLength();
  530. if(numPiece > 0) {
  531. bitfieldMan->setBitRange(0, numPiece-1);
  532. }
  533. size_t r = (length%bitfieldMan->getBlockLength())/Piece::BLOCK_LENGTH;
  534. if(r > 0) {
  535. PieceHandle p(new Piece(numPiece, bitfieldMan->getBlockLength(numPiece)));
  536. for(size_t i = 0; i < r; ++i) {
  537. p->completeBlock(i);
  538. }
  539. #ifdef ENABLE_MESSAGE_DIGEST
  540. p->setHashAlgo(downloadContext->getPieceHashAlgo());
  541. #endif // ENABLE_MESSAGE_DIGEST
  542. addUsedPiece(p);
  543. }
  544. }
  545. }
  546. void DefaultPieceStorage::markPieceMissing(size_t index)
  547. {
  548. bitfieldMan->unsetBit(index);
  549. }
  550. void DefaultPieceStorage::addInFlightPiece(const Pieces& pieces)
  551. {
  552. usedPieces.insert(usedPieces.end(), pieces.begin(), pieces.end());
  553. std::sort(usedPieces.begin(), usedPieces.end());
  554. }
  555. size_t DefaultPieceStorage::countInFlightPiece()
  556. {
  557. return usedPieces.size();
  558. }
  559. void DefaultPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  560. {
  561. pieces.insert(pieces.end(), usedPieces.begin(), usedPieces.end());
  562. }
  563. void DefaultPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  564. {
  565. _diskWriterFactory = diskWriterFactory;
  566. }
  567. void DefaultPieceStorage::addPieceStats(const unsigned char* bitfield,
  568. size_t bitfieldLength)
  569. {
  570. _pieceStatMan->addPieceStats(bitfield, bitfieldLength);
  571. }
  572. void DefaultPieceStorage::subtractPieceStats(const unsigned char* bitfield,
  573. size_t bitfieldLength)
  574. {
  575. _pieceStatMan->subtractPieceStats(bitfield, bitfieldLength);
  576. }
  577. void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
  578. size_t newBitfieldLength,
  579. const unsigned char* oldBitfield)
  580. {
  581. _pieceStatMan->updatePieceStats(newBitfield, newBitfieldLength,
  582. oldBitfield);
  583. }
  584. void DefaultPieceStorage::addPieceStats(size_t index)
  585. {
  586. _pieceStatMan->addPieceStats(index);
  587. }
  588. } // namespace aria2