Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
nsDiskCacheDevice Class Reference

#include <nsDiskCacheDevice.h>

Inheritance diagram for nsDiskCacheDevice:
Inheritance graph
[legend]
Collaboration diagram for nsDiskCacheDevice:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsDiskCacheDevice ()
virtual ~nsDiskCacheDevice ()
virtual nsresult Init ()
 methods of nsCacheDevice
virtual nsresult Shutdown ()
 NOTE: called while holding the cache service lock.
virtual const char * GetDeviceID (void)
virtual nsCacheEntryFindEntry (nsCString *key, PRBool *collision)
 FindEntry -.
virtual nsresult DeactivateEntry (nsCacheEntry *entry)
 NOTE: called while holding the cache service lock.
virtual nsresult BindEntry (nsCacheEntry *entry)
 BindEntry() no hash number collision -> no problem collision record not active -> evict, no problem record is active record is already doomed -> record shouldn't have been in map, no problem record is not doomed -> doom, and replace record in map.
virtual void DoomEntry (nsCacheEntry *entry)
 NOTE: called while holding the cache service lock.
virtual nsresult OpenInputStreamForEntry (nsCacheEntry *entry, nsCacheAccessMode mode, PRUint32 offset, nsIInputStream **result)
 NOTE: called while holding the cache service lock.
virtual nsresult OpenOutputStreamForEntry (nsCacheEntry *entry, nsCacheAccessMode mode, PRUint32 offset, nsIOutputStream **result)
 NOTE: called while holding the cache service lock.
virtual nsresult GetFileForEntry (nsCacheEntry *entry, nsIFile **result)
 NOTE: called while holding the cache service lock.
virtual nsresult OnDataSizeChange (nsCacheEntry *entry, PRInt32 deltaSize)
 This routine will get called every time an open descriptor is written to.
virtual nsresult Visit (nsICacheVisitor *visitor)
virtual nsresult EvictEntries (const char *clientID)
 Device must evict entries associated with clientID.
void SetCacheParentDirectory (nsILocalFile *parentDir)
 Preference accessors.
void SetCapacity (PRUint32 capacity)
 NOTE: called while holding the cache service lock.
void getCacheDirectory (nsILocalFile **result)
PRUint32 getCacheCapacity ()
PRUint32 getCacheSize ()
PRUint32 getEntryCount ()
PRBool Initialized ()
nsDiskCacheMapCacheMap ()
nsresult Shutdown_Private (PRBool flush)
 nsDiskCacheDevice ()
virtual ~nsDiskCacheDevice ()
 nsCacheDevice methods
virtual nsresult Init ()
virtual nsresult Shutdown ()
virtual const char * GetDeviceID (void)
virtual nsCacheEntryFindEntry (nsCString *key, PRBool *collision)
virtual nsresult DeactivateEntry (nsCacheEntry *entry)
virtual nsresult BindEntry (nsCacheEntry *entry)
virtual void DoomEntry (nsCacheEntry *entry)
virtual nsresult OpenInputStreamForEntry (nsCacheEntry *entry, nsCacheAccessMode mode, PRUint32 offset, nsIInputStream **result)
virtual nsresult OpenOutputStreamForEntry (nsCacheEntry *entry, nsCacheAccessMode mode, PRUint32 offset, nsIOutputStream **result)
virtual nsresult GetFileForEntry (nsCacheEntry *entry, nsIFile **result)
virtual nsresult OnDataSizeChange (nsCacheEntry *entry, PRInt32 deltaSize)
virtual nsresult Visit (nsICacheVisitor *visitor)
virtual nsresult EvictEntries (const char *clientID)
 Device must evict entries associated with clientID.
void SetCacheParentDirectory (nsILocalFile *parentDir)
 Preference accessors.
void SetCapacity (PRUint32 capacity)
nsILocalFileCacheDirectory ()
PRUint32 CacheCapacity ()
PRUint32 CacheSize ()
PRUint32 EntryCount ()

Private Member Functions

nsresult OpenDiskCache ()
 Private methods.
nsresult ClearDiskCache ()
nsresult InitializeCacheDirectory ()
nsresult EvictDiskCacheEntries (PRInt32 targetCapacity)
PRBool Initialized ()
nsresult EvictDiskCacheEntries (PRUint32 targetCapacity)
nsresult UpdateEntry (nsCacheEntry *entry)
nsresult UpdateEntrySize (nsCacheEntry *entry, PRUint32 newSize)
nsresult DeleteEntry (nsCacheEntry *entry, PRBool deleteData)
nsresult DeleteData (nsCacheEntry *entry)
nsresult EnableEvictionObserver ()
nsresult DisableEvictionObserver ()

