OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  *
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  */
17 #pragma once
19 #include <cstdio>
20 #include <cstring>
21 #include <mutex>
22 #include <set>
23 #include <vector>
25 #ifdef __APPLE__
26 #include <fcntl.h>
27 #endif
29 #include "../../Shared/types.h"
30 #include "Logger/Logger.h"
31 #include "OSDependent/heavyai_fs.h"
32 #include "Page.h"
33 namespace File_Namespace {
35 struct Page;
51 constexpr int32_t DELETE_CONTINGENT = -1;
52 constexpr int32_t ROLLOFF_CONTINGENT = -2;
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::mutex freePagesMutex_;
64  std::mutex readWriteMutex_;
68  const int32_t fileId,
69  FILE* f,
70  const size_t pageSize,
71  const size_t numPages,
72  const bool init = false);
75  ~FileInfo();
78  // for each apge
79  void initNewFile();
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);
87  void openExistingFile(std::vector<HeaderInfo>& headerVec);
89  void print(bool pagesummary);
92  inline size_t size() const { return pageSize * numPages; }
96  int32_t syncToDisk();
99  inline size_t available() { return freePages.size() * pageSize; }
102  inline size_t numFreePages() {
103  std::lock_guard<std::mutex> lock(freePagesMutex_);
104  return freePages.size();
105  }
108  inline size_t used() { return size() - available(); }
110  void freePageImmediate(int32_t page_num);
111  void recoverPage(const ChunkKey& chunk_key, int32_t page_num);
112 };
114 bool is_page_deleted_with_checkpoint(int32_t table_epoch,
115  int32_t page_epoch,
116  int32_t contingent);
118 bool is_page_deleted_without_checkpoint(int32_t table_epoch,
119  int32_t page_epoch,
120  int32_t contingent);
122 } // 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)
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
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
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:92
std::mutex freePagesMutex_
set of page numbers of free pages
Definition: FileInfo.h:63
constexpr int32_t ROLLOFF_CONTINGENT
Definition: FileInfo.h:52
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 used()
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:108
size_t numFreePages()
Returns the number of free pages available.
Definition: FileInfo.h:102
void print(bool pagesummary)
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:217
size_t numPages
the fixed size of each page in the file
Definition: FileInfo.h:60
bool isDirty
the number of pages in the file
Definition: FileInfo.h:61
size_t available()
Returns the number of free bytes available.
Definition: FileInfo.h:99