| 31 | } |
| 32 | |
| 33 | std::shared_ptr<RamFileBlockCache::Block> RamFileBlockCache::Lookup( |
| 34 | const Key& key) { |
| 35 | mutex_lock lock(mu_); |
| 36 | auto entry = block_map_.find(key); |
| 37 | if (entry != block_map_.end()) { |
| 38 | if (BlockNotStale(entry->second)) { |
| 39 | return entry->second; |
| 40 | } else { |
| 41 | // Remove the stale block and continue. |
| 42 | RemoveFile_Locked(key.first); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Insert a new empty block, setting the bookkeeping to sentinel values |
| 47 | // in order to update them as appropriate. |
| 48 | auto new_entry = std::make_shared<Block>(); |
| 49 | lru_list_.push_front(key); |
| 50 | lra_list_.push_front(key); |
| 51 | new_entry->lru_iterator = lru_list_.begin(); |
| 52 | new_entry->lra_iterator = lra_list_.begin(); |
| 53 | new_entry->timestamp = env_->NowSeconds(); |
| 54 | block_map_.emplace(std::make_pair(key, new_entry)); |
| 55 | return new_entry; |
| 56 | } |
| 57 | |
| 58 | // Remove blocks from the cache until we do not exceed our maximum size. |
| 59 | void RamFileBlockCache::Trim() { |