2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © 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
|
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
#include <cstddef> // for size_t
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <cstdint> // for intX_t, uintX_t
|
|
|
|
#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
|
|
|
|
2023-06-21 03:50:36 +00:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2022-02-19 13:45:19 +00:00
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/block-info.h"
|
2023-11-14 23:23:51 +00:00
|
|
|
#include "libtransmission/values.h"
|
2022-02-19 13:45:19 +00:00
|
|
|
|
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:
|
2023-06-21 03:50:36 +00:00
|
|
|
using BlockData = small::max_size_vector<uint8_t, tr_block_info::BlockSize>;
|
2023-11-14 23:23:51 +00:00
|
|
|
using Memory = libtransmission::Values::Memory;
|
2023-05-13 19:16:00 +00:00
|
|
|
|
2023-11-14 23:23:51 +00:00
|
|
|
Cache(tr_torrents const& torrents, Memory max_size);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-11-14 23:23:51 +00:00
|
|
|
int set_limit(Memory max_size);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-10-24 21:57:07 +00:00
|
|
|
// @return any error code from cacheTrim()
|
2023-05-13 19:16:00 +00:00
|
|
|
int write_block(tr_torrent_id_t tor, tr_block_index_t block, std::unique_ptr<BlockData> writeme);
|
2022-10-24 21:57:07 +00:00
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
int read_block(tr_torrent const& tor, tr_block_info::Location const& loc, size_t len, uint8_t* setme);
|
|
|
|
int flush_torrent(tr_torrent_id_t tor_id);
|
|
|
|
int flush_file(tr_torrent const& tor, 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;
|
2023-05-13 19:16:00 +00:00
|
|
|
std::unique_ptr<BlockData> buf;
|
2022-06-12 00:46:30 +00:00
|
|
|
};
|
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
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
[[nodiscard]] static Key make_key(tr_torrent const& tor, tr_block_info::Location loc) noexcept;
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] static std::pair<CIter, CIter> find_biggest_span(CIter begin, CIter end) noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] static CIter find_span_end(CIter span_begin, CIter end) 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-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int write_contiguous(CIter begin, CIter 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-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int flush_span(CIter begin, CIter end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2022-06-12 00:46:30 +00:00
|
|
|
// @return any error code from writeContiguous()
|
2023-06-27 17:08:29 +00:00
|
|
|
[[nodiscard]] int flush_biggest();
|
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-11-14 23:23:51 +00:00
|
|
|
[[nodiscard]] static constexpr size_t get_max_blocks(Memory const max_size) noexcept
|
|
|
|
{
|
|
|
|
return max_size.base_quantity() / tr_block_info::BlockSize;
|
|
|
|
}
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-12-02 20:16:36 +00:00
|
|
|
[[nodiscard]] CIter get_block(tr_torrent const& tor, tr_block_info::Location const& loc) noexcept;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
2023-11-12 17:53:04 +00:00
|
|
|
tr_torrents const& torrents_;
|
2022-06-12 00:46:30 +00:00
|
|
|
|
|
|
|
Blocks blocks_ = {};
|
|
|
|
size_t max_blocks_ = 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;
|
2023-06-28 15:23:38 +00:00
|
|
|
|
|
|
|
static constexpr struct
|
|
|
|
{
|
2024-02-18 04:43:24 +00:00
|
|
|
[[nodiscard]] constexpr bool operator()(Key const& key, CacheBlock const& block) const
|
2023-06-28 15:23:38 +00:00
|
|
|
{
|
|
|
|
return key < block.key;
|
|
|
|
}
|
2024-02-18 04:43:24 +00:00
|
|
|
[[nodiscard]] constexpr bool operator()(CacheBlock const& block, Key const& key) const
|
2023-06-28 15:23:38 +00:00
|
|
|
{
|
|
|
|
return block.key < key;
|
|
|
|
}
|
|
|
|
} CompareCacheBlockByKey{};
|
2022-06-12 00:46:30 +00:00
|
|
|
};
|