OmniSciDB  5ade3759e0
Buffer.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 
22 #ifndef DATAMGR_MEMORY_BUFFER_BUFFER_H
23 #define DATAMGR_MEMORY_BUFFER_BUFFER_H
24 
25 #include "../AbstractBuffer.h"
26 #include "BufferSeg.h"
27 
28 #include <iostream>
29 #include <mutex>
30 //#include <boost/thread/locks.hpp>
31 //#include <boost/thread/mutex.hpp>
32 
33 using namespace Data_Namespace;
34 
35 namespace Buffer_Namespace {
36 
37 class BufferMgr;
38 
45 class Buffer : public AbstractBuffer {
46  friend class BufferMgr;
47  friend class FileMgr;
48 
49  public:
63  /*
64  Buffer(const int8_t * mem, const size_t numPages, const size_t pageSize, const int
65  epoch);
66  */
67 
68  Buffer(BufferMgr* bm,
69  BufferList::iterator segIt,
70  const int deviceId,
71  const size_t pageSize = 512,
72  const size_t numBytes = 0);
73 
75  ~Buffer() override;
76 
86  void read(int8_t* const dst,
87  const size_t numBytes,
88  const size_t offset = 0,
89  const MemoryLevel dstBufferType = CPU_LEVEL,
90  const int deviceId = -1) override;
91 
92  void reserve(const size_t numBytes) override;
102  void write(int8_t* src,
103  const size_t numBytes,
104  const size_t offset = 0,
105  const MemoryLevel srcBufferType = CPU_LEVEL,
106  const int deviceId = -1) override;
107 
108  void append(int8_t* src,
109  const size_t numBytes,
110  const MemoryLevel srcBufferType = CPU_LEVEL,
111  const int deviceId = -1) override;
112 
117  int8_t* getMemoryPtr() override;
118 
119  inline size_t size() const override { return size_; }
120 
122  inline size_t reservedSize() const override { return pageSize_ * numPages_; }
124 
125  inline size_t pageCount() const override { return numPages_; }
126 
128 
129  inline size_t pageSize() const override { return pageSize_; }
130 
132  inline bool isDirty() const override { return isDirty_; }
133 
134  inline int pin() override {
135  std::lock_guard<std::mutex> pinLock(pinMutex_);
136  return (++pinCount_);
137  }
138 
139  inline int unPin() override {
140  std::lock_guard<std::mutex> pinLock(pinMutex_);
141  return (--pinCount_);
142  }
143  inline int getPinCount() override {
144  std::lock_guard<std::mutex> pinLock(pinMutex_);
145  return (pinCount_);
146  }
147 
148  protected:
149  int8_t* mem_;
150 
151  private:
152  Buffer(const Buffer&); // private copy constructor
153  Buffer& operator=(const Buffer&); // private overloaded assignment operator
154  virtual void readData(int8_t* const dst,
155  const size_t numBytes,
156  const size_t offset = 0,
157  const MemoryLevel dstBufferType = CPU_LEVEL,
158  const int dstDeviceId = -1) = 0;
159  virtual void writeData(int8_t* const src,
160  const size_t numBytes,
161  const size_t offset = 0,
162  const MemoryLevel srcBufferType = CPU_LEVEL,
163  const int srcDeviceId = -1) = 0;
164 
166  BufferList::iterator segIt_;
167  // size_t numBytes_;
168  size_t pageSize_;
169  size_t numPages_;
170  int epoch_;
171  // std::vector<Page> pages_; /// a vector of pages (page metadata) that compose the
172  // buffer
173  std::vector<bool> pageDirtyFlags_;
175  std::mutex pinMutex_;
176 };
177 
178 } // namespace Buffer_Namespace
179 
180 #endif // DATAMGR_MEMORY_BUFFER_BUFFER_H
bool isDirty() const override
Returns whether or not the buffer has been modified since the last flush/checkpoint.
Definition: Buffer.h:132
size_t numPages_
the size of each page in the buffer
Definition: Buffer.h:169
int64_t * src
int getPinCount() override
Definition: Buffer.h:143
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:96
std::mutex pinMutex_
Definition: Buffer.h:175
size_t read(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Reads the specified number of bytes from the offset position in file f into buf.
Definition: File.cpp:113
BufferList::iterator segIt_
Definition: Buffer.h:166
An AbstractBuffer is a unit of data management for a data manager.
size_t append(FILE *f, const size_t size, int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:136
size_t pageCount() const override
Returns the number of pages in the buffer.
Definition: Buffer.h:125
std::vector< bool > pageDirtyFlags_
indicates when the buffer was last flushed
Definition: Buffer.h:173
size_t write(FILE *f, const size_t offset, const size_t size, int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:121
size_t pageSize() const override
Returns the size in bytes of each page in the buffer.
Definition: Buffer.h:129
int unPin() override
Definition: Buffer.h:139
size_t reservedSize() const override
Returns the total number of bytes allocated for the buffer.
Definition: Buffer.h:122
int pin() override
Definition: Buffer.h:134
size_t size() const override
Definition: Buffer.h:119
Note(s): Forbid Copying Idiom 4.1.
Definition: Buffer.h:45