OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ForeignTableRefresh.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 "ForeignTableRefresh.h"
18 #include "Catalog/Catalog.h"
19 #include "LockMgr/LockMgr.h"
20 
22 
23 namespace foreign_storage {
25  const ForeignTable& td,
26  const bool evict_cached_entries) {
27  auto& data_mgr = catalog.getDataMgr();
28  ChunkKey table_key{catalog.getCurrentDB().dbId, td.tableId};
29  ResultSetCacheInvalidator::invalidateCachesByTable(boost::hash_value(table_key));
30 
32 
33  if (catalog.getForeignTable(td.tableId)->isAppendMode() && !evict_cached_entries) {
34  ChunkMetadataVector metadata_vec;
35  data_mgr.getChunkMetadataVecForKeyPrefix(metadata_vec, table_key);
36  int last_fragment_id = 0;
37  for (const auto& [key, metadata] : metadata_vec) {
38  if (key[CHUNK_KEY_FRAGMENT_IDX] > last_fragment_id) {
39  last_fragment_id = key[CHUNK_KEY_FRAGMENT_IDX];
40  }
41  }
42  for (const auto& [key, metadata] : metadata_vec) {
43  if (key[CHUNK_KEY_FRAGMENT_IDX] == last_fragment_id) {
44  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::CPU_LEVEL);
45  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::GPU_LEVEL);
46  }
47  }
48  } else {
49  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::CPU_LEVEL);
50  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::GPU_LEVEL);
51  }
52 
53  try {
54  data_mgr.getPersistentStorageMgr()->getForeignStorageMgr()->refreshTable(
55  table_key, evict_cached_entries);
57  } catch (PostEvictionRefreshException& e) {
59  throw e.getOriginalException();
60  }
61 }
62 
64  const std::string& table_name,
65  const bool evict_cached_entries) {
66  auto table_lock =
67  std::make_unique<lockmgr::TableSchemaLockContainer<lockmgr::WriteLock>>(
69  catalog, table_name, false));
70 
71  const TableDescriptor* td = (*table_lock)();
72  if (td->storageType != StorageType::FOREIGN_TABLE) {
73  throw std::runtime_error{
74  table_name +
75  " is not a foreign table. Refreshes are applicable to only foreign tables."};
76  }
77 
78  auto foreign_table = dynamic_cast<const ForeignTable*>(td);
79  CHECK(foreign_table);
80  refresh_foreign_table_unlocked(catalog, *foreign_table, evict_cached_entries);
81 }
82 } // namespace foreign_storage
static void invalidateCachesByTable(size_t table_key)
const foreign_storage::ForeignTable * getForeignTable(const std::string &tableName) const
Definition: Catalog.cpp:1596
std::vector< int > ChunkKey
Definition: types.h:37
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:114
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:229
#define CHUNK_KEY_FRAGMENT_IDX
Definition: types.h:42
void refresh_foreign_table(Catalog_Namespace::Catalog &catalog, const std::string &table_name, const bool evict_cached_entries)
std::runtime_error getOriginalException()
This file contains the class specification and related data structures for Catalog.
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:228
std::vector< std::pair< ChunkKey, std::shared_ptr< ChunkMetadata >>> ChunkMetadataVector
bool isAppendMode() const
Checks if the table is in append mode.
void removeFragmenterForTable(const int table_id) const
Definition: Catalog.cpp:3629
#define CHECK(condition)
Definition: Logger.h:223
void refresh_foreign_table_unlocked(Catalog_Namespace::Catalog &catalog, const ForeignTable &td, const bool evict_cached_entries)
static constexpr char const * FOREIGN_TABLE
void updateForeignTableRefreshTimes(const int32_t table_id)
Definition: Catalog.cpp:5002