// Pattern: ring buffer over a fixed number of keys
// Textbook section: Volume V, chapter 32, "Design patterns", §32.5.
// Task: a ring buffer of the last N values (e.g., a readings log).
// Solution: a fixed set of keys 0..N-1; a write overwrites the slot
//   (counter % N); a cursor can traverse the whole buffer in order.
// Trade-offs: an old element is overwritten by a new one — only the "last N";
//   a history log requires a different pattern.
// Build: g++ -std=c++17 -I<libmdbx-include> -I../common 43-pattern-ring-buffer.c++ -lmdbx
// Run: ./43-pattern-ring-buffer
// Expected output:
//   buffer contents: s10 s11 s7 s8 s9
//   ok: ring-buffer pattern works
// On failure — message to stderr and exit 1.
//
// SPDX-License-Identifier: Apache-2.0

#include <cstdint>
#include <iostream>
#include <string>

#include "common.h++"
#include <mdbx.h++>

using buffer = mdbx::buffer<mdbx::default_allocator, mdbx::default_capacity_policy>;

namespace {
constexpr unsigned kSlots = 5;
}

int main() {
  try {
    const std::string path = std::string(example::tmpdir()) + "/mdbx-43-pattern-ring-buffer.mdbx";
    mdbx::env::remove(path);
    mdbx::env_managed env = example::env_open(path);

    // Write 12 elements into a buffer with 5 slots.
    {
      auto txn = env.start_write();
      auto ring = txn.create_map("ring", mdbx::key_mode::ordinal, mdbx::value_mode::single);
      for (unsigned i = 0; i < 12; ++i) {
        const unsigned slot = i % kSlots;
        txn.upsert(ring, buffer::key_from_u64(slot), mdbx::slice("s" + std::to_string(i)));
      }
      txn.commit();
    }

    // Traverse the buffer in slot order.
    auto rtxn = env.start_read();
    auto ring = rtxn.open_map("ring", mdbx::key_mode::ordinal, mdbx::value_mode::single);
    std::string contents;
    auto cur = rtxn.open_cursor(ring);
    for (auto r = cur.to_first(); r; r = cur.to_next(false))
      contents += r.value.as_string() + " ";
    std::cout << "buffer contents: " << contents << "\n";
    rtxn.abort();

    std::cout << "ok: ring-buffer pattern works\n";
    return EXIT_SUCCESS;
  } catch (const std::exception &ex) {
    std::cerr << "error: " << ex.what() << "\n";
    return EXIT_FAILURE;
  }
}