DefaultPieceStorage.cc 19 KB

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