38#include <gtest/gtest.h>
52namespace fs = std::filesystem;
64 static std::atomic<unsigned long long> counter{0};
65 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
66 const auto id = std::to_string(
now) +
"_" + std::to_string(counter++);
68 const fs::path dir = fs::temp_directory_path() /
"aleph_ringcache_tests";
69 fs::create_directories(dir);
70 return {dir / (
id +
".pars"), dir / (
id +
".cache")};
76 explicit TempFiles(TempPaths p) : paths(
std::
move(p)) {}
80 fs::remove(paths.pars,
ec);
81 fs::remove(paths.cache,
ec);
89 for (
size_t i = 0; i < arr.
size(); ++i)
90 out.push_back(arr[i]);
113 int buf[4] = {0, 0, 0, 0};
115 EXPECT_EQ(std::vector<int>(
buf,
buf + 4), std::vector<int>({1, 2, 3, 4}));
140 for (
int v : {10, 20, 30, 40})
143 auto sub = cache.read_from(1, 2);
148 auto sub_ptr = cache.read_from(ptr, 3);
152 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
153 iterated.push_back(it.get_curr_ne());
160 TempFiles
tmp(paths);
164 for (
int v : {1, 2, 3})
195 auto all = cache.read_all();
205 auto all = cache.read_all();
220 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
222 auto first_two = cache.read_from(ptr, 2);
226 EXPECT_EQ(ptr.get_pos_respect_to_head(), 1u);
229 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
236 auto wrapped = cache.read_from(head_ptr, 3);
240 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
241 iterated.push_back(it.get_curr_ne());
248 TempFiles
tmp(paths);
252 cache.
init(
tmp.paths.pars.string());
295 for (
int i = 0; i < 4; ++i)
300 for (
int i = 0; i < 4; ++i)
424 cache.
init(
tmp.paths.pars.string());
437 for (
int i = 0; i < 5; ++i)
467 for (
int i = 0; i < 4; ++i)
473 auto ptr2 = ptr + (-1);
489 return id ==
other.id && value ==
other.value &&
490 std::strncmp(tag,
other.tag, 8) == 0;
498 tmp.paths.cache.string(), 3);
501 TestRecord
r1{1, 3.14,
"alpha"};
502 TestRecord r2{2, 2.71,
"beta"};
503 TestRecord
r3{3, 1.41,
"gamma"};
565 TempFiles
tmp(paths);
bool operator==(const Time &l, const Time &r)
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
size_t size() const noexcept
Count the number of elements of the list.
Defines a pointer to a specific location in the cache.
Persistent ring buffer cache with file-backed storage.
T read(const Pointer &ptr)
Read the entry pointed to by ptr
T read_first()
Read the oldest entry in the cache.
Array< T > read_all()
Read all entries in insertion order.
bool is_empty() const noexcept
Returns true if the cache is empty.
Iterator get_it()
Returns a forward iterator starting at the oldest entry.
bool put(const T &item)
Insert an item into the cache.
void init(const std::string &pars_fname)
Initialize a cache constructed with the default constructor.
size_t size() const noexcept
Returns the number of entries stored in the cache.
size_t capacity() const noexcept
Returns the maximum capacity.
bool is_full() const noexcept
Returns true if the cache is full (no more entries can be inserted)
bool is_initialized() const
True if the cache has been initialized with a valid parameters file.
Array< T > read_from(const size_t pos, const size_t m)
Read up to m entries starting from the pos-th oldest position.
bool get(const size_t m=1) noexcept
Extracts (deletes) from the cache the m oldest inserted items.
void flush()
Flushes the current cache state to disk.
void close()
Flushes state and closes the underlying streams.
static void create(const std::string &pars_file_name, const std::string &cache_file_name, const size_t num_entries)
Create a brand new on-disk ring cache.
void resize(const size_t sz)
Resize the maximum capacity of the cache.
size_t avail() const noexcept
Returns the number of available entries.
T oldest()
Alias for read_first().
T read_last()
Read the youngest (most recently inserted) entry in the cache.
Main namespace for Aleph-w library functions.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
DynList< T > maps(const C &c, Op op)
Classic map operation.
double sub(double a, double b)
File-backed ring buffer cache for persistent storage.