Private Attributes

nsCOMPtr< nsILocalFilemCacheDirectory
 Member variables.
nsDiskCacheBindery mBindery
PRUint32 mCacheCapacity
nsDiskCacheMapmCacheMap
PRPackedBool mInitialized
nsCOMPtr< mozIStorageConnectionmDB
nsCOMPtr< mozIStorageStatementmStatement_CacheSize
nsCOMPtr< mozIStorageStatementmStatement_EntryCount
nsCOMPtr< mozIStorageStatementmStatement_UpdateEntry
nsCOMPtr< mozIStorageStatementmStatement_UpdateEntrySize
nsCOMPtr< mozIStorageStatementmStatement_DeleteEntry
nsCOMPtr< mozIStorageStatementmStatement_FindEntry
nsCOMPtr< mozIStorageStatementmStatement_BindEntry
PRInt32 mDeltaCounter

Detailed Description

Definition at line 57 of file nsDiskCacheDevice.h.


Constructor & Destructor Documentation

Definition at line 334 of file nsDiskCacheDevice.cpp.

Definition at line 341 of file nsDiskCacheDevice.cpp.

{
    Shutdown();
    delete mCacheMap;
}

Here is the call graph for this function:

nsCacheDevice methods


Member Function Documentation

virtual nsresult nsDiskCacheDevice::BindEntry ( nsCacheEntry entry) [virtual]

Implements nsCacheDevice.

BindEntry() no hash number collision -> no problem collision record not active -> evict, no problem record is active record is already doomed -> record shouldn't have been in map, no problem record is not doomed -> doom, and replace record in map.

        walk matching hashnumber list to find lowest generation number
        take generation number from other (data/meta) location,
            or walk active list

NOTE: called while holding the cache service lock

Implements nsCacheDevice.

Definition at line 538 of file nsDiskCacheDevice.cpp.

