NCBI C++ ToolKit
exclude_blob_cache.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef EXCLUDE_BLOB_CACHE__HPP
2 #define EXCLUDE_BLOB_CACHE__HPP
3 
4 /* $Id: exclude_blob_cache.hpp 97836 2022-08-29 14:58:43Z satskyse $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergey Satskiy
30  *
31  * File Description:
32  *
33  */
34 
35 
36 #include <string>
37 #include <set>
38 #include <list>
39 #include <atomic>
40 #include <mutex>
41 #include <map>
42 #include <vector>
43 using namespace std;
44 
45 #include "pubseq_gateway_types.hpp"
46 
47 
51 };
52 
53 
55 {
56  int m_Sat;
57  int m_SatKey;
60 
61  SExcludeBlobId(int sat, int sat_key):
62  m_Sat(sat), m_SatKey(sat_key), m_Completed(false)
63  {}
64 
65  bool operator < (const SExcludeBlobId & other) const
66  {
67  if (m_Sat == other.m_Sat)
68  return m_SatKey < other.m_SatKey;
69  return m_Sat < other.m_Sat;
70  }
71 
72  bool operator == (const SExcludeBlobId & other) const
73  {
74  return m_Sat == other.m_Sat && m_SatKey == other.m_SatKey;
75  }
76 };
77 
78 
79 
81 {
82  public:
84  m_LastTouch(psg_clock_t::now())
85  {}
86 
88  {}
89 
90  public:
91  // The 'completed' and 'completed_time' values are filled
92  // only if the blob is in the cache
93  bool IsInCache(int sat, int sat_key,
94  bool & completed,
95  psg_time_point_t & completed_time);
96  EPSGS_CacheAddResult AddBlobId(int sat, int sat_key,
97  bool & completed,
98  psg_time_point_t & completed_time);
99 
100  // Return true if the required blob id was found
101  bool SetCompleted(int sat, int sat_key, bool new_val);
102  bool Remove(int sat, int sat_key);
103 
104  void Purge(size_t purged_size);
105  void Clear(void);
106 
107  public:
108  // The lock is exposed so that the upper level can grab it before
109  // the upper lock is released. An alternative would be to store the
110  // lock in the upper level but it seems better to store it here because
111  // it goes into the cathegory of the user associated data
112  mutex m_Lock;
114 
115  private:
116  set<SExcludeBlobId> m_ExcludeBlobs;
117  list<SExcludeBlobId> m_LRU;
118 };
119 
120 
122 {
123  public:
125  m_Head(nullptr)
126  {}
127 
129  {
130  SNode * current = m_Head.load();
131  while (current != nullptr) {
132  delete current->m_Data;
133  auto next_item = current->m_Next;
134  delete current;
135  current = next_item;
136  }
137  }
138 
139  // Non copyable
143 
144  public:
146  {
147  SNode * item = m_Head.load();
148  if (item == nullptr)
149  return new CUserExcludeBlobs();
150 
151  m_Lock.lock();
152 
153  item = m_Head.load();
154  auto object = item->m_Data;
155  m_Head = item->m_Next;
156 
157  m_Lock.unlock();
158 
159  delete item;
160  return object;
161  }
162 
163  void Return(CUserExcludeBlobs * user_exclude_blobs)
164  {
165  SNode * returned_node = new SNode(user_exclude_blobs);
166  lock_guard<mutex> guard(m_Lock);
167 
168  returned_node->m_Next = m_Head.load();
169  m_Head = returned_node;
170  }
171 
172  private:
173  struct SNode {
176 
177  SNode(CUserExcludeBlobs * exclude_blobs) :
178  m_Next(nullptr), m_Data(exclude_blobs)
179  {}
180  };
181 
182  atomic<SNode *> m_Head;
183  mutex m_Lock;
184 };
185 
186 
187 
189 {
190  public:
191  CExcludeBlobCache(size_t inactivity_timeout,
192  size_t max_cache_size, size_t purged_size) :
193  m_InactivityTimeout(inactivity_timeout),
194  m_MaxCacheSize(max_cache_size), m_PurgedSize(purged_size)
195  {
196  m_ToPurge.reserve(128); // arbitrary reservation
197  m_ToDiscard.reserve(128); // arbitrary reservation
198  }
199 
201  {
202  for (auto & item : m_UserBlobs)
203  delete item.second;
204  }
205 
206  public:
207  // The 'completed' and 'completed_time' values are filled only
208  // if the blob is in the cache
209  EPSGS_CacheAddResult AddBlobId(const string & user,
210  int sat, int sat_key,
211  bool & completed,
212  psg_time_point_t & completed_time);
213  bool IsInCache(const string & user,
214  int sat, int sat_key,
215  bool & completed,
216  psg_time_point_t & completed_time);
217 
218  // Return true if the required blob id was found
219  bool SetCompleted(const string & user,
220  int sat, int sat_key, bool new_val);
221  bool Remove(const string & user,
222  int sat, int sat_key);
223 
224  void Purge(void);
225 
226  size_t Size(void) {
227  size_t size = 0;
228  lock_guard<mutex> guard(m_Lock);
229 
230  size = m_UserBlobs.size();
231  return size;
232  }
233 
234  private:
235  map<string, CUserExcludeBlobs *> m_UserBlobs;
236  mutex m_Lock;
238 
239  chrono::seconds m_InactivityTimeout;
241  size_t m_PurgedSize;
242 
243  vector<CUserExcludeBlobs *> m_ToPurge;
244  vector<CUserExcludeBlobs *> m_ToDiscard;
245 };
246 
247 
248 #endif
chrono::seconds m_InactivityTimeout
vector< CUserExcludeBlobs * > m_ToPurge
vector< CUserExcludeBlobs * > m_ToDiscard
map< string, CUserExcludeBlobs * > m_UserBlobs
CExcludeBlobCache(size_t inactivity_timeout, size_t max_cache_size, size_t purged_size)
CUserExcludeBlobsPool m_Pool
CUserExcludeBlobsPool(CUserExcludeBlobsPool const &)=delete
CUserExcludeBlobsPool(CUserExcludeBlobsPool &&)=delete
const CUserExcludeBlobsPool & operator=(const CUserExcludeBlobsPool &)=delete
void Return(CUserExcludeBlobs *user_exclude_blobs)
CUserExcludeBlobs * Get(void)
set< SExcludeBlobId > m_ExcludeBlobs
psg_time_point_t m_LastTouch
list< SExcludeBlobId > m_LRU
bool operator<(const CEquivRange &A, const CEquivRange &B)
bool operator==(const CEquivRange &A, const CEquivRange &B)
EPSGS_CacheAddResult
@ ePSGS_Added
@ ePSGS_AlreadyInCache
#define false
Definition: bool.h:36
const struct ncbi::grid::netcache::search::fields::SIZE size
#define nullptr
Definition: ncbimisc.hpp:45
chrono::steady_clock psg_clock_t
psg_clock_t::time_point psg_time_point_t
SNode(CUserExcludeBlobs *exclude_blobs)
psg_time_point_t m_CompletedTime
SExcludeBlobId(int sat, int sat_key)
Modified on Sun Apr 21 03:44:13 2024 by modify_doxy.py rev. 669887