NCBI C++ ToolKit
vdbcache.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: vdbcache.cpp 100838 2023-09-18 17:14:05Z vasilche $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Eugene Vasilchenko
27  *
28  * File Description:
29  * Access to SRA files
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
35 
36 
39 
40 
42  unsigned force_reopen_seconds,
43  unsigned rechech_seconds)
44  : m_CacheMap(size_limit),
45  m_ForceReopenSeconds(force_reopen_seconds),
46  m_RecheckSeconds(rechech_seconds)
47 {
48 }
49 
50 
52 {
53 }
54 
55 
57 {
58  CMutexGuard guard(GetCacheMutex());
60 }
61 
62 
64 {
65  m_ForceReopenSeconds = seconds;
66 }
67 
68 
70 {
71  m_RecheckSeconds = seconds;
72 }
73 
74 
76 {
77 }
78 
79 
81 {
82 }
83 
84 
86  const string& acc_or_path) const
87 {
88  return !m_ExpirationInfo || m_ExpirationInfo->IsExpired(cache, acc_or_path);
89 }
90 
91 
93  const string& acc_or_path)
94 {
95  m_ExpirationInfo = new CExpirationInfo(cache, acc_or_path);
96 }
97 
98 
100  const string& acc_or_path)
101  : m_ForceReopenDeadline(cache.m_ForceReopenSeconds),
102  m_RecheckDeadline(cache.m_RecheckSeconds),
103  m_DereferencedPath(DereferncePath(cache.m_Mgr, acc_or_path)),
104  m_Timestamp(GetTimestamp(cache.m_Mgr, m_DereferencedPath))
105 {
106  //LOG_POST("CVDBCacheWithExpiration: "<<acc_or_path<<" info: "
107  // << m_DereferencedPath << ", " << m_Timestamp);
108 }
109 
110 
112 {
113 }
114 
115 
117  const string& acc_or_path)
118 {
119  string path;
120  try {
121  path = mgr.FindDereferencedAccPath(acc_or_path);
122  }
123  catch ( CSraException& exc ) {
124  if ( exc.GetErrCode() == exc.eNotFoundDb ||
125  exc.GetErrCode() == exc.eProtectedDb ) {
126  // no such SNP NA accession, return empty string
127  return string();
128  }
129  }
130  catch ( exception& /*ignored*/ ) {
131  }
132  // in case of error assume original accession or path
133  return path.empty()? acc_or_path: path;
134 }
135 
136 
138  const string& path)
139 {
140  if ( !path.empty() ) {
141  try {
142  return mgr.GetTimestamp(path);
143  }
144  catch ( exception& /*ignored*/ ) {
145  }
146  }
147  // in case of error return empty timestamp
148  return CTime();
149 }
150 
151 
153  const string& acc_or_path) const
154 {
155  if ( m_ForceReopenDeadline.IsExpired() ) {
156  // forced expiration
157  //LOG_POST("CVDBCacheWithExpiration: force reopen of "<<acc_or_path);
158  return true;
159  }
160  if ( !m_RecheckDeadline.IsExpired() ) {
161  // not re-checking yet
162  return false;
163  }
164  // check if the file has changed
165  string new_path = DereferncePath(cache.m_Mgr, acc_or_path);
166  if ( new_path != m_DereferencedPath ) {
167  // actual file path has changed
168  //LOG_POST("CVDBCacheWithExpiration: path of "<<acc_or_path<<" has changed: "
169  // << m_DereferencedPath << " -> " << new_path);
170  return true;
171  }
172  CTime new_timestamp = GetTimestamp(cache.m_Mgr, new_path);
173  if ( new_timestamp != m_Timestamp ) {
174  // file timestamp has changed
175  //LOG_POST("CVDBCacheWithExpiration: timestamp of "<<acc_or_path<<" has changed: "
176  // << m_Timestamp << " -> " << new_timestamp);
177  return true;
178  }
179  // everything is the same, set deadline for the next re-check
180  //LOG_POST("CVDBCacheWithExpiration: "<<acc_or_path<<" is the same: "
181  // << m_DereferencedPath << ", " << m_Timestamp);
182  m_RecheckDeadline = CDeadline(cache.m_RecheckSeconds);
183  return false;
184 }
185 
186 
188 {
189  CMutexGuard guard(GetCacheMutex());
190  CRef<CSlot>& slot = m_CacheMap[acc_or_path];
191  if ( !slot ) {
192  slot = new CSlot();
193  }
194  return slot;
195 }
196 
197 
CDeadline.
Definition: ncbitime.hpp:1830
CRef –.
Definition: ncbiobj.hpp:618
@ eProtectedDb
DB is protected.
Definition: exception.hpp:98
@ eNotFoundDb
DB main file not found.
Definition: exception.hpp:92
virtual TErrCode GetErrCode(void) const
Definition: sraread.cpp:163
CTime –.
Definition: ncbitime.hpp:296
static CTime GetTimestamp(const CVDBMgr &mgr, const string &path)
Definition: vdbcache.cpp:137
CExpirationInfo(const CVDBCacheWithExpiration &cache, const string &acc_or_path)
Definition: vdbcache.cpp:99
bool IsExpired(const CVDBCacheWithExpiration &cache, const string &acc_or_path) const
Definition: vdbcache.cpp:152
static string DereferncePath(const CVDBMgr &mgr, const string &acc_or_path)
Definition: vdbcache.cpp:116
void UpdateExpiration(const CVDBCacheWithExpiration &cache, const string &acc_or_path)
Definition: vdbcache.cpp:92
virtual ~CSlot() override
Definition: vdbcache.cpp:80
bool IsExpired(const CVDBCacheWithExpiration &cache, const string &acc_or_path) const
Definition: vdbcache.cpp:85
CMutex & GetCacheMutex()
Definition: vdbcache.hpp:141
virtual ~CVDBCacheWithExpiration()
Definition: vdbcache.cpp:51
unsigned m_ForceReopenSeconds
Definition: vdbcache.hpp:160
void SetForceReopenSeconds(unsigned seconds)
Definition: vdbcache.cpp:63
void set_size_limit(size_t limit)
Definition: vdbcache.cpp:56
friend class CExpirationInfo
Definition: vdbcache.hpp:147
CVDBCacheWithExpiration(size_t size_limit, unsigned force_reopen_seconds=CDeadline::eInfinite, unsigned rechech_seconds=CDeadline::eInfinite)
Definition: vdbcache.cpp:41
void SetRecheckSeconds(unsigned seconds)
Definition: vdbcache.cpp:69
CRef< CSlot > GetSlot(const string &acc_or_path)
Definition: vdbcache.cpp:187
string FindDereferencedAccPath(const string &acc_or_path) const
Definition: vdbread.cpp:527
CTime GetTimestamp(const string &path) const
Definition: vdbread.cpp:557
string
Definition: cgiapp.hpp:687
void set_size_limit(size_t size_limit)
BEGIN_NAMESPACE(objects)
END_NCBI_NAMESPACE
Definition: vdbcache.cpp:199
END_NAMESPACE(objects)
BEGIN_NCBI_NAMESPACE
Definition: vdbcache.cpp:37
Modified on Sat Apr 20 12:19:36 2024 by modify_doxy.py rev. 669887