diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2026-05-10 03:36:59 -0300 |
|---|---|---|
| committer | Marcos Magueta <maguetamarcos@gmail.com> | 2026-05-10 03:36:59 -0300 |
| commit | 2864398c7b723c1d43166d422ea7028ef33cf9e3 (patch) | |
| tree | ec63cd647239612167c36e521610f69023ed9303 /src/InMemoryBackend.cpp | |
| parent | 7ffd4d99bbb8e358c6694c27c7ff859967872ece (diff) | |
| download | RNT-2864398c7b723c1d43166d422ea7028ef33cf9e3.tar.gz RNT-2864398c7b723c1d43166d422ea7028ef33cf9e3.zip | |
sqlite backend with actual pagination
Diffstat (limited to 'src/InMemoryBackend.cpp')
| -rw-r--r-- | src/InMemoryBackend.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/InMemoryBackend.cpp b/src/InMemoryBackend.cpp new file mode 100644 index 0000000..4d637fe --- /dev/null +++ b/src/InMemoryBackend.cpp @@ -0,0 +1,55 @@ +#include "InMemoryBackend.h" + +#include <picosha2.h> +#include <numeric> + +namespace nt +{ + std::string InMemoryBackend::PathKey(const std::vector<std::string>& path) + { + return std::accumulate( + std::next(path.begin()), path.end(), path.front(), + [](std::string a, const std::string& b) { return std::move(a) + "/" + b; }); + } + + std::string InMemoryBackend::Put(std::vector<uint8_t> value) + { + const std::string hash = picosha2::hash256_hex_string(value.begin(), value.end()); + kv_.emplace(hash, std::move(value)); + return hash; + } + + std::optional<std::vector<uint8_t>> InMemoryBackend::Get(const std::string& hash) + { + auto it = kv_.find(hash); + if (it == kv_.end()) + return std::nullopt; + return it->second; + } + + void InMemoryBackend::LinkTuple(const std::vector<std::string>& relation_path, + const std::string& tuple_hash) + { + auto& hashes = index_[PathKey(relation_path)]; + for (const auto& h : hashes) + if (h == tuple_hash) return; + hashes.push_back(tuple_hash); + } + + std::vector<std::string> InMemoryBackend::TupleHashes( + const std::vector<std::string>& relation_path, + std::size_t offset, + std::size_t limit) + { + auto it = index_.find(PathKey(relation_path)); + if (it == index_.end() || offset >= it->second.size()) + return {}; + + const auto& all = it->second; + auto begin = all.begin() + static_cast<ptrdiff_t>(offset); + auto end = (offset + limit <= all.size()) + ? begin + static_cast<ptrdiff_t>(limit) + : all.end(); + return { begin, end }; + } +} |
