| Index: net/disk_cache/v3/index_table.cc
|
| ===================================================================
|
| --- net/disk_cache/v3/index_table.cc (revision 0)
|
| +++ net/disk_cache/v3/index_table.cc (revision 0)
|
| @@ -0,0 +1,717 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/disk_cache/v3/index_table.h"
|
| +
|
| +#include <set>
|
| +
|
| +#include "net/base/net_errors.h"
|
| +#include "net/disk_cache/disk_cache.h"
|
| +#include "net/disk_cache/v3/backend_impl_v3.h"
|
| +#include "net/disk_cache/v3/backend_work_item.h"
|
| +
|
| +using base::Time;
|
| +using base::TimeDelta;
|
| +using disk_cache::CellId;
|
| +using disk_cache::CellList;
|
| +using disk_cache::IndexCell;
|
| +using disk_cache::IndexIterator;
|
| +
|
| +namespace {
|
| +
|
| +const uint32 kMaxAddress = 1 << 22;
|
| +const uint32 kBasicMask = 0xFFFF;
|
| +
|
| +int GetSum(const IndexCell& cell) {
|
| + return cell.timestamp_and_sum >> 6;
|
| +}
|
| +
|
| +// This is a quite peculiar way to calculate the sum, so it will not match if
|
| +// compared a gainst a pure 2 bit, modulo 2 sum.
|
| +int CalculateSum(const IndexCell& cell) {
|
| + uint32* word = bit_cast<uint32*>(&cell);
|
| + uint32 result = word[0] + word[1];
|
| + result += result >> 16;
|
| + result += (result >> 8) + (cell.timestamp_and_hash & 0x3f);
|
| + result += result >> 4;
|
| + result += result >> 2;
|
| + return result & 3;
|
| +}
|
| +
|
| +bool SanityCheck(const IndexCell& cell) {
|
| + if (GetSum(cell) != CalculateSum(cell))
|
| + return false;
|
| +
|
| + if (cell.state > disk_cache::ENTRY_USED ||
|
| + cell.group == disk_cache::ENTRY_RESERVED ||
|
| + cell.group > disk_cache::ENTRY_EVICTED) {
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +inline bool HashMatches(uint32 stored, uint32 value, int extra_bits) {
|
| + return (stored >> extra_bits) == (value >> (14 + extra_bits));
|
| +}
|
| +
|
| +inline bool MisplacedHash(uint32 stored, uint32 value, int extra_bits) {
|
| + if (!extra_bits)
|
| + return false;
|
| + uint32 mask = (1 << extra_bits) - 1;
|
| + return (stored & mask) != ((value >> 14) & mask);
|
| +}
|
| +
|
| +inline int GetNextBucket(int min_bucket_id, int max_bucket_id,
|
| + disk_cache::IndexBucket* table,
|
| + disk_cache::IndexBucket** bucket) {
|
| + if (!(*bucket)->next)
|
| + return 0;
|
| +
|
| + int bucket_id = (*bucket)->next / 4;
|
| + if (bucket_id < min_bucket_id || bucket_id > max_bucket_id) {
|
| + (*bucket)->next = 0;
|
| + return 0;
|
| + }
|
| + *bucket = &table[bucket_id - min_bucket_id];
|
| + return bucket_id;
|
| +}
|
| +
|
| +void UpdateListWithCell(int bucket_hash, const IndexCell& cell, CellList* list,
|
| + int* list_time) {
|
| + if (!list)
|
| + return;
|
| +
|
| + int time = disk_cache::EntryCell::GetTimestamp(cell);
|
| + if (time < *list_time) {
|
| + *list_time = time;
|
| + list->clear();
|
| + }
|
| + if (time == *list_time) {
|
| + CellId cell_id = {
|
| + (disk_cache::EntryCell::GetHash(cell) << 14) + bucket_hash,
|
| + disk_cache::Addr::FromEntryAddress(cell.address)
|
| + };
|
| + list->push_back(cell_id);
|
| + }
|
| +}
|
| +
|
| +void GetNewestFromBucket(disk_cache::IndexBucket* bucket, int bucket_hash,
|
| + int limit_time, IndexIterator* iterator) {
|
| + for (int i = 0; i < 4; i++) {
|
| + IndexCell& current_cell = bucket->cells[i];
|
| + if (!current_cell.address)
|
| + continue;
|
| + DCHECK(SanityCheck(current_cell));
|
| + if (!disk_cache::EntryCell::IsNormalState(current_cell))
|
| + continue;
|
| +
|
| + int time = disk_cache::EntryCell::GetTimestamp(current_cell);
|
| + switch (current_cell.group) {
|
| + case disk_cache::ENTRY_NO_USE:
|
| + case disk_cache::ENTRY_LOW_USE:
|
| + case disk_cache::ENTRY_HIGH_USE:
|
| + if (iterator->forward && time >= limit_time)
|
| + continue;
|
| + if (!iterator->forward && time <= limit_time)
|
| + continue;
|
| +
|
| + if ((iterator->forward && time > iterator->timestamp) ||
|
| + (!iterator->forward && time < iterator->timestamp)) {
|
| + iterator->timestamp = time;
|
| + iterator->cells.clear();
|
| + }
|
| + if (time == iterator->timestamp) {
|
| + CellId cell_id = {
|
| + (disk_cache::EntryCell::GetHash(current_cell) << 14) + bucket_hash,
|
| + disk_cache::Addr::FromEntryAddress(current_cell.address)
|
| + };
|
| + iterator->cells.push_back(cell_id);
|
| + }
|
| + }
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +namespace disk_cache {
|
| +
|
| +EntryCell::EntryCell() : cell_id_(0), hash_(0) {
|
| + cell_.Clear();
|
| +}
|
| +
|
| +EntryCell::EntryCell(int32 cell_id, uint32 hash, Addr address)
|
| + : cell_id_(cell_id),
|
| + hash_(hash) {
|
| + DCHECK(ValidAddress(address) || !address.value());
|
| +
|
| + cell_.Clear();
|
| + cell_.address = address.value() & 0xFFFFFF;
|
| + cell_.state = ENTRY_NEW;
|
| + cell_.group = ENTRY_NO_USE;
|
| + cell_.hash = (hash >> 16) & kBasicMask;
|
| + cell_.timestamp_and_hash = (hash >> 14) & 3;
|
| +}
|
| +
|
| +EntryCell::EntryCell(int32 cell_id, uint32 hash, const IndexCell& cell)
|
| + : cell_id_(cell_id),
|
| + hash_(hash),
|
| + cell_(cell) {
|
| +}
|
| +
|
| +EntryCell::~EntryCell() {
|
| +}
|
| +
|
| +Addr EntryCell::GetAddress() const {
|
| + if (group() == ENTRY_EVICTED)
|
| + return Addr::FromEvictedAddress(cell_.address);
|
| + else
|
| + return Addr::FromEntryAddress(cell_.address);
|
| +}
|
| +
|
| +int EntryCell::GetTimestamp() const {
|
| + return GetTimestamp(cell_);
|
| +}
|
| +
|
| +void EntryCell::set_reuse(int count) {
|
| + DCHECK_LT(count, 16);
|
| + DCHECK_GE(count, 0);
|
| + cell_.reuse = count;
|
| +}
|
| +
|
| +// This operation destroys (ignores) the sum.
|
| +void EntryCell::set_timestamp(int timestamp) {
|
| + DCHECK_LT(timestamp, 1 << 20);
|
| + DCHECK_GE(timestamp, 0);
|
| + cell_.timestamp_and_hash = ((timestamp & 0x3fff) << 2) +
|
| + (cell_.timestamp_and_hash & 3);
|
| + cell_.timestamp_and_sum = (timestamp >> 14) & 0x3f;
|
| +}
|
| +
|
| +// Static.
|
| +bool EntryCell::ValidAddress(Addr address) {
|
| + if (!address.is_initialized() ||
|
| + (address.file_type() != BLOCK_EVICTED &&
|
| + address.file_type() != BLOCK_ENTRIES)) {
|
| + return false;
|
| + }
|
| +
|
| + return (address.value() & 0xF00000) < kMaxAddress;
|
| +}
|
| +
|
| +// Static.
|
| +uint32 EntryCell::GetHash(const IndexCell& cell) {
|
| + return (cell.hash << 2) + (cell.timestamp_and_hash & 3);
|
| +}
|
| +
|
| +// Static.
|
| +int EntryCell::GetTimestamp(const IndexCell& cell) {
|
| + return (cell.timestamp_and_hash >> 2) +
|
| + ((cell.timestamp_and_sum & 0x3f) << 14);
|
| +}
|
| +
|
| +// Static.
|
| +bool EntryCell::IsNormalState(const IndexCell& cell) {
|
| + EntryState state = static_cast<EntryState>(cell.state);
|
| + DCHECK_NE(state, ENTRY_FREE);
|
| + return state != ENTRY_DELETED && state != ENTRY_FIXING;
|
| +}
|
| +
|
| +void EntryCell::FixSum() {
|
| + cell_.timestamp_and_sum = (cell_.timestamp_and_sum & 0x3f) |
|
| + (CalculateSum(cell_) << 6);
|
| +}
|
| +
|
| +EntrySet::EntrySet() : evicted_count(0), current(0) {
|
| +}
|
| +
|
| +// -----------------------------------------------------------------------
|
| +
|
| +IndexTable::IndexTable(BackendImplV3* backend)
|
| + : backend_(backend),
|
| + header_(NULL),
|
| + main_table_(NULL),
|
| + extra_table_(NULL) {
|
| +}
|
| +
|
| +void IndexTable::Init(InitResult* params) {
|
| + bool growing = header_ != NULL;
|
| + IndexBucket* old_extra_table = NULL;
|
| + header_ = ¶ms->index_bitmap->header;
|
| +
|
| + if (params->main_table) {
|
| + if (main_table_) {
|
| + DCHECK_EQ(header_->table_len / kIndexTablesize,
|
| + backup_header_->table_len / kIndexTablesize + 1);
|
| + old_extra_table = extra_table_;
|
| + }
|
| + main_table_ = params->main_table;
|
| + }
|
| + DCHECK(main_table_);
|
| + extra_table_ = params->extra_table;
|
| +
|
| + extra_bits_ = header_->table_len / kIndexTablesize - 1;
|
| + DCHECK_GE(extra_bits_, 0);
|
| + DCHECK_LE(extra_bits_, 6);
|
| + mask_ = ((kIndexTablesize / 4) << extra_bits_) - 1;
|
| +
|
| + int num_words = (header_->table_len + 31) / 32 ;
|
| + bitmap_.reset(new Bitmap(params->index_bitmap->bitmap, header_->table_len,
|
| + num_words));
|
| +
|
| + if (growing) {
|
| + int old_num_words = (backup_header_->table_len + 31) / 32;
|
| + DCHECK_GE(num_words, old_num_words);
|
| + scoped_ptr<uint32[]> storage(new uint32[num_words]);
|
| + memcpy(storage.get(), backup_bitmap_storage_.get(), old_num_words * 4);
|
| + memset(storage.get() + old_num_words, 0, (num_words - old_num_words) * 4);
|
| +
|
| + backup_bitmap_storage_.swap(storage);
|
| + backup_header_->table_len = header_->table_len;
|
| + } else {
|
| + backup_bitmap_storage_.reset(params->backup_bitmap.release());
|
| + backup_header_.reset(params->backup_header.release());
|
| + }
|
| +
|
| + num_words = (backup_header_->table_len + 31) / 32;
|
| + backup_bitmap_.reset(new Bitmap(backup_bitmap_storage_.get(),
|
| + backup_header_->table_len, num_words));
|
| + if (old_extra_table)
|
| + MoveCells(old_extra_table);
|
| +}
|
| +
|
| +void IndexTable::Reset() {
|
| + header_ = NULL;
|
| + main_table_ = NULL;
|
| + extra_table_ = NULL;
|
| + bitmap_.reset();
|
| + backup_bitmap_.reset();
|
| + backup_header_.reset();
|
| + backup_bitmap_storage_.reset();
|
| +}
|
| +
|
| +EntrySet IndexTable::LookupEntry(uint32 hash) {
|
| + EntrySet entries;
|
| + int bucket_id = static_cast<int>(hash & mask_);
|
| + IndexBucket* bucket = &main_table_[bucket_id];
|
| + for (;;) {
|
| + for (int i = 0; i < 4; i++) {
|
| + IndexCell* current_cell = &bucket->cells[i];
|
| + if (!current_cell->address)
|
| + continue;
|
| + if (!SanityCheck(*current_cell)) {
|
| + NOTREACHED();
|
| + current_cell->Clear();
|
| + continue;
|
| + }
|
| + int cell_id = bucket_id * 4 + i;
|
| + if (MisplacedHash(EntryCell::GetHash(*current_cell), hash, extra_bits_)) {
|
| + HandleMisplacedCell(current_cell, cell_id, hash & mask_);
|
| + } else if (HashMatches(EntryCell::GetHash(*current_cell), hash,
|
| + extra_bits_)) {
|
| + CheckState(current_cell, cell_id);
|
| + if (current_cell->state != ENTRY_DELETED) {
|
| + EntryCell entry_cell(cell_id, hash, *current_cell);
|
| + entries.cells.push_back(entry_cell);
|
| + if (current_cell->group == ENTRY_EVICTED)
|
| + entries.evicted_count++;
|
| + }
|
| + }
|
| + }
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| + return entries;
|
| +}
|
| +
|
| +EntryCell IndexTable::CreateEntryCell(uint32 hash, Addr address) {
|
| + DCHECK(EntryCell::ValidAddress(address));
|
| +
|
| + int bucket_id = static_cast<int>(hash & mask_);
|
| + int cell_id = 0;
|
| + IndexBucket* bucket = &main_table_[bucket_id];
|
| + IndexCell* current_cell = NULL;
|
| + bool found = false;
|
| + for (; !found;) {
|
| + for (int i = 0; i < 4 && !found; i++) {
|
| + current_cell = &bucket->cells[i];
|
| + if (!current_cell->address) {
|
| + cell_id = bucket_id * 4 + i;
|
| + found = true;
|
| + }
|
| + }
|
| + if (found)
|
| + break;
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| +
|
| + if (!found) {
|
| + bucket_id = NewExtraBucket();
|
| + if (bucket_id) {
|
| + cell_id = bucket_id * 4;
|
| + bucket->next = cell_id;
|
| + found = true;
|
| + } else {
|
| + // address 0 is a reserved value, and the caller interprets it as invalid.
|
| + address.set_value(0);
|
| + }
|
| + }
|
| +
|
| + EntryCell entry_cell(cell_id, hash, address);
|
| + if (address.file_type() == BLOCK_EVICTED)
|
| + entry_cell.set_group(ENTRY_EVICTED);
|
| + else
|
| + entry_cell.set_group(ENTRY_NO_USE);
|
| + Save(&entry_cell);
|
| +
|
| + if (found) {
|
| + bitmap_->Set(cell_id, true);
|
| + backup_bitmap_->Set(cell_id, true);
|
| + header()->used_cells++;
|
| + }
|
| +
|
| + return entry_cell;
|
| +}
|
| +
|
| +bool IndexTable::SetSate(uint32 hash, Addr address, EntryState state) {
|
| + EntryCell cell = FindEntryCellImpl(hash, address, state == ENTRY_FREE);
|
| + if (!cell.IsValid())
|
| + return false;
|
| +
|
| + if (state == ENTRY_DELETED) {
|
| + bitmap_->Set(cell.cell_id(), false);
|
| + backup_bitmap_->Set(cell.cell_id(), false);
|
| + } else if (state == ENTRY_FREE) {
|
| + DCHECK_EQ(ENTRY_DELETED, cell.state());
|
| + cell.Clear();
|
| + Write(cell);
|
| + header()->used_cells--;
|
| + return true;
|
| + }
|
| + cell.set_state(state);
|
| +
|
| + Save(&cell);
|
| + return true;
|
| +}
|
| +
|
| +int IndexTable::GetTimestamp(Time time) {
|
| + TimeDelta delta = time - Time::FromInternalValue(header_->base_time);
|
| + return std::max(delta.InMinutes(), 0);
|
| +}
|
| +
|
| +void IndexTable::UpdateTime(uint32 hash, Addr address) {
|
| + EntryCell cell = FindEntryCell(hash, address);
|
| + if (!cell.IsValid())
|
| + return;
|
| +
|
| + int minutes = GetTimestamp(backend_->GetCurrentTime());
|
| +
|
| + // Keep about 3 months of headroom.
|
| + const int kMaxTimestamp = (1 << 20) - 60 * 24 * 90;
|
| + if (minutes > kMaxTimestamp) {
|
| + // TODO(rvargas):
|
| + // Update header->old_time and trigger a timer
|
| + // Rebaseline timestamps and don't update sums
|
| + // Start a timer (about 2 backups)
|
| + // fix all ckecksums and trigger another timer
|
| + // update header->old_time because rebaseline is done.
|
| + minutes = std::min(minutes, (1 << 20) - 1);
|
| + }
|
| +
|
| + cell.set_timestamp(minutes);
|
| + Save(&cell);
|
| +}
|
| +
|
| +EntryCell IndexTable::FindEntryCell(uint32 hash, Addr address) {
|
| + return FindEntryCellImpl(hash, address, false);
|
| +}
|
| +
|
| +void IndexTable::Save(EntryCell* cell) {
|
| + cell->FixSum();
|
| + Write(*cell);
|
| +}
|
| +
|
| +void IndexTable::GetOldest(CellList* no_use, CellList* low_use,
|
| + CellList* high_use) {
|
| + header_->num_no_use_entries = 0;
|
| + header_->num_low_use_entries = 0;
|
| + header_->num_high_use_entries = 0;
|
| + header_->num_evicted_entries = 0;
|
| +
|
| + int no_use_time = kint32max;
|
| + int low_use_time = kint32max;
|
| + int high_use_time = kint32max;
|
| + for (int i = 0; i < static_cast<int32>(mask_ + 1); i++) {
|
| + int bucket_id = i;
|
| + IndexBucket* bucket = &main_table_[i];
|
| + for (;;) {
|
| + GetOldestFromBucket(bucket, i, no_use, &no_use_time, low_use,
|
| + &low_use_time, high_use, &high_use_time);
|
| +
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| + }
|
| + header_->num_entries = header_->num_no_use_entries +
|
| + header_->num_low_use_entries +
|
| + header_->num_high_use_entries +
|
| + header_->num_evicted_entries;
|
| +}
|
| +
|
| +bool IndexTable::GetNextCells(IndexIterator* iterator) {
|
| + int current_time = iterator->timestamp;
|
| + size_t initial_size = iterator->cells.size();
|
| + iterator->timestamp = iterator->forward ? 0 : kint32max;
|
| +
|
| + for (int i = 0; i < static_cast<int32>(mask_ + 1); i++) {
|
| + int bucket_id = i;
|
| + IndexBucket* bucket = &main_table_[i];
|
| + for (;;) {
|
| + GetNewestFromBucket(bucket, i, current_time, iterator);
|
| +
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| + }
|
| + return initial_size != iterator->cells.size();
|
| +}
|
| +
|
| +// -----------------------------------------------------------------------
|
| +
|
| +EntryCell IndexTable::FindEntryCellImpl(uint32 hash, Addr address,
|
| + bool allow_deleted) {
|
| + int bucket_id = static_cast<int>(hash & mask_);
|
| + IndexBucket* bucket = &main_table_[bucket_id];
|
| + for (;;) {
|
| + for (int i = 0; i < 4; i++) {
|
| + IndexCell* current_cell = &bucket->cells[i];
|
| + if (!current_cell->address)
|
| + continue;
|
| + DCHECK(SanityCheck(*current_cell));
|
| + if (EntryCell::GetHash(*current_cell) == (hash >> 14)) {
|
| + // We have a match.
|
| + int cell_id = bucket_id * 4 + i;
|
| + EntryCell entry_cell(cell_id, hash, *current_cell);
|
| + if (entry_cell.GetAddress() != address)
|
| + continue;
|
| +
|
| + if (!allow_deleted && entry_cell.state() == ENTRY_DELETED)
|
| + continue;
|
| +
|
| + return entry_cell;
|
| + }
|
| + }
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| + return EntryCell();
|
| +}
|
| +
|
| +void IndexTable::CheckState(IndexCell* cell, int32 cell_id) {
|
| + if (cell->state != ENTRY_FIXING) {
|
| + bool present = ((cell->state & 3) != 0); // Look at the last two bits.
|
| + if (present != bitmap_->Get(cell_id) ||
|
| + present != backup_bitmap_->Get(cell_id)) {
|
| + // There's a mismatch.
|
| + if (cell->state == ENTRY_DELETED) {
|
| + // We were in the process of deleting this entry. Finish now.
|
| + backend_->DeleteCell(cell, cell_id);
|
| + } else {
|
| + cell->state = ENTRY_FIXING;
|
| + }
|
| + }
|
| + }
|
| +
|
| + if (cell->state == ENTRY_FIXING)
|
| + backend_->FixCell(cell, cell_id);
|
| +}
|
| +
|
| +void IndexTable::Write(const EntryCell& cell) {
|
| + if (!cell.IsValid())
|
| + return;
|
| +
|
| + IndexBucket* bucket = NULL;
|
| + int bucket_id = cell.cell_id() / 4;
|
| + if (bucket_id < static_cast<int32>(mask_ + 1)) {
|
| + bucket = &main_table_[bucket_id];
|
| + } else {
|
| + DCHECK_LE(bucket_id, header()->max_bucket);
|
| + bucket = &extra_table_[bucket_id - (mask_ + 1)];
|
| + }
|
| + memcpy(&bucket->cells[cell.cell_id() % 4], &cell.cell(), sizeof(IndexCell));
|
| +}
|
| +
|
| +int IndexTable::NewExtraBucket() {
|
| + if (header()->table_len - header()->max_bucket * 4 < kNumExtraBlocks)
|
| + backend_->GrowIndex();
|
| +
|
| + if (header()->max_bucket * 4 == header()->table_len - 4)
|
| + return 0;
|
| +
|
| + header()->max_bucket++;
|
| + return header()->max_bucket;
|
| +}
|
| +
|
| +void IndexTable::GetOldestFromBucket(IndexBucket* bucket, int bucket_hash,
|
| + CellList* no_use, int* no_use_time,
|
| + CellList* low_use, int* low_use_time,
|
| + CellList* high_use, int* high_use_time) {
|
| + for (int i = 0; i < 4; i++) {
|
| + IndexCell& current_cell = bucket->cells[i];
|
| + if (!current_cell.address)
|
| + continue;
|
| + DCHECK(SanityCheck(current_cell));
|
| + if (!EntryCell::IsNormalState(current_cell))
|
| + continue;
|
| +
|
| + int time = EntryCell::GetTimestamp(current_cell);
|
| + switch (current_cell.group) {
|
| + case ENTRY_NO_USE:
|
| + UpdateListWithCell(bucket_hash, current_cell, no_use, no_use_time);
|
| + header_->num_no_use_entries++;
|
| + break;
|
| + case ENTRY_LOW_USE:
|
| + UpdateListWithCell(bucket_hash, current_cell, low_use, low_use_time);
|
| + header_->num_low_use_entries++;
|
| + break;
|
| + case ENTRY_HIGH_USE:
|
| + UpdateListWithCell(bucket_hash, current_cell, high_use, high_use_time);
|
| + header_->num_high_use_entries++;
|
| + break;
|
| + case ENTRY_EVICTED:
|
| + header_->num_evicted_entries++;
|
| + break;
|
| + default:
|
| + NOTREACHED();
|
| + }
|
| + }
|
| +}
|
| +
|
| +void IndexTable::MoveCells(IndexBucket* old_extra_table) {
|
| + int max_hash = (mask_ + 1) / 2;
|
| + int max_bucket = header()->max_bucket;
|
| + header()->max_bucket = mask_;
|
| +
|
| + // A cell stores the upper 18 bits of the hash (h >> 14). If the table is say
|
| + // 8 times the original size (growing from 4x), the bit that we are interested
|
| + // in would be the 3rd bit of the stored value, in other words multiplir >> 1.
|
| + uint32 new_bit = ((mask_ + 1) * 4 / kIndexTablesize) >> 1;
|
| +
|
| + for (int i = 0; i < max_hash; i++) {
|
| + int bucket_id = i;
|
| + IndexBucket* bucket = &main_table_[i];
|
| + for (;;) {
|
| + for (int j = 0; j < 4; j++) {
|
| + IndexCell& current_cell = bucket->cells[j];
|
| + if (!current_cell.address)
|
| + continue;
|
| + DCHECK(SanityCheck(current_cell));
|
| + if (bucket_id == i) {
|
| + if (EntryCell::GetHash(current_cell) & new_bit) {
|
| + // Move this cell to the upper half of the table.
|
| + MoveSingleCell(¤t_cell, bucket_id * 4 + j, i, true);
|
| + }
|
| + } else {
|
| + // All cells on extra buckets have to move.
|
| + MoveSingleCell(¤t_cell, bucket_id * 4 + j, i, true);
|
| + }
|
| + }
|
| +
|
| + bucket_id = GetNextBucket(max_hash, max_bucket, old_extra_table, &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| + }
|
| +}
|
| +
|
| +void IndexTable::MoveSingleCell(IndexCell* current_cell, int cell_id,
|
| + int main_table_index, bool growing) {
|
| + uint32 hash = (EntryCell::GetHash(*current_cell) << 14) | main_table_index;
|
| + EntryCell old_cell(cell_id, hash, *current_cell);
|
| +
|
| + EntryCell new_cell = CreateEntryCell(hash, old_cell.GetAddress());
|
| + if (!new_cell.IsValid())
|
| + return; // We'll deal with this entry later.
|
| +
|
| + new_cell.set_state(old_cell.state());
|
| + new_cell.set_group(old_cell.group());
|
| + new_cell.set_reuse(old_cell.reuse());
|
| + new_cell.set_timestamp(old_cell.GetTimestamp());
|
| + Save(&new_cell);
|
| +
|
| + if (old_cell.state() == ENTRY_DELETED) {
|
| + bitmap_->Set(new_cell.cell_id(), false);
|
| + backup_bitmap_->Set(new_cell.cell_id(), false);
|
| + }
|
| +
|
| + if (!growing || cell_id / 4 == main_table_index) {
|
| + // Only delete entries that live on the main table.
|
| + old_cell.Clear();
|
| + Write(old_cell);
|
| + }
|
| +}
|
| +
|
| +void IndexTable::HandleMisplacedCell(IndexCell* current_cell, int cell_id,
|
| + int main_table_index) {
|
| + // The cell may be misplaced, or a duplicate cell exists with this data.
|
| + uint32 hash = (EntryCell::GetHash(*current_cell) << 14) | main_table_index;
|
| + MoveSingleCell(current_cell, cell_id, main_table_index, false);
|
| +
|
| + // Now look for a duplicate cell.
|
| + CheckBucketList(hash & mask_);
|
| +}
|
| +
|
| +void IndexTable::CheckBucketList(int bucket_id) {
|
| + typedef std::pair<int, EntryGroup> AddressAndGroup;
|
| + std::set<AddressAndGroup> entries;
|
| + IndexBucket* bucket = &main_table_[bucket_id];
|
| + for (;;) {
|
| + for (int i = 0; i < 4; i++) {
|
| + IndexCell* current_cell = &bucket->cells[i];
|
| + if (!current_cell->address)
|
| + continue;
|
| + if (!SanityCheck(*current_cell)) {
|
| + NOTREACHED();
|
| + current_cell->Clear();
|
| + continue;
|
| + }
|
| + int cell_id = bucket_id * 4 + i;
|
| + EntryCell cell(cell_id, 0, *current_cell);
|
| + if (!entries.insert(std::make_pair(cell.GetAddress().value(),
|
| + cell.group())).second) {
|
| + current_cell->Clear();
|
| + continue;
|
| + }
|
| + CheckState(current_cell, cell_id);
|
| + }
|
| +
|
| + bucket_id = GetNextBucket(mask_ + 1, header()->max_bucket, extra_table_,
|
| + &bucket);
|
| + if (!bucket_id)
|
| + break;
|
| + }
|
| +}
|
| +
|
| +// Things we can be doing:
|
| +//
|
| +// - Updating timestamps
|
| +// Fast, but we go through a few backup cycles to make sure it sticks
|
| +// - Growing the extra table -> increase bitmap size, remap
|
| +// Just a matter of tripping to the cache thread. Everything keeps moving
|
| +// - Growing the whole table -> relocating all entries, may take a while
|
| +// - Evictions... only when we are NOT doing something else? just find the
|
| +// entries again so that there's no invalidation of lists
|
| +
|
| +} // namespace disk_cache
|
|
|
| Property changes on: net\disk_cache\v3\index_table.cc
|
| ___________________________________________________________________
|
| Added: svn:eol-style
|
| + LF
|
|
|
|
|