OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileInfo.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 #pragma once
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <mutex>
22 #include <set>
23 #include <vector>
24 
25 #ifdef __APPLE__
26 #include <fcntl.h>
27 #endif
28 
29 #include "../../Shared/types.h"
30 #include "Logger/Logger.h"
31 #include "OSDependent/heavyai_fs.h"
32 #include "Page.h"
33 namespace File_Namespace {
34 
35 struct Page;
36 
51 constexpr int32_t DELETE_CONTINGENT = -1;
52 constexpr int32_t ROLLOFF_CONTINGENT = -2;
53 
54 class FileMgr;
55 struct FileInfo {
57  int32_t fileId;
58  FILE* f;
59  size_t pageSize;
60  size_t numPages;
61  bool isDirty{false}; // True if writes have occured since last sync
62  std::set<size_t> freePages;
63  std::string file_path;
64  mutable std::mutex freePagesMutex_;
65  mutable std::mutex readWriteMutex_;
66 
69  const int32_t fileId,
70  FILE* f,
71  const size_t pageSize,
72  const size_t numPages,
73  const std::string& file_path,
74  const bool init = false);
75 
77  ~FileInfo();
78 
80  // for each apge
81  void initNewFile();
82 
83  void freePageDeferred(int32_t pageId);
84  void freePage(int32_t pageId, const bool isRolloff, int32_t epoch);
85  int32_t getFreePage();
86  size_t write(const size_t offset, const size_t size, const int8_t* buf);
87  size_t read(const size_t offset, const size_t size, int8_t* buf);
88 
89  void openExistingFile(std::vector<HeaderInfo>& headerVec);
90 
92  std::string print() const;
93 
95  inline size_t size() const { return pageSize * numPages; }
96 
99  int32_t syncToDisk();
100 
102  inline size_t available() const { return numFreePages() * pageSize; }
103 
105  inline size_t numFreePages() const {
106  std::lock_guard<std::mutex> lock(freePagesMutex_);
107  return freePages.size();
108  }
109 
110  inline std::set<size_t> getFreePages() const {
111  std::lock_guard<std::mutex> lock(freePagesMutex_);
112  return freePages;
113  }
114 
116  inline size_t used() const { return size() - available(); }
117 
118  void freePageImmediate(int32_t page_num);
119  void recoverPage(const ChunkKey& chunk_key, int32_t page_num);
120 };
121 
122 bool is_page_deleted_with_checkpoint(int32_t table_epoch,
123  int32_t page_epoch,
124  int32_t contingent);
125 
126 bool is_page_deleted_without_checkpoint(int32_t table_epoch,
127  int32_t page_epoch,
128  int32_t contingent);
129 
130 } // namespace File_Namespace
std::vector< int > ChunkKey
Definition: types.h:36
size_t write(const size_t offset, const size_t size, const int8_t *buf)
Definition: FileInfo.cpp:64
bool is_page_deleted_without_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:271
std::mutex readWriteMutex_
Definition: FileInfo.h:65
void freePageImmediate(int32_t page_num)
Definition: FileInfo.cpp:245
std::string print() const
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:216
std::string file_path
set of page numbers of free pages
Definition: FileInfo.h:63
size_t numFreePages() const
Returns the number of free pages available.
Definition: FileInfo.h:105
void freePage(int32_t pageId, const bool isRolloff, int32_t epoch)
Definition: FileInfo.cpp:187
FileInfo(FileMgr *fileMgr, const int32_t fileId, FILE *f, const size_t pageSize, const size_t numPages, const std::string &file_path, const bool init=false)
Constructor.
Definition: FileInfo.cpp:31
std::set< size_t > freePages
Definition: FileInfo.h:62
size_t pageSize
file stream object for the represented file
Definition: FileInfo.h:59
constexpr int32_t DELETE_CONTINGENT
A FileInfo type has a file pointer and metadata about a file.
Definition: FileInfo.h:51
void init(LogOptions const &log_opts)
Definition: Logger.cpp:364
size_t used() const
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:116
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:56
size_t size() const
Returns the number of bytes used by the file.
Definition: FileInfo.h:95
std::mutex freePagesMutex_
Definition: FileInfo.h:64
constexpr int32_t ROLLOFF_CONTINGENT
Definition: FileInfo.h:52
~FileInfo()
Destructor.
Definition: FileInfo.cpp:49
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:70
bool is_page_deleted_with_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:259
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:58
This file contains the declaration and definition of a Page type and a MultiPage type.
void openExistingFile(std::vector< HeaderInfo > &headerVec)
Definition: FileInfo.cpp:75
void recoverPage(const ChunkKey &chunk_key, int32_t page_num)
Definition: FileInfo.cpp:252
void freePageDeferred(int32_t pageId)
Definition: FileInfo.cpp:172
size_t available() const
Returns the number of free bytes available.
Definition: FileInfo.h:102
size_t numPages
the fixed size of each page in the file
Definition: FileInfo.h:60
std::set< size_t > getFreePages() const
Definition: FileInfo.h:110
bool isDirty
the number of pages in the file
Definition: FileInfo.h:61