DefaultPieceStorage.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 "BitfieldMan.h"
  48. #include "message.h"
  49. #include "DefaultDiskWriterFactory.h"
  50. #include "FileEntry.h"
  51. #include "DlAbortEx.h"
  52. #include "util.h"
  53. #include "a2functional.h"
  54. #include "Option.h"
  55. #include "StringFormat.h"
  56. #include "RarestPieceSelector.h"
  57. #include "array_fun.h"
  58. #include "PieceStatMan.h"
  59. #include "wallclock.h"
  60. namespace aria2 {
  61. DefaultPieceStorage::DefaultPieceStorage
  62. (const SharedHandle<DownloadContext>& downloadContext, const Option* option):
  63. _downloadContext(downloadContext),
  64. _bitfieldMan(new BitfieldMan(downloadContext->getPieceLength(),
  65. downloadContext->getTotalLength())),
  66. _diskWriterFactory(new DefaultDiskWriterFactory()),
  67. _endGamePieceNum(END_GAME_PIECE_NUM),
  68. _logger(LogFactory::getInstance()),
  69. _option(option),
  70. _pieceStatMan(new PieceStatMan(downloadContext->getNumPieces(), true)),
  71. _pieceSelector(new RarestPieceSelector(_pieceStatMan))
  72. {}
  73. DefaultPieceStorage::~DefaultPieceStorage() {
  74. delete _bitfieldMan;
  75. }
  76. bool DefaultPieceStorage::isEndGame()
  77. {
  78. return _bitfieldMan->countMissingBlock() <= _endGamePieceNum;
  79. }
  80. bool DefaultPieceStorage::getMissingPieceIndex(size_t& index,
  81. const unsigned char* bitfield,
  82. size_t length)
  83. {
  84. const size_t mislen = _bitfieldMan->getBitfieldLength();
  85. array_ptr<unsigned char> misbitfield(new unsigned char[mislen]);
  86. bool r;
  87. if(isEndGame()) {
  88. r = _bitfieldMan->getAllMissingIndexes(misbitfield, mislen,
  89. bitfield, length);
  90. } else {
  91. r = _bitfieldMan->getAllMissingUnusedIndexes(misbitfield, mislen,
  92. bitfield, length);
  93. }
  94. if(r) {
  95. // We assume indexes is sorted using comparator less.
  96. return
  97. _pieceSelector->select(index, misbitfield,_bitfieldMan->countBlock());
  98. } else {
  99. return false;
  100. }
  101. }
  102. SharedHandle<Piece> DefaultPieceStorage::checkOutPiece(size_t index)
  103. {
  104. _bitfieldMan->setUseBit(index);
  105. SharedHandle<Piece> 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. SharedHandle<Piece> DefaultPieceStorage::getPiece(size_t index)
  122. {
  123. SharedHandle<Piece> 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 SharedHandle<Piece>& piece)
  136. {
  137. std::deque<SharedHandle<Piece> >::iterator i =
  138. std::lower_bound(_usedPieces.begin(), _usedPieces.end(), piece);
  139. _usedPieces.insert(i, piece);
  140. if(_logger->debug()) {
  141. _logger->debug("_usedPieces.size()=%lu",
  142. static_cast<unsigned long>(_usedPieces.size()));
  143. }
  144. }
  145. SharedHandle<Piece> DefaultPieceStorage::findUsedPiece(size_t index) const
  146. {
  147. SharedHandle<Piece> p(new Piece());
  148. p->setIndex(index);
  149. std::deque<SharedHandle<Piece> >::const_iterator i =
  150. std::lower_bound(_usedPieces.begin(), _usedPieces.end(), p);
  151. if(i != _usedPieces.end() && (*i) == p) {
  152. return *i;
  153. } else {
  154. p.reset(0);
  155. return p;
  156. }
  157. }
  158. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  159. (const unsigned char* bitfield, size_t length)
  160. {
  161. size_t index;
  162. if(getMissingPieceIndex(index, bitfield, length)) {
  163. return checkOutPiece(index);
  164. } else {
  165. return SharedHandle<Piece>();
  166. }
  167. }
  168. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  169. (const BitfieldMan& bitfield)
  170. {
  171. return getMissingPiece(bitfield.getBitfield(), bitfield.getBitfieldLength());
  172. }
  173. #ifdef ENABLE_BITTORRENT
  174. bool DefaultPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)
  175. {
  176. return _bitfieldMan->hasMissingPiece(peer->getBitfield(),
  177. peer->getBitfieldLength());
  178. }
  179. SharedHandle<Piece>
  180. DefaultPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  181. {
  182. return getMissingPiece(peer->getBitfield(), peer->getBitfieldLength());
  183. }
  184. void DefaultPieceStorage::createFastIndexBitfield
  185. (BitfieldMan& bitfield, const SharedHandle<Peer>& peer)
  186. {
  187. for(std::vector<size_t>::const_iterator itr =
  188. peer->getPeerAllowedIndexSet().begin(),
  189. eoi = peer->getPeerAllowedIndexSet().end(); itr != eoi; ++itr) {
  190. if(!_bitfieldMan->isBitSet(*itr) && peer->hasPiece(*itr)) {
  191. bitfield.setBit(*itr);
  192. }
  193. }
  194. }
  195. SharedHandle<Piece> DefaultPieceStorage::getMissingFastPiece
  196. (const SharedHandle<Peer>& peer)
  197. {
  198. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  199. BitfieldMan tempBitfield(_bitfieldMan->getBlockLength(),
  200. _bitfieldMan->getTotalLength());
  201. createFastIndexBitfield(tempBitfield, peer);
  202. return getMissingPiece(tempBitfield);
  203. } else {
  204. return SharedHandle<Piece>();
  205. }
  206. }
  207. static void unsetExcludedIndexes(BitfieldMan& bitfield,
  208. const std::vector<size_t>& excludedIndexes)
  209. {
  210. std::for_each(excludedIndexes.begin(), excludedIndexes.end(),
  211. std::bind1st(std::mem_fun(&BitfieldMan::unsetBit), &bitfield));
  212. }
  213. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  214. (const SharedHandle<Peer>& peer, const std::vector<size_t>& excludedIndexes)
  215. {
  216. BitfieldMan tempBitfield(_bitfieldMan->getBlockLength(),
  217. _bitfieldMan->getTotalLength());
  218. tempBitfield.setBitfield(peer->getBitfield(), peer->getBitfieldLength());
  219. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  220. return getMissingPiece(tempBitfield);
  221. }
  222. SharedHandle<Piece> DefaultPieceStorage::getMissingFastPiece
  223. (const SharedHandle<Peer>& peer, const std::vector<size_t>& excludedIndexes)
  224. {
  225. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  226. BitfieldMan tempBitfield(_bitfieldMan->getBlockLength(),
  227. _bitfieldMan->getTotalLength());
  228. createFastIndexBitfield(tempBitfield, peer);
  229. unsetExcludedIndexes(tempBitfield, excludedIndexes);
  230. return getMissingPiece(tempBitfield);
  231. } else {
  232. return SharedHandle<Piece>();
  233. }
  234. }
  235. #endif // ENABLE_BITTORRENT
  236. bool DefaultPieceStorage::hasMissingUnusedPiece()
  237. {
  238. size_t index;
  239. return _bitfieldMan->getFirstMissingUnusedIndex(index);
  240. }
  241. SharedHandle<Piece> DefaultPieceStorage::getSparseMissingUnusedPiece
  242. (const unsigned char* ignoreBitfield, size_t length)
  243. {
  244. size_t index;
  245. if(_bitfieldMan->getSparseMissingUnusedIndex(index, ignoreBitfield, length)) {
  246. return checkOutPiece(index);
  247. } else {
  248. return SharedHandle<Piece>();
  249. }
  250. }
  251. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece(size_t index)
  252. {
  253. if(hasPiece(index) || isPieceUsed(index)) {
  254. return SharedHandle<Piece>();
  255. } else {
  256. return checkOutPiece(index);
  257. }
  258. }
  259. void DefaultPieceStorage::deleteUsedPiece(const SharedHandle<Piece>& piece)
  260. {
  261. if(piece.isNull()) {
  262. return;
  263. }
  264. std::deque<SharedHandle<Piece> >::iterator i =
  265. std::lower_bound(_usedPieces.begin(), _usedPieces.end(), piece);
  266. if(i != _usedPieces.end() && (*i) == piece) {
  267. _usedPieces.erase(i);
  268. }
  269. }
  270. // void DefaultPieceStorage::reduceUsedPieces(size_t upperBound)
  271. // {
  272. // size_t usedPiecesSize = usedPieces.size();
  273. // if(usedPiecesSize <= upperBound) {
  274. // return;
  275. // }
  276. // size_t delNum = usedPiecesSize-upperBound;
  277. // int fillRate = 10;
  278. // while(delNum && fillRate <= 15) {
  279. // delNum -= deleteUsedPiecesByFillRate(fillRate, delNum);
  280. // fillRate += 5;
  281. // }
  282. // }
  283. // size_t DefaultPieceStorage::deleteUsedPiecesByFillRate(int fillRate,
  284. // size_t delNum)
  285. // {
  286. // size_t deleted = 0;
  287. // for(Pieces::iterator itr = usedPieces.begin();
  288. // itr != usedPieces.end() && deleted < delNum;) {
  289. // SharedHandle<Piece>& piece = *itr;
  290. // if(!bitfieldMan->isUseBitSet(piece->getIndex()) &&
  291. // piece->countCompleteBlock() <= piece->countBlock()*(fillRate/100.0)) {
  292. // logger->info(MSG_DELETING_USED_PIECE,
  293. // piece->getIndex(),
  294. // (piece->countCompleteBlock()*100)/piece->countBlock(),
  295. // fillRate);
  296. // itr = usedPieces.erase(itr);
  297. // ++deleted;
  298. // } else {
  299. // ++itr;
  300. // }
  301. // }
  302. // return deleted;
  303. // }
  304. void DefaultPieceStorage::completePiece(const SharedHandle<Piece>& piece)
  305. {
  306. if(piece.isNull()) {
  307. return;
  308. }
  309. deleteUsedPiece(piece);
  310. // if(!isEndGame()) {
  311. // reduceUsedPieces(100);
  312. // }
  313. if(allDownloadFinished()) {
  314. return;
  315. }
  316. _bitfieldMan->setBit(piece->getIndex());
  317. _bitfieldMan->unsetUseBit(piece->getIndex());
  318. addPieceStats(piece->getIndex());
  319. if(downloadFinished()) {
  320. _downloadContext->resetDownloadStopTime();
  321. if(isSelectiveDownloadingMode()) {
  322. _logger->notice(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
  323. // following line was commented out in order to stop sending request
  324. // message after user-specified files were downloaded.
  325. //finishSelectiveDownloadingMode();
  326. } else {
  327. _logger->info(MSG_DOWNLOAD_COMPLETED);
  328. }
  329. }
  330. }
  331. bool DefaultPieceStorage::isSelectiveDownloadingMode()
  332. {
  333. return _bitfieldMan->isFilterEnabled();
  334. }
  335. // not unittested
  336. void DefaultPieceStorage::cancelPiece(const SharedHandle<Piece>& piece)
  337. {
  338. if(piece.isNull()) {
  339. return;
  340. }
  341. _bitfieldMan->unsetUseBit(piece->getIndex());
  342. if(!isEndGame()) {
  343. if(piece->getCompletedLength() == 0) {
  344. deleteUsedPiece(piece);
  345. }
  346. }
  347. }
  348. bool DefaultPieceStorage::hasPiece(size_t index)
  349. {
  350. return _bitfieldMan->isBitSet(index);
  351. }
  352. bool DefaultPieceStorage::isPieceUsed(size_t index)
  353. {
  354. return _bitfieldMan->isUseBitSet(index);
  355. }
  356. uint64_t DefaultPieceStorage::getTotalLength()
  357. {
  358. return _bitfieldMan->getTotalLength();
  359. }
  360. uint64_t DefaultPieceStorage::getFilteredTotalLength()
  361. {
  362. return _bitfieldMan->getFilteredTotalLength();
  363. }
  364. uint64_t DefaultPieceStorage::getCompletedLength()
  365. {
  366. uint64_t completedLength =
  367. _bitfieldMan->getCompletedLength()+getInFlightPieceCompletedLength();
  368. uint64_t totalLength = getTotalLength();
  369. if(completedLength > totalLength) {
  370. completedLength = totalLength;
  371. }
  372. return completedLength;
  373. }
  374. uint64_t DefaultPieceStorage::getFilteredCompletedLength()
  375. {
  376. return _bitfieldMan->getFilteredCompletedLength()+
  377. getInFlightPieceCompletedLength();
  378. }
  379. size_t DefaultPieceStorage::getInFlightPieceCompletedLength() const
  380. {
  381. return std::accumulate(_usedPieces.begin(), _usedPieces.end(),
  382. 0, adopt2nd(std::plus<size_t>(),
  383. mem_fun_sh(&Piece::getCompletedLength)));
  384. }
  385. // not unittested
  386. void DefaultPieceStorage::setupFileFilter()
  387. {
  388. const std::vector<SharedHandle<FileEntry> >& fileEntries =
  389. _downloadContext->getFileEntries();
  390. bool allSelected = true;
  391. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  392. fileEntries.begin(), eoi = fileEntries.end();
  393. i != eoi; ++i) {
  394. if(!(*i)->isRequested()) {
  395. allSelected = false;
  396. break;
  397. }
  398. }
  399. if(allSelected) {
  400. return;
  401. }
  402. for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
  403. fileEntries.begin(), eoi = fileEntries.end(); i != eoi; ++i) {
  404. if((*i)->isRequested()) {
  405. _bitfieldMan->addFilter((*i)->getOffset(), (*i)->getLength());
  406. }
  407. }
  408. _bitfieldMan->enableFilter();
  409. }
  410. // not unittested
  411. void DefaultPieceStorage::clearFileFilter()
  412. {
  413. _bitfieldMan->clearFilter();
  414. }
  415. // not unittested
  416. bool DefaultPieceStorage::downloadFinished()
  417. {
  418. // TODO iterate all requested FileEntry and Call
  419. // bitfieldMan->isBitSetOffsetRange()
  420. return _bitfieldMan->isFilteredAllBitSet();
  421. }
  422. // not unittested
  423. bool DefaultPieceStorage::allDownloadFinished()
  424. {
  425. return _bitfieldMan->isAllBitSet();
  426. }
  427. // not unittested
  428. void DefaultPieceStorage::initStorage()
  429. {
  430. if(_downloadContext->getFileEntries().size() == 1) {
  431. if(_logger->debug()) {
  432. _logger->debug("Instantiating DirectDiskAdaptor");
  433. }
  434. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  435. directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
  436. directDiskAdaptor->setFileEntries
  437. (_downloadContext->getFileEntries().begin(),
  438. _downloadContext->getFileEntries().end());
  439. DiskWriterHandle writer =
  440. _diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
  441. if(_option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  442. writer->allowDirectIO();
  443. }
  444. directDiskAdaptor->setDiskWriter(writer);
  445. _diskAdaptor = directDiskAdaptor;
  446. } else {
  447. if(_logger->debug()) {
  448. _logger->debug("Instantiating MultiDiskAdaptor");
  449. }
  450. MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
  451. multiDiskAdaptor->setFileEntries(_downloadContext->getFileEntries().begin(),
  452. _downloadContext->getFileEntries().end());
  453. if(_option->getAsBool(PREF_ENABLE_DIRECT_IO)) {
  454. multiDiskAdaptor->allowDirectIO();
  455. }
  456. multiDiskAdaptor->setPieceLength(_downloadContext->getPieceLength());
  457. multiDiskAdaptor->setMaxOpenFiles
  458. (_option->getAsInt(PREF_BT_MAX_OPEN_FILES));
  459. _diskAdaptor = multiDiskAdaptor;
  460. }
  461. #ifdef HAVE_POSIX_FALLOCATE
  462. if(_option->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
  463. _diskAdaptor->enableFallocate();
  464. }
  465. #endif // HAVE_POSIX_FALLOCATE
  466. }
  467. void DefaultPieceStorage::setBitfield(const unsigned char* bitfield,
  468. size_t bitfieldLength)
  469. {
  470. _bitfieldMan->setBitfield(bitfield, bitfieldLength);
  471. addPieceStats(bitfield, bitfieldLength);
  472. }
  473. size_t DefaultPieceStorage::getBitfieldLength()
  474. {
  475. return _bitfieldMan->getBitfieldLength();
  476. }
  477. const unsigned char* DefaultPieceStorage::getBitfield()
  478. {
  479. return _bitfieldMan->getBitfield();
  480. }
  481. DiskAdaptorHandle DefaultPieceStorage::getDiskAdaptor() {
  482. return _diskAdaptor;
  483. }
  484. size_t DefaultPieceStorage::getPieceLength(size_t index)
  485. {
  486. return _bitfieldMan->getBlockLength(index);
  487. }
  488. void DefaultPieceStorage::advertisePiece(cuid_t cuid, size_t index)
  489. {
  490. HaveEntry entry(cuid, index, global::wallclock);
  491. _haves.push_front(entry);
  492. }
  493. void
  494. DefaultPieceStorage::getAdvertisedPieceIndexes(std::vector<size_t>& indexes,
  495. cuid_t myCuid,
  496. const Timer& lastCheckTime)
  497. {
  498. for(std::deque<HaveEntry>::const_iterator itr = _haves.begin(),
  499. eoi = _haves.end(); itr != eoi; ++itr) {
  500. const HaveEntry& have = *itr;
  501. if(have.getCuid() == myCuid) {
  502. continue;
  503. }
  504. if(lastCheckTime > have.getRegisteredTime()) {
  505. break;
  506. }
  507. indexes.push_back(have.getIndex());
  508. }
  509. }
  510. class FindElapsedHave
  511. {
  512. private:
  513. time_t elapsed;
  514. public:
  515. FindElapsedHave(time_t elapsed):elapsed(elapsed) {}
  516. bool operator()(const HaveEntry& have) {
  517. if(have.getRegisteredTime().difference(global::wallclock) >= elapsed) {
  518. return true;
  519. } else {
  520. return false;
  521. }
  522. }
  523. };
  524. void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
  525. {
  526. std::deque<HaveEntry>::iterator itr =
  527. std::find_if(_haves.begin(), _haves.end(), FindElapsedHave(elapsed));
  528. if(itr != _haves.end()) {
  529. if(_logger->debug()) {
  530. _logger->debug(MSG_REMOVED_HAVE_ENTRY, _haves.end()-itr);
  531. }
  532. _haves.erase(itr, _haves.end());
  533. }
  534. }
  535. void DefaultPieceStorage::markAllPiecesDone()
  536. {
  537. _bitfieldMan->setAllBit();
  538. }
  539. void DefaultPieceStorage::markPiecesDone(uint64_t length)
  540. {
  541. if(length == _bitfieldMan->getTotalLength()) {
  542. _bitfieldMan->setAllBit();
  543. } else if(length == 0) {
  544. // TODO this would go to markAllPiecesUndone()
  545. _bitfieldMan->clearAllBit();
  546. _usedPieces.clear();
  547. } else {
  548. size_t numPiece = length/_bitfieldMan->getBlockLength();
  549. if(numPiece > 0) {
  550. _bitfieldMan->setBitRange(0, numPiece-1);
  551. }
  552. size_t r = (length%_bitfieldMan->getBlockLength())/Piece::BLOCK_LENGTH;
  553. if(r > 0) {
  554. SharedHandle<Piece> p
  555. (new Piece(numPiece, _bitfieldMan->getBlockLength(numPiece)));
  556. for(size_t i = 0; i < r; ++i) {
  557. p->completeBlock(i);
  558. }
  559. #ifdef ENABLE_MESSAGE_DIGEST
  560. p->setHashAlgo(_downloadContext->getPieceHashAlgo());
  561. #endif // ENABLE_MESSAGE_DIGEST
  562. addUsedPiece(p);
  563. }
  564. }
  565. }
  566. void DefaultPieceStorage::markPieceMissing(size_t index)
  567. {
  568. _bitfieldMan->unsetBit(index);
  569. }
  570. void DefaultPieceStorage::addInFlightPiece
  571. (const std::vector<SharedHandle<Piece> >& pieces)
  572. {
  573. _usedPieces.insert(_usedPieces.end(), pieces.begin(), pieces.end());
  574. std::sort(_usedPieces.begin(), _usedPieces.end());
  575. }
  576. size_t DefaultPieceStorage::countInFlightPiece()
  577. {
  578. return _usedPieces.size();
  579. }
  580. void DefaultPieceStorage::getInFlightPieces
  581. (std::vector<SharedHandle<Piece> >& pieces)
  582. {
  583. pieces.insert(pieces.end(), _usedPieces.begin(), _usedPieces.end());
  584. }
  585. void DefaultPieceStorage::setDiskWriterFactory
  586. (const DiskWriterFactoryHandle& diskWriterFactory)
  587. {
  588. _diskWriterFactory = diskWriterFactory;
  589. }
  590. void DefaultPieceStorage::addPieceStats(const unsigned char* bitfield,
  591. size_t bitfieldLength)
  592. {
  593. _pieceStatMan->addPieceStats(bitfield, bitfieldLength);
  594. }
  595. void DefaultPieceStorage::subtractPieceStats(const unsigned char* bitfield,
  596. size_t bitfieldLength)
  597. {
  598. _pieceStatMan->subtractPieceStats(bitfield, bitfieldLength);
  599. }
  600. void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
  601. size_t newBitfieldLength,
  602. const unsigned char* oldBitfield)
  603. {
  604. _pieceStatMan->updatePieceStats(newBitfield, newBitfieldLength,
  605. oldBitfield);
  606. }
  607. void DefaultPieceStorage::addPieceStats(size_t index)
  608. {
  609. _pieceStatMan->addPieceStats(index);
  610. }
  611. } // namespace aria2