OmniSciDB  2e3a973ef4
MutableCachePersistentStorageMgr.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
20  const std::string& data_dir,
21  const size_t num_reader_threads,
22  const DiskCacheConfig& disk_cache_config)
23  : PersistentStorageMgr(data_dir, num_reader_threads, disk_cache_config) {
26 }
27 
29  const ChunkKey& chunk_key,
30  const size_t page_size,
31  const size_t initial_size) {
32  auto buf = PersistentStorageMgr::createBuffer(chunk_key, page_size, initial_size);
33  if (isChunkPrefixCacheable(chunk_key)) {
34  cached_buffer_map_.emplace(chunk_key, buf);
35  }
36  return buf;
37 }
38 
40  const bool purge) {
41  // No need to delete for FSI-only cache as Foreign Tables are immutable and we should
42  // not be deleting buffers for them.
43  CHECK(!isForeignStorage(chunk_key));
44  disk_cache_->deleteBufferIfExists(chunk_key);
45  cached_buffer_map_.erase(chunk_key);
46  PersistentStorageMgr::deleteBuffer(chunk_key, purge);
47 }
48 
50  const ChunkKey& chunk_key_prefix,
51  const bool purge) {
52  CHECK(has_table_prefix(chunk_key_prefix));
53  disk_cache_->clearForTablePrefix(get_table_key(chunk_key_prefix));
54 
55  ChunkKey upper_prefix(chunk_key_prefix);
56  upper_prefix.push_back(std::numeric_limits<int>::max());
57  auto end_it = cached_buffer_map_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
58  for (auto&& chunk_it = cached_buffer_map_.lower_bound(chunk_key_prefix);
59  chunk_it != end_it;) {
60  chunk_it = cached_buffer_map_.erase(chunk_it);
61  }
62  PersistentStorageMgr::deleteBuffersWithPrefix(chunk_key_prefix, purge);
63 }
64 
66  AbstractBuffer* source_buffer,
67  const size_t num_bytes) {
68  auto buf = PersistentStorageMgr::putBuffer(chunk_key, source_buffer, num_bytes);
69  disk_cache_->cacheChunk(chunk_key, source_buffer);
70  return buf;
71 }
72 
74  for (auto& [key, buf] : cached_buffer_map_) {
75  if (buf->isDirty()) {
77  global_file_mgr_->fetchBuffer(key, &temp_buf, 0);
78  disk_cache_->cacheChunk(key, &temp_buf);
79  }
80  }
82 }
83 
84 void MutableCachePersistentStorageMgr::checkpoint(const int db_id, const int tb_id) {
85  ChunkKey chunk_prefix{db_id, tb_id};
86  ChunkKey upper_prefix(chunk_prefix);
87  upper_prefix.push_back(std::numeric_limits<int>::max());
88  auto end_it = cached_buffer_map_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
89  for (auto&& chunk_it = cached_buffer_map_.lower_bound(chunk_prefix); chunk_it != end_it;
90  ++chunk_it) {
91  if (chunk_it->second->isDirty()) {
93  global_file_mgr_->fetchBuffer(chunk_it->first, &temp_buf, 0);
94  disk_cache_->cacheChunk(chunk_it->first, &temp_buf);
95  }
96  }
97  PersistentStorageMgr::global_file_mgr_->checkpoint(db_id, tb_id);
98 }
99 
101  const int table_id) {
103  const ChunkKey table_key{db_id, table_id};
104  ChunkKey upper_prefix(table_key);
105  upper_prefix.push_back(std::numeric_limits<int>::max());
106  auto end_it = cached_buffer_map_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
107  for (auto&& chunk_it = cached_buffer_map_.lower_bound(table_key); chunk_it != end_it;) {
108  chunk_it = cached_buffer_map_.erase(chunk_it);
109  }
110 }
AbstractBuffer * createBuffer(const ChunkKey &chunk_key, const size_t page_size, const size_t initial_size) override
bool isEnabledForMutableTables() const
bool isForeignStorage(const ChunkKey &chunk_key) const
void deleteBuffer(const ChunkKey &chunk_key, const bool purge) override
AbstractBuffer * createBuffer(const ChunkKey &chunk_key, const size_t page_size, const size_t initial_size) override
An AbstractBuffer is a unit of data management for a data manager.
void deleteBuffersWithPrefix(const ChunkKey &chunk_key_prefix, const bool purge) override
ChunkKey get_table_key(const ChunkKey &key)
Definition: types.h:52
bool isChunkPrefixCacheable(const ChunkKey &chunk_prefix) const
std::map< const ChunkKey, AbstractBuffer * > cached_buffer_map_
void deleteBuffersWithPrefix(const ChunkKey &chunk_key_prefix, const bool purge) override
bool has_table_prefix(const ChunkKey &key)
Definition: types.h:48
std::unique_ptr< foreign_storage::ForeignStorageCache > disk_cache_
void removeTableRelatedDS(const int db_id, const int table_id) override
DiskCacheConfig disk_cache_config_
MutableCachePersistentStorageMgr(const std::string &data_dir, const size_t num_reader_threads, const DiskCacheConfig &disk_cache_config)
void removeTableRelatedDS(const int db_id, const int table_id) override
std::unique_ptr< File_Namespace::GlobalFileMgr > global_file_mgr_
#define CHECK(condition)
Definition: Logger.h:197
std::vector< int > ChunkKey
Definition: types.h:37
void deleteBuffer(const ChunkKey &chunk_key, const bool purge) override
AbstractBuffer * putBuffer(const ChunkKey &chunk_key, AbstractBuffer *source_buffer, const size_t num_bytes) override
AbstractBuffer * putBuffer(const ChunkKey &chunk_key, AbstractBuffer *source_buffer, const size_t num_bytes) override