bdb_blobcache.cpp

来自「ncbi源码」· C++ 代码 · 共 1,387 行 · 第 1/3 页

CPP
1,387
字号
    m_CacheAttrDB->key = key;    m_CacheAttrDB->version = version;    m_CacheAttrDB->subkey = (m_TimeStampFlag & fTrackSubKey) ? subkey : "";    EBDB_ErrCode ret = m_CacheAttrDB->Fetch();    if (ret != eBDB_Ok) {        return 0;    }        return (int) m_CacheAttrDB->time_stamp;}void CBDB_Cache::Purge(time_t           access_timeout,                       EKeepVersions    keep_last_version){    CFastMutexGuard guard(x_BDB_BLOB_CacheMutex);    if (keep_last_version == eDropAll && access_timeout == 0) {        x_TruncateDB();        return;    }    // Search the database for obsolete cache entries    vector<SCacheDescr> cache_entries;    {{    CBDB_FileCursor cur(*m_CacheAttrDB);    cur.SetCondition(CBDB_FileCursor::eFirst);    CTime time_stamp(CTime::eCurrent);    time_t curr = (int)time_stamp.GetTimeT();    int timeout = GetTimeout();    while (cur.Fetch() == eBDB_Ok) {        time_t db_time_stamp = m_CacheAttrDB->time_stamp;        int version = m_CacheAttrDB->version;        const char* key = m_CacheAttrDB->key;        int overflow = m_CacheAttrDB->overflow;        const char* subkey = m_CacheAttrDB->subkey;        if (curr - timeout > db_time_stamp) {            cache_entries.push_back(                            SCacheDescr(key, version, subkey, overflow));        }    } // while    }}    CBDB_Transaction trans(*m_Env);    m_CacheDB->SetTransaction(&trans);    m_CacheAttrDB->SetTransaction(&trans);    ITERATE(vector<SCacheDescr>, it, cache_entries) {        x_DropBlob(it->key.c_str(),                    it->version,                    it->subkey.c_str(),                    it->overflow);    }    trans.Commit();}void CBDB_Cache::Purge(const string&    key,                       const string&    subkey,                       time_t           access_timeout,                       EKeepVersions    keep_last_version){    CFastMutexGuard guard(x_BDB_BLOB_CacheMutex);    if (key.empty() ||         (keep_last_version == eDropAll && access_timeout == 0)) {        x_TruncateDB();        return;    }    // Search the database for obsolete cache entries    vector<SCacheDescr> cache_entries;    {{    CBDB_FileCursor cur(*m_CacheAttrDB);    cur.SetCondition(CBDB_FileCursor::eEQ);    cur.From << key;    CTime time_stamp(CTime::eCurrent);    time_t curr = (int)time_stamp.GetTimeT();    int timeout = GetTimeout();    while (cur.Fetch() == eBDB_Ok) {        time_t db_time_stamp = m_CacheAttrDB->time_stamp;        int version = m_CacheAttrDB->version;        const char* x_key = m_CacheAttrDB->key;        int overflow = m_CacheAttrDB->overflow;        string x_subkey = (const char*) m_CacheAttrDB->subkey;        if (subkey.empty()) {                    }        if ( (curr - timeout > db_time_stamp) ||            (subkey.empty() || (subkey == x_subkey))            ) {            cache_entries.push_back(                            SCacheDescr(x_key, version, x_subkey, overflow));        }    } // while    }}    CBDB_Transaction trans(*m_Env);    m_CacheDB->SetTransaction(&trans);    m_CacheAttrDB->SetTransaction(&trans);    ITERATE(vector<SCacheDescr>, it, cache_entries) {        x_DropBlob(it->key.c_str(),                    it->version,                    it->subkey.c_str(),                    it->overflow);    }        trans.Commit();}bool CBDB_Cache::x_CheckTimestampExpired(){    int timeout = GetTimeout();    int db_time_stamp = m_CacheAttrDB->time_stamp;    if (timeout) {        CTime time_stamp(CTime::eCurrent);        time_t curr = (int)time_stamp.GetTimeT();        if (curr - timeout > db_time_stamp) {            _TRACE("local cache item expired:"                    << db_time_stamp << " curr=" << curr                    << " diff=" << curr - db_time_stamp);            return true;        }    }    return false;}void CBDB_Cache::x_UpdateAccessTime(const string&  key,                                    int            version,                                    const string&  subkey){    CBDB_Transaction trans(*m_Env);    m_CacheDB->SetTransaction(&trans);    m_CacheAttrDB->SetTransaction(&trans);    m_CacheAttrDB->key = key;    m_CacheAttrDB->version = version;    m_CacheAttrDB->subkey = (m_TimeStampFlag & fTrackSubKey) ? subkey : "";    EBDB_ErrCode ret =m_CacheAttrDB->Fetch();    if (ret != eBDB_Ok) {        m_CacheAttrDB->overflow = 0;    }    CTime time_stamp(CTime::eCurrent);    m_CacheAttrDB->time_stamp = (unsigned)time_stamp.GetTimeT();    m_CacheAttrDB->UpdateInsert();    trans.Commit();}void CBDB_Cache::x_TruncateDB(){    LOG_POST(Info << "CBDB_BLOB_Cache:: cache database truncated");    m_CacheDB->Truncate();    m_CacheAttrDB->Truncate();    // Scan the directory, delete overflow BLOBs    // TODO: remove overflow files only matching cache specific name    //       signatures. Since several caches may live in the same    //       directory we may delete some "extra" files    CDir dir(m_Path);    string ext;    string ov_("ov_");    if (dir.Exists()) {        CDir::TEntries  content(dir.GetEntries());        ITERATE(CDir::TEntries, it, content) {            if (!(*it)->IsFile()) {                if (ext == ov_) {                    (*it)->Remove();                }            }        }    }}void CBDB_Cache::x_DropBlob(const char*    key,                            int            version,                            const char*    subkey,                            int            overflow){    _ASSERT(key);    _ASSERT(subkey);    if (overflow == 1) {        string path;        s_MakeOverflowFileName(path, m_Path, key, version, subkey);        CDirEntry entry(path);        if (entry.Exists()) {            entry.Remove();        }    }    m_CacheDB->key = key;    m_CacheDB->version = version;    m_CacheDB->subkey = subkey;    m_CacheDB->Delete(CBDB_RawFile::eIgnoreError);    m_CacheAttrDB->key = key;    m_CacheAttrDB->version = version;    m_CacheAttrDB->subkey = subkey;    m_CacheAttrDB->Delete(CBDB_RawFile::eIgnoreError);}const char* kBDBCacheDriverName = "bdbcache";/// Class factory for BDB implementation of ICache////// @internal///class CBDB_CacheReaderCF :     public CSimpleClassFactoryImpl<ICache, CBDB_Cache>{public:    typedef       CSimpleClassFactoryImpl<ICache, CBDB_Cache> TParent;public:    CBDB_CacheReaderCF() : TParent(kBDBCacheDriverName, 0)    {    }    ~CBDB_CacheReaderCF()    {    }};void NCBI_BDB_ICacheEntryPoint(     CPluginManager<ICache>::TDriverInfoList&   info_list,     CPluginManager<ICache>::EEntryPointRequest method){    CHostEntryPointImpl<CBDB_CacheReaderCF>::       NCBI_EntryPointImpl(info_list, method);}CBDB_CacheHolder::CBDB_CacheHolder(ICache* blob_cache, ICache* id_cache) : m_BlobCache(blob_cache),  m_IdCache(id_cache){}CBDB_CacheHolder::~CBDB_CacheHolder(){    delete m_BlobCache;    delete m_IdCache;}END_NCBI_SCOPE/* * =========================================================================== * $Log: bdb_blobcache.cpp,v $ * Revision 1000.5  2004/06/01 18:37:14  gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.54 * * Revision 1.54  2004/05/25 18:43:51  kuznets * Fixed bug in setting cache RAM size, added additional protection when joining * existing environment. * * Revision 1.53  2004/05/24 18:03:03  kuznets * CBDB_Cache::Open added parameter to specify RAM cache size * * Revision 1.52  2004/05/17 20:55:11  gorelenk * Added include of PCH ncbi_pch.hpp * * Revision 1.51  2004/04/28 16:58:56  kuznets * Fixed deadlock in CBDB_Cache::Remove * * Revision 1.50  2004/04/28 12:21:32  kuznets * Cleaned up dead code * * Revision 1.49  2004/04/28 12:11:22  kuznets * Replaced static string with char* (fix crash on Linux) * * Revision 1.48  2004/04/27 19:12:01  kuznets * Commented old cache implementation * * Revision 1.47  2004/03/26 14:54:21  kuznets * Transaction checkpoint after database creation * * Revision 1.46  2004/03/26 14:05:39  kuznets * Force transaction checkpoints and turn-off buffering * * Revision 1.45  2004/03/24 13:51:03  friedman * Fixed mutex comments * * Revision 1.44  2004/03/23 19:22:08  friedman * Replaced 'static CFastMutex' with DEFINE_STATIC_FAST_MUTEX * * Revision 1.43  2004/02/27 17:29:50  kuznets * +CBDB_CacheHolder * * Revision 1.42  2004/02/06 16:16:40  vasilche * Fixed delete m_Buffer -> delete[] m_Buffer. * * Revision 1.41  2004/02/02 21:24:29  vasilche * Fixed buffering of overflow file streams - open file in constructor. * Fixed processing of timestamps when fTimeStampOnRead flag is not set. * * Revision 1.40  2004/01/29 20:31:06  vasilche * Removed debug messages. * * Revision 1.39  2004/01/07 18:58:10  vasilche * Make message about joining to non-transactional environment a warning. * * Revision 1.38  2003/12/30 16:29:12  kuznets * Fixed a bug in overflow file naming. * * Revision 1.37  2003/12/29 18:47:20  kuznets * Cache opening changed to use free threaded environment. * * Revision 1.36  2003/12/29 17:08:15  kuznets * CBDB_CacheIWriter - using transactions to save BDB data * * Revision 1.35  2003/12/29 16:53:25  kuznets * Made Flush transactional * * Revision 1.34  2003/12/29 15:39:59  vasilche * Fixed subkey value in GetReadStream/GetWriteStream. * * Revision 1.33  2003/12/29 12:57:15  kuznets * Changes in Purge() method to make cursor loop lock independent * from the record delete function * * Revision 1.32  2003/12/16 13:45:11  kuznets * ICache implementation made transaction protected * * Revision 1.31  2003/12/08 16:13:15  kuznets * Added plugin mananger support * * Revision 1.30  2003/11/28 17:35:05  vasilche * Fixed new[]/delete discrepancy. * * Revision 1.29  2003/11/26 13:09:16  kuznets * Fixed bug in mutex locking * * Revision 1.28  2003/11/25 19:36:35  kuznets * + ICache implementation * * Revision 1.27  2003/11/06 14:20:38  kuznets * Warnings cleaned up * * Revision 1.26  2003/10/24 13:54:03  vasilche * Rolled back incorrect fix of PendingCount(). * * Revision 1.25  2003/10/24 13:41:23  kuznets * Completed PendingCount implementaion * * Revision 1.24  2003/10/24 12:37:42  kuznets * Implemented cache locking using PID guard * * Revision 1.23  2003/10/23 13:46:38  vasilche * Implemented PendingCount() method. * * Revision 1.22  2003/10/22 19:08:29  vasilche * Added Flush() implementation. * * Revision 1.21  2003/10/21 12:11:27  kuznets * Fixed non-updated timestamp in Int cache. * * Revision 1.20  2003/10/20 20:44:20  vasilche * Added return true for overflow file read. * * Revision 1.19  2003/10/20 20:41:37  kuznets * Fixed bug in BlobCache::Read * * Revision 1.18  2003/10/20 20:35:33  kuznets * Blob cache Purge improved. * * Revision 1.17  2003/10/20 20:34:03  kuznets * Fixed bug with writing BLOB overflow attribute * * Revision 1.16  2003/10/20 20:15:30  kuznets * Fixed bug with expiration time retrieval * * Revision 1.15  2003/10/20 19:58:26  kuznets * Fixed bug in int cache expiration algorithm * * Revision 1.14  2003/10/20 17:53:03  kuznets * Dismissed blob cache entry overwrite protection. * * Revision 1.13  2003/10/20 16:34:20  kuznets * BLOB cache Store operation reimplemented to use external files. * BDB cache shared between tables by using common environment. * Overflow file limit set to 1M (was 2M) * * Revision 1.12  2003/10/17 14:11:41  kuznets * Implemented cached read from berkeley db BLOBs * * Revision 1.11  2003/10/16 19:29:18  kuznets * Added Int cache (AKA id resolution cache) * * Revision 1.10  2003/10/16 12:08:16  ucko * Address GCC 2.95 errors about missing sprintf declaration, and avoid * possible buffer overflows, by rewriting s_MakeOverflowFileName to use * C++ strings. * * Revision 1.9  2003/10/16 00:30:57  ucko * ios_base -> IOS_BASE (should fix GCC 2.95 build) * * Revision 1.8  2003/10/15 18:39:13  kuznets * Fixed minor incompatibility with the C++ language. * * Revision 1.7  2003/10/15 18:13:16  kuznets * Implemented new cache architecture based on combination of BDB tables * and plain files. Fixes the performance degradation in Berkeley DB * when it has to work with a lot of overflow pages. * * Revision 1.6  2003/10/06 16:24:19  kuznets * Fixed bug in Purge function * (truncated cache files with some parameters combination). * * Revision 1.5  2003/10/02 20:13:25  kuznets * Minor code cleanup * * Revision 1.4  2003/09/29 16:26:34  kuznets * Reflected ERW_Result rename + cleaned up 64-bit compilation * * Revision 1.3  2003/09/29 15:45:17  kuznets * Minor warning fixed * * Revision 1.2  2003/09/24 15:59:45  kuznets * Reflected changes in IReader/IWriter <util/reader_writer.hpp> * * Revision 1.1  2003/09/24 14:30:17  kuznets * Initial revision * * * =========================================================================== */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?