OmniSciDB  c0231cc57d
 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  mutable std::mutex freePagesMutex_;
64  mutable std::mutex readWriteMutex_;
65 
68  const int32_t fileId,
69  FILE* f,
70  const size_t pageSize,
71  const size_t numPages,
72  const bool init = false);
73 
75  ~FileInfo();
76 
78  // for each apge
79  void initNewFile();
80 
81  void freePageDeferred(int32_t pageId);
82  void freePage(int32_t pageId, const bool isRolloff, int32_t epoch);
83  int32_t getFreePage();
84  size_t write(const size_t offset, const size_t size, const int8_t* buf);
85  size_t read(const size_t offset, const size_t size, int8_t* buf);
86 
87  void openExistingFile(std::vector<HeaderInfo>& headerVec);
88 
90  std::string print() const;
91 
93  inline size_t size() const { return pageSize * numPages; }
94 
97  int32_t syncToDisk();
98 
100  inline size_t available() const { return numFreePages() * pageSize; }
101 
103  inline size_t numFreePages() const {
104  std::lock_guard<std::mutex> lock(freePagesMutex_);
105  return freePages.size();
106  }
107 
108  inline std::set<size_t> getFreePages() const {
109  std::lock_guard<std::mutex> lock(freePagesMutex_);
110  return freePages;
111  }
112 
114  inline size_t used() const { return size() - available(); }
115 
116  void freePageImmediate(int32_t page_num);
117  void recoverPage(const ChunkKey& chunk_key, int32_t page_num);
118 };
119 
120 bool is_page_deleted_with_checkpoint(int32_t table_epoch,
121  int32_t page_epoch,
122  int32_t contingent);
123 
124 bool is_page_deleted_without_checkpoint(int32_t table_epoch,
125  int32_t page_epoch,
126  int32_t contingent);
127 
128 } // namespace File_Namespace
std::vector< int > ChunkKey
Definition: types.h:36
FileInfo(FileMgr *fileMgr, const int32_t fileId, FILE *f, const size_t pageSize, const size_t numPages, const bool init=false)
Constructor.
Definition: FileInfo.cpp:31
size_t write(const size_t offset, const size_t size, const int8_t *buf)
Definition: FileInfo.cpp:63
bool is_page_deleted_without_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:283
std::mutex readWriteMutex_
Definition: FileInfo.h:64
void freePageImmediate(int32_t page_num)
Definition: FileInfo.cpp:246
std::string print() const
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:217
size_t numFreePages() const
Returns the number of free pages available.
Definition: FileInfo.h:103
void freePage(int32_t pageId, const bool isRolloff, int32_t epoch)
Definition: FileInfo.cpp:185
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:308
size_t used() const
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:114
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:50
size_t size() const
Returns the number of bytes used by the file.
Definition: FileInfo.h:93
std::mutex freePagesMutex_
set of page numbers of free pages
Definition: FileInfo.h:63
constexpr int32_t ROLLOFF_CONTINGENT
Definition: FileInfo.h:52
~FileInfo()
Destructor.
Definition: FileInfo.cpp:43
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:69
bool is_page_deleted_with_checkpoint(int32_t table_epoch, int32_t page_epoch, int32_t contingent)
Definition: FileInfo.cpp:271
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:74
void recoverPage(const ChunkKey &chunk_key, int32_t page_num)
Definition: FileInfo.cpp:259
void freePageDeferred(int32_t pageId)
Definition: FileInfo.cpp:170
size_t available() const
Returns the number of free bytes available.
Definition: FileInfo.h:100
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:108
bool isDirty
the number of pages in the file
Definition: FileInfo.h:61