|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <unordered_map> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | +#include <chrono> |
| 24 | +#include <mutex> |
| 25 | +#include <shared_mutex> |
| 26 | +#include <fstream> |
| 27 | +#include <atomic> |
| 28 | +#include <memory> |
| 29 | +#include <cstdint> |
| 30 | + |
| 31 | +#include "cripts/Context.hpp" |
| 32 | +#include "cripts/Time.hpp" |
| 33 | + |
| 34 | +// Implemented in the .cc file |
| 35 | +int _cripts_cache_group_sync(TSCont cont, TSEvent event, void *edata); |
| 36 | + |
| 37 | +namespace cripts::Cache |
| 38 | +{ |
| 39 | + |
| 40 | +class Group |
| 41 | +{ |
| 42 | +private: |
| 43 | + using self_type = Group; |
| 44 | + |
| 45 | + struct _Entry { |
| 46 | + cripts::Time::Point timestamp; // Timestamp of when the entry was created |
| 47 | + size_t length; // Length of the group ID |
| 48 | + uint32_t prefix; // First 4 characters of the group ID |
| 49 | + uint64_t hash; // Hash value of the group ID, needed when writing to disk |
| 50 | + }; |
| 51 | + |
| 52 | + using _MapType = std::unordered_map<uint64_t, _Entry>; |
| 53 | + |
| 54 | + struct _MapSlot { |
| 55 | + std::unique_ptr<_MapType> map; |
| 56 | + std::string path; |
| 57 | + cripts::Time::Point created; |
| 58 | + cripts::Time::Point last_write; |
| 59 | + cripts::Time::Point last_sync; |
| 60 | + }; |
| 61 | + |
| 62 | +public: |
| 63 | + static constexpr uint64_t VERSION = (static_cast<uint64_t>('C') << 56) | (static_cast<uint64_t>('G') << 48) | |
| 64 | + (static_cast<uint64_t>('M') << 40) | (static_cast<uint64_t>('A') << 32) | |
| 65 | + (static_cast<uint64_t>('P') << 24) | (static_cast<uint64_t>('S') << 16) | |
| 66 | + (static_cast<uint64_t>('0') << 8) | 0x00; // Change this on version bump |
| 67 | + |
| 68 | + Group(const std::string &name, const std::string &base_dir, size_t max_entries = 1024, size_t num_maps = 3) |
| 69 | + { |
| 70 | + Initialize(name, base_dir, num_maps, max_entries, std::chrono::seconds{63072000}); |
| 71 | + } |
| 72 | + |
| 73 | + // Not used at the moment. |
| 74 | + Group() = default; |
| 75 | + |
| 76 | + ~Group() { WriteToDisk(); } |
| 77 | + |
| 78 | + Group(const self_type &) = delete; |
| 79 | + self_type &operator=(const self_type &) = delete; |
| 80 | + |
| 81 | + void Initialize(const std::string &name, const std::string &base_dir, size_t num_maps = 3, size_t max_entries = 1024, |
| 82 | + std::chrono::seconds max_age = std::chrono::seconds{63072000}); |
| 83 | + |
| 84 | + void |
| 85 | + SetMaxEntries(size_t max_entries) |
| 86 | + { |
| 87 | + std::unique_lock lock(_mutex); |
| 88 | + _max_entries = max_entries; |
| 89 | + } |
| 90 | + |
| 91 | + void |
| 92 | + SetMaxAge(std::chrono::seconds max_age) |
| 93 | + { |
| 94 | + std::unique_lock lock(_mutex); |
| 95 | + _max_age = max_age; |
| 96 | + } |
| 97 | + |
| 98 | + void Insert(cripts::string_view key); |
| 99 | + void Insert(const std::vector<cripts::string_view> &keys); |
| 100 | + bool Lookup(cripts::string_view key, cripts::Time::Point age) const; |
| 101 | + bool Lookup(const std::vector<cripts::string_view> &keys, cripts::Time::Point age) const; |
| 102 | + |
| 103 | + bool |
| 104 | + Lookup(cripts::string_view key, time_t age) const |
| 105 | + { |
| 106 | + return Lookup(key, cripts::Time::Clock::from_time_t(age)); |
| 107 | + } |
| 108 | + |
| 109 | + bool |
| 110 | + Lookup(const std::vector<cripts::string_view> &keys, time_t age) const |
| 111 | + { |
| 112 | + return Lookup(keys, cripts::Time::Clock::from_time_t(age)); |
| 113 | + } |
| 114 | + |
| 115 | + cripts::Time::Point |
| 116 | + LastSync() const |
| 117 | + { |
| 118 | + std::shared_lock lock(_mutex); |
| 119 | + return _last_sync; |
| 120 | + } |
| 121 | + |
| 122 | + void WriteToDisk(); |
| 123 | + void LoadFromDisk(); |
| 124 | + |
| 125 | +private: |
| 126 | + mutable std::shared_mutex _mutex; |
| 127 | + std::string _name = "CacheGroup"; |
| 128 | + size_t _num_maps = 3; |
| 129 | + size_t _max_entries = 1024; |
| 130 | + std::chrono::seconds _max_age = std::chrono::seconds(63072000); |
| 131 | + std::atomic<size_t> _map_index = 0; |
| 132 | + cripts::Time::Point _last_sync = cripts::Time::Point{}; |
| 133 | + |
| 134 | + std::vector<_MapSlot> _slots; |
| 135 | + std::ofstream _txn_log; |
| 136 | + std::string _log_path; |
| 137 | + std::string _base_dir; |
| 138 | + |
| 139 | + void appendLog(const _Entry &entry); |
| 140 | + void clearLog(); |
| 141 | + void syncMap(size_t index); |
| 142 | + |
| 143 | +public: |
| 144 | + class Manager |
| 145 | + { |
| 146 | + friend int ::_cripts_cache_group_sync(TSCont cont, TSEvent event, void *edata); |
| 147 | + using self_type = Manager; |
| 148 | + |
| 149 | + public: |
| 150 | + static void *Factory(const std::string &name, size_t max_entries = 1024, size_t num_maps = 3); |
| 151 | + |
| 152 | + Manager(const self_type &) = delete; |
| 153 | + self_type &operator=(const self_type &) = delete; |
| 154 | + |
| 155 | + protected: |
| 156 | + void _scheduleCont(); |
| 157 | + |
| 158 | + std::unordered_map<std::string, std::weak_ptr<Group>> _groups; |
| 159 | + std::mutex _mutex; |
| 160 | + |
| 161 | + private: |
| 162 | + Manager() |
| 163 | + { |
| 164 | + _base_dir = TSRuntimeDirGet(); |
| 165 | + |
| 166 | + if (std::filesystem::exists(_base_dir)) { |
| 167 | + _base_dir += "/cache_groups"; |
| 168 | + if (!std::filesystem::exists(_base_dir)) { |
| 169 | + std::filesystem::create_directories(_base_dir); |
| 170 | + std::filesystem::permissions(_base_dir, std::filesystem::perms::group_write, std::filesystem::perm_options::add); |
| 171 | + } |
| 172 | + } |
| 173 | + _scheduleCont(); // Kick it off |
| 174 | + } |
| 175 | + |
| 176 | + ~Manager() |
| 177 | + { |
| 178 | + if (_action) { |
| 179 | + TSActionCancel(_action); |
| 180 | + _action = nullptr; |
| 181 | + } |
| 182 | + if (_cont) { |
| 183 | + TSContDestroy(_cont); |
| 184 | + _cont = nullptr; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + static self_type &_instance(); |
| 189 | + |
| 190 | + TSCont _cont = nullptr; |
| 191 | + TSAction _action = nullptr; |
| 192 | + std::string _base_dir; |
| 193 | + }; |
| 194 | +}; |
| 195 | +} // namespace cripts::Cache |
0 commit comments