DefaultPieceStorage.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #ifndef D_DEFAULT_PIECE_STORAGE_H
  36. #define D_DEFAULT_PIECE_STORAGE_H
  37. #include "PieceStorage.h"
  38. #include <deque>
  39. #include <set>
  40. #include "a2functional.h"
  41. namespace aria2 {
  42. class DownloadContext;
  43. class BitfieldMan;
  44. class Option;
  45. class DiskWriterFactory;
  46. class FileEntry;
  47. class PieceStatMan;
  48. class PieceSelector;
  49. class StreamPieceSelector;
  50. #define END_GAME_PIECE_NUM 20
  51. struct HaveEntry {
  52. HaveEntry(uint64_t haveIndex, cuid_t cuid, size_t index, Timer registeredTime)
  53. : haveIndex(haveIndex),
  54. cuid(cuid),
  55. index(index),
  56. registeredTime(std::move(registeredTime))
  57. {
  58. }
  59. uint64_t haveIndex;
  60. cuid_t cuid;
  61. size_t index;
  62. Timer registeredTime;
  63. };
  64. class DefaultPieceStorage : public PieceStorage {
  65. private:
  66. std::shared_ptr<DownloadContext> downloadContext_;
  67. std::unique_ptr<BitfieldMan> bitfieldMan_;
  68. std::shared_ptr<DiskAdaptor> diskAdaptor_;
  69. std::shared_ptr<DiskWriterFactory> diskWriterFactory_;
  70. typedef std::set<std::shared_ptr<Piece>, DerefLess<std::shared_ptr<Piece>>>
  71. UsedPieceSet;
  72. UsedPieceSet usedPieces_;
  73. bool endGame_;
  74. size_t endGamePieceNum_;
  75. const Option* option_;
  76. // The next unique index on HaveEntry, which is ever strictly
  77. // increasing sequence of integer.
  78. uint64_t nextHaveIndex_;
  79. std::deque<HaveEntry> haves_;
  80. std::shared_ptr<PieceStatMan> pieceStatMan_;
  81. std::unique_ptr<PieceSelector> pieceSelector_;
  82. std::unique_ptr<StreamPieceSelector> streamPieceSelector_;
  83. WrDiskCache* wrDiskCache_;
  84. #ifdef ENABLE_BITTORRENT
  85. void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  86. size_t minMissingBlocks, const unsigned char* bitfield,
  87. size_t length, cuid_t cuid);
  88. void createFastIndexBitfield(BitfieldMan& bitfield,
  89. const std::shared_ptr<Peer>& peer);
  90. #endif // ENABLE_BITTORRENT
  91. std::shared_ptr<Piece> checkOutPiece(size_t index, cuid_t cuid);
  92. // size_t deleteUsedPiecesByFillRate(int fillRate, size_t toDelete);
  93. // void reduceUsedPieces(size_t upperBound);
  94. void deleteUsedPiece(const std::shared_ptr<Piece>& piece);
  95. std::shared_ptr<Piece> findUsedPiece(size_t index) const;
  96. // Returns the sum of completed length of in-flight pieces
  97. int64_t getInFlightPieceCompletedLength() const;
  98. // Returns the sum of completed length of in-flight pieces
  99. // intersecting filter ranges.
  100. int64_t getInFlightPieceFilteredCompletedLength() const;
  101. public:
  102. // Setting randomPieceStatsOrdering to true means a piece is chosen in
  103. // random when more than 2 pieces has the same rarity.
  104. // If it is set to false, a piece whose index is smallest has the highest
  105. // priority.
  106. DefaultPieceStorage(const std::shared_ptr<DownloadContext>& downloadContext,
  107. const Option* option);
  108. virtual ~DefaultPieceStorage();
  109. #ifdef ENABLE_BITTORRENT
  110. virtual bool
  111. hasMissingPiece(const std::shared_ptr<Peer>& peer) CXX11_OVERRIDE;
  112. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  113. size_t minMissingBlocks,
  114. const std::shared_ptr<Peer>& peer,
  115. cuid_t cuid) CXX11_OVERRIDE;
  116. virtual void getMissingPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  117. size_t minMissingBlocks,
  118. const std::shared_ptr<Peer>& peer,
  119. const std::vector<size_t>& excludedIndexes,
  120. cuid_t cuid) CXX11_OVERRIDE;
  121. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  122. size_t minMissingBlocks,
  123. const std::shared_ptr<Peer>& peer,
  124. cuid_t cuid) CXX11_OVERRIDE;
  125. virtual void getMissingFastPiece(std::vector<std::shared_ptr<Piece>>& pieces,
  126. size_t minMissingBlocks,
  127. const std::shared_ptr<Peer>& peer,
  128. const std::vector<size_t>& excludedIndexes,
  129. cuid_t cuid) CXX11_OVERRIDE;
  130. virtual std::shared_ptr<Piece>
  131. getMissingPiece(const std::shared_ptr<Peer>& peer,
  132. cuid_t cuid) CXX11_OVERRIDE;
  133. virtual std::shared_ptr<Piece>
  134. getMissingPiece(const std::shared_ptr<Peer>& peer,
  135. const std::vector<size_t>& excludedIndexes,
  136. cuid_t cuid) CXX11_OVERRIDE;
  137. std::shared_ptr<Piece> getMissingFastPiece(const std::shared_ptr<Peer>& peer,
  138. cuid_t cuid);
  139. std::shared_ptr<Piece>
  140. getMissingFastPiece(const std::shared_ptr<Peer>& peer,
  141. const std::vector<size_t>& excludedIndexes, cuid_t cuid);
  142. #endif // ENABLE_BITTORRENT
  143. virtual bool hasMissingUnusedPiece() CXX11_OVERRIDE;
  144. virtual std::shared_ptr<Piece>
  145. getMissingPiece(size_t minSplitSize, const unsigned char* ignoreBitfield,
  146. size_t length, cuid_t cuid) CXX11_OVERRIDE;
  147. virtual std::shared_ptr<Piece> getMissingPiece(size_t index,
  148. cuid_t cuid) CXX11_OVERRIDE;
  149. virtual std::shared_ptr<Piece> getPiece(size_t index) CXX11_OVERRIDE;
  150. virtual void
  151. completePiece(const std::shared_ptr<Piece>& piece) CXX11_OVERRIDE;
  152. virtual void cancelPiece(const std::shared_ptr<Piece>& piece,
  153. cuid_t cuid) CXX11_OVERRIDE;
  154. virtual bool hasPiece(size_t index) CXX11_OVERRIDE;
  155. virtual bool isPieceUsed(size_t index) CXX11_OVERRIDE;
  156. virtual int64_t getTotalLength() CXX11_OVERRIDE;
  157. virtual int64_t getFilteredTotalLength() CXX11_OVERRIDE;
  158. virtual int64_t getCompletedLength() CXX11_OVERRIDE;
  159. virtual int64_t getFilteredCompletedLength() CXX11_OVERRIDE;
  160. virtual void initStorage() CXX11_OVERRIDE;
  161. virtual void setupFileFilter() CXX11_OVERRIDE;
  162. virtual void clearFileFilter() CXX11_OVERRIDE;
  163. virtual bool downloadFinished() CXX11_OVERRIDE;
  164. virtual bool allDownloadFinished() CXX11_OVERRIDE;
  165. virtual void setBitfield(const unsigned char* bitfield,
  166. size_t bitfieldLength) CXX11_OVERRIDE;
  167. virtual size_t getBitfieldLength() CXX11_OVERRIDE;
  168. virtual const unsigned char* getBitfield() CXX11_OVERRIDE;
  169. virtual void setEndGamePieceNum(size_t num) CXX11_OVERRIDE
  170. {
  171. endGamePieceNum_ = num;
  172. }
  173. size_t getEndGamePieceNum() const { return endGamePieceNum_; }
  174. virtual bool isSelectiveDownloadingMode() CXX11_OVERRIDE;
  175. virtual bool isEndGame() CXX11_OVERRIDE { return endGame_; }
  176. virtual void enterEndGame() CXX11_OVERRIDE { endGame_ = true; }
  177. virtual std::shared_ptr<DiskAdaptor> getDiskAdaptor() CXX11_OVERRIDE;
  178. virtual WrDiskCache* getWrDiskCache() CXX11_OVERRIDE;
  179. virtual void flushWrDiskCacheEntry() CXX11_OVERRIDE;
  180. virtual int32_t getPieceLength(size_t index) CXX11_OVERRIDE;
  181. virtual void advertisePiece(cuid_t cuid, size_t index,
  182. Timer registeredTime) CXX11_OVERRIDE;
  183. virtual uint64_t
  184. getAdvertisedPieceIndexes(std::vector<size_t>& indexes, cuid_t myCuid,
  185. uint64_t lastHaveIndex) CXX11_OVERRIDE;
  186. virtual void removeAdvertisedPiece(const Timer& expiry) CXX11_OVERRIDE;
  187. virtual void markAllPiecesDone() CXX11_OVERRIDE;
  188. virtual void markPiecesDone(int64_t length) CXX11_OVERRIDE;
  189. virtual void markPieceMissing(size_t index) CXX11_OVERRIDE;
  190. virtual void addInFlightPiece(
  191. const std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE;
  192. virtual size_t countInFlightPiece() CXX11_OVERRIDE;
  193. virtual void
  194. getInFlightPieces(std::vector<std::shared_ptr<Piece>>& pieces) CXX11_OVERRIDE;
  195. virtual void addPieceStats(size_t index) CXX11_OVERRIDE;
  196. virtual void addPieceStats(const unsigned char* bitfield,
  197. size_t bitfieldLength) CXX11_OVERRIDE;
  198. virtual void subtractPieceStats(const unsigned char* bitfield,
  199. size_t bitfieldLength) CXX11_OVERRIDE;
  200. virtual void
  201. updatePieceStats(const unsigned char* newBitfield, size_t newBitfieldLength,
  202. const unsigned char* oldBitfield) CXX11_OVERRIDE;
  203. virtual size_t getNextUsedIndex(size_t index) CXX11_OVERRIDE;
  204. virtual void onDownloadIncomplete() CXX11_OVERRIDE;
  205. /**
  206. * This method is made private for test purpose only.
  207. */
  208. void addUsedPiece(const std::shared_ptr<Piece>& piece);
  209. void setDiskWriterFactory(
  210. const std::shared_ptr<DiskWriterFactory>& diskWriterFactory);
  211. const std::shared_ptr<PieceStatMan>& getPieceStatMan() const
  212. {
  213. return pieceStatMan_;
  214. }
  215. void setPieceSelector(std::unique_ptr<PieceSelector> pieceSelector);
  216. const std::unique_ptr<PieceSelector>& getPieceSelector() const
  217. {
  218. return pieceSelector_;
  219. }
  220. std::unique_ptr<PieceSelector> popPieceSelector();
  221. void setWrDiskCache(WrDiskCache* wrDiskCache) { wrDiskCache_ = wrDiskCache; }
  222. };
  223. } // namespace aria2
  224. #endif // D_DEFAULT_PIECE_STORAGE_H