OmniSciDB  8a228a1076
PersistentStorageMgr.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 
17 #include "PersistentStorageMgr.h"
18 #include "Catalog/Catalog.h"
19 
20 PersistentStorageMgr::PersistentStorageMgr(const std::string& data_dir,
21  const size_t num_reader_threads,
22  const DiskCacheConfig& disk_cache_config)
23  : AbstractBufferMgr(0)
24  , global_file_mgr_(
25  std::make_unique<File_Namespace::GlobalFileMgr>(0,
26  data_dir,
27  num_reader_threads)) {
28  if (disk_cache_config.is_enabled) {
29  disk_cache_ = std::make_unique<foreign_storage::ForeignStorageCache>(
30  disk_cache_config.path, num_reader_threads, disk_cache_config.entry_limit);
32  std::make_unique<foreign_storage::ForeignStorageMgr>(disk_cache_.get());
33  } else {
34  foreign_storage_mgr_ = std::make_unique<foreign_storage::ForeignStorageMgr>();
35  }
36 }
37 
39  const size_t page_size,
40  const size_t initial_size) {
41  return global_file_mgr_->createBuffer(chunk_key, page_size, initial_size);
42 }
43 
44 void PersistentStorageMgr::deleteBuffer(const ChunkKey& chunk_key, const bool purge) {
45  global_file_mgr_->deleteBuffer(chunk_key, purge);
46 }
47 
49  const bool purge) {
50  global_file_mgr_->deleteBuffersWithPrefix(chunk_key_prefix, purge);
51 }
52 
54  const size_t num_bytes) {
55  if (isForeignStorage(chunk_key)) {
56  return foreign_storage_mgr_->getBuffer(chunk_key, num_bytes);
57  } else {
58  return global_file_mgr_->getBuffer(chunk_key, num_bytes);
59  }
60 }
61 
63  AbstractBuffer* destination_buffer,
64  const size_t num_bytes) {
65  if (isForeignStorage(chunk_key)) {
66  foreign_storage_mgr_->fetchBuffer(chunk_key, destination_buffer, num_bytes);
67  } else {
68  global_file_mgr_->fetchBuffer(chunk_key, destination_buffer, num_bytes);
69  }
70 }
71 
73  AbstractBuffer* source_buffer,
74  const size_t num_bytes) {
75  return global_file_mgr_->putBuffer(chunk_key, source_buffer, num_bytes);
76 }
77 
79  global_file_mgr_->getChunkMetadataVec(chunk_metadata);
80  foreign_storage_mgr_->getChunkMetadataVec(chunk_metadata);
81 }
82 
84  ChunkMetadataVector& chunk_metadata,
85  const ChunkKey& keyPrefix) {
86  if (isForeignStorage(keyPrefix)) {
87  foreign_storage_mgr_->getChunkMetadataVecForKeyPrefix(chunk_metadata, keyPrefix);
88  } else {
89  global_file_mgr_->getChunkMetadataVecForKeyPrefix(chunk_metadata, keyPrefix);
90  }
91 }
92 
94  return global_file_mgr_->isBufferOnDevice(chunk_key);
95 }
96 
98  return global_file_mgr_->printSlabs();
99 }
100 
102  global_file_mgr_->clearSlabs();
103 }
104 
106  return global_file_mgr_->getMaxSize();
107 }
108 
110  return global_file_mgr_->getInUseSize();
111 }
112 
114  return global_file_mgr_->getAllocated();
115 }
116 
118  return global_file_mgr_->isAllocationCapped();
119 }
120 
122  global_file_mgr_->checkpoint();
123 }
124 
125 void PersistentStorageMgr::checkpoint(const int db_id, const int tb_id) {
126  global_file_mgr_->checkpoint(db_id, tb_id);
127 }
128 
129 AbstractBuffer* PersistentStorageMgr::alloc(const size_t num_bytes) {
130  return global_file_mgr_->alloc(num_bytes);
131 }
132 
134  global_file_mgr_->free(buffer);
135 }
136 
138  return PERSISTENT_STORAGE_MGR;
139 }
140 
142  return ToString(PERSISTENT_STORAGE_MGR);
143 }
144 
146  return global_file_mgr_->getNumChunks();
147 }
148 
150  return global_file_mgr_.get();
151 }
152 
153 void PersistentStorageMgr::removeTableRelatedDS(const int db_id, const int table_id) {
154  if (isForeignStorage({db_id, table_id})) {
155  foreign_storage_mgr_->removeTableRelatedDS(db_id, table_id);
156  }
157  global_file_mgr_->removeTableRelatedDS(db_id, table_id);
158 }
159 
161  auto db_id = chunk_key[0];
162  auto table_id = chunk_key[1];
163 
164  auto catalog = Catalog_Namespace::Catalog::get(db_id);
165  CHECK(catalog);
166 
167  auto table = catalog->getMetadataForTableImpl(table_id, false);
168  CHECK(table);
169  return table->storageType == StorageType::FOREIGN_TABLE;
170 }
171 
173  return foreign_storage_mgr_.get();
174 }
175 
177  return disk_cache_ ? disk_cache_.get() : nullptr;
178 }
size_t getInUseSize() override
std::string getStringMgrType() override
size_t getAllocated() override
AbstractBuffer * alloc(const size_t num_bytes) override
PersistentStorageMgr(const std::string &data_dir, const size_t num_reader_threads, const DiskCacheConfig &disk_cache_config)
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3671
void deleteBuffer(const ChunkKey &chunk_key, const bool purge) override
This file contains the class specification and related data structures for Catalog.
size_t getMaxSize() override
AbstractBuffer * createBuffer(const ChunkKey &chunk_key, const size_t page_size, const size_t initial_size) override
bool isForeignStorage(const ChunkKey &chunk_key)
void getChunkMetadataVecForKeyPrefix(ChunkMetadataVector &chunk_metadata, const ChunkKey &chunk_key_prefix) override
An AbstractBuffer is a unit of data management for a data manager.
void deleteBuffersWithPrefix(const ChunkKey &chunk_key_prefix, const bool purge) override
foreign_storage::ForeignStorageMgr * getForeignStorageMgr() const
MgrType getMgrType() override
size_t getNumChunks() override
std::unique_ptr< foreign_storage::ForeignStorageCache > disk_cache_
File_Namespace::GlobalFileMgr * getGlobalFileMgr()
AbstractBuffer * getBuffer(const ChunkKey &chunk_key, const size_t num_bytes) override
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:35
foreign_storage::ForeignStorageCache * getDiskCache() const
void getChunkMetadataVec(ChunkMetadataVector &chunk_metadata) override
std::vector< std::pair< ChunkKey, std::shared_ptr< ChunkMetadata > >> ChunkMetadataVector
AbstractBuffer * putBuffer(const ChunkKey &chunk_key, AbstractBuffer *source_buffer, const size_t num_bytes) override
void fetchBuffer(const ChunkKey &chunk_key, AbstractBuffer *destination_buffer, const size_t num_bytes) override
bool isBufferOnDevice(const ChunkKey &chunk_key) override
static constexpr char const * FOREIGN_TABLE
std::unique_ptr< foreign_storage::ForeignStorageMgr > foreign_storage_mgr_
bool isAllocationCapped() override
std::string printSlabs() override
void free(AbstractBuffer *buffer) override