2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2010-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2010-06-19 14:25:11 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstdint> // intX_t, uintX_t
|
2022-06-20 04:08:58 +00:00
|
|
|
#include <memory> // std::unique_ptr
|
2022-06-12 00:46:30 +00:00
|
|
|
#include <utility> // std::pair
|
|
|
|
#include <vector>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2022-02-19 13:45:19 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "block-info.h"
|
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
class tr_torrents;
|
2011-01-29 18:56:53 +00:00
|
|
|
struct evbuffer;
|
2021-12-15 21:25:42 +00:00
|
|
|
struct tr_torrent;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
class Cache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Cache(tr_torrents& torrents, int64_t max_bytes);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
int setLimit(int64_t new_limit);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
[[nodiscard]] constexpr auto getLimit() const noexcept
|
|
|
|
{
|
|
|
|
return max_bytes_;
|
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-20 04:08:58 +00:00
|
|
|
void writeBlock(tr_torrent_id_t tor, tr_block_index_t block, std::unique_ptr<std::vector<uint8_t>>& writeme);
|
2022-06-12 00:46:30 +00:00
|
|
|
int readBlock(tr_torrent* torrent, tr_block_info::Location loc, uint32_t len, uint8_t* setme);
|
|
|
|
int prefetchBlock(tr_torrent* torrent, tr_block_info::Location loc, uint32_t len);
|
|
|
|
int flushTorrent(tr_torrent* torrent);
|
|
|
|
int flushFile(tr_torrent* torrent, tr_file_index_t file);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
private:
|
|
|
|
using Key = std::pair<tr_torrent_id_t, tr_block_index_t>;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
struct CacheBlock
|
|
|
|
{
|
|
|
|
Key key;
|
2022-06-20 04:08:58 +00:00
|
|
|
std::unique_ptr<std::vector<uint8_t>> buf;
|
2022-06-12 00:46:30 +00:00
|
|
|
time_t time_added = {};
|
|
|
|
};
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
using Blocks = std::vector<CacheBlock>;
|
|
|
|
using CIter = Blocks::const_iterator;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
struct CompareCacheBlockByKey
|
|
|
|
{
|
|
|
|
[[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block)
|
|
|
|
{
|
|
|
|
return key < block.key;
|
|
|
|
}
|
|
|
|
[[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key)
|
|
|
|
{
|
|
|
|
return block.key < key;
|
|
|
|
}
|
|
|
|
};
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
[[nodiscard]] static Key makeKey(tr_torrent const* torrent, tr_block_info::Location loc) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
[[nodiscard]] static std::pair<CIter, CIter> findContiguous(CIter const begin, CIter const end, CIter const iter) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from tr_ioWrite()
|
|
|
|
[[nodiscard]] int writeContiguous(CIter const begin, CIter const end) const;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from writeContiguous()
|
|
|
|
[[nodiscard]] int flushSpan(CIter const begin, CIter const end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from writeContiguous()
|
|
|
|
[[nodiscard]] int flushOldest();
|
|
|
|
|
|
|
|
// @return any error code from writeContiguous()
|
|
|
|
[[nodiscard]] int cacheTrim();
|
|
|
|
|
|
|
|
[[nodiscard]] static size_t getMaxBlocks(int64_t max_bytes) noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] CIter getBlock(tr_torrent const* torrent, tr_block_info::Location loc) noexcept;
|
|
|
|
|
|
|
|
tr_torrents& torrents_;
|
|
|
|
|
|
|
|
Blocks blocks_ = {};
|
|
|
|
size_t max_blocks_ = 0;
|
|
|
|
size_t max_bytes_ = 0;
|
|
|
|
|
|
|
|
mutable size_t disk_writes_ = 0;
|
|
|
|
mutable size_t disk_write_bytes_ = 0;
|
|
|
|
mutable size_t cache_writes_ = 0;
|
|
|
|
mutable size_t cache_write_bytes_ = 0;
|
|
|
|
};
|