OmniSciDB  471d68cefb
 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 
19 #include "LockMgr/LockMgr.h"
20 
21 namespace foreign_storage {
23  const std::string& table_name,
24  const bool evict_cached_entries) {
25  auto& data_mgr = catalog.getDataMgr();
26  auto table_lock =
27  std::make_unique<lockmgr::TableSchemaLockContainer<lockmgr::WriteLock>>(
29  catalog, table_name, false));
30 
31  const TableDescriptor* td = (*table_lock)();
32  if (td->storageType != StorageType::FOREIGN_TABLE) {
33  throw std::runtime_error{
34  table_name +
35  " is not a foreign table. Refreshes are applicable to only foreign tables."};
36  }
37 
38  catalog.removeFragmenterForTable(td->tableId);
39  ChunkKey table_key{catalog.getCurrentDB().dbId, td->tableId};
40 
41  if (catalog.getForeignTable(td->tableId)->isAppendMode() && !evict_cached_entries) {
42  ChunkMetadataVector metadata_vec;
43  data_mgr.getChunkMetadataVecForKeyPrefix(metadata_vec, table_key);
44  int last_fragment_id = 0;
45  for (const auto& [key, metadata] : metadata_vec) {
46  if (key[CHUNK_KEY_FRAGMENT_IDX] > last_fragment_id) {
47  last_fragment_id = key[CHUNK_KEY_FRAGMENT_IDX];
48  }
49  }
50  for (const auto& [key, metadata] : metadata_vec) {
51  if (key[CHUNK_KEY_FRAGMENT_IDX] == last_fragment_id) {
52  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::CPU_LEVEL);
53  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::GPU_LEVEL);
54  }
55  }
56  } else {
57  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::CPU_LEVEL);
58  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::GPU_LEVEL);
59  }
60 
61  try {
62  data_mgr.getPersistentStorageMgr()->getForeignStorageMgr()->refreshTable(
63  table_key, evict_cached_entries);
64  catalog.updateForeignTableRefreshTimes(td->tableId);
65  } catch (PostEvictionRefreshException& e) {
66  catalog.updateForeignTableRefreshTimes(td->tableId);
67  throw e.getOriginalException();
68  }
69 }
70 } // namespace foreign_storage
const foreign_storage::ForeignTable * getForeignTable(const std::string &tableName) const
Definition: Catalog.cpp:1485
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:113
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:224
#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()
const DBMetadata & getCurrentDB() const
Definition: Catalog.h:223
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:3475
static constexpr char const * FOREIGN_TABLE
void updateForeignTableRefreshTimes(const int32_t table_id)
Definition: Catalog.cpp:4784