Bug 1182961 (part 1, attempt 2) - Use nsTHashtable::Iterator in CacheIndex. r=michal.

This commit is contained in:
Nicholas Nethercote 2015-07-28 21:14:39 -07:00
Родитель 96b97bdbd8
Коммит bf4d4d1817
2 изменённых файлов: 61 добавлений и 84 удалений

Просмотреть файл

@ -1488,64 +1488,58 @@ CacheIndex::ProcessPendingOperations()
AssertOwnsLock();
mPendingUpdates.EnumerateEntries(&CacheIndex::UpdateEntryInIndex, this);
for (auto iter = mPendingUpdates.Iter(); !iter.Done(); iter.Next()) {
CacheIndexEntryUpdate* update = iter.Get();
LOG(("CacheIndex::ProcessPendingOperations() [hash=%08x%08x%08x%08x%08x]",
LOGSHA1(update->Hash())));
MOZ_ASSERT(update->IsFresh());
CacheIndexEntry* entry = mIndex.GetEntry(*update->Hash());
{
CacheIndexEntryAutoManage emng(update->Hash(), this);
emng.DoNotSearchInUpdates();
if (update->IsRemoved()) {
if (entry) {
if (entry->IsRemoved()) {
MOZ_ASSERT(entry->IsFresh());
MOZ_ASSERT(entry->IsDirty());
} else if (!entry->IsDirty() && entry->IsFileEmpty()) {
// Entries with empty file are not stored in index on disk. Just
// remove the entry, but only in case the entry is not dirty, i.e.
// the entry file was empty when we wrote the index.
mIndex.RemoveEntry(*update->Hash());
entry = nullptr;
} else {
entry->MarkRemoved();
entry->MarkDirty();
entry->MarkFresh();
}
}
} else if (entry) {
// Some information in mIndex can be newer than in mPendingUpdates (see
// bug 1074832). This will copy just those values that were really
// updated.
update->ApplyUpdate(entry);
} else {
// There is no entry in mIndex, copy all information from
// mPendingUpdates to mIndex.
entry = mIndex.PutEntry(*update->Hash());
*entry = *update;
}
}
iter.Remove();
}
MOZ_ASSERT(mPendingUpdates.Count() == 0);
EnsureCorrectStats();
}
// static
PLDHashOperator
CacheIndex::UpdateEntryInIndex(CacheIndexEntryUpdate *aEntry, void* aClosure)
{
CacheIndex *index = static_cast<CacheIndex *>(aClosure);
LOG(("CacheFile::UpdateEntryInIndex() [hash=%08x%08x%08x%08x%08x]",
LOGSHA1(aEntry->Hash())));
MOZ_ASSERT(aEntry->IsFresh());
CacheIndexEntry *entry = index->mIndex.GetEntry(*aEntry->Hash());
CacheIndexEntryAutoManage emng(aEntry->Hash(), index);
emng.DoNotSearchInUpdates();
if (aEntry->IsRemoved()) {
if (entry) {
if (entry->IsRemoved()) {
MOZ_ASSERT(entry->IsFresh());
MOZ_ASSERT(entry->IsDirty());
} else if (!entry->IsDirty() && entry->IsFileEmpty()) {
// Entries with empty file are not stored in index on disk. Just remove
// the entry, but only in case the entry is not dirty, i.e. the entry
// file was empty when we wrote the index.
index->mIndex.RemoveEntry(*aEntry->Hash());
entry = nullptr;
} else {
entry->MarkRemoved();
entry->MarkDirty();
entry->MarkFresh();
}
}
return PL_DHASH_REMOVE;
}
if (entry) {
// Some information in mIndex can be newer than in mPendingUpdates (see bug
// 1074832). This will copy just those values that were really updated.
aEntry->ApplyUpdate(entry);
} else {
// There is no entry in mIndex, copy all information from mPendingUpdates
// to mIndex.
entry = index->mIndex.PutEntry(*aEntry->Hash());
*entry = *aEntry;
}
return PL_DHASH_REMOVE;
}
bool
CacheIndex::WriteIndexToDiskIfNeeded()
{
@ -1969,7 +1963,13 @@ CacheIndex::WriteLogToDisk()
NS_ENSURE_SUCCESS(rv, rv);
WriteLogHelper wlh(fd);
mIndex.EnumerateEntries(&CacheIndex::WriteEntryToLog, &wlh);
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
CacheIndexEntry* entry = iter.Get();
if (entry->IsRemoved() || entry->IsDirty()) {
wlh.AddEntry(entry);
}
iter.Remove();
}
rv = wlh.Finish();
PR_Close(fd);
@ -2002,19 +2002,6 @@ CacheIndex::WriteLogToDisk()
return NS_OK;
}
// static
PLDHashOperator
CacheIndex::WriteEntryToLog(CacheIndexEntry *aEntry, void* aClosure)
{
WriteLogHelper *wlh = static_cast<WriteLogHelper *>(aClosure);
if (aEntry->IsRemoved() || aEntry->IsDirty()) {
wlh->AddEntry(aEntry);
}
return PL_DHASH_REMOVE;
}
void
CacheIndex::ReadIndexFromDisk()
{
@ -2392,7 +2379,10 @@ CacheIndex::EnsureNoFreshEntry()
#ifdef DEBUG_STATS
CacheIndexStats debugStats;
debugStats.DisableLogging();
mIndex.EnumerateEntries(&CacheIndex::SumIndexStats, &debugStats);
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
debugStats.BeforeChange(nullptr);
debugStats.AfterChange(iter.Get());
}
MOZ_ASSERT(debugStats.Fresh() == 0);
#endif
}
@ -2404,21 +2394,14 @@ CacheIndex::EnsureCorrectStats()
MOZ_ASSERT(mPendingUpdates.Count() == 0);
CacheIndexStats debugStats;
debugStats.DisableLogging();
mIndex.EnumerateEntries(&CacheIndex::SumIndexStats, &debugStats);
for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) {
debugStats.BeforeChange(nullptr);
debugStats.AfterChange(iter.Get());
}
MOZ_ASSERT(debugStats == mIndexStats);
#endif
}
// static
PLDHashOperator
CacheIndex::SumIndexStats(CacheIndexEntry *aEntry, void* aClosure)
{
CacheIndexStats *stats = static_cast<CacheIndexStats *>(aClosure);
stats->BeforeChange(nullptr);
stats->AfterChange(aEntry);
return PL_DHASH_NEXT;
}
void
CacheIndex::FinishRead(bool aSucceeded)
{

Просмотреть файл

@ -723,8 +723,6 @@ private:
// Merge all pending operations from mPendingUpdates into mIndex.
void ProcessPendingOperations();
static PLDHashOperator UpdateEntryInIndex(CacheIndexEntryUpdate *aEntry,
void* aClosure);
// Following methods perform writing of the index file.
//
@ -761,9 +759,6 @@ private:
// Writes journal to the disk and clears dirty flag in index header.
nsresult WriteLogToDisk();
static PLDHashOperator WriteEntryToLog(CacheIndexEntry *aEntry,
void* aClosure);
// Following methods perform reading of the index from the disk.
//
// Index is read at startup just after initializing the CacheIndex. There are
@ -818,7 +813,6 @@ private:
// In debug build this method is called after processing pending operations
// to make sure mIndexStats contains correct information.
void EnsureCorrectStats();
static PLDHashOperator SumIndexStats(CacheIndexEntry *aEntry, void* aClosure);
// Finalizes reading process.
void FinishRead(bool aSucceeded);