OmniSciDB  85c2d10cdc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_chunk_keys_.emplace(chunk_key);
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_chunk_keys_.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_chunk_keys_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
58  for (auto&& chunk_key_it = cached_chunk_keys_.lower_bound(chunk_key_prefix);
59  chunk_key_it != end_it;) {
60  chunk_key_it = cached_chunk_keys_.erase(chunk_key_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 : cached_chunk_keys_) {
75  if (global_file_mgr_->getBuffer(key)->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_chunk_keys_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
89  for (auto&& chunk_key_it = cached_chunk_keys_.lower_bound(chunk_prefix);
90  chunk_key_it != end_it;
91  ++chunk_key_it) {
92  if (global_file_mgr_->getBuffer(*chunk_key_it)->isDirty()) {
94  global_file_mgr_->fetchBuffer(*chunk_key_it, &temp_buf, 0);
95  disk_cache_->cacheChunk(*chunk_key_it, &temp_buf);
96  }
97  }
98  PersistentStorageMgr::global_file_mgr_->checkpoint(db_id, tb_id);
99 }
100 
102  const int table_id) {
104  const ChunkKey table_key{db_id, table_id};
105  ChunkKey upper_prefix(table_key);
106  upper_prefix.push_back(std::numeric_limits<int>::max());
107  auto end_it = cached_chunk_keys_.upper_bound(static_cast<const ChunkKey>(upper_prefix));
108  for (auto&& chunk_key_it = cached_chunk_keys_.lower_bound(table_key);
109  chunk_key_it != end_it;) {
110  chunk_key_it = cached_chunk_keys_.erase(chunk_key_it);
111  }
112 }
std::vector< int > ChunkKey
Definition: types.h:37
AbstractBuffer * createBuffer(const ChunkKey &chunk_key, const size_t page_size, const size_t initial_size) override
bool isEnabledForMutableTables() const
void deleteBuffer(const ChunkKey &chunk_key, const bool purge) override
ChunkKey get_table_key(const ChunkKey &key)
Definition: types.h:52
AbstractBuffer * createBuffer(const ChunkKey &chunk_key, const size_t page_size, const size_t initial_size) override
bool isForeignStorage(const ChunkKey &chunk_key) const
bool has_table_prefix(const ChunkKey &key)
Definition: types.h:48
An AbstractBuffer is a unit of data management for a data manager.
void deleteBuffersWithPrefix(const ChunkKey &chunk_key_prefix, const bool purge) override
bool isChunkPrefixCacheable(const ChunkKey &chunk_prefix) const
void deleteBuffersWithPrefix(const ChunkKey &chunk_key_prefix, const bool purge) override
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
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