HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_Cache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_Cache.h
7  *
8  * COMMENTS:
9  *
10  *
11  */
12 
13 #ifndef __NET_CACHE_H__
14 #define __NET_CACHE_H__
15 
16 #include "NET_API.h"
17 
18 #include <UT/UT_ErrorCode.h>
19 #include <UT/UT_Function.h>
20 #include <UT/UT_Map.h>
21 #include <UT/UT_NonCopyable.h>
22 #include <UT/UT_SQLORM.h>
23 #include <UT/UT_StringArray.h>
24 #include <UT/UT_StringHolder.h>
25 #include <UT/UT_WorkBuffer.h>
26 
27 #include <SYS/SYS_Types.h>
28 
30 {
31 public:
34 
35  virtual ~NET_ICache() = default;
37 
39  {
40  if (timeout == -1)
41  timeout = myDefaultTimeout;
42  else if (timeout == 0)
43  timeout = -1;
44  return time(nullptr) + timeout;
45  }
47  const UT_StringHolder& key,
48  int version = -1)
49  {
50  if (version == -1)
51  version = myDefaultVersion;
52 
53  return generateKey(myKeyPrefix, key, version);
54  }
55  virtual bool add(
56  const UT_StringHolder& key,
57  int value,
58  UT_ErrorCode* ec = nullptr,
59  int64 timeout = -1,
60  int version = -1)
61  {
62  UT_StringHolder vstr;
63  vstr.format("{}", value);
64  return add(key, vstr, ec, timeout, version);
65  }
66  virtual bool add(
67  const UT_StringHolder& key,
68  const UT_StringHolder& value,
69  UT_ErrorCode* ec = nullptr,
70  int64 timeout = -1,
71  int version = -1)
72  = 0;
73  virtual bool get(
74  const UT_StringRef& key,
75  int& value,
76  int def = -1,
77  UT_ErrorCode* ec = nullptr,
78  int version = -1)
79  {
81  if (get(key, v, v, ec, version))
82  {
83  value = v.toInt();
84  return true;
85  }
86  else
87  {
88  value = def;
89  return false;
90  }
91  }
92  virtual bool get(
93  const UT_StringRef& key,
96  UT_ErrorCode* ec = nullptr,
97  int version = -1)
98  = 0;
99  virtual bool set(
100  const UT_StringHolder& key,
101  int value,
102  UT_ErrorCode* ec = nullptr,
103  int64 timeout = -1,
104  int version = -1)
105  {
106  UT_StringHolder vstr;
107  vstr.format("{}", value);
108  return set(key, vstr, ec, timeout, version);
109  }
110  virtual bool set(
111  const UT_StringHolder& key,
112  const UT_StringHolder& value,
113  UT_ErrorCode* ec = nullptr,
114  int64 timeout = -1,
115  int version = -1) = 0;
116  virtual bool touch(
117  const UT_StringRef& key,
118  UT_ErrorCode* ec = nullptr,
119  int timeout = -1,
120  int version = -1)
121  = 0;
122  virtual bool remove(
123  const UT_StringRef& key,
124  UT_ErrorCode* ec = nullptr,
125  int version = -1)
126  = 0;
127  virtual void getMany(
128  const UT_StringArray& keys,
130  UT_ErrorCode* ec = nullptr,
131  int version = -1)
132  {
133  results.clear();
134  for (auto&& key : keys)
135  {
137  if (get(key, value, /*def=*/UT_StringHolder::theEmptyString,
138  nullptr, version))
139  {
140  results[key] = value;
141  }
142  }
143  }
144  virtual void getMany(
145  const UT_StringArray& keys,
147  UT_ErrorCode* ec = nullptr,
148  int version = -1)
149  {
150  results.clear();
151  for (auto&& key : keys)
152  {
153  int value = 0;
154  if (get(key, value, /*def=*/-1, nullptr, version))
155  {
156  results[key] = value;
157  }
158  }
159  }
160  virtual int getOrSet(
161  const UT_StringRef& key,
162  int_def_clb_t&& clb,
163  UT_ErrorCode& ec,
164  int64 timeout = -1,
165  int version=-1)
166  {
167  int value = -1;
168  UT_ErrorCode* tec = &ec;
169  if (!get(key, value, value, tec, version))
170  {
171  int def = clb(ec);
172  if (ec)
173  return def;
174 
175  add(key, def, tec, timeout, version);
176 
177  get(key, def, /*def=*/-1, tec, version);
178  return def;
179  }
180  return value;
181  }
182  virtual int getOrSet(
183  const UT_StringRef& key,
184  int def,
185  UT_ErrorCode* ec = nullptr,
186  int64 timeout = -1,
187  int version = -1)
188  {
189  int value = -1;
190  if (!get(key, value, def, ec, version))
191  {
192  add(key, def, ec, timeout, version);
193 
194  get(key, def, /*def=*/-1, ec, version);
195  return def;
196  }
197  return value;
198  }
200  const UT_StringRef& key,
201  str_def_clb_t&& clb,
202  UT_ErrorCode& ec,
203  int64 timeout = -1,
204  int version=-1)
205  {
207  UT_ErrorCode* tec = &ec;
208  if (!get(key, value, UT_StringHolder::theEmptyString, tec, version))
209  {
210  UT_StringHolder def = clb(ec);
211  if (ec)
212  return def;
213 
214  add(key, def, tec, timeout, version);
215 
216  get(key, def, /*def=*/UT_StringHolder::theEmptyString, tec, version);
217  return def;
218  }
219  return value;
220  }
222  const UT_StringRef& key,
223  const UT_StringHolder& def,
224  UT_ErrorCode* ec = nullptr,
225  int64 timeout = -1,
226  int version = -1)
227  {
229  if (!get(key, value, def, ec, version))
230  {
231  add(key, def, ec, timeout, version);
232 
233  // Retrieve the value from the cache to prevent race conditions
234  get(key, value, /*def=*/UT_StringHolder::theEmptyString, ec,
235  version);
236  return value;
237  }
238  return value;
239  }
240 
241  virtual bool hasKey(
242  const UT_StringRef& key,
243  UT_ErrorCode* ec = nullptr,
244  int version = -1)
245  {
246  int v = 0;
247  return get(key, /*value=*/v, /*def=*/v, ec, version);
248  }
249 
250  virtual bool incr(
251  const UT_StringRef& key,
252  int &value,
253  int delta = 1,
254  UT_ErrorCode* ec = nullptr,
255  int version = -1)
256  {
257  if (get(key, value, value, ec, version))
258  {
259  value = value + delta;
260  return set(key, value, ec, /*timeout=*/-1, version);
261  }
262  return false;
263  }
264  virtual bool decr(
265  const UT_StringRef& key,
266  int &value,
267  int delta = 1,
268  UT_ErrorCode* ec = nullptr,
269  int version = -1)
270  {
271  return incr(key, value, -delta, ec, version);
272  }
273 
276  int64 timeout = -1,
277  int version = -1)
278  {
279  for (auto&& [key, value] : data)
280  set(key, value, nullptr, timeout, version);
281  // NB: Some backends support returning which keys failed to be
282  // inserted.
283  return UT_StringArray{};
284  }
287  int64 timeout = -1,
288  int version = -1)
289  {
290  for (auto&& [key, value] : data)
291  set(key, value, nullptr, timeout, version);
292  // NB: Some backends support returning which keys failed to be
293  // inserted.
294  return UT_StringArray{};
295  }
296  virtual void removeMany(
297  const UT_StringArray& keys,
298  UT_ErrorCode* ec,
299  int version = -1)
300  {
301  for (auto&& key : keys)
302  remove(key, ec, version);
303  }
304  virtual void clear(UT_ErrorCode* ec = nullptr) = 0;
305  virtual int incrVersion(
306  const UT_StringRef& key,
307  int delta = 1,
308  UT_ErrorCode* ec = nullptr,
309  int version = -1)
310  {
311  if (version == -1)
312  version = myDefaultVersion;
313 
314  int value = 0;
315  if (get(key, value, value, ec, version))
316  {
317  if (!set(key, value, ec, /*timeout=*/-1, version + delta))
318  return value;
319  remove(key, ec, version);
320  }
321  else
322  {
323  return version;
324  }
325  return version + delta;
326  }
327  virtual int decrVersion(
328  const UT_StringRef& key,
329  int delta = 1,
330  UT_ErrorCode* ec = nullptr,
331  int version = -1)
332  {
333  return incrVersion(key, -delta, ec, version);
334  }
335 
336  virtual void close(UT_ErrorCode* ec = nullptr) {}
337 
338 protected:
339  NET_ICache() = default;
340 
342  const UT_StringHolder& key_prefix,
343  const UT_StringHolder& key,
344  int version)
345  {
346  UT_WorkBuffer wbuf;
347  wbuf.append(key_prefix);
348  wbuf.append(':');
349  wbuf.append(key);
350  wbuf.append(':');
351  wbuf.appendFormat("{}", version);
352  return wbuf;
353  }
354 
355  int myDefaultTimeout = 300;
356  int myMaxEntries = 300;
357  int myCullFequency = 3;
358 
360  int myDefaultVersion = 1;
361 };
362 
363 
365 {
366 public:
367  NET_DatabaseCache(const UT_StringHolder& table_name);
368  ~NET_DatabaseCache() override;
369 
371 
372  bool open(UT_SqlOrm& orm, UT_ErrorCode& ec);
373 
374  bool add(
375  const UT_StringHolder& key,
376  int value,
377  UT_ErrorCode* ec = nullptr,
378  int64 timeout = -1,
379  int version = -1) override
380  {
381  UT_StringHolder vstr;
382  vstr.format("{}", value);
383  return add(key, vstr, ec, timeout, version);
384  }
385  bool add(
386  const UT_StringHolder& key,
387  const UT_StringHolder& value,
388  UT_ErrorCode* ec = nullptr,
389  int64 timeout = -1,
390  int version = -1) override;
391  bool get(
392  const UT_StringRef& key,
393  int& value,
394  int def = -1,
395  UT_ErrorCode* ec = nullptr,
396  int version = -1) override;
397  bool get(
398  const UT_StringRef& key,
401  UT_ErrorCode* ec = nullptr,
402  int version = -1) override;
403  void getMany(
404  const UT_StringArray& keys,
406  UT_ErrorCode* ec = nullptr,
407  int version = -1) override;
408  void getMany(
409  const UT_StringArray& keys,
411  UT_ErrorCode* ec = nullptr,
412  int version = -1) override;
413  bool set(
414  const UT_StringHolder& key,
415  const UT_StringHolder& value,
416  UT_ErrorCode* ec = nullptr,
417  int64 timeout = -1,
418  int version = -1) override;
419  bool set(
420  const UT_StringHolder& key,
421  int value,
422  UT_ErrorCode* ec = nullptr,
423  int64 timeout = -1,
424  int version = -1) override
425  {
426  UT_StringHolder vstr;
427  vstr.format("{}", value);
428  return set(key, vstr, ec, timeout, version);
429  }
430  bool touch(
431  const UT_StringRef& key,
432  UT_ErrorCode* ec = nullptr,
433  int timeout = -1,
434  int version = -1) override;
435  bool remove(
436  const UT_StringRef& key,
437  UT_ErrorCode* ec = nullptr,
438  int version = -1) override;
440  const UT_StringArray& keys,
441  UT_ErrorCode* ec = nullptr,
442  int version = -1) override
443  {
444  UT_StringArray ks(keys);
445  for (auto&& key : ks)
446  key = makeKey(key, version);
447  UT_ErrorCode oec;
448  doRemoveMany_(keys, oec);
449  if (ec)
450  *ec = oec;
451  }
452  void clear(UT_ErrorCode* ec = nullptr) override;
453  void close(UT_ErrorCode* ec = nullptr) override;
454 
455  bool incr(
456  const UT_StringRef& key,
457  int& value,
458  int delta = 1,
459  UT_ErrorCode* ec = nullptr,
460  int version = -1) override;
461 
462 private:
463  enum DatabaseOp
464  {
465  DB_OP_SET,
466  DB_OP_TOUCH,
467  DB_OP_ADD
468  };
469 
470  bool doSet_(
471  DatabaseOp op,
472  const UT_StringHolder& key,
473  const UT_StringHolder& value,
474  UT_ErrorCode& ec,
475  int64 timeout);
476  void doCull_(UT_SqlStatement& db, time_t now, int count);
477  bool doRemoveMany_(const UT_StringArray& key, UT_ErrorCode& ec);
478  void doGetMany_(
479  const UT_StringArray& keys,
480  UT_Function<void(const UT_StringHolder&, const UT_StringHolder&)>&&
481  result_func,
482  UT_ErrorCode* ec,
483  int version);
484  bool doIncrement_(
485  const UT_StringHolder& key,
487  int delta,
488  UT_ErrorCode& ec);
489 
490  UT_StringHolder myTableName;
491  UT_SqlOrm* myORM = nullptr;
492 };
493 
494 #endif // __NET_CACHE_H__
495 
virtual int64 getBackendTimeout(int64 timeout=-1)
Definition: NET_Cache.h:38
UT_NON_COPYABLE(NET_ICache)
virtual bool touch(const UT_StringRef &key, UT_ErrorCode *ec=nullptr, int timeout=-1, int version=-1)=0
virtual int getOrSet(const UT_StringRef &key, int_def_clb_t &&clb, UT_ErrorCode &ec, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:160
bool set(const UT_StringHolder &key, int value, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1) override
Definition: NET_Cache.h:419
GT_API const UT_StringHolder time
const GLdouble * v
Definition: glcorearb.h:837
void removeMany(const UT_StringArray &keys, UT_ErrorCode *ec=nullptr, int version=-1) override
Definition: NET_Cache.h:439
GLsizei const GLfloat * value
Definition: glcorearb.h:824
virtual UT_StringArray setMany(const UT_Map< UT_StringHolder, UT_StringHolder > &data, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:274
virtual bool hasKey(const UT_StringRef &key, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:241
virtual void close(UT_ErrorCode *ec=nullptr)
Definition: NET_Cache.h:336
void clear()
Definition: UT_Map.h:184
virtual bool decr(const UT_StringRef &key, int &value, int delta=1, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:264
#define NET_API
Definition: NET_API.h:9
virtual bool incr(const UT_StringRef &key, int &value, int delta=1, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:250
virtual int incrVersion(const UT_StringRef &key, int delta=1, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:305
size_t appendFormat(const char *fmt, const Args &...args)
UT_StringHolder myKeyPrefix
Definition: NET_Cache.h:359
virtual UT_StringHolder generateKey(const UT_StringHolder &key_prefix, const UT_StringHolder &key, int version)
Definition: NET_Cache.h:341
int open(float queuesize) override
UT_StringHolder makeKey(const UT_StringHolder &key, int version=-1)
Definition: NET_Cache.h:46
GLbitfield GLuint64 timeout
Definition: glcorearb.h:1599
static const UT_StringHolder theEmptyString
UT_Function< int(UT_ErrorCode &ec)> int_def_clb_t
Definition: NET_Cache.h:32
virtual bool set(const UT_StringHolder &key, int value, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:99
virtual void getMany(const UT_StringArray &keys, UT_Map< UT_StringHolder, UT_StringHolder > &results, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:127
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
virtual UT_StringHolder getOrSet(const UT_StringRef &key, const UT_StringHolder &def, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:221
size_t format(const char *fmt, const Args &...args)
Format a string using the same formatting codes as UTformat.
std::function< T > UT_Function
Definition: UT_Function.h:37
GT_API const UT_StringHolder version
std::error_code UT_ErrorCode
Definition: UT_ErrorCode.h:20
virtual void removeMany(const UT_StringArray &keys, UT_ErrorCode *ec, int version=-1)
Definition: NET_Cache.h:296
SYS_FORCE_INLINE void append(char character)
virtual int decrVersion(const UT_StringRef &key, int delta=1, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:327
UT_Function< UT_StringHolder(UT_ErrorCode &ec)> str_def_clb_t
Definition: NET_Cache.h:33
virtual void clear(UT_ErrorCode *ec=nullptr)=0
Definition: core.h:1131
int toInt() const
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
virtual int getOrSet(const UT_StringRef &key, int def, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:182
virtual UT_StringHolder getOrSet(const UT_StringRef &key, str_def_clb_t &&clb, UT_ErrorCode &ec, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:199
virtual bool add(const UT_StringHolder &key, int value, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:55
virtual void getMany(const UT_StringArray &keys, UT_Map< UT_StringHolder, int > &results, UT_ErrorCode *ec=nullptr, int version=-1)
Definition: NET_Cache.h:144
GLint GLsizei count
Definition: glcorearb.h:405
bool add(const UT_StringHolder &key, int value, UT_ErrorCode *ec=nullptr, int64 timeout=-1, int version=-1) override
Definition: NET_Cache.h:374
Definition: format.h:895
virtual UT_StringArray setMany(const UT_Map< UT_StringHolder, int > &data, int64 timeout=-1, int version=-1)
Definition: NET_Cache.h:285