2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 sts=4 cin et: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef _nsDiskCacheMap_h_
|
|
|
|
#define _nsDiskCacheMap_h_
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsError.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2012-08-20 05:08:46 -07:00
|
|
|
#include "nsITimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsDiskCache.h"
|
|
|
|
#include "nsDiskCacheBlockFile.h"
|
|
|
|
|
|
|
|
|
|
|
|
class nsDiskCacheBinding;
|
|
|
|
struct nsDiskCacheEntry;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheRecord
|
|
|
|
*
|
|
|
|
* Cache Location Format
|
|
|
|
*
|
|
|
|
* 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit
|
|
|
|
*
|
|
|
|
* 0011 0000 0000 0000 0000 0000 0000 0000 : File Selector (0 = separate file)
|
|
|
|
* 0000 0011 0000 0000 0000 0000 0000 0000 : number of extra contiguous blocks 1-4
|
|
|
|
* 0100 1100 0000 0000 0000 0000 0000 0000 : reserved bits
|
|
|
|
* 0000 0000 1111 1111 1111 1111 1111 1111 : block# 0-16777216 (2^24)
|
|
|
|
*
|
|
|
|
* 0000 0000 1111 1111 1111 1111 0000 0000 : eFileSizeMask (size of file in k: see note)
|
|
|
|
* 0000 0000 0000 0000 0000 0000 1111 1111 : eFileGenerationMask
|
|
|
|
*
|
|
|
|
* File Selector:
|
|
|
|
* 0 = separate file on disk
|
|
|
|
* 1 = 256 byte block file
|
|
|
|
* 2 = 1k block file
|
|
|
|
* 3 = 4k block file
|
|
|
|
*
|
2011-05-20 10:27:11 -07:00
|
|
|
* eFileSizeMask note: Files larger than 65535 KiB have this limit stored in
|
|
|
|
* the location. The file itself must be examined to
|
|
|
|
* determine its actual size if necessary.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-01-18 06:12:10 -08:00
|
|
|
/*
|
|
|
|
We have 3 block files with roughly the same max size (32MB)
|
|
|
|
1 - block size 256B, number of blocks 131072
|
|
|
|
2 - block size 1kB, number of blocks 32768
|
|
|
|
3 - block size 4kB, number of blocks 8192
|
|
|
|
*/
|
|
|
|
#define kNumBlockFiles 3
|
|
|
|
#define SIZE_SHIFT(idx) (2 * ((idx) - 1))
|
|
|
|
#define BLOCK_SIZE_FOR_INDEX(idx) ((idx) ? (256 << SIZE_SHIFT(idx)) : 0)
|
|
|
|
#define BITMAP_SIZE_FOR_INDEX(idx) ((idx) ? (131072 >> SIZE_SHIFT(idx)) : 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Min and max values for the number of records in the DiskCachemap
|
|
|
|
#define kMinRecordCount 512
|
|
|
|
|
|
|
|
#define kSeparateFile 0
|
|
|
|
#define kBuckets (1 << 5) // must be a power of 2!
|
|
|
|
|
2011-05-20 10:27:11 -07:00
|
|
|
// Maximum size in K which can be stored in the location (see eFileSizeMask).
|
|
|
|
// Both data and metadata can be larger, but only up to kMaxDataSizeK can be
|
|
|
|
// counted into total cache size. I.e. if there are entries where either data or
|
|
|
|
// metadata is larger than kMaxDataSizeK, the total cache size will be
|
|
|
|
// inaccurate (smaller) than the actual cache size. The alternative is to stat
|
|
|
|
// the files to find the real size, which was decided against for performance
|
|
|
|
// reasons. See bug #651100 comment #21.
|
|
|
|
#define kMaxDataSizeK 0xFFFF
|
|
|
|
|
2010-11-24 07:42:30 -08:00
|
|
|
// preallocate up to 1MB of separate cache file
|
|
|
|
#define kPreallocateLimit 1 * 1024 * 1024
|
|
|
|
|
2012-08-20 05:08:46 -07:00
|
|
|
// The minimum amount of milliseconds to wait before re-attempting to
|
|
|
|
// revalidate the cache.
|
2012-09-04 12:05:19 -07:00
|
|
|
#define kRevalidateCacheTimeout 3000
|
2012-08-20 05:08:46 -07:00
|
|
|
#define kRevalidateCacheTimeoutTolerance 10
|
|
|
|
#define kRevalidateCacheErrorTimeout 1000
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsDiskCacheRecord {
|
|
|
|
|
|
|
|
private:
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mHashNumber;
|
|
|
|
uint32_t mEvictionRank;
|
|
|
|
uint32_t mDataLocation;
|
|
|
|
uint32_t mMetaLocation;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
eLocationInitializedMask = 0x80000000,
|
|
|
|
|
|
|
|
eLocationSelectorMask = 0x30000000,
|
|
|
|
eLocationSelectorOffset = 28,
|
|
|
|
|
|
|
|
eExtraBlocksMask = 0x03000000,
|
|
|
|
eExtraBlocksOffset = 24,
|
|
|
|
|
|
|
|
eReservedMask = 0x4C000000,
|
|
|
|
|
|
|
|
eBlockNumberMask = 0x00FFFFFF,
|
|
|
|
|
|
|
|
eFileSizeMask = 0x00FFFF00,
|
|
|
|
eFileSizeOffset = 8,
|
|
|
|
eFileGenerationMask = 0x000000FF,
|
|
|
|
eFileReservedMask = 0x4F000000
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
nsDiskCacheRecord()
|
|
|
|
: mHashNumber(0), mEvictionRank(0), mDataLocation(0), mMetaLocation(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ValidRecord()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if ((mDataLocation & eReservedMask) || (mMetaLocation & eReservedMask))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HashNumber accessors
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t HashNumber() const { return mHashNumber; }
|
|
|
|
void SetHashNumber( uint32_t hashNumber) { mHashNumber = hashNumber; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// EvictionRank accessors
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t EvictionRank() const { return mEvictionRank; }
|
|
|
|
void SetEvictionRank( uint32_t rank) { mEvictionRank = rank ? rank : 1; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// DataLocation accessors
|
2011-09-28 23:19:26 -07:00
|
|
|
bool DataLocationInitialized() const { return 0 != (mDataLocation & eLocationInitializedMask); }
|
2007-03-22 10:30:00 -07:00
|
|
|
void ClearDataLocation() { mDataLocation = 0; }
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataFile() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint32_t)(mDataLocation & eLocationSelectorMask) >> eLocationSelectorOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetDataBlocks( uint32_t index, uint32_t startBlock, uint32_t blockCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// clear everything
|
|
|
|
mDataLocation = 0;
|
|
|
|
|
|
|
|
// set file index
|
2011-01-18 06:12:10 -08:00
|
|
|
NS_ASSERTION( index < (kNumBlockFiles + 1), "invalid location index");
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION( index > 0,"invalid location index");
|
|
|
|
mDataLocation |= (index << eLocationSelectorOffset) & eLocationSelectorMask;
|
|
|
|
|
|
|
|
// set startBlock
|
|
|
|
NS_ASSERTION(startBlock == (startBlock & eBlockNumberMask), "invalid block number");
|
|
|
|
mDataLocation |= startBlock & eBlockNumberMask;
|
|
|
|
|
|
|
|
// set blockCount
|
|
|
|
NS_ASSERTION( (blockCount>=1) && (blockCount<=4),"invalid block count");
|
|
|
|
--blockCount;
|
|
|
|
mDataLocation |= (blockCount << eExtraBlocksOffset) & eExtraBlocksMask;
|
|
|
|
|
|
|
|
mDataLocation |= eLocationInitializedMask;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataBlockCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint32_t)((mDataLocation & eExtraBlocksMask) >> eExtraBlocksOffset) + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataStartBlock() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (mDataLocation & eBlockNumberMask);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataBlockSize() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return BLOCK_SIZE_FOR_INDEX(DataFile());
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t DataFileSize() const { return (mDataLocation & eFileSizeMask) >> eFileSizeOffset; }
|
|
|
|
void SetDataFileSize(uint32_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION((mDataLocation & eFileReservedMask) == 0, "bad location");
|
|
|
|
mDataLocation &= ~eFileSizeMask; // clear eFileSizeMask
|
|
|
|
mDataLocation |= (size << eFileSizeOffset) & eFileSizeMask;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t DataFileGeneration() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (mDataLocation & eFileGenerationMask);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetDataFileGeneration( uint8_t generation)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// clear everything, (separate file index = 0)
|
|
|
|
mDataLocation = 0;
|
|
|
|
mDataLocation |= generation & eFileGenerationMask;
|
|
|
|
mDataLocation |= eLocationInitializedMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetaLocation accessors
|
2011-09-28 23:19:26 -07:00
|
|
|
bool MetaLocationInitialized() const { return 0 != (mMetaLocation & eLocationInitializedMask); }
|
2007-03-22 10:30:00 -07:00
|
|
|
void ClearMetaLocation() { mMetaLocation = 0; }
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaLocation() const { return mMetaLocation; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaFile() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint32_t)(mMetaLocation & eLocationSelectorMask) >> eLocationSelectorOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetMetaBlocks( uint32_t index, uint32_t startBlock, uint32_t blockCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// clear everything
|
|
|
|
mMetaLocation = 0;
|
|
|
|
|
|
|
|
// set file index
|
2011-01-18 06:12:10 -08:00
|
|
|
NS_ASSERTION( index < (kNumBlockFiles + 1), "invalid location index");
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION( index > 0, "invalid location index");
|
|
|
|
mMetaLocation |= (index << eLocationSelectorOffset) & eLocationSelectorMask;
|
|
|
|
|
|
|
|
// set startBlock
|
|
|
|
NS_ASSERTION(startBlock == (startBlock & eBlockNumberMask), "invalid block number");
|
|
|
|
mMetaLocation |= startBlock & eBlockNumberMask;
|
|
|
|
|
|
|
|
// set blockCount
|
|
|
|
NS_ASSERTION( (blockCount>=1) && (blockCount<=4),"invalid block count");
|
|
|
|
--blockCount;
|
|
|
|
mMetaLocation |= (blockCount << eExtraBlocksOffset) & eExtraBlocksMask;
|
|
|
|
|
|
|
|
mMetaLocation |= eLocationInitializedMask;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaBlockCount() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint32_t)((mMetaLocation & eExtraBlocksMask) >> eExtraBlocksOffset) + 1;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaStartBlock() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (mMetaLocation & eBlockNumberMask);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaBlockSize() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return BLOCK_SIZE_FOR_INDEX(MetaFile());
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t MetaFileSize() const { return (mMetaLocation & eFileSizeMask) >> eFileSizeOffset; }
|
|
|
|
void SetMetaFileSize(uint32_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mMetaLocation &= ~eFileSizeMask; // clear eFileSizeMask
|
|
|
|
mMetaLocation |= (size << eFileSizeOffset) & eFileSizeMask;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t MetaFileGeneration() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (mMetaLocation & eFileGenerationMask);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetMetaFileGeneration( uint8_t generation)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// clear everything, (separate file index = 0)
|
|
|
|
mMetaLocation = 0;
|
|
|
|
mMetaLocation |= generation & eFileGenerationMask;
|
|
|
|
mMetaLocation |= eLocationInitializedMask;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t Generation() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if ((mDataLocation & eLocationInitializedMask) &&
|
|
|
|
(DataFile() == 0))
|
|
|
|
return DataFileGeneration();
|
|
|
|
|
|
|
|
if ((mMetaLocation & eLocationInitializedMask) &&
|
|
|
|
(MetaFile() == 0))
|
|
|
|
return MetaFileGeneration();
|
|
|
|
|
|
|
|
return 0; // no generation
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
void Swap()
|
|
|
|
{
|
|
|
|
mHashNumber = htonl(mHashNumber);
|
|
|
|
mEvictionRank = htonl(mEvictionRank);
|
|
|
|
mDataLocation = htonl(mDataLocation);
|
|
|
|
mMetaLocation = htonl(mMetaLocation);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
void Unswap()
|
|
|
|
{
|
|
|
|
mHashNumber = ntohl(mHashNumber);
|
|
|
|
mEvictionRank = ntohl(mEvictionRank);
|
|
|
|
mDataLocation = ntohl(mDataLocation);
|
|
|
|
mMetaLocation = ntohl(mMetaLocation);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheRecordVisitor
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
enum { kDeleteRecordAndContinue = -1,
|
|
|
|
kStopVisitingRecords = 0,
|
|
|
|
kVisitNextRecord = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsDiskCacheRecordVisitor {
|
|
|
|
public:
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual int32_t VisitRecord( nsDiskCacheRecord * mapRecord) = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheHeader
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
struct nsDiskCacheHeader {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mVersion; // cache version.
|
|
|
|
uint32_t mDataSize; // size of cache in units of 1024bytes.
|
|
|
|
int32_t mEntryCount; // number of entries stored in cache.
|
|
|
|
uint32_t mIsDirty; // dirty flag.
|
|
|
|
int32_t mRecordCount; // Number of records
|
|
|
|
uint32_t mEvictionRank[kBuckets]; // Highest EvictionRank of the bucket
|
|
|
|
uint32_t mBucketUsage[kBuckets]; // Number of used entries in the bucket
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDiskCacheHeader()
|
|
|
|
: mVersion(nsDiskCache::kCurrentVersion)
|
|
|
|
, mDataSize(0)
|
|
|
|
, mEntryCount(0)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mIsDirty(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mRecordCount(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Swap()
|
|
|
|
{
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
mVersion = htonl(mVersion);
|
|
|
|
mDataSize = htonl(mDataSize);
|
|
|
|
mEntryCount = htonl(mEntryCount);
|
|
|
|
mIsDirty = htonl(mIsDirty);
|
|
|
|
mRecordCount = htonl(mRecordCount);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < kBuckets ; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mEvictionRank[i] = htonl(mEvictionRank[i]);
|
|
|
|
mBucketUsage[i] = htonl(mBucketUsage[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unswap()
|
|
|
|
{
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
mVersion = ntohl(mVersion);
|
|
|
|
mDataSize = ntohl(mDataSize);
|
|
|
|
mEntryCount = ntohl(mEntryCount);
|
|
|
|
mIsDirty = ntohl(mIsDirty);
|
|
|
|
mRecordCount = ntohl(mRecordCount);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < kBuckets ; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mEvictionRank[i] = ntohl(mEvictionRank[i]);
|
|
|
|
mBucketUsage[i] = ntohl(mBucketUsage[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheMap
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class nsDiskCacheMap {
|
|
|
|
public:
|
|
|
|
|
2007-12-28 20:22:54 -08:00
|
|
|
nsDiskCacheMap() :
|
2012-07-30 07:20:58 -07:00
|
|
|
mCacheDirectory(nullptr),
|
|
|
|
mMapFD(nullptr),
|
2012-08-20 05:08:46 -07:00
|
|
|
mCleanFD(nullptr),
|
2012-07-30 07:20:58 -07:00
|
|
|
mRecordArray(nullptr),
|
2007-12-28 20:22:54 -08:00
|
|
|
mBufferSize(0),
|
2012-07-30 07:20:58 -07:00
|
|
|
mBuffer(nullptr),
|
2012-08-20 05:08:46 -07:00
|
|
|
mMaxRecordCount(16384), // this default value won't matter
|
|
|
|
mIsDirtyCacheFlushed(false),
|
|
|
|
mLastInvalidateTime(0)
|
2010-06-10 19:46:51 -07:00
|
|
|
{ }
|
2007-12-28 20:22:54 -08:00
|
|
|
|
2012-08-20 05:08:46 -07:00
|
|
|
~nsDiskCacheMap()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
(void) Close(true);
|
2007-12-28 20:22:54 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* File Operations
|
|
|
|
*
|
|
|
|
* Open
|
|
|
|
*
|
|
|
|
* Creates a new cache map file if one doesn't exist.
|
|
|
|
* Returns error if it detects change in format or cache wasn't closed.
|
|
|
|
*/
|
2012-07-20 16:47:52 -07:00
|
|
|
nsresult Open( nsIFile * cacheDirectory,
|
2012-09-04 12:05:19 -07:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo,
|
|
|
|
bool reportCacheCleanTelemetryData);
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult Close(bool flush);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Trim();
|
|
|
|
|
|
|
|
nsresult FlushHeader();
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult FlushRecords( bool unswap);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void NotifyCapacityChange(uint32_t capacity);
|
2010-06-10 19:46:51 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Record operations
|
|
|
|
*/
|
|
|
|
nsresult AddRecord( nsDiskCacheRecord * mapRecord, nsDiskCacheRecord * oldRecord);
|
|
|
|
nsresult UpdateRecord( nsDiskCacheRecord * mapRecord);
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult FindRecord( uint32_t hashNumber, nsDiskCacheRecord * mapRecord);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult DeleteRecord( nsDiskCacheRecord * mapRecord);
|
|
|
|
nsresult VisitRecords( nsDiskCacheRecordVisitor * visitor);
|
|
|
|
nsresult EvictRecords( nsDiskCacheRecordVisitor * visitor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disk Entry operations
|
|
|
|
*/
|
|
|
|
nsresult DeleteStorage( nsDiskCacheRecord * record);
|
|
|
|
|
|
|
|
nsresult GetFileForDiskCacheRecord( nsDiskCacheRecord * record,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool meta,
|
|
|
|
bool createPath,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFile ** result);
|
|
|
|
|
|
|
|
nsresult GetLocalFileForDiskCacheRecord( nsDiskCacheRecord * record,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool meta,
|
|
|
|
bool createPath,
|
2012-06-05 19:08:30 -07:00
|
|
|
nsIFile ** result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-12-28 20:22:54 -08:00
|
|
|
// On success, this returns the buffer owned by nsDiskCacheMap,
|
|
|
|
// so it must not be deleted by the caller.
|
|
|
|
nsDiskCacheEntry * ReadDiskCacheEntry( nsDiskCacheRecord * record);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult WriteDiskCacheEntry( nsDiskCacheBinding * binding);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult ReadDataCacheBlocks(nsDiskCacheBinding * binding, char * buffer, uint32_t size);
|
|
|
|
nsresult WriteDataCacheBlocks(nsDiskCacheBinding * binding, char * buffer, uint32_t size);
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult DeleteStorage( nsDiskCacheRecord * record, bool metaData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Statistical Operations
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
void IncrementTotalSize( uint32_t delta)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mHeader.mDataSize += delta;
|
2011-10-17 07:59:28 -07:00
|
|
|
mHeader.mIsDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void DecrementTotalSize( uint32_t delta)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mHeader.mDataSize >= delta, "disk cache size negative?");
|
|
|
|
mHeader.mDataSize = mHeader.mDataSize > delta ? mHeader.mDataSize - delta : 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mHeader.mIsDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
inline void IncrementTotalSize( uint32_t blocks, uint32_t blockSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Round up to nearest K
|
|
|
|
IncrementTotalSize(((blocks*blockSize) + 0x03FF) >> 10);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
inline void DecrementTotalSize( uint32_t blocks, uint32_t blockSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Round up to nearest K
|
|
|
|
DecrementTotalSize(((blocks*blockSize) + 0x03FF) >> 10);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t TotalSize() { return mHeader.mDataSize; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t EntryCount() { return mHeader.mEntryCount; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private methods
|
|
|
|
*/
|
2012-07-20 16:47:52 -07:00
|
|
|
nsresult OpenBlockFiles(nsDiskCache::CorruptCacheInfo * corruptInfo);
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult CloseBlockFiles(bool flush);
|
|
|
|
bool CacheFilesExist();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-06 07:49:05 -08:00
|
|
|
nsresult CreateCacheSubDirectories();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t CalculateFileIndex(uint32_t size);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult GetBlockFileForIndex( uint32_t index, nsIFile ** result);
|
|
|
|
uint32_t GetBlockSizeForIndex( uint32_t index) const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return BLOCK_SIZE_FOR_INDEX(index);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetBitMapSizeForIndex( uint32_t index) const {
|
2011-01-18 06:12:10 -08:00
|
|
|
return BITMAP_SIZE_FOR_INDEX(index);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// returns the bucket number
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetBucketIndex( uint32_t hashNumber) const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return (hashNumber & (kBuckets - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the size of the bucket (in number of records)
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetRecordsPerBucket() const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return mHeader.mRecordCount / kBuckets;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets the first record in the bucket
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDiskCacheRecord *GetFirstRecordInBucket(uint32_t bucket) const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return mRecordArray + bucket * GetRecordsPerBucket();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t GetBucketRank(uint32_t bucketIndex, uint32_t targetRank);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t VisitEachRecord(uint32_t bucketIndex,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDiskCacheRecordVisitor * visitor,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t evictionRank);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult GrowRecords();
|
|
|
|
nsresult ShrinkRecords();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult EnsureBuffer(uint32_t bufSize);
|
2007-12-28 20:22:54 -08:00
|
|
|
|
|
|
|
// The returned structure will point to the buffer owned by nsDiskCacheMap,
|
|
|
|
// so it must not be deleted by the caller.
|
|
|
|
nsDiskCacheEntry * CreateDiskCacheEntry(nsDiskCacheBinding * binding,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t * size);
|
2007-12-28 20:22:54 -08:00
|
|
|
|
2012-08-20 05:08:46 -07:00
|
|
|
// Initializes the _CACHE_CLEAN_ related functionality
|
|
|
|
nsresult InitCacheClean(nsIFile * cacheDirectory,
|
2012-09-04 12:05:19 -07:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo,
|
|
|
|
bool reportCacheCleanTelemetryData);
|
2012-08-20 05:08:46 -07:00
|
|
|
// Writes out a value of '0' or '1' in the _CACHE_CLEAN_ file
|
|
|
|
nsresult WriteCacheClean(bool clean);
|
|
|
|
// Resets the timout for revalidating the cache
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult ResetCacheTimer(int32_t timeout = kRevalidateCacheTimeout);
|
2012-08-20 05:08:46 -07:00
|
|
|
// Invalidates the cache, calls WriteCacheClean and ResetCacheTimer
|
|
|
|
nsresult InvalidateCache();
|
|
|
|
// Determines if the cache is in a safe state
|
|
|
|
bool IsCacheInSafeState();
|
|
|
|
// Revalidates the cache by writting out the header, records, and finally
|
|
|
|
// by calling WriteCacheClean(true).
|
|
|
|
nsresult RevalidateCache();
|
|
|
|
// Timer which revalidates the cache
|
|
|
|
static void RevalidateTimerCallback(nsITimer *aTimer, void *arg);
|
|
|
|
|
|
|
|
/**
|
2007-03-22 10:30:00 -07:00
|
|
|
* data members
|
|
|
|
*/
|
|
|
|
private:
|
2012-08-20 05:08:46 -07:00
|
|
|
nsCOMPtr<nsITimer> mCleanCacheTimer;
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> mCacheDirectory;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRFileDesc * mMapFD;
|
2012-08-20 05:08:46 -07:00
|
|
|
PRFileDesc * mCleanFD;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDiskCacheRecord * mRecordArray;
|
2011-01-18 06:12:10 -08:00
|
|
|
nsDiskCacheBlockFile mBlockFile[kNumBlockFiles];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mBufferSize;
|
2007-12-28 20:22:54 -08:00
|
|
|
char * mBuffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDiskCacheHeader mHeader;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mMaxRecordCount;
|
2012-08-20 05:08:46 -07:00
|
|
|
bool mIsDirtyCacheFlushed;
|
|
|
|
PRIntervalTime mLastInvalidateTime;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _nsDiskCacheMap_h_
|