From e3d9b3d4b915d57f4f09e7fac622d6074e954321 Mon Sep 17 00:00:00 2001 From: gagan Date: Thu, 21 May 1998 18:04:34 +0000 Subject: [PATCH] Added nsCachePref for cache preferences. This will have the interface for libpref or nsPref stuff. Added database support for dbm. General progress. This is not in the main build as yet. --- network/cache/nu/src/makefile.win | 54 ++-- network/cache/nu/src/nsCacheManager.cpp | 135 +++++---- network/cache/nu/src/nsCacheObject.cpp | 334 ++++++++++++++++++---- network/cache/nu/src/nsCachePref.cpp | 32 ++- network/cache/nu/src/nsDiskModule.cpp | 155 +++++++++- network/cache/nu/src/nsMemCacheObject.cpp | 10 +- network/cache/nu/src/nsMemModule.cpp | 33 ++- 7 files changed, 587 insertions(+), 166 deletions(-) diff --git a/network/cache/nu/src/makefile.win b/network/cache/nu/src/makefile.win index 63b06679ebf3..06fb19808d5f 100644 --- a/network/cache/nu/src/makefile.win +++ b/network/cache/nu/src/makefile.win @@ -19,52 +19,56 @@ IGNORE_MANIFEST = 1 DEPTH=..\..\..\.. -MAKE_OBJ_TYPE = DLL - -LIBNAME = .\$(OBJDIR)\cachelib - -DLL = $(LIBNAME).dll +MAKE_OBJ_TYPE = EXE +DLLNAME = cachelib +#DLL = .\$(OBJDIR)\$(DLLNAME).dll LIBRARY_NAME=cachelib +# Remember to change libplc21 to $whatever it is... +LLIBS = \ + $(LIBNSPR) \ + $(DIST)\lib\libplc21.lib \ + $(DIST)\lib\dbm32.lib \ + $(DIST)\lib\xpcom32.lib \ + $(NULL) + +MISCDEP = $(LLIBS) + MODULE = cache -CPP_OBJS = \ - .\$(OBJDIR)\nsCacheObject.obj \ - .\$(OBJDIR)\nsCacheModule.obj \ - .\$(OBJDIR)\nsMemModule.obj \ - .\$(OBJDIR)\nsDiskModule.obj \ - .\$(OBJDIR)\nsCacheTrace.obj \ - .\$(OBJDIR)\nsCacheManager.obj \ - .\$(OBJDIR)\nsCachePref.obj \ +OBJS = \ + .\$(OBJDIR)\nsCacheObject.obj \ + .\$(OBJDIR)\nsCacheModule.obj \ + .\$(OBJDIR)\nsMemModule.obj \ + .\$(OBJDIR)\nsDiskModule.obj \ + .\$(OBJDIR)\nsCacheTrace.obj \ + .\$(OBJDIR)\nsCacheManager.obj \ + .\$(OBJDIR)\nsCachePref.obj \ .\$(OBJDIR)\nsMemCacheObject.obj \ $(NULL) +# .\$(OBJDIR)\nsHash.obj \ + LINCS = \ -I$(PUBLIC)\xpcom \ + -I$(PUBLIC)\dbm \ -I..\public \ -I..\include \ $(NULL) LCFLAGS = \ $(LCFLAGS) \ - -DNS_DLLNAME=$(DLLNAME).dll \ - $(NULL) - -# Remember to change libplc21 to $whatever it is... -LLIBS = \ - $(LIBNSPR) \ - $(DIST)\lib\libplc21.lib \ - $(DIST)\lib\xpcom32.lib \ $(NULL) + #-DNS_DLLNAME=$(DLLNAME).dll \ include <$(DEPTH)\config\rules.mak> -install:: $(DLL) - $(MAKE_INSTALL) $(LIBNAME).$(DLL_SUFFIX) $(DIST)\bin - $(MAKE_INSTALL) $(LIBNAME).$(LIB_SUFFIX) $(DIST)\lib +install:: $(EXE) + $(MAKE_INSTALL) .\$(OBJDIR)\$(DLLNAME).$(LIB_SUFFIX) $(DIST)\lib +# $(MAKE_INSTALL) .\$(OBJDIR)\$(DLLNAME).$(DLL_SUFFIX) $(DIST)\bin clobber:: - rm -f $(DIST)\bin\$(DLLNAME).$(DLL_SUFFIX) rm -f $(DIST)\lib\$(DLLNAME).$(LIB_SUFFIX) +# rm -f $(DIST)\bin\$(DLLNAME).$(DLL_SUFFIX) diff --git a/network/cache/nu/src/nsCacheManager.cpp b/network/cache/nu/src/nsCacheManager.cpp index c5b8cb17d58e..fb8d914ce485 100644 --- a/network/cache/nu/src/nsCacheManager.cpp +++ b/network/cache/nu/src/nsCacheManager.cpp @@ -37,53 +37,56 @@ static nsCacheManager TheManager; -nsCacheManager::nsCacheManager(): m_pFirstModule(0), m_pPref(new nsCachePref()) +nsCacheManager::nsCacheManager(): m_pFirstModule(0) { Init(); } nsCacheManager::~nsCacheManager() { - if (m_pFirstModule) - delete m_pFirstModule; - delete m_pPref; + if (m_pFirstModule) + delete m_pFirstModule; } -nsCacheManager* nsCacheManager::GetInstance() +nsCacheManager* +nsCacheManager::GetInstance() { - return &TheManager; + return &TheManager; } /* Caller must free returned char* */ -const char* nsCacheManager::Trace() const +const char* +nsCacheManager::Trace() const { - char linebuffer[128]; - char* total; - + char linebuffer[128]; + char* total; + sprintf(linebuffer, "nsCacheManager: Modules = %d\n", Entries()); - total = new char[strlen(linebuffer) + 1]; + total = new char[strlen(linebuffer) + 1]; strcpy(total, linebuffer); - return total; + return total; } -PRInt32 nsCacheManager::AddModule(nsCacheModule* pModule) +PRInt32 +nsCacheManager::AddModule(nsCacheModule* pModule) { - if (pModule) - { - if (m_pFirstModule) - LastModule()->Next(pModule); - else - m_pFirstModule = pModule; - - return Entries()-1; - } - else - return -1; + if (pModule) + { + if (m_pFirstModule) + LastModule()->Next(pModule); + else + m_pFirstModule = pModule; + + return Entries()-1; + } + else + return -1; } -PRBool nsCacheManager::Contains(const char* i_url) const +PRBool +nsCacheManager::Contains(const char* i_url) const { if (m_pFirstModule) { @@ -100,7 +103,8 @@ PRBool nsCacheManager::Contains(const char* i_url) const return PR_FALSE; } -nsCacheObject* nsCacheManager::GetObject(const char* i_url) const +nsCacheObject* +nsCacheManager::GetObject(const char* i_url) const { if (m_pFirstModule) { @@ -117,67 +121,74 @@ nsCacheObject* nsCacheManager::GetObject(const char* i_url) const return 0; } -PRInt32 nsCacheManager::Entries() const +PRInt32 +nsCacheManager::Entries() const { - if (m_pFirstModule) - { - PRInt32 count=1; - nsCacheModule* pModule = m_pFirstModule; - while (pModule = pModule->Next()) - { - count++; - } - return count; - } - return 0; + if (m_pFirstModule) + { + PRInt32 count=1; + nsCacheModule* pModule = m_pFirstModule; + while (pModule = pModule->Next()) + { + count++; + } + return count; + } + return 0; } -nsCacheModule* nsCacheManager::GetModule(PRInt32 i_index) const +nsCacheModule* +nsCacheManager::GetModule(PRInt32 i_index) const { - if ((i_index < 0) || (i_index >= Entries())) - return 0; - nsCacheModule* pModule = m_pFirstModule; - for (PRInt32 i=0; i<=i_index; pModule = pModule->Next()) - i++; - PR_ASSERT(pModule); - return pModule; + if ((i_index < 0) || (i_index >= Entries())) + return 0; + nsCacheModule* pModule = m_pFirstModule; + for (PRInt32 i=0; i<=i_index; pModule = pModule->Next()) + i++; + PR_ASSERT(pModule); + return pModule; } -nsMemModule* nsCacheManager::GetMemModule() const +nsMemModule* +nsCacheManager::GetMemModule() const { return (nsMemModule*) m_pFirstModule; } -nsDiskModule* nsCacheManager::GetDiskModule() const +nsDiskModule* +nsCacheManager::GetDiskModule() const { return (m_pFirstModule) ? (nsDiskModule*) m_pFirstModule->Next() : NULL; } -void nsCacheManager::Init() +void +nsCacheManager::Init() { if (m_pFirstModule) delete m_pFirstModule; - m_pFirstModule = new nsMemModule(m_pPref->MemCacheSize()); - // m_pFirstModule->Next((new nsDiskModule(m_pPref->DiskCacheSize())); + m_pFirstModule = new nsMemModule(nsCachePref::MemCacheSize()); + // m_pFirstModule->Next((new nsDiskModule(nsCachePref::DiskCacheSize())); } -nsCacheModule* nsCacheManager::LastModule() const +nsCacheModule* +nsCacheManager::LastModule() const { - if (m_pFirstModule) - { - nsCacheModule* pModule = m_pFirstModule; - while(pModule->Next()) { - pModule = pModule->Next(); - } - return pModule; - } - return 0; + if (m_pFirstModule) + { + nsCacheModule* pModule = m_pFirstModule; + while(pModule->Next()) { + pModule = pModule->Next(); + } + return pModule; + } + return 0; } -PRUint32 nsCacheManager::WorstCaseTime() const +PRUint32 +nsCacheManager::WorstCaseTime(void) const { PRIntervalTime start = PR_IntervalNow(); while (this->Contains("a vague string that should not be in any of the modules")); diff --git a/network/cache/nu/src/nsCacheObject.cpp b/network/cache/nu/src/nsCacheObject.cpp index 3ba43f53e69a..ee663bdb8524 100644 --- a/network/cache/nu/src/nsCacheObject.cpp +++ b/network/cache/nu/src/nsCacheObject.cpp @@ -19,6 +19,10 @@ #include #include +#include +#include +#include + #include "nsCacheObject.h" #include "nsCacheTrace.h" @@ -31,91 +35,321 @@ static const PRIntervalTime DEFAULT_EXPIRES = PR_SecondsToInterval(86400); +// m_Etag, m_Expires, m_Flags, m_Hits, m_LastAccessed, m_LastModified, m_Module, m_Size, m_Url +#define READ_WRITE_FORMAT "%s,%d,%d,%d,%d,%d,%d,%d,%s" +#define READ_WRITE_ELEMENTS m_Etag,m_Expires,m_Flags,m_Hits,m_LastAccessed,m_LastModified,m_Module,m_Size,m_Url + +#if !defined(IS_LITTLE_ENDIAN) && !defined(IS_BIG_ENDIAN) +ERROR! Must have a byte order +#endif + +#ifdef IS_LITTLE_ENDIAN +#define COPY_INT32(_a,_b) memcpy(_a, _b, sizeof(int32)); +#else +#define COPY_INT32(_a,_b) /* swap */ \ + do { \ + ((char *)(_a))[0] = ((char *)(_b))[3]; \ + ((char *)(_a))[1] = ((char *)(_b))[2]; \ + ((char *)(_a))[2] = ((char *)(_b))[1]; \ + ((char *)(_a))[3] = ((char *)(_b))[0]; \ + } while(0) +#endif + +/* Convenient macros used in stuffing and reading back cache object to a void * */ +/* TODO: Change these to file scope functions */ +#define STUFF_STRING(string) \ +{ \ + len = (string ? PL_strlen(string)+1 : 0); \ + COPY_INT32((void *)cur_ptr, &len); \ + cur_ptr = cur_ptr + sizeof(int32); \ + if(len) \ + memcpy((void *)cur_ptr, string, len); \ + cur_ptr += len; \ +} + +#define STUFF_NUMBER(number) \ +{ \ + COPY_INT32((void *)cur_ptr, &number); \ + cur_ptr = cur_ptr + sizeof(PRUint32); \ +} + +#define STUFF_TIME(number) \ +{ \ + COPY_INT32((void *)cur_ptr, &number); \ + cur_ptr = cur_ptr + sizeof(PRIntervalTime); \ +} + +#define STUFF_BOOL(bool_val) \ +{ \ + if(bool_val) \ + ((char *)(cur_ptr))[0] = 1; \ + else \ + ((char *)(cur_ptr))[0] = 0; \ + cur_ptr = cur_ptr + sizeof(char); \ +} + +/* if any strings are larger than this then + * there was a serious database error + */ +#define MAX_HUGE_STRING_SIZE 10000 + +#define RETRIEVE_STRING(string) \ +{ \ + if(cur_ptr > max_ptr) \ + { \ + return PR_TRUE; \ + } \ + COPY_INT32(&len, cur_ptr); \ + cur_ptr += sizeof(int32); \ + if(len) \ + { \ + if(len > MAX_HUGE_STRING_SIZE) \ + { \ + return PR_FALSE; \ + } \ + string = (char*)PR_Malloc(len); \ + if(!string) \ + { \ + return PR_FALSE; \ + } \ + memcpy(string, cur_ptr, len); \ + cur_ptr += len; \ + } \ +} + +#define RETRIEVE_NUMBER(number) \ +{ \ + if(cur_ptr > max_ptr) \ + return PR_TRUE; \ + COPY_INT32(&number, cur_ptr); \ + cur_ptr += sizeof(int32); \ +} + +#define RETRIEVE_TIME(number) \ +{ \ + if(cur_ptr > max_ptr) \ + return PR_TRUE; \ + COPY_INT32(&number, cur_ptr); \ + cur_ptr += sizeof(PRIntervalTime); \ +} + +#define RETRIEVE_BOOL(bool) \ +{ \ + if(cur_ptr > max_ptr) \ + return PR_TRUE; \ + if(((char *)(cur_ptr))[0]) \ + bool = TRUE; \ + else \ + bool = FALSE; \ + cur_ptr += sizeof(char); \ +} + nsCacheObject::nsCacheObject(): - m_Flags(INIT), - m_Url(new char[1]), - m_Etag(new char[1]), - m_Module(-1) + m_Flags(INIT), + m_Url(new char[1]), + m_Etag(new char[1]), + m_Module(-1), + m_pInfo(0) { - Init(); - *m_Url = '\0'; - *m_Etag = '\0'; + Init(); + *m_Url = '\0'; + *m_Etag = '\0'; } nsCacheObject::~nsCacheObject() { - delete[] m_Url; - delete[] m_Etag; + delete[] m_Url; + delete[] m_Etag; } nsCacheObject::nsCacheObject(const nsCacheObject& another): - m_Flags(another.m_Flags), - m_Url(new char[strlen(another.m_Url)+1]), - m_Etag(new char[strlen(another.m_Etag)+1]) + m_Flags(another.m_Flags), + m_Url(new char[strlen(another.m_Url)+1]), + m_Etag(new char[strlen(another.m_Etag)+1]), + m_pInfo(0) { + strcpy(m_Url, another.m_Url); + strcpy(m_Etag, another.m_Etag); - strcpy(m_Url, another.m_Url); - strcpy(m_Etag, another.m_Etag); - - m_Hits = another.m_Hits; - m_LastAccessed = another.m_LastAccessed; - m_LastModified = another.m_LastModified; - m_Size = another.m_Size; + m_Hits = another.m_Hits; + m_LastAccessed = another.m_LastAccessed; + m_LastModified = another.m_LastModified; + m_Size = another.m_Size; m_Module = another.m_Module; } nsCacheObject::nsCacheObject(const char* i_url): - m_Flags(INIT), - m_Url(new char[strlen(i_url)+1]), - m_Etag(new char[1]) + m_Flags(INIT), + m_Url(new char[strlen(i_url)+1]), + m_Etag(new char[1]), + m_Module(-1), + m_pInfo(0) { - Init(); - PR_ASSERT(i_url); - strcpy(m_Url, i_url); - *m_Etag = '\0'; + Init(); + PR_ASSERT(i_url); + strcpy(m_Url, i_url); + *m_Etag = '\0'; } void nsCacheObject::Address(const char* i_url) { - PR_ASSERT(i_url && *i_url); - if (m_Url) - delete[] m_Url; - m_Url = new char[strlen(i_url) + 1]; - strcpy(m_Url, i_url); + PR_ASSERT(i_url && *i_url); + if (m_Url) + delete[] m_Url; + m_Url = new char[strlen(i_url) + 1]; + strcpy(m_Url, i_url); } void nsCacheObject::Etag(const char* i_etag) { - PR_ASSERT(i_etag && *i_etag); - if (m_Etag) - delete[] m_Etag; - m_Etag = new char[strlen(i_etag) + 1]; - strcpy(m_Etag, i_etag); + PR_ASSERT(i_etag && *i_etag); + if (m_Etag) + delete[] m_Etag; + m_Etag = new char[strlen(i_etag) + 1]; + strcpy(m_Etag, i_etag); +} + +void* nsCacheObject::Info(void) const +{ + if (m_pInfo) + return m_pInfo; + + nsCacheObject* pThis = (nsCacheObject*) this; + if (m_Url){ +/* + char* tmpBuff = PR_smprintf(READ_WRITE_FORMAT, READ_WRITE_ELEMENTS); + int tmpLen = PL_strlen(tmpBuff); + pThis->m_pInfo = PR_Malloc(tmpLen); + memcpy(pThis->m_pInfo, tmpBuff, tmpLen); + PR_Free(tmpBuff); +*/ + pThis->m_info_size = sizeof(nsCacheObject); + pThis->m_info_size -= sizeof(void*); //m_info is not being serialized + + //Add the strings sizes + pThis->m_info_size += PL_strlen(m_Etag)+1; + pThis->m_info_size += PL_strlen(m_Url)+1; + + void* new_obj = PR_Malloc(m_info_size * sizeof(char)); + + memset(new_obj, 0, m_info_size *sizeof(char)); + + if (!new_obj) + { + PR_Free(new_obj); + return 0; + } + + PRUint32 len; + + char* cur_ptr = (char*) new_obj; + /* put the total size of the struct into + * the first field so that we have + * a cross check against corruption + */ + COPY_INT32((void *)cur_ptr, &m_info_size); + cur_ptr += sizeof(PRUint32); + + /* put the version number of the structure + * format that we are using. By using a version + * string when writting we can support + * backwards compatibility in our reading code + */ + COPY_INT32((void *)cur_ptr, &kCACHE_VERSION); + cur_ptr += sizeof(PRUint32); + + STUFF_STRING(m_Etag); + STUFF_TIME(m_Expires); + STUFF_NUMBER(m_Flags); + STUFF_NUMBER(m_Hits); + STUFF_TIME(m_LastAccessed); + STUFF_TIME(m_LastModified); + STUFF_NUMBER(m_Module); + STUFF_NUMBER(m_Size); + STUFF_STRING(m_Url); + + // Important Assertion. Dont remove! + PR_ASSERT(cur_ptr == (char*) new_obj + m_info_size); + pThis->m_pInfo = new_obj; + } + return m_pInfo; +} + +PRBool nsCacheObject::Info(void* i_data) +{ + if (!i_data) + return PR_FALSE; + + char* cur_ptr = (char*) i_data; + + //Reset the m_pInfo; + PR_FREEIF(m_pInfo); + + COPY_INT32(&m_info_size, cur_ptr); + char* max_ptr = cur_ptr + m_info_size; + cur_ptr += sizeof(PRUint32); + + PRUint32 version; + COPY_INT32(&version, cur_ptr); + cur_ptr += sizeof(PRUint32); + + PR_ASSERT(version == kCACHE_VERSION); + if (version != kCACHE_VERSION) + { + //TODO Bad cache version + return PR_FALSE; + } + + PRUint32 len; + //m_Etag,m_Expires,m_Flags,m_Hits,m_LastAccessed,m_LastModified,m_Module,m_Size,m_Url + RETRIEVE_STRING(m_Etag); + RETRIEVE_TIME(m_Expires); + RETRIEVE_NUMBER(m_Flags); + RETRIEVE_NUMBER(m_Hits); + RETRIEVE_TIME(m_LastAccessed); + RETRIEVE_TIME(m_LastModified); + RETRIEVE_NUMBER(m_Module); + RETRIEVE_NUMBER(m_Size); + RETRIEVE_STRING(m_Url); + + // Most important assertion! Don't ever remove! + PR_ASSERT(cur_ptr == max_ptr); + return PR_TRUE; +} + +PRUint32 nsCacheObject::InfoSize(void) const +{ + if (!m_pInfo) + { + ((nsCacheObject*) this)->Info(); + } + return m_info_size; } void nsCacheObject::Init() { - m_Expires = PR_IntervalNow() + DEFAULT_EXPIRES; + m_Expires = PR_IntervalNow() + DEFAULT_EXPIRES; m_Hits = 0; } /* Caller must free returned string */ -const char* nsCacheObject::Trace() const +// TODO change to use PR_stuff... +const char* nsCacheObject::Trace() const { - char linebuffer[256]; - char* total; + char linebuffer[256]; + char* total; - sprintf(linebuffer, "nsCacheObject:URL=%s,SIZE=%d,ET=%s,\n\tLM=%d,LA=%d,EXP=%d,HITS=%d\n", - m_Url, - m_Size, - m_Etag, - m_LastModified, - m_LastAccessed, - m_Expires, - m_Hits); + sprintf(linebuffer, "nsCacheObject:URL=%s,SIZE=%d,ET=%s,\n\tLM=%d,LA=%d,EXP=%d,HITS=%d\n", + m_Url, + m_Size, + m_Etag, + m_LastModified, + m_LastAccessed, + m_Expires, + m_Hits); - total = new char[strlen(linebuffer) +1]; + total = new char[PL_strlen(linebuffer) +1]; strcpy(total, linebuffer); - return total; + return total; } diff --git a/network/cache/nu/src/nsCachePref.cpp b/network/cache/nu/src/nsCachePref.cpp index 455e39c3891a..dc95c40176da 100644 --- a/network/cache/nu/src/nsCachePref.cpp +++ b/network/cache/nu/src/nsCachePref.cpp @@ -21,24 +21,42 @@ static const PRUint32 MEM_CACHE_SIZE_DEFAULT = 1024*1024; static const PRUint32 DISK_CACHE_SIZE_DEFAULT = 5*MEM_CACHE_SIZE_DEFAULT; -nsCachePref::nsCachePref() +static nsCachePref ThePrefs; + +nsCachePref::nsCachePref(void) { + //Read all the stuff from pref here. } nsCachePref::~nsCachePref() { } -PRUint32 nsCachePref::MemCacheSize() const -{ - return MEM_CACHE_SIZE_DEFAULT; -} - -PRUint32 nsCachePref::DiskCacheSize() const +PRUint32 nsCachePref::DiskCacheSize() { return DISK_CACHE_SIZE_DEFAULT; } +const char* nsCachePref::DiskCacheDBFilename(void) +{ + return "fat.db"; +} + +const char* nsCachePref::DiskCacheFolder(void) +{ + return 0; +} + +nsCachePref* nsCachePref::GetInstance() +{ + return &ThePrefs; +} + +PRUint32 nsCachePref::MemCacheSize() +{ + return MEM_CACHE_SIZE_DEFAULT; +} + /* nsrefcnt nsCachePref::AddRef(void) { diff --git a/network/cache/nu/src/nsDiskModule.cpp b/network/cache/nu/src/nsDiskModule.cpp index 0242743cd79d..2bbce2cdab7e 100644 --- a/network/cache/nu/src/nsDiskModule.cpp +++ b/network/cache/nu/src/nsDiskModule.cpp @@ -24,28 +24,173 @@ */ #include +#include +#include + #include "nsDiskModule.h" #include "nsCacheObject.h" +#include "nsCacheManager.h" + +#include "mcom_db.h" + // // Constructor: nsDiskModule // nsDiskModule::nsDiskModule(const PRUint32 size): - nsCacheModule(size) + nsCacheModule(size), + m_pDB(0) { - } nsDiskModule::~nsDiskModule() { + if (m_pDB) + { + (*m_pDB->sync)(m_pDB, 0); + (*m_pDB->close)(m_pDB); + m_pDB = 0; + } +} + +PRBool nsDiskModule::AddObject(nsCacheObject* io_pObject) +{ + if (!InitDB()) + { + // Set some error state TODO + return PR_FALSE; + } + + if (io_pObject && io_pObject->Address()) + { + static DBT key,data; + + io_pObject->Module(nsCacheManager::DISK); + + PR_FREEIF(key.data); + PR_FREEIF(data.data); + + key.data = (void*)io_pObject->Address(); /* Later on change this to include post data- io_pObject->KeyData() */ + key.size = PL_strlen(io_pObject->Address()); + + data.data = io_pObject->Info(); + data.size = io_pObject->InfoSize(); + + int status = (*m_pDB->put)(m_pDB, &key, &data, 0); + if (status == 0) + { +// if (m_Sync == EVERYTIME) + status = (*m_pDB->sync)(m_pDB, 0); + } + return (status == 0); + } + return PR_FALSE; +} + +PRBool nsDiskModule::Contains(nsCacheObject* io_pObject) const +{ + if (!m_pDB) + { + nsDiskModule* pThis = (nsDiskModule*) this; + pThis->InitDB(); + } + + if (!m_pDB || !io_pObject) + return PR_FALSE; + + nsCacheObject* pTemp = GetObject(io_pObject->Address()); + if (pTemp) + { + io_pObject = pTemp; + return PR_TRUE; + } + return PR_FALSE; +} + +PRBool nsDiskModule::Contains(const char* i_url) const +{ + if (!m_pDB) + { + nsDiskModule* pThis = (nsDiskModule*) this; + pThis->InitDB(); + } + + if (!m_pDB || !i_url || !*i_url) + return PR_FALSE; + + DBT key, data; + + key.data = (void*) i_url; + key.size = PL_strlen(i_url); + + int status = (*m_pDB->get)(m_pDB, &key, &data, 0); + + return (status == 0); } -nsCacheObject* nsDiskModule::GetObject(PRUint32 i_index) const +nsCacheObject* nsDiskModule::GetObject(const PRUint32 i_index) const { + if (!m_pDB) + { + nsDiskModule* pThis = (nsDiskModule*) this; + pThis->InitDB(); + } + + if (!m_pDB) + return 0; + return 0; } -PRBool nsDiskModule::AddObject(nsCacheObject* i_pObject) +nsCacheObject* nsDiskModule::GetObject(const char* i_url) const { - return PR_FALSE; + if (!m_pDB) + { + nsDiskModule* pThis = (nsDiskModule*) this; + pThis->InitDB(); + } + + if (!m_pDB || !i_url || !*i_url) + return 0; + + DBT key, data; + + key.data = (void*) i_url; + key.size = PL_strlen(i_url); + + if (0 == (*m_pDB->get)(m_pDB, &key, &data, 0)) + { + nsCacheObject* pTemp = new nsCacheObject(); + pTemp->Info(data.data); + return pTemp; + } + + return 0; +} + +PRBool nsDiskModule::InitDB(void) +{ + + if (m_pDB) + return PR_TRUE; + + HASHINFO hash_info = { + 16*1024, /* bucket size */ + 0, /* fill factor */ + 0, /* number of elements */ + 0, /* bytes to cache */ + 0, /* hash function */ + 0}; /* byte order */ + + m_pDB = dbopen( + nsCachePref::DiskCacheDBFilename(), + O_RDWR | O_CREAT, + 0600, + DB_HASH, + &hash_info); + + if (!m_pDB) + return PR_FALSE; + + return PR_TRUE; } diff --git a/network/cache/nu/src/nsMemCacheObject.cpp b/network/cache/nu/src/nsMemCacheObject.cpp index 924c2eedc184..e073d7aee044 100644 --- a/network/cache/nu/src/nsMemCacheObject.cpp +++ b/network/cache/nu/src/nsMemCacheObject.cpp @@ -27,11 +27,11 @@ nsMemCacheObject::~nsMemCacheObject() { - if (m_pNextObject) - { - delete m_pNextObject; - m_pNextObject = 0; - } + if (m_pNextObject) + { + delete m_pNextObject; + m_pNextObject = 0; + } if (m_pObject) { diff --git a/network/cache/nu/src/nsMemModule.cpp b/network/cache/nu/src/nsMemModule.cpp index 98300fe8cd80..e915ca4143dc 100644 --- a/network/cache/nu/src/nsMemModule.cpp +++ b/network/cache/nu/src/nsMemModule.cpp @@ -22,6 +22,7 @@ #include "nsMemModule.h" #include "nsMemCacheObject.h" +#include "nsCacheManager.h" /* * nsMemModule @@ -41,35 +42,38 @@ nsMemModule::nsMemModule(const PRUint32 size): nsMemModule::~nsMemModule() { - if (m_pFirstObject) { - delete m_pFirstObject; - m_pFirstObject = 0; - } + if (m_pFirstObject) { + delete m_pFirstObject; + m_pFirstObject = 0; + } } -PRBool nsMemModule::AddObject(nsCacheObject* i_pObject) +PRBool nsMemModule::AddObject(nsCacheObject* io_pObject) { #if 0 - if (i_pObject) + if (io_pObject) { - m_ht.Put( + m_ht.Put(io_pObject->Address(), io_pObject); } return PR_FALSE; #endif - if (i_pObject) + if (io_pObject) { if (m_pFirstObject) { - LastObject()->Next(new nsMemCacheObject(i_pObject)); + LastObject()->Next(new nsMemCacheObject(io_pObject)); } else { - m_pFirstObject = new nsMemCacheObject(i_pObject); + m_pFirstObject = new nsMemCacheObject(io_pObject); } m_Entries++; - return PR_TRUE; + + io_pObject->Module(nsCacheManager::MEM); + + return PR_TRUE; } return PR_FALSE; } @@ -174,4 +178,9 @@ nsHashKey* nsMemModule::nsMemKey::Clone() { return new nsMemModule::nsMemKey(); } -*/ + +nsMemModule::nsMemKey::neMemKey() +{ +} + + */ \ No newline at end of file