DefaultPieceStorage.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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
  61. (const DownloadContextHandle& downloadContext,
  62. const Option* option,
  63. const SharedHandle<PieceSelector>& pieceSelector):
  64. downloadContext(downloadContext),
  65. bitfieldMan(BitfieldManFactory::getFactoryInstance()->
  66. createBitfieldMan(downloadContext->getPieceLength(),
  67. downloadContext->getTotalLength())),
  68. _diskWriterFactory(new DefaultDiskWriterFactory()),
  69. endGamePieceNum(END_GAME_PIECE_NUM),
  70. logger(LogFactory::getInstance()),
  71. option(option),
  72. _pieceSelector(pieceSelector)
  73. {
  74. if(_pieceSelector.isNull()) {
  75. _pieceSelector.reset
  76. (new RarestPieceSelector(downloadContext->getNumPieces(), true));
  77. }
  78. }
  79. DefaultPieceStorage::~DefaultPieceStorage() {
  80. delete bitfieldMan;
  81. }
  82. bool DefaultPieceStorage::hasMissingPiece(const PeerHandle& peer)
  83. {
  84. return bitfieldMan->hasMissingPiece(peer->getBitfield(),
  85. peer->getBitfieldLength());
  86. }
  87. bool DefaultPieceStorage::isEndGame()
  88. {
  89. return bitfieldMan->countMissingBlock() <= endGamePieceNum;
  90. }
  91. bool DefaultPieceStorage::getMissingPieceIndex(size_t& index,
  92. const unsigned char* bitfield,
  93. size_t& length)
  94. {
  95. std::deque<size_t> indexes;
  96. bool r;
  97. if(isEndGame()) {
  98. r = bitfieldMan->getAllMissingIndexes(indexes, bitfield, length);
  99. } else {
  100. r = bitfieldMan->getAllMissingUnusedIndexes(indexes, bitfield, length);
  101. }
  102. if(r) {
  103. // We assume indexes is sorted using comparator less.
  104. return _pieceSelector->select(index, indexes);
  105. } else {
  106. return false;
  107. }
  108. }
  109. PieceHandle DefaultPieceStorage::checkOutPiece(size_t index)
  110. {
  111. bitfieldMan->setUseBit(index);
  112. PieceHandle piece = findUsedPiece(index);
  113. if(piece.isNull()) {
  114. piece.reset(new Piece(index, bitfieldMan->getBlockLength(index)));
  115. #ifdef ENABLE_MESSAGE_DIGEST
  116. piece->setHashAlgo(downloadContext->getPieceHashAlgo());
  117. #endif // ENABLE_MESSAGE_DIGEST
  118. addUsedPiece(piece);
  119. return piece;
  120. } else {
  121. return piece;
  122. }
  123. }
  124. /**
  125. * Newly instantiated piece is not added to usedPieces.
  126. * Because it is waste of memory and there is no chance to use them later.
  127. */
  128. PieceHandle DefaultPieceStorage::getPiece(size_t index)
  129. {
  130. PieceHandle piece;
  131. if(0 <= index && index <= bitfieldMan->getMaxIndex()) {
  132. piece = findUsedPiece(index);
  133. if(piece.isNull()) {
  134. piece.reset(new Piece(index, bitfieldMan->getBlockLength(index)));
  135. if(hasPiece(index)) {
  136. piece->setAllBlock();
  137. }
  138. }
  139. }
  140. return piece;
  141. }
  142. void DefaultPieceStorage::addUsedPiece(const PieceHandle& piece)
  143. {
  144. std::deque<SharedHandle<Piece> >::iterator i =
  145. std::lower_bound(usedPieces.begin(), usedPieces.end(), piece);
  146. usedPieces.insert(i, piece);
  147. logger->debug("usedPieces.size()=%lu",
  148. static_cast<unsigned long>(usedPieces.size()));
  149. }
  150. PieceHandle DefaultPieceStorage::findUsedPiece(size_t index) const
  151. {
  152. SharedHandle<Piece> p(new Piece());
  153. p->setIndex(index);
  154. std::deque<SharedHandle<Piece> >::const_iterator i =
  155. std::lower_bound(usedPieces.begin(), usedPieces.end(), p);
  156. if(i != usedPieces.end() && (*i) == p) {
  157. return *i;
  158. } else {
  159. p.reset(0);
  160. return p;
  161. }
  162. }
  163. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  164. (const unsigned char* bitfield, size_t length)
  165. {
  166. size_t index;
  167. if(getMissingPieceIndex(index, bitfield, length)) {
  168. return checkOutPiece(index);
  169. } else {
  170. return SharedHandle<Piece>();
  171. }
  172. }
  173. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  174. (const BitfieldMan& bitfield)
  175. {
  176. return getMissingPiece(bitfield.getBitfield(), bitfield.getBitfieldLength());
  177. }
  178. PieceHandle DefaultPieceStorage::getMissingPiece(const SharedHandle<Peer>& peer)
  179. {
  180. return getMissingPiece(peer->getBitfield(), peer->getBitfieldLength());
  181. }
  182. void DefaultPieceStorage::createFastIndexBitfield
  183. (BitfieldMan& bitfield, const SharedHandle<Peer>& peer)
  184. {
  185. for(std::deque<size_t>::const_iterator itr =
  186. peer->getPeerAllowedIndexSet().begin();
  187. itr != peer->getPeerAllowedIndexSet().end(); ++itr) {
  188. if(!bitfieldMan->isBitSet(*itr) && peer->hasPiece(*itr)) {
  189. bitfield.setBit(*itr);
  190. }
  191. }
  192. }
  193. PieceHandle DefaultPieceStorage::getMissingFastPiece
  194. (const SharedHandle<Peer>& peer)
  195. {
  196. if(peer->isFastExtensionEnabled() && peer->countPeerAllowedIndexSet() > 0) {
  197. BitfieldMan tempBitfield(bitfieldMan->getBlockLength(),
  198. bitfieldMan->getTotalLength());
  199. createFastIndexBitfield(tempBitfield, peer);
  200. return getMissingPiece(tempBitfield);
  201. } else {
  202. return SharedHandle<Piece>();
  203. }
  204. }
  205. static void unsetExcludedIndexes(BitfieldMan& bitfield,
  206. const std::deque<size_t>& excludedIndexes)
  207. {
  208. for(std::deque<size_t>::const_iterator i = excludedIndexes.begin();
  209. i != excludedIndexes.end(); ++i) {
  210. bitfield.unsetBit(*i);
  211. }
  212. }
  213. SharedHandle<Piece> DefaultPieceStorage::getMissingPiece
  214. (const SharedHandle<Peer>& peer, const std::deque<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::deque<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. PieceHandle DefaultPieceStorage::getMissingPiece()
  236. {
  237. size_t index;
  238. if(bitfieldMan->getSparseMissingUnusedIndex(index)) {
  239. return checkOutPiece(index);
  240. } else {
  241. return SharedHandle<Piece>();
  242. }
  243. }
  244. PieceHandle DefaultPieceStorage::getMissingPiece(size_t index)
  245. {
  246. if(hasPiece(index) || isPieceUsed(index)) {
  247. return SharedHandle<Piece>();
  248. } else {
  249. return checkOutPiece(index);
  250. }
  251. }
  252. void DefaultPieceStorage::deleteUsedPiece(const PieceHandle& piece)
  253. {
  254. if(piece.isNull()) {
  255. return;
  256. }
  257. std::deque<SharedHandle<Piece> >::iterator i =
  258. std::lower_bound(usedPieces.begin(), usedPieces.end(), piece);
  259. if(i != usedPieces.end() && (*i) == piece) {
  260. usedPieces.erase(i);
  261. }
  262. }
  263. // void DefaultPieceStorage::reduceUsedPieces(size_t upperBound)
  264. // {
  265. // size_t usedPiecesSize = usedPieces.size();
  266. // if(usedPiecesSize <= upperBound) {
  267. // return;
  268. // }
  269. // size_t delNum = usedPiecesSize-upperBound;
  270. // int fillRate = 10;
  271. // while(delNum && fillRate <= 15) {
  272. // delNum -= deleteUsedPiecesByFillRate(fillRate, delNum);
  273. // fillRate += 5;
  274. // }
  275. // }
  276. // size_t DefaultPieceStorage::deleteUsedPiecesByFillRate(int fillRate,
  277. // size_t delNum)
  278. // {
  279. // size_t deleted = 0;
  280. // for(Pieces::iterator itr = usedPieces.begin();
  281. // itr != usedPieces.end() && deleted < delNum;) {
  282. // PieceHandle& piece = *itr;
  283. // if(!bitfieldMan->isUseBitSet(piece->getIndex()) &&
  284. // piece->countCompleteBlock() <= piece->countBlock()*(fillRate/100.0)) {
  285. // logger->info(MSG_DELETING_USED_PIECE,
  286. // piece->getIndex(),
  287. // (piece->countCompleteBlock()*100)/piece->countBlock(),
  288. // fillRate);
  289. // itr = usedPieces.erase(itr);
  290. // ++deleted;
  291. // } else {
  292. // ++itr;
  293. // }
  294. // }
  295. // return deleted;
  296. // }
  297. void DefaultPieceStorage::completePiece(const PieceHandle& piece)
  298. {
  299. if(piece.isNull()) {
  300. return;
  301. }
  302. deleteUsedPiece(piece);
  303. // if(!isEndGame()) {
  304. // reduceUsedPieces(100);
  305. // }
  306. if(allDownloadFinished()) {
  307. return;
  308. }
  309. bitfieldMan->setBit(piece->getIndex());
  310. bitfieldMan->unsetUseBit(piece->getIndex());
  311. addPieceStats(piece->getIndex());
  312. if(downloadFinished()) {
  313. downloadContext->resetDownloadStopTime();
  314. diskAdaptor->onDownloadComplete();
  315. if(isSelectiveDownloadingMode()) {
  316. logger->notice(MSG_SELECTIVE_DOWNLOAD_COMPLETED);
  317. // following line was commented out in order to stop sending request
  318. // message after user-specified files were downloaded.
  319. //finishSelectiveDownloadingMode();
  320. } else {
  321. logger->info(MSG_DOWNLOAD_COMPLETED);
  322. }
  323. }
  324. }
  325. bool DefaultPieceStorage::isSelectiveDownloadingMode()
  326. {
  327. return bitfieldMan->isFilterEnabled();
  328. }
  329. void DefaultPieceStorage::finishSelectiveDownloadingMode()
  330. {
  331. bitfieldMan->clearFilter();
  332. diskAdaptor->addAllDownloadEntry();
  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::setFileFilter(const std::deque<std::string>& filePaths)
  383. {
  384. if(downloadContext->getFileMode() != DownloadContext::MULTI || filePaths.empty()) {
  385. return;
  386. }
  387. diskAdaptor->removeAllDownloadEntry();
  388. for(std::deque<std::string>::const_iterator pitr = filePaths.begin();
  389. pitr != filePaths.end(); pitr++) {
  390. if(!diskAdaptor->addDownloadEntry(*pitr)) {
  391. throw DlAbortEx(StringFormat(EX_NO_SUCH_FILE_ENTRY, (*pitr).c_str()).str());
  392. }
  393. FileEntryHandle fileEntry = diskAdaptor->getFileEntryFromPath(*pitr);
  394. bitfieldMan->addFilter(fileEntry->getOffset(), fileEntry->getLength());
  395. }
  396. bitfieldMan->enableFilter();
  397. }
  398. void DefaultPieceStorage::setFileFilter(IntSequence seq)
  399. {
  400. std::deque<int32_t> fileIndexes = seq.flush();
  401. // TODO Is sorting necessary?
  402. std::sort(fileIndexes.begin(), fileIndexes.end());
  403. fileIndexes.erase(std::unique(fileIndexes.begin(), fileIndexes.end()), fileIndexes.end());
  404. std::deque<std::string> filePaths;
  405. const FileEntries& entries = diskAdaptor->getFileEntries();
  406. int32_t entriesSize = entries.size();
  407. for(int32_t i = 0; i < entriesSize; i++) {
  408. if(std::find(fileIndexes.begin(), fileIndexes.end(), i+1) != fileIndexes.end()) {
  409. logger->debug("index=%d is %s", i+1, entries[i]->getPath().c_str());
  410. filePaths.push_back(entries[i]->getPath());
  411. }
  412. }
  413. setFileFilter(filePaths);
  414. }
  415. // not unittested
  416. void DefaultPieceStorage::clearFileFilter()
  417. {
  418. bitfieldMan->clearFilter();
  419. diskAdaptor->addAllDownloadEntry();
  420. }
  421. // not unittested
  422. bool DefaultPieceStorage::downloadFinished()
  423. {
  424. // TODO iterate all requested FileEntry and Call bitfieldMan->isBitSetOffsetRange()
  425. return bitfieldMan->isFilteredAllBitSet();
  426. }
  427. // not unittested
  428. bool DefaultPieceStorage::allDownloadFinished()
  429. {
  430. return bitfieldMan->isAllBitSet();
  431. }
  432. // not unittested
  433. void DefaultPieceStorage::initStorage()
  434. {
  435. if(downloadContext->getFileMode() == DownloadContext::SINGLE) {
  436. logger->debug("Instantiating DirectDiskAdaptor");
  437. DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
  438. writer->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
  439. DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
  440. directDiskAdaptor->setDiskWriter(writer);
  441. directDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
  442. this->diskAdaptor = directDiskAdaptor;
  443. } else {
  444. // file mode == DownloadContext::MULTI
  445. if(option->getAsBool(PREF_DIRECT_FILE_MAPPING)) {
  446. logger->debug("Instantiating MultiDiskAdaptor");
  447. MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
  448. multiDiskAdaptor->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
  449. multiDiskAdaptor->setPieceLength(downloadContext->getPieceLength());
  450. multiDiskAdaptor->setTopDir(downloadContext->getName());
  451. multiDiskAdaptor->setMaxOpenFiles(option->getAsInt(PREF_BT_MAX_OPEN_FILES));
  452. this->diskAdaptor = multiDiskAdaptor;
  453. } else {
  454. logger->debug("Instantiating CopyDiskAdaptor");
  455. DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
  456. writer->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
  457. CopyDiskAdaptorHandle copyDiskAdaptor(new CopyDiskAdaptor());
  458. copyDiskAdaptor->setDiskWriter(writer);
  459. copyDiskAdaptor->setTempFilename(downloadContext->getName()+".a2tmp");
  460. copyDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
  461. copyDiskAdaptor->setTopDir(downloadContext->getName());
  462. this->diskAdaptor = copyDiskAdaptor;
  463. }
  464. }
  465. diskAdaptor->setStoreDir(downloadContext->getDir());
  466. diskAdaptor->setFileEntries(downloadContext->getFileEntries());
  467. }
  468. void DefaultPieceStorage::setBitfield(const unsigned char* bitfield,
  469. size_t bitfieldLength)
  470. {
  471. bitfieldMan->setBitfield(bitfield, bitfieldLength);
  472. addPieceStats(bitfield, bitfieldLength);
  473. }
  474. size_t DefaultPieceStorage::getBitfieldLength()
  475. {
  476. return bitfieldMan->getBitfieldLength();
  477. }
  478. const unsigned char* DefaultPieceStorage::getBitfield()
  479. {
  480. return bitfieldMan->getBitfield();
  481. }
  482. DiskAdaptorHandle DefaultPieceStorage::getDiskAdaptor() {
  483. return diskAdaptor;
  484. }
  485. size_t DefaultPieceStorage::getPieceLength(size_t index)
  486. {
  487. return bitfieldMan->getBlockLength(index);
  488. }
  489. void DefaultPieceStorage::advertisePiece(int32_t cuid, size_t index)
  490. {
  491. HaveEntry entry(cuid, index);
  492. haves.push_front(entry);
  493. }
  494. void
  495. DefaultPieceStorage::getAdvertisedPieceIndexes(std::deque<size_t>& indexes,
  496. int32_t myCuid,
  497. const Time& lastCheckTime)
  498. {
  499. for(Haves::const_iterator itr = haves.begin(); itr != haves.end(); itr++) {
  500. const Haves::value_type& have = *itr;
  501. if(have.getCuid() == myCuid) {
  502. continue;
  503. }
  504. if(lastCheckTime.isNewer(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().elapsed(elapsed)) {
  518. return true;
  519. } else {
  520. return false;
  521. }
  522. }
  523. };
  524. void DefaultPieceStorage::removeAdvertisedPiece(time_t elapsed)
  525. {
  526. Haves::iterator itr =
  527. std::find_if(haves.begin(), haves.end(), FindElapsedHave(elapsed));
  528. if(itr != haves.end()) {
  529. logger->debug(MSG_REMOVED_HAVE_ENTRY, haves.end()-itr);
  530. haves.erase(itr, haves.end());
  531. }
  532. }
  533. void DefaultPieceStorage::markAllPiecesDone()
  534. {
  535. bitfieldMan->setAllBit();
  536. }
  537. void DefaultPieceStorage::markPiecesDone(uint64_t length)
  538. {
  539. if(length == bitfieldMan->getTotalLength()) {
  540. bitfieldMan->setAllBit();
  541. } else {
  542. size_t numPiece = length/bitfieldMan->getBlockLength();
  543. if(numPiece > 0) {
  544. bitfieldMan->setBitRange(0, numPiece-1);
  545. }
  546. size_t r = (length%bitfieldMan->getBlockLength())/Piece::BLOCK_LENGTH;
  547. if(r > 0) {
  548. PieceHandle p(new Piece(numPiece, bitfieldMan->getBlockLength(numPiece)));
  549. for(size_t i = 0; i < r; ++i) {
  550. p->completeBlock(i);
  551. }
  552. #ifdef ENABLE_MESSAGE_DIGEST
  553. p->setHashAlgo(downloadContext->getPieceHashAlgo());
  554. #endif // ENABLE_MESSAGE_DIGEST
  555. addUsedPiece(p);
  556. }
  557. }
  558. }
  559. void DefaultPieceStorage::markPieceMissing(size_t index)
  560. {
  561. bitfieldMan->unsetBit(index);
  562. }
  563. void DefaultPieceStorage::addInFlightPiece(const Pieces& pieces)
  564. {
  565. usedPieces.insert(usedPieces.end(), pieces.begin(), pieces.end());
  566. std::sort(usedPieces.begin(), usedPieces.end());
  567. }
  568. size_t DefaultPieceStorage::countInFlightPiece()
  569. {
  570. return usedPieces.size();
  571. }
  572. void DefaultPieceStorage::getInFlightPieces(std::deque<SharedHandle<Piece> >& pieces)
  573. {
  574. pieces.insert(pieces.end(), usedPieces.begin(), usedPieces.end());
  575. }
  576. void DefaultPieceStorage::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
  577. {
  578. _diskWriterFactory = diskWriterFactory;
  579. }
  580. void DefaultPieceStorage::addPieceStats(const unsigned char* bitfield,
  581. size_t bitfieldLength)
  582. {
  583. _pieceSelector->addPieceStats(bitfield, bitfieldLength);
  584. }
  585. void DefaultPieceStorage::subtractPieceStats(const unsigned char* bitfield,
  586. size_t bitfieldLength)
  587. {
  588. _pieceSelector->subtractPieceStats(bitfield, bitfieldLength);
  589. }
  590. void DefaultPieceStorage::updatePieceStats(const unsigned char* newBitfield,
  591. size_t newBitfieldLength,
  592. const unsigned char* oldBitfield)
  593. {
  594. _pieceSelector->updatePieceStats(newBitfield, newBitfieldLength,
  595. oldBitfield);
  596. }
  597. void DefaultPieceStorage::addPieceStats(size_t index)
  598. {
  599. _pieceSelector->addPieceStats(index);
  600. }
  601. } // namespace aria2