{
    if (!Initialized())  return  NS_ERROR_NOT_INITIALIZED;
    nsresult rv = NS_OK;
    nsDiskCacheRecord record, oldRecord;
    
    // create a new record for this entry
    record.SetHashNumber(nsDiskCache::Hash(entry->Key()->get()));
    record.SetEvictionRank(ULONG_MAX - SecondsFromPRTime(PR_Now()));

    if (!entry->IsDoomed()) {
        // if entry isn't doomed, add it to the cache map
        rv = mCacheMap->AddRecord(&record, &oldRecord); // deletes old record, if any
        if (NS_FAILED(rv))  return rv;
        
        PRUint32    oldHashNumber = oldRecord.HashNumber();
        if (oldHashNumber) {
            // gotta evict this one first
            nsDiskCacheBinding * oldBinding = mBindery.FindActiveBinding(oldHashNumber);
            if (oldBinding) {
                // XXX if debug : compare keys for hashNumber collision

                if (!oldBinding->mCacheEntry->IsDoomed()) {
                // we've got a live one!
                    nsCacheService::DoomEntry(oldBinding->mCacheEntry);
                    // storage will be delete when oldBinding->mCacheEntry is Deactivated
                }
            } else {
                // delete storage
                // XXX if debug : compare keys for hashNumber collision
                rv = mCacheMap->DeleteStorage(&oldRecord);
                if (NS_FAILED(rv))  return rv;  // XXX delete record we just added?
            }
        }
    }
    
    // Make sure this entry has its associated nsDiskCacheBinding attached.
    nsDiskCacheBinding *  binding = mBindery.CreateBinding(entry, &record);
    NS_ASSERTION(binding, "nsDiskCacheDevice::BindEntry");
    if (!binding) return NS_ERROR_OUT_OF_MEMORY;
    NS_ASSERTION(binding->mRecord.ValidRecord(), "bad cache map record");

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 95 of file nsDiskCacheDeviceSQL.h.

{ return mCacheCapacity; }

Definition at line 94 of file nsDiskCacheDeviceSQL.h.

{ return mCacheDirectory; }

Definition at line 105 of file nsDiskCacheDevice.h.

{ return mCacheMap; }

Here is the caller graph for this function:

Definition at line 533 of file nsDiskCacheDeviceSQL.cpp.

{
  AutoResetStatement statement(mStatement_CacheSize);

  PRBool hasRows;
  nsresult rv = statement->ExecuteStep(&hasRows);
  NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);
  
  return (PRUint32) statement->AsInt32(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 889 of file nsDiskCacheDevice.cpp.

{
    if (mBindery.ActiveBindings())
        return NS_ERROR_CACHE_IN_USE;

    nsresult rv = Shutdown_Private(PR_FALSE);  // false: don't bother flushing
    if (NS_FAILED(rv))
        return rv;

    // If the disk cache directory is already gone, then it's not an error if
    // we fail to delete it ;-)
    rv = DeleteDir(mCacheDirectory, PR_TRUE, PR_FALSE);
    if (NS_FAILED(rv) && rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST)
        return rv;

    return Init();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Implements nsCacheDevice.

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 495 of file nsDiskCacheDevice.cpp.

{
    nsresult              rv = NS_OK;
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    NS_ASSERTION(binding, "DeactivateEntry: binding == nsnull");
    if (!binding)  return NS_ERROR_UNEXPECTED;

    if (entry->IsDoomed()) {
        // delete data, entry, record from disk for entry
        rv = mCacheMap->DeleteStorage(&binding->mRecord);

    } else {
        // save stuff to disk for entry
        rv = mCacheMap->WriteDiskCacheEntry(binding);
        if (NS_FAILED(rv)) {
            // clean up as best we can
            (void) mCacheMap->DeleteRecordAndStorage(&binding->mRecord);
            binding->mDoomed = PR_TRUE; // record is no longer in cache map
        }
    }

    mBindery.RemoveBinding(binding); // extract binding from collision detection stuff
    delete entry;   // which will release binding
    return rv;
}

Here is the call graph for this function:

Definition at line 661 of file nsDiskCacheDeviceSQL.cpp.

{
  nsDiskCacheBinding *binding = (nsDiskCacheBinding *) entry->Data();
  NS_ENSURE_STATE(binding);

  return binding->mDataFile->Remove(PR_FALSE);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsDiskCacheDevice::DeleteEntry ( nsCacheEntry entry,
PRBool  deleteData 
) [private]

Definition at line 630 of file nsDiskCacheDeviceSQL.cpp.

{
  if (deleteData)
  {
    nsresult rv = DeleteData(entry);
    if (NS_FAILED(rv))
      return rv;
  }

  // Decompose the key into "ClientID" and "Key"
  nsCAutoString keyBuf;
  const char *cid, *key;
  if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
    return NS_ERROR_UNEXPECTED;

  AutoResetStatement statement(mStatement_DeleteEntry);

  nsresult rv;
  rv  = statement->BindCStringParameter(0, cid);
  rv |= statement->BindCStringParameter(1, key);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool hasRows;
  rv = statement->ExecuteStep(&hasRows);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ASSERTION(!hasRows, "DELETE should not result in output");
  return rv;
}

Here is the call graph for this function:

Definition at line 689 of file nsDiskCacheDeviceSQL.cpp.

{
#if 0
  int res = sqlite3_exec(mDB, "DROP TRIGGER cache_on_delete;",
                         NULL, NULL, NULL);
  NS_ENSURE_SQLITE_RESULT(res, NS_ERROR_UNEXPECTED);
#endif

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual void nsDiskCacheDevice::DoomEntry ( nsCacheEntry entry) [virtual]

Implements nsCacheDevice.

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 588 of file nsDiskCacheDevice.cpp.

{
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    NS_ASSERTION(binding, "DoomEntry: binding == nsnull");
    if (!binding)  return;

    if (!binding->mDoomed) {
        // so it can't be seen by FindEntry() ever again.
        nsresult rv = mCacheMap->DoomRecord(&binding->mRecord);
        NS_ASSERTION(NS_SUCCEEDED(rv),"DoomRecord failed.");
        binding->mDoomed = PR_TRUE; // record in no longer in cache map
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 670 of file nsDiskCacheDeviceSQL.cpp.

{
#if 0
  // use CreateTrigger .. maybe do this only once, and have a member
  // variable to control whether or not it is active.

  int res =
      sqlite3_exec(mDB, "CREATE TEMP TRIGGER cache_on_delete AFTER DELETE"
                        " ON moz_cache FOR EACH ROW BEGIN SELECT"
                        " cache_eviction_observer("
                        "  OLD.clientID, OLD.key, OLD.generation);"
                        " END;", NULL, NULL, NULL);
  NS_ENSURE_SQLITE_RESULT(res, NS_ERROR_UNEXPECTED);
#endif

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 545 of file nsDiskCacheDeviceSQL.cpp.

{
  AutoResetStatement statement(mStatement_EntryCount);

  PRBool hasRows;
  nsresult rv = statement->ExecuteStep(&hasRows);
  NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);

  return (PRUint32) statement->AsInt32(0);
}

Here is the call graph for this function:

Definition at line 1253 of file nsDiskCacheDeviceSQL.cpp.

{
  LOG(("nsDiskCacheDevice::EvictDiskCacheEntries [goal=%u delta=%d]\n",
      desiredCapacity, mCacheCapacity - desiredCapacity));

  // need trigger to fire user defined function after a row is deleted
  // so we can delete the corresponding data file.

  // BEGIN
  // while ("SELECT Sum(DataSize) FROM moz_cache;" > desiredCapacity)
  //   DELETE FROM moz_cache WHERE Min(LastFetched);
  // END

  nsresult rv = EnableEvictionObserver();
  NS_ENSURE_SUCCESS(rv, rv);

  PRUint32 lastCacheSize = PR_UINT32_MAX, cacheSize;
  for (;;)
  {
    cacheSize = CacheSize();
    if (cacheSize <= desiredCapacity)
      break;
    if (cacheSize == lastCacheSize)
    {
      LOG(("unable to reduce cache size to target capacity!\n"));
      break;
    }

    rv = mDB->ExecuteSimpleSQL(
        NS_LITERAL_CSTRING("DELETE FROM moz_cache WHERE LastFetched IN ("
                           " SELECT Min(LastFetched) FROM moz_cache"
                           " WHERE Flags=0);"));
    if (NS_FAILED(rv))
    {
      LOG(("failure while deleting Min(LastFetched)\n"));
      break;
    }

    lastCacheSize = cacheSize;
  }

  DisableEvictionObserver();
  return NS_OK;
}

Here is the call graph for this function:

Definition at line 925 of file nsDiskCacheDevice.cpp.

{
    nsresult rv;
    
    if (mCacheMap->TotalSize() < targetCapacity)  return NS_OK;

    nsDiskCacheEvictor  evictor(this, mCacheMap, &mBindery, targetCapacity, nsnull);
    rv = mCacheMap->EvictRecords(&evictor);
    
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual nsresult nsDiskCacheDevice::EvictEntries ( const char *  clientID) [virtual]

Device must evict entries associated with clientID.

If clientID == nsnull, all entries must be evicted. Active entries must be doomed, rather than evicted.

Implements nsCacheDevice.

nsresult nsDiskCacheDevice::EvictEntries ( const char *  clientID) [virtual]

Device must evict entries associated with clientID.

If clientID == nsnull, all entries must be evicted. Active entries must be doomed, rather than evicted.

Implements nsCacheDevice.

Definition at line 804 of file nsDiskCacheDevice.cpp.

{
    if (!Initialized())  return NS_ERROR_NOT_INITIALIZED;
    nsresult  rv;

    if (clientID == nsnull) {
        // we're clearing the entire disk cache
        rv = ClearDiskCache();
        if (rv != NS_ERROR_CACHE_IN_USE)
            return rv;
    }

    nsDiskCacheEvictor  evictor(this, mCacheMap, &mBindery, 0, clientID);
    rv = mCacheMap->VisitRecords(&evictor);
    
    if (clientID == nsnull)     // we tried to clear the entire cache
        rv = mCacheMap->Trim(); // so trim cache block files (if possible)
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual nsCacheEntry* nsDiskCacheDevice::FindEntry ( nsCString key,
PRBool collision 
) [virtual]

Implements nsCacheDevice.

nsCacheEntry * nsDiskCacheDevice::FindEntry ( nsCString key,
PRBool collision 
) [virtual]

FindEntry -.

cases:  key not in disk cache, hash number free
        key not in disk cache, hash number used
        key in disk cache

NOTE: called while holding the cache service lock

Implements nsCacheDevice.

Definition at line 445 of file nsDiskCacheDevice.cpp.

{
    if (!Initialized())  return nsnull;  // NS_ERROR_NOT_INITIALIZED
    nsresult                rv;
    nsDiskCacheRecord       record;
    nsCacheEntry *          entry   = nsnull;
    nsDiskCacheBinding *    binding = nsnull;
    PLDHashNumber           hashNumber = nsDiskCache::Hash(key->get());

    *collision = PR_FALSE;

#if DEBUG  /*because we shouldn't be called for active entries */
    binding = mBindery.FindActiveBinding(hashNumber);
    NS_ASSERTION(!binding, "FindEntry() called for a bound entry.");
    binding = nsnull;
#endif
    
    // lookup hash number in cache map
    rv = mCacheMap->FindRecord(hashNumber, &record);
    if (NS_FAILED(rv))  return nsnull;  // XXX log error?
    
    nsDiskCacheEntry * diskEntry;
    rv = mCacheMap->ReadDiskCacheEntry(&record, &diskEntry);
    if (NS_FAILED(rv))  return nsnull;
    
    // compare key to be sure
    if (nsCRT::strcmp(diskEntry->mKeyStart, key->get()) == 0) {
        entry = diskEntry->CreateCacheEntry(this);
    } else {
        *collision = PR_TRUE;
    }
    delete [] (char *)diskEntry;
    
    // If we had a hash collision or CreateCacheEntry failed, return nsnull
    if (!entry)  return nsnull;
    
    binding = mBindery.CreateBinding(entry, &record);
    if (!binding) {
        delete entry;
        return nsnull;
    }
    
    return entry;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1002 of file nsDiskCacheDevice.cpp.

{
    return mCacheCapacity;
}

Definition at line 981 of file nsDiskCacheDevice.cpp.

{
    *result = mCacheDirectory;
    NS_IF_ADDREF(*result);
}

Definition at line 1008 of file nsDiskCacheDevice.cpp.

{
    return mCacheMap->TotalSize();
}

Here is the call graph for this function:

virtual const char* nsDiskCacheDevice::GetDeviceID ( void  ) [virtual]

Implements nsCacheDevice.

const char * nsDiskCacheDevice::GetDeviceID ( void  ) [virtual]

Implements nsCacheDevice.

Definition at line 429 of file nsDiskCacheDevice.cpp.

Definition at line 1014 of file nsDiskCacheDevice.cpp.

{
    return mCacheMap->EntryCount();
}

Here is the call graph for this function:

virtual nsresult nsDiskCacheDevice::GetFileForEntry ( nsCacheEntry entry,
nsIFile **  result 
) [virtual]

Implements nsCacheDevice.

nsresult nsDiskCacheDevice::GetFileForEntry ( nsCacheEntry entry,
nsIFile **  result 
) [virtual]

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 657 of file nsDiskCacheDevice.cpp.

{
    NS_ENSURE_ARG_POINTER(result);
    *result = nsnull;

    nsresult             rv;
        
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    if (!binding) {
        NS_WARNING("GetFileForEntry: binding == nsnull");
        return NS_ERROR_UNEXPECTED;
    }
    
    // check/set binding->mRecord for separate file, sync w/mCacheMap
    if (binding->mRecord.DataLocationInitialized()) {
        if (binding->mRecord.DataFile() != 0)
            return NS_ERROR_NOT_AVAILABLE;  // data not stored as separate file

        NS_ASSERTION(binding->mRecord.DataFileGeneration() == binding->mGeneration, "error generations out of sync");
    } else {
        binding->mRecord.SetDataFileGeneration(binding->mGeneration);
        binding->mRecord.SetDataFileSize(0);    // 1k minimum
        if (!binding->mDoomed) {
            // record stored in cache map, so update it
            rv = mCacheMap->UpdateRecord(&binding->mRecord);
            if (NS_FAILED(rv))  return rv;
        }
    }
    
    nsCOMPtr<nsIFile>  file;
    rv = mCacheMap->GetFileForDiskCacheRecord(&binding->mRecord,
                                              nsDiskCache::kData,
                                              getter_AddRefs(file));
    if (NS_FAILED(rv))  return rv;
    
    NS_IF_ADDREF(*result = file);
    return NS_OK;
}

Here is the call graph for this function:

virtual nsresult nsDiskCacheDevice::Init ( ) [virtual]

Implements nsCacheDevice.

methods of nsCacheDevice

nsCacheDevice implementation

Implements nsCacheDevice.

Definition at line 352 of file nsDiskCacheDevice.cpp.

{
    nsresult rv;

    NS_ENSURE_TRUE(!Initialized(), NS_ERROR_FAILURE);
       
    if (!mCacheDirectory) return NS_ERROR_FAILURE;
    rv = mBindery.Init();
    if (NS_FAILED(rv)) return rv;
    
    // Open Disk Cache
    rv = OpenDiskCache();
    if (NS_FAILED(rv)) {
        goto error_exit;
    }

    mInitialized = PR_TRUE;
    return NS_OK;

error_exit:
    if (mCacheMap)  {
        (void) mCacheMap->Close(PR_FALSE);
        delete mCacheMap;
        mCacheMap = nsnull;
    }

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 909 of file nsDiskCacheDevice.cpp.

{
    nsresult rv;
    
    rv = mCacheDirectory->Create(nsIFile::DIRECTORY_TYPE, 0777);
    CACHE_LOG_PATH(PR_LOG_ALWAYS, "\ncreate cache directory: %s\n", mCacheDirectory);
    CACHE_LOG_ALWAYS(("mCacheDirectory->Create() = %x\n", rv));
    if (NS_FAILED(rv))  return rv;

    // reopen the cache map     
    rv = mCacheMap->Open(mCacheDirectory);
    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsDiskCacheDevice::Initialized ( ) [inline, private]

Definition at line 101 of file nsDiskCacheDeviceSQL.h.

{ return mDB != nsnull; }

Definition at line 104 of file nsDiskCacheDevice.h.

{ return mInitialized; }

Here is the caller graph for this function:

virtual nsresult nsDiskCacheDevice::OnDataSizeChange ( nsCacheEntry entry,
PRInt32  deltaSize 
) [virtual]

Implements nsCacheDevice.

nsresult nsDiskCacheDevice::OnDataSizeChange ( nsCacheEntry entry,
PRInt32  deltaSize 
) [virtual]

This routine will get called every time an open descriptor is written to.

NOTE: called while holding the cache service lock

Implements nsCacheDevice.

Definition at line 704 of file nsDiskCacheDevice.cpp.

{
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    NS_ASSERTION(binding, "OnDataSizeChange: binding == nsnull");
    if (!binding)  return NS_ERROR_UNEXPECTED;

    NS_ASSERTION(binding->mRecord.ValidRecord(), "bad record");

    PRUint32  newSize = entry->DataSize() + deltaSize;
    PRUint32  maxSize = PR_MIN(mCacheCapacity / 2, kMaxDataFileSize);
    if (newSize > maxSize) {
        nsresult rv = nsCacheService::DoomEntry(entry);
        NS_ASSERTION(NS_SUCCEEDED(rv),"DoomEntry() failed.");
        return NS_ERROR_ABORT;
    }

    PRUint32  sizeK = ((entry->DataSize() + 0x03FF) >> 10); // round up to next 1k
    PRUint32  newSizeK =  ((newSize + 0x3FF) >> 10);

    NS_ASSERTION(sizeK < USHRT_MAX, "data size out of range");
    NS_ASSERTION(newSizeK < USHRT_MAX, "data size out of range");

    // pre-evict entries to make space for new data
    PRInt32  targetCapacity = (PRInt32)(mCacheCapacity - ((newSizeK - sizeK) * 1024));
    EvictDiskCacheEntries(targetCapacity);
    
    return NS_OK;
}

Here is the call graph for this function:

Private methods.

private methods

Definition at line 835 of file nsDiskCacheDevice.cpp.

{
    nsresult  rv;

    // Try opening cache map file.
    NS_ASSERTION(mCacheMap == nsnull, "leaking mCacheMap");
    mCacheMap = new nsDiskCacheMap;
    if (!mCacheMap)
        return NS_ERROR_OUT_OF_MEMORY;
    
    // if we don't have a cache directory, create one and open it
    PRBool exists;
    rv = mCacheDirectory->Exists(&exists);
    if (NS_FAILED(rv))
        return rv;

    PRBool trashing = PR_FALSE;
    if (exists) {
        rv = mCacheMap->Open(mCacheDirectory);        
        // move "corrupt" caches to trash
        if (rv == NS_ERROR_FILE_CORRUPTED) {
            rv = DeleteDir(mCacheDirectory, PR_TRUE, PR_FALSE);
            if (NS_FAILED(rv))
                return rv;
            exists = PR_FALSE;
            trashing = PR_TRUE;
        }
        else if (NS_FAILED(rv))
            return rv;
    }

    // if we don't have a cache directory, create one and open it
    if (!exists) {
        rv = InitializeCacheDirectory();
        if (NS_FAILED(rv))
            return rv;
    }

    if (!trashing) {
        // delete any trash files leftover from a previous run
        nsCOMPtr<nsIFile> trashDir;
        GetTrashDir(mCacheDirectory, &trashDir);
        if (trashDir) {
            PRBool exists;
            if (NS_SUCCEEDED(trashDir->Exists(&exists)) && exists)
                DeleteDir(trashDir, PR_FALSE, PR_FALSE);
        }
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual nsresult nsDiskCacheDevice::OpenInputStreamForEntry ( nsCacheEntry entry,
nsCacheAccessMode  mode,
PRUint32  offset,
nsIInputStream **  result 
) [virtual]

Implements nsCacheDevice.

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 607 of file nsDiskCacheDevice.cpp.

{
    NS_ENSURE_ARG_POINTER(entry);
    NS_ENSURE_ARG_POINTER(result);

    nsresult             rv;
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    NS_ENSURE_TRUE(binding, NS_ERROR_UNEXPECTED);
    
    NS_ASSERTION(binding->mCacheEntry == entry, "binding & entry don't point to each other");

    rv = binding->EnsureStreamIO();
    if (NS_FAILED(rv)) return rv;

    return binding->mStreamIO->GetInputStream(offset, result);
}

Here is the call graph for this function:

virtual nsresult nsDiskCacheDevice::OpenOutputStreamForEntry ( nsCacheEntry entry,
nsCacheAccessMode  mode,
PRUint32  offset,
nsIOutputStream **  result 
) [virtual]

Implements nsCacheDevice.

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 632 of file nsDiskCacheDevice.cpp.

{
    NS_ENSURE_ARG_POINTER(entry);
    NS_ENSURE_ARG_POINTER(result);

    nsresult             rv;
    nsDiskCacheBinding * binding = GetCacheEntryBinding(entry);
    NS_ENSURE_TRUE(binding, NS_ERROR_UNEXPECTED);
    
    NS_ASSERTION(binding->mCacheEntry == entry, "binding & entry don't point to each other");

    rv = binding->EnsureStreamIO();
    if (NS_FAILED(rv)) return rv;

    return binding->mStreamIO->GetOutputStream(offset, result);
}

Here is the call graph for this function:

Preference accessors.

Preference accessors.

methods for prefs

Definition at line 947 of file nsDiskCacheDevice.cpp.

{
    nsresult rv;
    PRBool  exists;

    if (Initialized()) {
        NS_ASSERTION(PR_FALSE, "Cannot switch cache directory when initialized");
        return;
    }

    if (!parentDir) {
        mCacheDirectory = nsnull;
        return;
    }

    // ensure parent directory exists
    rv = parentDir->Exists(&exists);
    if (NS_SUCCEEDED(rv) && !exists)
        rv = parentDir->Create(nsIFile::DIRECTORY_TYPE, 0700);
    if (NS_FAILED(rv))  return;

    // ensure cache directory exists
    nsCOMPtr<nsIFile> directory;
    
    rv = parentDir->Clone(getter_AddRefs(directory));
    if (NS_FAILED(rv))  return;
    rv = directory->AppendNative(NS_LITERAL_CSTRING("Cache"));
    if (NS_FAILED(rv))  return;
    
    mCacheDirectory = do_QueryInterface(directory);
}

Here is the call graph for this function:

Here is the caller graph for this function:

NOTE: called while holding the cache service lock.

Definition at line 992 of file nsDiskCacheDevice.cpp.

{
    mCacheCapacity = capacity * 1024;
    if (Initialized()) {
        // start evicting entries if the new size is smaller!
        EvictDiskCacheEntries((PRInt32)mCacheCapacity);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

virtual nsresult nsDiskCacheDevice::Shutdown ( ) [virtual]

Implements nsCacheDevice.

NOTE: called while holding the cache service lock.

Implements nsCacheDevice.

Definition at line 386 of file nsDiskCacheDevice.cpp.

{
    nsresult rv = Shutdown_Private(PR_TRUE);
    if (NS_FAILED(rv))
        return rv;

    if (mCacheDirectory) {
        // delete any trash files left-over before shutting down.
        nsCOMPtr<nsIFile> trashDir;
        GetTrashDir(mCacheDirectory, &trashDir);
        if (trashDir) {
            PRBool exists;
            if (NS_SUCCEEDED(trashDir->Exists(&exists)) && exists)
                DeleteDir(trashDir, PR_FALSE, PR_TRUE);
        }
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 408 of file nsDiskCacheDevice.cpp.

{
    if (Initialized()) {
        // check cache limits in case we need to evict.
        EvictDiskCacheEntries((PRInt32)mCacheCapacity);

        // write out persistent information about the cache.
        (void) mCacheMap->Close(flush);
        delete mCacheMap;
        mCacheMap = nsnull;

        mBindery.Reset();

        mInitialized = PR_FALSE;
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 557 of file nsDiskCacheDeviceSQL.cpp.

{
  // Decompose the key into "ClientID" and "Key"
  nsCAutoString keyBuf;
  const char *cid, *key;
  if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
    return NS_ERROR_UNEXPECTED;

  nsCString metaDataBuf;
  PRUint32 mdSize = entry->MetaDataSize();
  if (!EnsureStringLength(metaDataBuf, mdSize))
    return NS_ERROR_OUT_OF_MEMORY;
  char *md = metaDataBuf.BeginWriting();
  entry->FlattenMetaData(md, mdSize);

  nsDiskCacheRecord rec;
  rec.metaData = (const PRUint8 *) md;
  rec.metaDataLen = mdSize;
  rec.flags = 0;  // mark entry as inactive
  rec.dataSize = entry->DataSize();
  rec.fetchCount = entry->FetchCount();
  rec.lastFetched = PRTimeFromSeconds(entry->LastFetched());
  rec.lastModified = PRTimeFromSeconds(entry->LastModified());
  rec.expirationTime = PRTimeFromSeconds(entry->ExpirationTime());

  AutoResetStatement statement(mStatement_UpdateEntry);

  nsresult rv;
  rv  = statement->BindDataParameter(0, rec.metaData, rec.metaDataLen);
  rv |= statement->BindInt32Parameter(1, rec.flags);
  rv |= statement->BindInt32Parameter(2, rec.dataSize);
  rv |= statement->BindInt32Parameter(3, rec.fetchCount);
  rv |= statement->BindInt64Parameter(4, rec.lastFetched);
  rv |= statement->BindInt64Parameter(5, rec.lastModified);
  rv |= statement->BindInt64Parameter(6, rec.expirationTime);
  rv |= statement->BindCStringParameter(7, cid);
  rv |= statement->BindCStringParameter(8, key);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool hasRows;
  rv = statement->ExecuteStep(&hasRows);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ASSERTION(!hasRows, "UPDATE should not result in output");
  return rv;
}

Here is the call graph for this function:

nsresult nsDiskCacheDevice::UpdateEntrySize ( nsCacheEntry entry,
PRUint32  newSize 
) [private]

Definition at line 605 of file nsDiskCacheDeviceSQL.cpp.

{
  // Decompose the key into "ClientID" and "Key"
  nsCAutoString keyBuf;
  const char *cid, *key;
  if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
    return NS_ERROR_UNEXPECTED;

  AutoResetStatement statement(mStatement_UpdateEntrySize);

  nsresult rv;
  rv  = statement->BindInt32Parameter(0, newSize);
  rv |= statement->BindCStringParameter(1, cid);
  rv |= statement->BindCStringParameter(2, key);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool hasRows;
  rv = statement->ExecuteStep(&hasRows);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ASSERTION(!hasRows, "UPDATE should not result in output");
  return rv;
}

Here is the call graph for this function:

virtual nsresult nsDiskCacheDevice::Visit ( nsICacheVisitor visitor) [virtual]

Implements nsCacheDevice.

Implements nsCacheDevice.

Definition at line 784 of file nsDiskCacheDevice.cpp.

{
    if (!Initialized())  return NS_ERROR_NOT_INITIALIZED;
    nsDiskCacheDeviceInfo* deviceInfo = new nsDiskCacheDeviceInfo(this);
    nsCOMPtr<nsICacheDeviceInfo> ref(deviceInfo);
    
    PRBool keepGoing;
    nsresult rv = visitor->VisitDevice(DISK_CACHE_DEVICE_ID, deviceInfo, &keepGoing);
    if (NS_FAILED(rv)) return rv;
    
    if (keepGoing) {
        EntryInfoVisitor  infoVisitor(this, mCacheMap, visitor);
        return mCacheMap->VisitRecords(&infoVisitor);
    }

    return NS_OK;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 124 of file nsDiskCacheDevice.h.

Definition at line 125 of file nsDiskCacheDevice.h.

Member variables.

Definition at line 123 of file nsDiskCacheDevice.h.

Definition at line 126 of file nsDiskCacheDevice.h.

Definition at line 115 of file nsDiskCacheDeviceSQL.h.

Definition at line 126 of file nsDiskCacheDeviceSQL.h.

Definition at line 127 of file nsDiskCacheDevice.h.

Definition at line 122 of file nsDiskCacheDeviceSQL.h.

Definition at line 116 of file nsDiskCacheDeviceSQL.h.

Definition at line 120 of file nsDiskCacheDeviceSQL.h.

Definition at line 117 of file nsDiskCacheDeviceSQL.h.

Definition at line 121 of file nsDiskCacheDeviceSQL.h.

Definition at line 118 of file nsDiskCacheDeviceSQL.h.

Definition at line 119 of file nsDiskCacheDeviceSQL.h.


The documentation for this class was generated from the following files: