OmniSciDB  0bd2ec9cf4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
17 #ifndef FILEINFO_H
18 #define FILEINFO_H
19 
20 #include <fcntl.h>
21 #include <cstdio>
22 #include <cstring>
23 #include <mutex>
24 #include <set>
25 #include <vector>
26 #include "../../Shared/types.h"
27 #include "Page.h"
28 #include "Shared/Logger.h"
29 
30 namespace File_Namespace {
31 
32 struct Page;
33 
48 #define DELETE_CONTINGENT (-1)
49 
50 class FileMgr;
51 struct FileInfo {
53  int fileId;
54  FILE* f;
55  size_t pageSize;
56  size_t numPages;
57  // std::vector<Page*> pages; /// Page pointers for each page (including
58  // free pages)
59  std::set<size_t> freePages;
60  std::mutex freePagesMutex_;
61  std::mutex readWriteMutex_;
62 
65  const int fileId,
66  FILE* f,
67  const size_t pageSize,
68  const size_t numPages,
69  const bool init = false);
70 
72  ~FileInfo();
73 
75  // for each apge
76  void initNewFile();
77 
78  void freePageDeferred(int pageId);
79  void freePage(int pageId);
80  int getFreePage();
81  size_t write(const size_t offset, const size_t size, int8_t* buf);
82  size_t read(const size_t offset, const size_t size, int8_t* buf);
83 
84  void openExistingFile(std::vector<HeaderInfo>& headerVec, const int fileMgrEpoch);
86  void print(bool pagesummary);
87 
89  inline size_t size() { return pageSize * numPages; }
90 
91  inline int syncToDisk() {
92  if (fflush(f) != 0) {
93  LOG(FATAL) << "Error trying to flush changes to disk, the error was: "
94  << std::strerror(errno);
95  }
96 #ifdef __APPLE__
97  return fcntl(fileno(f), 51);
98 #else
99  return fsync(fileno(f));
100 #endif
101  }
102 
104  inline size_t available() { return freePages.size() * pageSize; }
105 
107  inline size_t numFreePages() {
108  std::lock_guard<std::mutex> lock(freePagesMutex_);
109  return freePages.size();
110  }
111 
113  inline size_t used() { return size() - available(); }
114 };
115 } // namespace File_Namespace
116 
117 #endif // kkkkk
#define LOG(tag)
Definition: Logger.h:188
std::mutex readWriteMutex_
Definition: FileInfo.h:61
void freePageDeferred(int pageId)
Definition: FileInfo.cpp:187
size_t size()
Returns the number of bytes used by the file.
Definition: FileInfo.h:89
size_t write(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:59
std::set< size_t > freePages
the number of pages in the file
Definition: FileInfo.h:59
size_t pageSize
file stream object for the represented file
Definition: FileInfo.h:55
void init(LogOptions const &log_opts)
Definition: Logger.cpp:272
void initNewFile()
Adds all pages to freePages and zeroes first four bytes of header.
Definition: FileInfo.cpp:47
std::mutex freePagesMutex_
set of page numbers of free pages
Definition: FileInfo.h:60
void freePage(int pageId)
Definition: FileInfo.cpp:202
FileInfo(FileMgr *fileMgr, const int fileId, FILE *f, const size_t pageSize, const size_t numPages, const bool init=false)
Constructor.
Definition: FileInfo.cpp:28
~FileInfo()
Destructor.
Definition: FileInfo.cpp:40
size_t read(const size_t offset, const size_t size, int8_t *buf)
Definition: FileInfo.cpp:64
FILE * f
unique file identifier (i.e., used for a file name)
Definition: FileInfo.h:54
void openExistingFile(std::vector< HeaderInfo > &headerVec, const int fileMgrEpoch)
Definition: FileInfo.cpp:69
size_t used()
Returns the amount of used bytes; size() - available()
Definition: FileInfo.h:113
size_t numFreePages()
Returns the number of free pages available.
Definition: FileInfo.h:107
void print(bool pagesummary)
Prints a summary of the file to stdout.
Definition: FileInfo.cpp:238
size_t numPages
the fixed size of each page in the file
Definition: FileInfo.h:56
size_t available()
Returns the number of free bytes available.
Definition: FileInfo.h:104