OmniSciDB  6686921089
 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 2017 MapD Technologies, 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 #pragma once
17 
18 #include <cstdio>
19 #include <cstring>
20 #include <mutex>
21 #include <set>
22 #include <vector>
23 
24 #ifdef __APPLE__
25 #include <fcntl.h>
26 #endif
27 
28 #include "../../Shared/types.h"
29 #include "Logger/Logger.h"
30 #include "OSDependent/omnisci_fs.h"
31 #include "Page.h"
32 extern bool g_read_only;
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::mutex freePagesMutex_;
64  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);
89  void print(bool pagesummary);
90 
92  inline size_t size() const { return pageSize * numPages; }
93 
96  int32_t syncToDisk();
97 
99  inline size_t available() { return freePages.size() * pageSize; }
100 
102  inline size_t numFreePages() {
103  std::lock_guard<std::mutex> lock(freePagesMutex_);
104  return freePages.size();
105  }
106 
108  inline size_t used() { return size() - available(); }
109 
110  void freePageImmediate(int32_t page_num);
111  void recoverPage(const ChunkKey& chunk_key, int32_t page_num);
112 };
113 
114 } // namespace File_Namespace
std::vector< int > ChunkKey
Definition: types.h:37
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:28
size_t write(const size_t offset, const size_t size, const int8_t *buf)
Definition: FileInfo.cpp:60
std::mutex readWriteMutex_
Definition: FileInfo.h:64
void freePageImmediate(int32_t page_num)
Definition: FileInfo.cpp:239
void freePage(int32_t pageId, const bool isRolloff, int32_t epoch)
Definition: FileInfo.cpp:178
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:290
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:47
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
bool g_read_only
Definition: File.cpp:38
~FileInfo()
Destructor.
Definition: FileInfo.cpp:40
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:66
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:58
void openExistingFile(std::vector< HeaderInfo > &headerVec)
Definition: FileInfo.cpp:71
void recoverPage(const ChunkKey &chunk_key, int32_t page_num)
Definition: FileInfo.cpp:252
void freePageDeferred(int32_t pageId)
Definition: FileInfo.cpp:163
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:210
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