| 278 | } |
| 279 | |
| 280 | Status PersistentCacheImpl::MakeRoomForWrite(int64_t size) { |
| 281 | std::lock_guard<std::mutex> _(lock_); |
| 282 | assert(size_.Get() <= opt_.cache_size); |
| 283 | |
| 284 | if (size + size_.Get() <= opt_.cache_size) { |
| 285 | // there is enough space to write |
| 286 | size_.Add(size); |
| 287 | return Status::OK(); |
| 288 | } |
| 289 | |
| 290 | // there is not enough space to fit the requested data |
| 291 | // we can clear some space by evicting cold data |
| 292 | while (size + size_.Get() > opt_.cache_size) { |
| 293 | std::unique_ptr<CacheFile> f(metadata_.Evict()); |
| 294 | if (!f) { |
| 295 | // nothing is evictable |
| 296 | return Status::IOError("No space for writing persistent cache."); |
| 297 | } |
| 298 | assert(!f->refs_); |
| 299 | if (DeleteFileAndReleaseCache(f.get()).ok()) { |
| 300 | LEVELDB_LOG("Remove evicted cache file: %lu.rc.", f->cacheid()); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | size_.Add(size); |
| 305 | assert(size_.Get() <= opt_.cache_size); |
| 306 | return Status::OK(); |
| 307 | } |
| 308 | |
| 309 | bool PersistentCacheImpl::Insert(const Slice& key, CacheFile* file) { |
| 310 | std::lock_guard<std::mutex> _(lock_); |