OmniSciDB  5ade3759e0
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 <unistd.h>
22 #include <cstdio>
23 #include <cstring>
24 #include <mutex>
25 #include <set>
26 #include <vector>
27 #include "../../Shared/types.h"
28 #include "Page.h"
29 #include "Shared/Logger.h"
30 
31 namespace File_Namespace {
32 
33 struct Page;
34 
49 #define DELETE_CONTINGENT (-1)
50 
51 class FileMgr;
52 struct FileInfo {
54  int fileId;
55  FILE* f;
56  size_t pageSize;
57  size_t numPages;
58  // std::vector<Page*> pages; /// Page pointers for each page (including
59  // free pages)
60  std::set<size_t> freePages;
61  std::mutex freePagesMutex_;
62  std::mutex readWriteMutex_;
63 
65  FileInfo(FileMgr* fileMgr,
66  const int fileId,
67  FILE* f,
68  const size_t pageSize,
69  const size_t numPages,
70  const bool init = false);
71 
73  ~FileInfo();
74 
76  // for each apge
77  void initNewFile();
78 
79  void freePageDeferred(int pageId);
80  void freePage(int pageId);
81  int getFreePage();
82  size_t write(const size_t offset, const size_t size, int8_t* buf);
83  size_t read(const size_t offset, const size_t size, int8_t* buf);
84 
85  void openExistingFile(std::vector<HeaderInfo>& headerVec, const int fileMgrEpoch);
87  void print(bool pagesummary);
88 
90  inline size_t size() { return pageSize * numPages; }
91 
92  inline int syncToDisk() {
93  if (fflush(f) != 0) {
94  LOG(FATAL) << "Error trying to flush changes to disk, the error was: "
95  << std::strerror(errno);
96  }
97 #ifdef __APPLE__
98  return fcntl(fileno(f), 51);
99 #else
100  return fsync(fileno(f));
101 #endif
102  }
103 
105  inline size_t available() { return freePages.size() * pageSize; }
106 
108  inline size_t numFreePages() {
109  std::lock_guard<std::mutex> lock(freePagesMutex_);
110  return freePages.size();
111  }
112 
114  inline size_t used() { return size() - available(); }
115 };
116 } // namespace File_Namespace
117 
118 #endif // kkkkk
#define LOG(tag)
Definition: Logger.h:182
std::mutex readWriteMutex_
Definition: FileInfo.h:62
void freePageDeferred(int pageId)
Definition: FileInfo.cpp:187
size_t size()
Returns the number of bytes used by the file.
Definition: FileInfo.h:90
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:60
size_t pageSize
file stream object for the represented file
Definition: FileInfo.h:56
void init(LogOptions const &log_opts)
Definition: Logger.cpp:260
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:61
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:55
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:114
size_t numFreePages()
Returns the number of free pages available.
Definition: FileInfo.h:108
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:57
size_t available()
Returns the number of free bytes available.
Definition: FileInfo.h:105