38#include <gtest/gtest.h>
57namespace fs = std::filesystem;
68long long process_id() noexcept
71 return static_cast<long long>(_getpid());
73 return static_cast<long long>(getpid());
77TempPaths make_temp_paths()
88 static std::atomic<unsigned long long>
counter{0};
89 const auto now = std::chrono::steady_clock::now().time_since_epoch().count();
90 const auto id = std::to_string(now) +
"_" + std::to_string(process_id()) +
91 "_" + std::to_string(
counter++);
93 const fs::path dir = fs::temp_directory_path() /
"aleph_ringcache_tests";
94 fs::create_directories(dir);
95 return {dir / (
id +
".pars"), dir / (
id +
".cache")};
101 explicit TempFiles(TempPaths p) : paths(
std::move(p)) {}
105 fs::remove(paths.pars, ec);
106 fs::remove(paths.cache, ec);
114 for (
size_t i = 0; i < arr.
size(); ++i)
115 out.push_back(arr[i]);
121 TempFiles tmp(make_temp_paths());
126 EXPECT_TRUE(cache.is_initialized());
127 EXPECT_EQ(cache.capacity(), 4u);
128 EXPECT_EQ(cache.size(), 0u);
129 EXPECT_TRUE(cache.read_all().is_empty());
131 EXPECT_TRUE(cache.put(1));
132 EXPECT_TRUE(cache.put(2));
133 EXPECT_TRUE(cache.put(3));
134 EXPECT_TRUE(cache.put(4));
135 EXPECT_FALSE(cache.put(5));
136 EXPECT_EQ(cache.size(), 4u);
138 int buf[4] = {0, 0, 0, 0};
139 EXPECT_TRUE(cache.read(buf, 4));
140 EXPECT_EQ(std::vector<int>(buf, buf + 4), std::vector<int>({1, 2, 3, 4}));
141 EXPECT_EQ(cache.read_first(), 1);
142 EXPECT_EQ(cache.read_last(), 4);
143 EXPECT_EQ(cache.oldest(), 1);
144 EXPECT_EQ(cache.oldest(2), 3);
146 EXPECT_TRUE(cache.get(2));
147 EXPECT_EQ(cache.size(), 2u);
148 EXPECT_EQ(cache.read_first(), 3);
149 EXPECT_EQ(cache.read_last(), 4);
150 EXPECT_TRUE(cache.get(2));
151 EXPECT_TRUE(cache.is_empty());
153 EXPECT_THROW(cache.read_first(), std::underflow_error);
154 EXPECT_THROW(cache.read_last(), std::underflow_error);
155 EXPECT_FALSE(cache.get(1));
156 EXPECT_FALSE(cache.read(buf, 1));
161 TempFiles tmp(make_temp_paths());
165 for (
int v : {10, 20, 30, 40})
166 ASSERT_TRUE(cache.put(v));
168 auto sub = cache.read_from(1, 2);
169 EXPECT_EQ(
to_vector(sub), std::vector<int>({20, 30}));
173 auto sub_ptr = cache.read_from(ptr, 3);
174 EXPECT_EQ(
to_vector(sub_ptr), std::vector<int>({30, 40}));
176 std::vector<int> iterated;
177 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
178 iterated.push_back(it.get_curr_ne());
179 EXPECT_EQ(iterated, std::vector<int>({10, 20, 30, 40}));
184 TempPaths paths = make_temp_paths();
185 TempFiles tmp(paths);
189 for (
int v : {1, 2, 3})
190 ASSERT_TRUE(cache.put(v));
195 EXPECT_EQ(reopened.size(), 3u);
196 EXPECT_EQ(reopened.read_first(), 1);
197 EXPECT_EQ(reopened.read_last(), 3);
198 auto all = reopened.read_all();
201 EXPECT_EQ(reopened.size(), 1u);
202 EXPECT_EQ(reopened.read_first(), 3);
207 TempFiles tmp(make_temp_paths());
213 EXPECT_EQ(cache.capacity(), 2u);
215 EXPECT_EQ(cache.capacity(), 5u);
216 EXPECT_TRUE(cache.put(9));
217 EXPECT_TRUE(cache.put(10));
218 EXPECT_TRUE(cache.put(11));
220 auto all = cache.read_all();
221 EXPECT_EQ(
to_vector(
all), std::vector<int>({7, 8, 9, 10, 11}));
226 TempFiles tmp(make_temp_paths());
230 auto all = cache.read_all();
231 EXPECT_TRUE(
all.is_empty());
236 TempFiles tmp(make_temp_paths());
245 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
247 auto first_two = cache.read_from(ptr, 2);
248 EXPECT_EQ(
to_vector(first_two), std::vector<int>({1, 2}));
251 EXPECT_EQ(ptr.get_pos_respect_to_head(), 1u);
254 EXPECT_EQ(ptr.get_pos_respect_to_head(), 0u);
261 auto wrapped = cache.read_from(head_ptr, 3);
262 EXPECT_EQ(
to_vector(wrapped), std::vector<int>({3, 4, 5}));
264 std::vector<int> iterated;
265 for (
auto it = cache.get_it(); it.has_curr(); it.next_ne())
266 iterated.push_back(it.get_curr_ne());
267 EXPECT_EQ(iterated, std::vector<int>({3, 4, 5}));
272 TempPaths paths = make_temp_paths();
273 TempFiles tmp(paths);
277 cache.
init(tmp.paths.pars.string());
284 EXPECT_EQ(reopened.size(), 1u);
285 EXPECT_EQ(reopened.read_first(), 42);
290 TempFiles tmp(make_temp_paths());
312 TempFiles tmp(make_temp_paths());
317 for (
int cycle = 0; cycle < 5; ++cycle)
320 for (
int i = 0; i < 4; ++i)
321 ASSERT_TRUE(cache.
put(cycle * 10 + i));
325 for (
int i = 0; i < 4; ++i)
326 EXPECT_EQ(cache.
oldest(i), cycle * 10 + i);
329 EXPECT_TRUE(cache.
get(4));
334 for (
int cycle = 0; cycle < 10; ++cycle)
337 cache.
put(cycle + 100);
338 EXPECT_EQ(cache.
size(), 2u);
340 EXPECT_EQ(cache.
read_last(), cycle + 100);
347 TempFiles tmp(make_temp_paths());
360 EXPECT_EQ(cache.
size(), 4u);
362 EXPECT_EQ(
to_vector(before), std::vector<int>({3, 4, 5, 6}));
367 EXPECT_EQ(cache.
size(), 4u);
371 EXPECT_EQ(
to_vector(after), std::vector<int>({3, 4, 5, 6}));
374 EXPECT_TRUE(cache.
put(7));
375 EXPECT_TRUE(cache.
put(8));
376 EXPECT_TRUE(cache.
put(9));
377 EXPECT_TRUE(cache.
put(10));
378 EXPECT_EQ(cache.
size(), 8u);
384 TempFiles tmp(make_temp_paths());
391 EXPECT_EQ(cache.
size(), 0u);
397 EXPECT_EQ(cache.
size(), 1u);
403 TempFiles tmp(make_temp_paths());
411 EXPECT_TRUE(it.has_curr());
412 EXPECT_EQ(it.get_curr(), 1);
414 EXPECT_EQ(it.get_curr(), 2);
416 EXPECT_FALSE(it.has_curr());
419 EXPECT_THROW(it.get_curr(), std::overflow_error);
421 EXPECT_THROW(it.next(), std::overflow_error);
426 TempFiles tmp(make_temp_paths());
434 EXPECT_EQ(cache.
oldest(0), 10);
435 EXPECT_EQ(cache.
oldest(1), 20);
436 EXPECT_EQ(cache.
oldest(2), 30);
439 EXPECT_THROW(cache.
oldest(3), std::overflow_error);
440 EXPECT_THROW(cache.
oldest(100), std::overflow_error);
445 TempFiles tmp(make_temp_paths());
449 cache.
init(tmp.paths.pars.string());
453 EXPECT_THROW(cache.
init(tmp.paths.pars.string()), std::domain_error);
458 TempFiles tmp(make_temp_paths());
462 for (
int i = 0; i < 5; ++i)
466 EXPECT_EQ(cache.
read(ptr), 20);
469 EXPECT_EQ(cache.
read(ptr), 10);
472 EXPECT_EQ(cache.
read(ptr), 0);
476 EXPECT_EQ(cache.
read(ptr), 40);
479 EXPECT_EQ(cache.
read(ptr), 20);
483 EXPECT_EQ(cache.
read(ptr), 0);
488 TempFiles tmp(make_temp_paths());
492 for (
int i = 0; i < 4; ++i)
496 EXPECT_EQ(cache.
read(ptr), 2);
498 auto ptr2 = ptr + (-1);
499 EXPECT_EQ(cache.
read(ptr2), 1);
502 EXPECT_EQ(cache.
read(ptr2), 3);
512 bool operator==(
const TestRecord &other)
const
514 return id == other.id &&
value == other.value &&
515 std::strncmp(tag, other.tag, 8) == 0;
521 TempFiles tmp(make_temp_paths());
523 tmp.paths.cache.string(), 3);
526 TestRecord r1{1, 3.14,
"alpha"};
527 TestRecord r2{2, 2.71,
"beta"};
528 TestRecord r3{3, 1.41,
"gamma"};
530 EXPECT_TRUE(cache.
put(r1));
531 EXPECT_TRUE(cache.
put(r2));
532 EXPECT_TRUE(cache.
put(r3));
537 EXPECT_EQ(cache.
oldest(1), r2);
545 TempFiles tmp(make_temp_paths());
551 EXPECT_EQ(cache.
size(), 0u);
552 EXPECT_EQ(cache.
avail(), 5u);
555 EXPECT_FALSE(cache.
get(1));
557 EXPECT_FALSE(cache.
read(buf, 1));
561 EXPECT_TRUE(
all.is_empty());
565 EXPECT_FALSE(it.has_curr());
570 TempFiles tmp(make_temp_paths());
580 EXPECT_TRUE(result.is_empty());
584 EXPECT_EQ(
to_vector(result2), std::vector<int>({2, 3}));
589 TempPaths paths = make_temp_paths();
590 TempFiles tmp(paths);
611 EXPECT_EQ(reopened.size(), 4u);
612 auto all = reopened.read_all();
613 EXPECT_EQ(
to_vector(
all), std::vector<int>({3, 4, 5, 6}));
618 TempFiles tmp(make_temp_paths());
626 EXPECT_EQ(cache.
size(), 3u);
630 EXPECT_EQ(cache.
size(), 0u);
632 EXPECT_TRUE(cache.
read_all().is_empty());
637 EXPECT_EQ(cache.
size(), 2u);
639 EXPECT_EQ(cache.
size(), 0u);
645 EXPECT_EQ(cache.
size(), 0u);
650 EXPECT_EQ(cache.
size(), 0u);
bool operator==(const Time &l, const Time &r)
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
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.
void clear() noexcept
Removes all entries from the cache.
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.
void empty() noexcept
Empties the cache; all entries are logically deleted.
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.
double sub(double a, double b)
File-backed ring buffer cache for persistent storage.