Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
archives.cpp
Go to the documentation of this file.
1#include <bucket/bucket.hpp>
2
3namespace Haio::Cdn::Bucket {
4
5/** the split is on the first ".zip/", so the outer archive is the one that opens */
6std::optional<ZipPath> splitZipPath(std::string_view path) {
7 constexpr std::string_view marker = ".zip/";
8
9 const auto at = path.find(marker);
10 if (at == std::string_view::npos) return std::nullopt;
11
12 const auto inside = path.substr(at + marker.size());
13 if (inside.empty()) return std::nullopt;
14
15 return ZipPath{std::string(path.substr(0, at + 4)), std::string(inside)};
16}
17
18ZipArchives::ZipArchives(size_t maxUsage) : maxUsage_(maxUsage) {}
19
20std::shared_ptr<const ZipArchives::Archive> ZipArchives::find(const std::string& key) {
21 const auto found = index_.find(key);
22 if (found == index_.end()) return nullptr;
23
24 order_.splice(order_.begin(), order_, found->second);
25 return found->second->archive;
26}
27
28std::shared_ptr<const ZipArchives::Archive> ZipArchives::keep(const std::string& key, std::vector<uint8_t> data, ZipIndex index) {
29 const auto size = data.size();
30 auto archive = std::make_shared<Archive>(Archive{std::move(data), std::move(index)});
31
32 // an archive too big to keep is still handed back, it just is not remembered
33 if (size > maxUsage_) return archive;
34
35 if (const auto found = index_.find(key); found != index_.end()) {
36 held_ -= found->second->size;
37 order_.erase(found->second);
38 index_.erase(found);
39 }
40
41 order_.push_front(Held{key, archive, size});
42 index_.emplace(key, order_.begin());
43 held_ += size;
44
45 while (held_ > maxUsage_ && !order_.empty()) {
46 const auto last = std::prev(order_.end());
47 held_ -= last->size;
48 index_.erase(last->key);
49 order_.erase(last);
50 }
51 return archive;
52}
53
54}
std::shared_ptr< const Archive > keep(const std::string &key, std::vector< uint8_t > data, ZipIndex index)
Definition archives.cpp:28
std::shared_ptr< const Archive > find(const std::string &key)
Definition archives.cpp:20
ZipArchives(size_t maxUsage)
Definition archives.cpp:18
std::optional< ZipPath > splitZipPath(std::string_view path)
Definition archives.cpp:6