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

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_SEQ_GRAPHIC___GRAPH_CACHE__HPP
2 #define GUI_WIDGETS_SEQ_GRAPHIC___GRAPH_CACHE__HPP
3 
4 /*
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: Andrei Shkeda
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/obj_pool.hpp>
36 #include <util/cache/icache.hpp>
38 #include <corelib/ncbidiag.hpp>
39 #include <corelib/request_ctx.hpp>
40 
41 #include <thread>
42 #include <atomic>
43 #include <mutex>
44 #include <queue>
45 #include <condition_variable>
46 
48 
49 ///< Storage factory provides ICache instance
50 ///< for permanent storage of cached data
52 {
53 public:
54  CGraphCacheFactory(const string& param)
55  : m_Param(param)
56  {
57  }
60  string nc_service = reg.GetString(m_Param, "service", "NC_SV_GRAPH_DEV");
61  string cache_name = reg.GetString(m_Param, "cache", "graph");
62  string client = reg.GetString(m_Param, "client", "sviewer");
63  return new CNetICacheClient(nc_service, cache_name, client);
64  }
65 
66  void DeleteObject(ICache* obj) {
67  }
68 private:
69  string m_Param;
70 };
71 
73 
74 /////////////////////////////////////////////////////////////////////////////
75 ///
76 /// CGraphCache --
77 ///
78 /// in-memeory cache for graph data
79 /// Provides concurrent access and asynchroneous storage operation for TData
80 ///
81 /// TData is expected to provide:
82 /// Init() - method to be called to initialize data
83 /// Save() - method to serialize data
84 ///
85 /// ICache storage is not used by default
86 /// the caller needs to invoke EnableICache() to enable it
87 ///
88 /// if ICache is enabled GraphCache start a backround thread (m_Thread) on first save request
89 /// The consecutive save request sends a copy of data to m_Queue for asynchroneous Save.
90 
91 
92 template<class TData>
93 class CGraphCache : public CObject
94 {
95 public:
96 
98  {
99  static CSafeStatic<CGraphCache> s_Cache;
100  return s_Cache.Get();
101  }
102 
103  virtual ~CGraphCache() {
104  try {
105  Stop();
106  } catch (exception& e) {
107  ERR_POST(Error << e.what());
108  }
109  }
110 
111  void Stop() {
112  if (m_Thread.joinable()) {
113  m_StopRequested = true;
114  m_CV.notify_all();
115  m_Thread.join();
116  }
117  }
118 
119  /// Retrieves TData from in-memory cache
120  /// @param data_key
121  /// memory access key generated by TData
122  /// @return
123  /// instance of TData or NULL if there are no cached data
124  CRef<TData> GetCachedData(const string& data_key)
125  {
127  {
128  lock_guard<mutex> lock(m_Lock);
129  if (m_Cache.count(data_key) != 0)
130  data = m_Cache[data_key];
131  }
132  return data;
133  }
134 
135  /// Retrieves TData from in-memory cache or if not found calls TData::Init to initialize new TData instance
136  /// @param data_key
137  /// memory access key generated by TData
138  /// @return
139  /// instance of TData or NULL if there are no cached data and TData does not have ICache
140  CRef<TData> GetData(const string& data_key)
141  {
143  {
144  lock_guard<mutex> lock(m_Lock);
145  if (m_Cache.count(data_key) > 0)
146  data = m_Cache[data_key];
147  else {
148  data = Ref(new TData(data_key));
149  m_Cache[data_key] = data;
150  }
151  }
152  if (m_HasICache)
153  data->Init(m_Storage);
154  return data;
155  }
156 
157 
158  /// Removes Data from in-memory cache
159  /// @param data_key
160  /// memory access key generated by TData
161  void RemoveData(const string& data_key)
162  {
163  lock_guard<mutex> lock(m_Lock);
164  auto it = m_Cache.find(data_key);
165  if (it != m_Cache.end()) {
166  m_Cache.erase(it);
167  }
168  }
169 
170  /// Clones TData, puts it into Save Queue for asynchroneous storage operation
171  /// @param data
172  /// Instance of TData
174  {
175  if (!data)
176  return;
177  if (!m_HasICache)
178  return;
179 
180  {
181  if (!m_Thread.joinable()) {
183  // start worker thread
184  m_Thread = thread(&CGraphCache::x_Run, this, context);
185  }
186  lock_guard<mutex> lock(m_Lock);
187  m_Queue.emplace(new TData(*data));
188  }
189  m_CV.notify_all();
190  }
191 
192  void EnableICache(bool value) {
193  m_HasICache = value;
194  }
195 private:
197  m_Storage(CGraphCacheFactory(TData::GetStorageName()))
198  {
199  }
200  friend class CSafeStatic_Allocator<CGraphCache>;
201  CGraphCache(const CGraphCache&) = delete;
202  CGraphCache& operator=(const CGraphCache&) = delete;
203 
204 private:
206  if (!context)
208  while (true) {
209  {
210  unique_lock<mutex> lock(m_Lock);
211  m_CV.wait(lock, [&] { return m_StopRequested || !m_Queue.empty();});
212  }
213  if (m_StopRequested)
214  break;
216  {
217  lock_guard<mutex> lock(m_Lock);
218  data = m_Queue.front();
219  m_Queue.pop();
220 
221  }
222  data->Save(m_Storage);
223  }
224  }
225 
226  mutex m_Lock; ///< Saving Queue and Cache lock
227 
228  queue<CRef<TData>> m_Queue; ///< Saving Queue for storage operations
229 
230  bool m_HasICache = false; ///< Flag indicates if data should be saved into ICache
231 
232  using TDataKey = string;
233  map<TDataKey, CRef<TData>> m_Cache; ///< in-memory data cache
234 
235  TGraphCachePool m_Storage; ///< ICache pool
236 
237  condition_variable m_CV; ///< wake-up signal for save requests
238  thread m_Thread; ///< background thread for save processing
239  atomic<bool> m_StopRequested{false}; ///< flag to stop the backround thread
240 
241 };
242 
243 
244 
245 
247 
248 #endif // GUI_WIDGETS_SEQ_GRAPHIC___GRAPH_CACHE__HPP
< Storage factory provides ICache instance for permanent storage of cached data
Definition: graph_cache.hpp:52
CGraphCacheFactory(const string &param)
Definition: graph_cache.hpp:54
void DeleteObject(ICache *obj)
Definition: graph_cache.hpp:66
ICache * CreateObject(void)
Definition: graph_cache.hpp:58
CGraphCache –.
Definition: graph_cache.hpp:94
void SaveData(CRef< TData > data)
Clones TData, puts it into Save Queue for asynchroneous storage operation.
string TDataKey
void RemoveData(const string &data_key)
Removes Data from in-memory cache.
mutex m_Lock
Saving Queue and Cache lock.
queue< CRef< TData > > m_Queue
Saving Queue for storage operations.
TGraphCachePool m_Storage
ICache pool.
void EnableICache(bool value)
thread m_Thread
background thread for save processing
map< TDataKey, CRef< TData > > m_Cache
in-memory data cache
virtual ~CGraphCache()
CRef< TData > GetData(const string &data_key)
Retrieves TData from in-memory cache or if not found calls TData::Init to initialize new TData instan...
void x_Run(CRef< CRequestContext > context)
CGraphCache(const CGraphCache &)=delete
condition_variable m_CV
wake-up signal for save requests
static CGraphCache & GetInstance()
Definition: graph_cache.hpp:97
atomic< bool > m_StopRequested
flag to stop the backround thread
CRef< TData > GetCachedData(const string &data_key)
Retrieves TData from in-memory cache.
bool m_HasICache
Flag indicates if data should be saved into ICache.
CGraphCache & operator=(const CGraphCache &)=delete
static CNcbiApplication * Instance(void)
Singleton method.
Definition: ncbiapp.cpp:264
CNcbiRegistry –.
Definition: ncbireg.hpp:913
Client to NetCache server (implements ICache interface)
CObject –.
Definition: ncbiobj.hpp:180
Helper class for object allocation/deallocation.
CSafeStatic<>::
T & Get(void)
Create the variable if not created yet, return the reference.
BLOB cache read/write/maintenance interface.
Definition: icache.hpp:64
void erase(iterator pos)
Definition: map.hpp:167
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
char data[12]
Definition: iconv.c:80
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
string
Definition: cgiapp.hpp:690
static void SetRequestContext(CRequestContext *ctx)
Shortcut to CDiagContextThreadData::GetThreadData().SetRequestContext()
Definition: ncbidiag.cpp:1907
static CRequestContext & GetRequestContext(void)
Shortcut to CDiagContextThreadData::GetThreadData().GetRequestContext()
Definition: ncbidiag.cpp:1901
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
void Error(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1197
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
virtual string GetString(const string &section, const string &name, const string &default_value, TFlags flags=0) const
Get the parameter string value.
Definition: ncbireg.cpp:321
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
Interfaces for a local cache of versioned binary large objects (BLOBS).
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Defines NCBI C++ diagnostic APIs, classes, and macros.
NetCache ICache client specs.
Defines CRequestContext class for NCBI C++ diagnostic API.
static CNamedPipeClient * client
static CS_CONTEXT * context
Definition: will_convert.c:21
Modified on Fri Sep 20 14:57:36 2024 by modify_doxy.py rev. 669887