Back to index

lightning-sunbird  0.9+nobinonly
nsDiskCacheBinding.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsDiskCacheBinding.h, released
00017  * May 10, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan  <gordon@netscape.com>
00026  *   Patrick C. Beard <beard@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 
00043 #ifndef _nsDiskCacheBinding_h_
00044 #define _nsDiskCacheBinding_h_
00045 
00046 #include "nspr.h"
00047 #include "pldhash.h"
00048 
00049 #include "nsISupports.h"
00050 #include "nsCacheEntry.h"
00051 
00052 #include "nsDiskCacheMap.h"
00053 #include "nsDiskCacheStreams.h"
00054 
00055 
00056 /******************************************************************************
00057  *  nsDiskCacheBinding
00058  *
00059  *  Created for disk cache specific data and stored in nsCacheEntry.mData as
00060  *  an nsISupports.  Also stored in nsDiskCacheHashTable, with collisions
00061  *  linked by the PRCList.
00062  *
00063  *****************************************************************************/
00064 
00065 class nsDiskCacheBinding : public nsISupports, public PRCList {
00066 public:
00067     NS_DECL_ISUPPORTS
00068 
00069     nsDiskCacheBinding(nsCacheEntry* entry, nsDiskCacheRecord * record);
00070     virtual ~nsDiskCacheBinding();
00071 
00072     nsresult EnsureStreamIO();
00073     PRBool   IsActive() { return mCacheEntry != nsnull;}
00074 
00075 // XXX make friends
00076 public:
00077     nsCacheEntry*           mCacheEntry;    // back pointer to parent nsCacheEntry
00078     nsDiskCacheRecord       mRecord;
00079     nsDiskCacheStreamIO*    mStreamIO;
00080     PRBool                  mDoomed;        // record is not stored in cache map
00081     PRUint8                 mGeneration;    // possibly just reservation
00082 };
00083 
00084 
00085 /******************************************************************************
00086  *  Utility Functions
00087  *****************************************************************************/
00088 
00089 nsDiskCacheBinding *   GetCacheEntryBinding(nsCacheEntry * entry);
00090 
00091 
00092 
00093 /******************************************************************************
00094  *  nsDiskCacheBindery
00095  *
00096  *  Used to keep track of nsDiskCacheBinding associated with active/bound (and
00097  *  possibly doomed) entries.  Lookups on 4 byte disk hash to find collisions
00098  *  (which need to be doomed, instead of just evicted.  Collisions are linked
00099  *  using a PRCList to keep track of current generation number.
00100  *
00101  *  Used to detect hash number collisions, and find available generation numbers.
00102  *
00103  *  Not all nsDiskCacheBinding have a generation number.
00104  *
00105  *  Generation numbers may be aquired late, or lost (when data fits in block file)
00106  *
00107  *  Collisions can occur:
00108  *      BindEntry()       - hashnumbers collide (possibly different keys)
00109  *
00110  *  Generation number required:
00111  *      DeactivateEntry() - metadata written to disk, may require file
00112  *      GetFileForEntry() - force data to require file
00113  *      writing to stream - data size may require file
00114  *
00115  *  Binding can be kept in PRCList in order of generation numbers.
00116  *  Binding with no generation number can be Appended to PRCList (last).
00117  *
00118  *****************************************************************************/
00119 
00120 class nsDiskCacheBindery {
00121 public:
00122     nsDiskCacheBindery();
00123     ~nsDiskCacheBindery();
00124 
00125     nsresult                Init();
00126     void                    Reset();
00127 
00128     nsDiskCacheBinding *    CreateBinding(nsCacheEntry *       entry,
00129                                           nsDiskCacheRecord *  record);
00130 
00131     nsDiskCacheBinding *    FindActiveBinding(PRUint32  hashNumber);
00132     void                    RemoveBinding(nsDiskCacheBinding * binding);
00133     PRBool                  ActiveBindings();
00134     
00135 private:
00136     nsresult                AddBinding(nsDiskCacheBinding * binding);
00137 
00138     // member variables
00139     static PLDHashTableOps ops;
00140     PLDHashTable           table;
00141     PRBool                 initialized;
00142 };
00143 
00144 #endif /* _nsDiskCacheBinding_h_ */