OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataBufferCache Class Reference

#include <TableFunctionsDataCache.h>

Public Member Functions

bool isKeyCached (const std::string &key) const
 
bool isKeyCachedAndSameLength (const std::string &key, const size_t num_bytes) const
 
template<typename T >
void getDataForKey (const std::string &key, T *dest_buffer) const
 
template<typename T >
const T & getDataRefForKey (const std::string &key) const
 
template<typename T >
const T * getDataPtrForKey (const std::string &key) const
 
template<typename T >
void putDataForKey (const std::string &key, T *const data_buffer, const size_t num_elements)
 

Private Member Functions

void copyData (int8_t *dest, const int8_t *source, const size_t num_bytes) const
 

Private Attributes

const size_t parallel_copy_min_bytes {1 << 20}
 
std::unordered_map
< std::string, std::shared_ptr
< CacheDataTf > > 
data_cache_
 
std::shared_mutex cache_mutex_
 

Static Private Attributes

static constexpr bool debug_print_ {false}
 

Detailed Description

Definition at line 41 of file TableFunctionsDataCache.h.

Member Function Documentation

void DataBufferCache::copyData ( int8_t *  dest,
const int8_t *  source,
const size_t  num_bytes 
) const
inlineprivate

Definition at line 118 of file TableFunctionsDataCache.h.

References parallel_copy_min_bytes, and threading_serial::parallel_for().

Referenced by getDataForKey(), and putDataForKey().

118  {
119  if (num_bytes < parallel_copy_min_bytes) {
120  std::memcpy(dest, source, num_bytes);
121  return;
122  }
123  const size_t max_bytes_per_thread = parallel_copy_min_bytes;
124  const size_t num_threads =
125  (num_bytes + max_bytes_per_thread - 1) / max_bytes_per_thread;
127  tbb::blocked_range<size_t>(0, num_threads, 1),
128  [&](const tbb::blocked_range<size_t>& r) {
129  const size_t end_chunk_idx = r.end();
130  for (size_t chunk_idx = r.begin(); chunk_idx != end_chunk_idx; ++chunk_idx) {
131  const size_t start_byte = chunk_idx * max_bytes_per_thread;
132  const size_t length =
133  std::min(start_byte + max_bytes_per_thread, num_bytes) - start_byte;
134  std::memcpy(dest + start_byte, source + start_byte, length);
135  }
136  });
137  }
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
const size_t parallel_copy_min_bytes

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename T >
void DataBufferCache::getDataForKey ( const std::string &  key,
T *  dest_buffer 
) const
inline

Definition at line 58 of file TableFunctionsDataCache.h.

References cache_mutex_, copyData(), data_cache_, and DEBUG_TIMER.

58  {
59  auto timer = DEBUG_TIMER(__func__);
60  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
61  const auto& cached_data_itr = data_cache_.find(key);
62  if (cached_data_itr == data_cache_.end()) {
63  const std::string error_msg = "Data for key " + key + " not found in cache.";
64  throw std::runtime_error(error_msg);
65  }
66  copyData(reinterpret_cast<int8_t*>(dest_buffer),
67  cached_data_itr->second->data_buffer,
68  cached_data_itr->second->num_bytes);
69  }
void copyData(int8_t *dest, const int8_t *source, const size_t num_bytes) const
#define DEBUG_TIMER(name)
Definition: Logger.h:412
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_

+ Here is the call graph for this function:

template<typename T >
const T* DataBufferCache::getDataPtrForKey ( const std::string &  key) const
inline

Definition at line 83 of file TableFunctionsDataCache.h.

References cache_mutex_, data_cache_, and heavydb.dtypes::T.

83  {
84  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
85  const auto& cached_data_itr = data_cache_.find(key);
86  if (cached_data_itr == data_cache_.end()) {
87  return nullptr;
88  }
89  return reinterpret_cast<const T* const>(cached_data_itr->second->data_buffer);
90  }
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_
template<typename T >
const T& DataBufferCache::getDataRefForKey ( const std::string &  key) const
inline

Definition at line 72 of file TableFunctionsDataCache.h.

References cache_mutex_, data_cache_, and heavydb.dtypes::T.

72  {
73  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
74  const auto& cached_data_itr = data_cache_.find(key);
75  if (cached_data_itr == data_cache_.end()) {
76  const std::string error_msg{"Data for key " + key + " not found in cache."};
77  throw std::runtime_error(error_msg);
78  }
79  return *reinterpret_cast<const T*>(cached_data_itr->second->data_buffer);
80  }
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_
bool DataBufferCache::isKeyCached ( const std::string &  key) const
inline

Definition at line 43 of file TableFunctionsDataCache.h.

References cache_mutex_, and data_cache_.

43  {
44  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
45  return data_cache_.count(key) > 0;
46  }
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_
bool DataBufferCache::isKeyCachedAndSameLength ( const std::string &  key,
const size_t  num_bytes 
) const
inline

Definition at line 48 of file TableFunctionsDataCache.h.

References cache_mutex_, and data_cache_.

48  {
49  std::shared_lock<std::shared_mutex> read_lock(cache_mutex_);
50  const auto& cached_data_itr = data_cache_.find(key);
51  if (cached_data_itr == data_cache_.end()) {
52  return false;
53  }
54  return num_bytes == cached_data_itr->second->num_bytes;
55  }
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_
template<typename T >
void DataBufferCache::putDataForKey ( const std::string &  key,
T *const  data_buffer,
const size_t  num_elements 
)
inline

Definition at line 93 of file TableFunctionsDataCache.h.

References cache_mutex_, copyData(), data_cache_, debug_print_, DEBUG_TIMER, and heavydb.dtypes::T.

95  {
96  auto timer = DEBUG_TIMER(__func__);
97  const size_t num_bytes(num_elements * sizeof(T));
98  auto cache_data = std::make_shared<CacheDataTf>(num_bytes);
99  copyData(cache_data->data_buffer, reinterpret_cast<int8_t*>(data_buffer), num_bytes);
100  std::unique_lock<std::shared_mutex> write_lock(cache_mutex_);
101  const auto& cached_data_itr = data_cache_.find(key);
102  if (data_cache_.find(key) != data_cache_.end()) {
103  if constexpr (debug_print_) {
104  const std::string warning_msg =
105  "Data for key " + key + " already exists in cache. Replacing.";
106  std::cout << warning_msg << std::endl;
107  }
108  cached_data_itr->second.reset();
109  cached_data_itr->second = cache_data;
110  return;
111  }
112  data_cache_.insert(std::make_pair(key, cache_data));
113  }
void copyData(int8_t *dest, const int8_t *source, const size_t num_bytes) const
#define DEBUG_TIMER(name)
Definition: Logger.h:412
static constexpr bool debug_print_
std::shared_mutex cache_mutex_
std::unordered_map< std::string, std::shared_ptr< CacheDataTf > > data_cache_

+ Here is the call graph for this function:

Member Data Documentation

std::shared_mutex DataBufferCache::cache_mutex_
mutableprivate
std::unordered_map<std::string, std::shared_ptr<CacheDataTf> > DataBufferCache::data_cache_
private
constexpr bool DataBufferCache::debug_print_ {false}
staticprivate

Definition at line 141 of file TableFunctionsDataCache.h.

Referenced by putDataForKey().

const size_t DataBufferCache::parallel_copy_min_bytes {1 << 20}
private

Definition at line 116 of file TableFunctionsDataCache.h.

Referenced by copyData().


The documentation for this class was generated from the following file: