OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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.getForeignTableUnlocked(td->tableId)->isAppendMode() &&
42  !evict_cached_entries) {
43  ChunkMetadataVector metadata_vec;
44  data_mgr.getChunkMetadataVecForKeyPrefix(metadata_vec, table_key);
45  int last_fragment_id = 0;
46  for (const auto& [key, metadata] : metadata_vec) {
47  if (key[CHUNK_KEY_FRAGMENT_IDX] > last_fragment_id) {
48  last_fragment_id = key[CHUNK_KEY_FRAGMENT_IDX];
49  }
50  }
51  for (const auto& [key, metadata] : metadata_vec) {
52  if (key[CHUNK_KEY_FRAGMENT_IDX] == last_fragment_id) {
53  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::CPU_LEVEL);
54  data_mgr.deleteChunksWithPrefix(key, MemoryLevel::GPU_LEVEL);
55  }
56  }
57  } else {
58  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::CPU_LEVEL);
59  data_mgr.deleteChunksWithPrefix(table_key, MemoryLevel::GPU_LEVEL);
60  }
61 
62  try {
63  data_mgr.getPersistentStorageMgr()->getForeignStorageMgr()->refreshTable(
64  table_key, evict_cached_entries);
65  catalog.updateForeignTableRefreshTimes(td->tableId);
66  } catch (PostEvictionRefreshException& e) {
67  catalog.updateForeignTableRefreshTimes(td->tableId);
68  throw e.getOriginalException();
69  }
70 }
71 } // namespace foreign_storage
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:102
const foreign_storage::ForeignTable * getForeignTableUnlocked(int tableId) const
Definition: Catalog.cpp:1727
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:223
#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:222
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:3444
static constexpr char const * FOREIGN_TABLE
void updateForeignTableRefreshTimes(const int32_t table_id)
Definition: Catalog.cpp:4727