SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Cache.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
8 #include "swcdb/core/Time.h"
9 
10 namespace SWC { namespace client {
11 
12 
13 
16  Map::clear();
17 }
18 
20  auto ms = Time::now_ms();
21 
23  for(auto c = begin(); c != cend(); ) {
24  for(auto r = c->second.cbegin(); r != c->second.cend(); ) {
25  if(ms - r->second.ts > m_expiry_ms->get()) {
26  r = c->second.erase(r);
27  } else {
28  ++r;
29  }
30  }
31  if(c->second.empty())
32  c = erase(c);
33  else
34  ++c;
35  }
36 }
37 
38 void CachedRangers::remove(const cid_t cid, const rid_t rid) {
40 
41  auto c = find(cid);
42  if(c == cend())
43  return;
44  auto r = c->second.find(rid);
45  if(r == c->second.cend())
46  return;
47  c->second.erase(r);
48  if(c->second.empty())
49  erase(c);
50 }
51 
52 bool CachedRangers::get(const cid_t cid, const rid_t rid,
53  Comm::EndPoints& endpoints) {
55  auto c = find(cid);
56  if(c != cend()) {
57  auto r = c->second.find(rid);
58  if(r != c->second.cend() &&
59  Time::now_ms() - r->second.ts < m_expiry_ms->get()) {
60  endpoints = r->second.endpoints;
61  return true;
62  }
63  }
64  return false;
65 }
66 
67 void CachedRangers::set(const cid_t cid, const rid_t rid,
68  const Comm::EndPoints& endpoints) {
69  int64_t ts = Time::now_ms();
71  auto& r = (*this)[cid][rid];
72  r.ts = ts;
73  r.endpoints = endpoints;
74 }
75 
76 
77 
78 }}
SWC::client::CachedRangers::m_mutex
Core::MutexSptd m_mutex
Definition: Cache.h:74
SWC::Core::MutexSptd::scope
Definition: MutexSptd.h:96
SWC::client::CachedRangers::remove
void remove(const cid_t cid, const rid_t rid)
Definition: Cache.cc:38
SWC::Config::Property::Value_int32_g::get
SWC_CAN_INLINE int32_t get() const noexcept
Definition: Property.h:610
SWC::client::CachedRangers::set
void set(const cid_t cid, const rid_t rid, const Comm::EndPoints &endpoints)
Definition: Cache.cc:67
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::client::CachedRangers::m_expiry_ms
Config::Property::Value_int32_g::Ptr m_expiry_ms
Definition: Cache.h:75
SWC::client::CachedRangers::clear
void clear()
Definition: Cache.cc:14
SWC::client::CachedRangers::clear_expired
void clear_expired()
Definition: Cache.cc:19
Time.h
SWC::cid_t
uint64_t cid_t
Definition: Identifiers.h:16
Cache.h
SWC::Core::Vector< EndPoint >
SWC::rid_t
uint64_t rid_t
Definition: Identifiers.h:17
SWC::Time::now_ms
SWC_CAN_INLINE int64_t now_ms() noexcept
Definition: Time.h:36
SWC::client::CachedRangers::get
bool get(const cid_t cid, const rid_t rid, Comm::EndPoints &endpoints)
Definition: Cache.cc:52