Bug 673470 - Optimize input buffer size. Cache active tables. r=dcamp

This commit is contained in:
Gian-Carlo Pascutto 2012-08-15 09:04:31 +02:00
parent fda672ce93
commit 1055fd4f4a
5 changed files with 55 additions and 33 deletions

View File

@ -182,6 +182,8 @@ Classifier::Open(nsIFile& aCacheDirectory)
return NS_ERROR_FAILURE;
}
RegenActiveTables();
return NS_OK;
}
@ -214,6 +216,7 @@ Classifier::Reset()
NS_ENSURE_SUCCESS(rv, rv);
mTableFreshness.Clear();
RegenActiveTables();
return NS_OK;
}
@ -368,6 +371,7 @@ Classifier::ApplyUpdates(nsTArray<TableUpdate*>* aUpdates)
}
}
aUpdates->Clear();
RegenActiveTables();
LOG(("Done applying updates."));
#if defined(PR_LOGGING)
@ -405,6 +409,41 @@ Classifier::DropStores()
mLookupCaches.Clear();
}
nsresult
Classifier::RegenActiveTables()
{
mActiveTablesCache.Clear();
nsTArray<nsCString> foundTables;
ScanStoreDir(foundTables);
for (uint32 i = 0; i < foundTables.Length(); i++) {
nsAutoPtr<HashStore> store(new HashStore(nsCString(foundTables[i]), mStoreDirectory));
if (!store)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = store->Open();
if (NS_FAILED(rv))
continue;
LookupCache *lookupCache = GetLookupCache(store->TableName());
if (!lookupCache) {
continue;
}
const ChunkSet &adds = store->AddChunks();
const ChunkSet &subs = store->SubChunks();
if (adds.Length() == 0 && subs.Length() == 0)
continue;
LOG(("Active table: %s", store->TableName().get()));
mActiveTablesCache.AppendElement(store->TableName());
}
return NS_OK;
}
nsresult
Classifier::ScanStoreDir(nsTArray<nsCString>& aTables)
{
@ -438,35 +477,7 @@ Classifier::ScanStoreDir(nsTArray<nsCString>& aTables)
nsresult
Classifier::ActiveTables(nsTArray<nsCString>& aTables)
{
aTables.Clear();
nsTArray<nsCString> foundTables;
ScanStoreDir(foundTables);
for (uint32 i = 0; i < foundTables.Length(); i++) {
nsAutoPtr<HashStore> store(new HashStore(nsCString(foundTables[i]), mStoreDirectory));
if (!store)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = store->Open();
if (NS_FAILED(rv))
continue;
LookupCache *lookupCache = GetLookupCache(store->TableName());
if (!lookupCache) {
continue;
}
const ChunkSet &adds = store->AddChunks();
const ChunkSet &subs = store->SubChunks();
if (adds.Length() == 0 && subs.Length() == 0)
continue;
LOG(("Active table: %s", store->TableName().get()));
aTables.AppendElement(store->TableName());
}
aTables = mActiveTablesCache;
return NS_OK;
}

View File

@ -104,6 +104,7 @@ public:
PrefixArray* aNoiseEntries);
private:
void DropStores();
nsresult RegenActiveTables();
nsresult ScanStoreDir(nsTArray<nsCString>& aTables);
nsresult ApplyTableUpdates(nsTArray<TableUpdate*>* aUpdates,
@ -116,6 +117,7 @@ private:
nsCOMPtr<nsIFile> mStoreDirectory;
nsTArray<HashStore*> mHashStores;
nsTArray<LookupCache*> mLookupCaches;
nsTArray<nsCString> mActiveTablesCache;
PRUint32 mHashKey;
// Stores the last time a given table was updated (seconds).
nsDataHashtable<nsCStringHashKey, PRInt64> mTableFreshness;

View File

@ -217,8 +217,12 @@ HashStore::Open()
return NS_OK;
}
PRInt64 fileSize;
rv = storeFile->GetFileSize(&fileSize);
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewBufferedInputStream(getter_AddRefs(mInputStream), origStream,
BUFFER_SIZE);
fileSize);
NS_ENSURE_SUCCESS(rv, rv);
rv = CheckChecksum(storeFile);
@ -784,6 +788,10 @@ HashStore::WriteFile()
rv = safeOut->Finish();
NS_ENSURE_SUCCESS(rv, rv);
PRInt64 fileSize;
rv = storeFile->GetFileSize(&fileSize);
NS_ENSURE_SUCCESS(rv, rv);
// Reopen the file now that we've rewritten it.
nsCOMPtr<nsIInputStream> origStream;
rv = NS_NewLocalFileInputStream(getter_AddRefs(origStream), storeFile,
@ -791,7 +799,7 @@ HashStore::WriteFile()
NS_ENSURE_SUCCESS(rv, rv);
rv = NS_NewBufferedInputStream(getter_AddRefs(mInputStream), origStream,
BUFFER_SIZE);
fileSize);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;

View File

@ -152,8 +152,6 @@ public:
nsresult ReadEntireStore();
private:
static const int BUFFER_SIZE = 6 * 1024 * 1024;
void Clear();
nsresult Reset();

View File

@ -150,6 +150,9 @@ private:
nsCOMPtr<nsICryptoHash> mCryptoHash;
nsAutoPtr<Classifier> mClassifier;
nsAutoPtr<ProtocolParser> mProtocolParser;
// Directory where to store the SB databases.
nsCOMPtr<nsIFile> mCacheDir;