2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2010-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint> // for size_t
|
|
|
|
#include <cstdint> // for intX_t, uintX_t
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <ctime>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <memory> // for std::unique_ptr
|
|
|
|
#include <utility> // for std::pair
|
2022-06-12 00:46:30 +00:00
|
|
|
#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;
|
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
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
int set_limit(int64_t new_limit);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] constexpr auto get_limit() const noexcept
|
2022-06-12 00:46:30 +00:00
|
|
|
{
|
|
|
|
return max_bytes_;
|
|
|
|
}
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
// @return any error code from cacheTrim()
|
2023-05-06 04:11:05 +00:00
|
|
|
int write_block(tr_torrent_id_t tor, tr_block_index_t block, std::unique_ptr<std::vector<uint8_t>> writeme);
|
2022-10-24 21:57:07 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
int read_block(tr_torrent* torrent, tr_block_info::Location const& loc, uint32_t len, uint8_t* setme);
|
|
|
|
int prefetch_block(tr_torrent* torrent, tr_block_info::Location const& loc, uint32_t len);
|
|
|
|
int flush_torrent(tr_torrent const* torrent);
|
|
|
|
int flush_file(tr_torrent const* 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
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] static Key make_key(tr_torrent const* torrent, tr_block_info::Location loc) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] static std::pair<CIter, CIter> find_contiguous(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()
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] int write_contiguous(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()
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] int flush_span(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()
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] int flush_oldest();
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
// @return any error code from writeContiguous()
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] int cache_trim();
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] static size_t get_max_blocks(int64_t max_bytes) noexcept;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-05-06 04:11:05 +00:00
|
|
|
[[nodiscard]] CIter get_block(tr_torrent const* torrent, tr_block_info::Location const& loc) noexcept;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|