If the entry exists, the Handle pins it so it doesn't go away, but the entry may be in any state (IN PROGRESS, TOMBSTONE, COMPLETE_UNSYNCED, COMPLETE). If the entry doesn't exist and acquire_write is true, it's created with the state IN_PROGRESS.
| 360 | // any state (IN PROGRESS, TOMBSTONE, COMPLETE_UNSYNCED, COMPLETE). If the entry doesn't |
| 361 | // exist and acquire_write is true, it's created with the state IN_PROGRESS. |
| 362 | TupleCacheMgr::UniqueHandle TupleCacheMgr::Lookup( |
| 363 | const Slice& key, bool acquire_write) { |
| 364 | if (!enabled_) return nullptr; |
| 365 | |
| 366 | UniqueHandle handle{new Handle()}; |
| 367 | if (Cache::UniqueHandle pos = cache_->Lookup(key); pos) { |
| 368 | handle->cache_handle = move(pos); |
| 369 | } else if (acquire_write) { |
| 370 | lock_guard<mutex> guard(creation_lock_); |
| 371 | // Retry lookup under the creation lock in case another thread added an entry. |
| 372 | if (Cache::UniqueHandle pos = cache_->Lookup(key); pos) { |
| 373 | handle->cache_handle = move(pos); |
| 374 | } else { |
| 375 | // Entry not found, create a new one. |
| 376 | filesystem::path path = |
| 377 | filesystem::path(cache_dir_) / (CACHE_FILE_PREFIX + key.ToString()); |
| 378 | Cache::UniquePendingHandle pending = CreateEntry(cache_.get(), key, path.string()); |
| 379 | if (UNLIKELY(!pending || debug_pos_ & DebugPos::FAIL_ALLOCATE)) { |
| 380 | VLOG_FILE << "Tuple Cache: CreateEntry failed for " << path; |
| 381 | return handle; |
| 382 | } |
| 383 | |
| 384 | // Insert into the cache. If immediately evicted, evict_callback_ handles cleanup |
| 385 | // and decrements the counter. |
| 386 | tuple_cache_entries_in_use_->Increment(1); |
| 387 | Cache::UniqueHandle chandle = cache_->Insert(move(pending), this); |
| 388 | if (UNLIKELY(!chandle || debug_pos_ & DebugPos::FAIL_INSERT)) { |
| 389 | // This shouldn't happen normally because the cache is recency-based and initial |
| 390 | // handle is small. |
| 391 | LOG(WARNING) << "Tuple Cache Entry was immediately evicted"; |
| 392 | return handle; |
| 393 | } |
| 394 | VLOG_FILE << "Tuple Cache Entry created for " << path; |
| 395 | handle->cache_handle = move(chandle); |
| 396 | handle->is_writer = true; |
| 397 | handle->base_charge = sizeof(TupleCacheEntry) + path.size() + 1; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return handle; |
| 402 | } |
| 403 | |
| 404 | bool TupleCacheMgr::IsAvailableForRead(UniqueHandle& handle) const { |
| 405 | if (!handle || !handle->cache_handle) return false; |
no test coverage detected