OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ForeignStorageCache.h
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 
26 #pragma once
27 
28 #include "../Shared/mapd_shared_mutex.h"
31 #include "ForeignDataWrapper.h"
32 
33 class CacheTooSmallException : public std::runtime_error {
34  public:
35  CacheTooSmallException(const std::string& msg) : std::runtime_error(msg) {}
36 };
37 
38 using namespace Data_Namespace;
39 
40 namespace foreign_storage {
41 
43  public:
45 
46  void checkpoint(const int32_t db_id, const int32_t tb_id);
47  void putBuffer(const ChunkKey&, AbstractBuffer*, const size_t numBytes = 0);
48  File_Namespace::FileBuffer* getCachedChunkIfExists(const ChunkKey&);
49  bool isMetadataCached(const ChunkKey&) const;
50  void cacheMetadataVec(const ChunkMetadataVector&);
51  void getCachedMetadataVecForKeyPrefix(ChunkMetadataVector&, const ChunkKey&) const;
52  bool hasCachedMetadataForKeyPrefix(const ChunkKey&) const;
53  void clearForTablePrefix(const ChunkKey&);
54  void clear();
55  std::vector<ChunkKey> getCachedChunksForKeyPrefix(const ChunkKey&) const;
56  ChunkToBufferMap getChunkBuffersForCaching(
57  const std::vector<ChunkKey>& chunk_keys) const;
58 
59  // Get a chunk buffer for writing to disk prior to metadata creation/caching
60  AbstractBuffer* getChunkBufferForPrecaching(const ChunkKey& chunk_key,
61  bool is_new_buffer);
62 
63  void deleteBufferIfExists(const ChunkKey& chunk_key);
64 
65  inline size_t getNumCachedChunks() const {
66  return caching_file_mgr_->getNumDataChunks();
67  }
68  inline size_t getNumCachedMetadata() const {
69  return caching_file_mgr_->getNumChunksWithMetadata();
70  }
71 
72  // Useful for debugging.
73  std::string dumpCachedChunkEntries() const;
74  std::string dumpCachedMetadataEntries() const;
75 
76  inline std::string getCacheDirectory() const {
77  return caching_file_mgr_->getFileMgrBasePath();
78  }
79 
80  inline std::string getCacheDirectoryForTable(int db_id, int tb_id) const {
81  return caching_file_mgr_->getTableFileMgrPath(db_id, tb_id);
82  }
83 
84  inline std::string getSerializedWrapperPath(int32_t db_id, int32_t tb_id) const {
85  return getCacheDirectoryForTable(db_id, tb_id) + "/" +
87  }
88 
89  void cacheMetadataWithFragIdGreaterOrEqualTo(const ChunkMetadataVector& metadata_vec,
90  const int frag_id);
91 
92  inline uint64_t getSpaceReservedByTable(int db_id, int tb_id) const {
93  return caching_file_mgr_->getSpaceReservedByTable(db_id, tb_id);
94  }
95 
96  void storeDataWrapper(const std::string& doc, int32_t db_id, int32_t tb_id);
97 
98  private:
99  // These methods are private and assume locks are already acquired when called.
100  std::set<ChunkKey>::iterator eraseChunk(const std::set<ChunkKey>::iterator&);
101  void eraseChunk(const ChunkKey& chunk_key);
102  void validatePath(const std::string&) const;
103 
104  // Underlying storage is handled by a CachingFileMgr unique to the cache.
105  std::unique_ptr<File_Namespace::CachingFileMgr> caching_file_mgr_;
106 
107 }; // ForeignStorageCache
108 } // namespace foreign_storage
std::vector< int > ChunkKey
Definition: types.h:37
std::map< ChunkKey, AbstractBuffer * > ChunkToBufferMap
Represents/provides access to contiguous data stored in the file system.
Definition: FileBuffer.h:58
uint64_t getSpaceReservedByTable(int db_id, int tb_id) const
std::vector< std::pair< ChunkKey, std::shared_ptr< ChunkMetadata >>> ChunkMetadataVector
An AbstractBuffer is a unit of data management for a data manager.
std::string getSerializedWrapperPath(int32_t db_id, int32_t tb_id) const
CacheTooSmallException(const std::string &msg)
std::unique_ptr< File_Namespace::CachingFileMgr > caching_file_mgr_
std::string getCacheDirectoryForTable(int db_id, int tb_id) const
static constexpr char WRAPPER_FILE_NAME[]