/
smychkov
/
SStorage
Обзор
Документация
Войти
/
smychkov
/
SStorage
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/cache/block_cache.cpp
100 строк
3 KB
Андрей Смычков
feat: LSM tree with levels, flush, compaction, recovery, block cache
25 апр 2026, 09:14
25 апр 2026, 09:14
4631f83
Код
Авторство
О чём код?
#include "block_cache.hpp" namespace sstorage { BlockCache::BlockCache(size_t capacityBytes) : capacity_(capacityBytes) { } //============================================================================ // get — при попадании переносит запись во front (LRU update) //============================================================================ std::shared_ptr<const std::string> BlockCache::get(const std::string& fileId, uint64_t offset) const { std::lock_guard<std::mutex> lk(mutex_); // mutable-эмуляция через const_cast: нам нужно обновить LRU-позицию auto& self = const_cast<BlockCache&>(*this); Key key{fileId, offset}; auto it = self.map_.find(key); if (it == self.map_.end()) return nullptr; // Переносим в начало списка (свежее) self.lru_.splice(self.lru_.begin(), self.lru_, it->second); return it->second->data; } //============================================================================ // put //============================================================================ void BlockCache::put(const std::string& fileId, uint64_t offset, std::shared_ptr<const std::string> data) { if (!data) return; std::lock_guard<std::mutex> lk(mutex_); Key key{fileId, offset}; auto it = map_.find(key); if (it != map_.end()) { // Обновление существующего: вычитаем старый размер currentBytes_ -= it->second->size; it->second->data = data; it->second->size = data->size(); currentBytes_ += it->second->size; lru_.splice(lru_.begin(), lru_, it->second); } else { // Новая запись Entry e{std::move(key), std::move(data), 0}; e.size = e.data->size(); lru_.push_front(std::move(e)); map_[lru_.front().key] = lru_.begin(); currentBytes_ += lru_.front().size; } evictLocked(); } //============================================================================ // evictLocked — выкидываем хвост пока не уложимся в capacity //============================================================================ void BlockCache::evictLocked() { while (currentBytes_ > capacity_ && !lru_.empty()) { auto& back = lru_.back(); currentBytes_ -= back.size; map_.erase(back.key); lru_.pop_back(); } } //============================================================================ // invalidateFile — при удалении SSTable-файла //============================================================================ void BlockCache::invalidateFile(const std::string& fileId) { std::lock_guard<std::mutex> lk(mutex_); for (auto it = lru_.begin(); it != lru_.end(); ) { if (it->key.fileId == fileId) { currentBytes_ -= it->size; map_.erase(it->key); it = lru_.erase(it); } else { ++it; } } } void BlockCache::clear() { std::lock_guard<std::mutex> lk(mutex_); lru_.clear(); map_.clear(); currentBytes_ = 0; } size_t BlockCache::currentBytes() const { std::lock_guard<std::mutex> lk(mutex_); return currentBytes_; } size_t BlockCache::count() const { std::lock_guard<std::mutex> lk(mutex_); return lru_.size(); } }