OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #pragma once
23 
24 #include <iostream>
25 #include <mutex>
26 
27 #include "DataMgr/AbstractBuffer.h"
29 
30 using namespace Data_Namespace;
31 
32 namespace Buffer_Namespace {
33 
34 class BufferMgr;
35 
42 class Buffer : public AbstractBuffer {
43  friend class BufferMgr;
44  friend class FileMgr;
45 
46  public:
60  /*
61  Buffer(const int8_t * mem, const size_t numPages, const size_t pageSize, const int
62  epoch);
63  */
64 
65  Buffer(BufferMgr* bm,
66  BufferList::iterator seg_it,
67  const int device_id,
68  const size_t page_size = 512,
69  const size_t num_bytes = 0);
70 
72  ~Buffer() override;
73 
83  void read(int8_t* const dst,
84  const size_t num_bytes,
85  const size_t offset = 0,
86  const MemoryLevel dst_buffer_type = CPU_LEVEL,
87  const int device_id = -1) override;
88 
89  void reserve(const size_t num_bytes) override;
99  void write(int8_t* src,
100  const size_t num_bytes,
101  const size_t offset = 0,
102  const MemoryLevel src_buffer_type = CPU_LEVEL,
103  const int device_id = -1) override;
104 
105  void append(int8_t* src,
106  const size_t num_bytes,
107  const MemoryLevel src_buffer_type = CPU_LEVEL,
108  const int deviceId = -1) override;
109 
114  int8_t* getMemoryPtr() override;
115 
116  inline size_t size() const override { return size_; }
117 
119  inline size_t reservedSize() const override { return page_size_ * num_pages_; }
120 
122  inline size_t pageCount() const override { return num_pages_; }
123 
125  inline size_t pageSize() const override { return page_size_; }
126 
128  inline bool isDirty() const override { return is_dirty_; }
129 
130  inline int pin() override {
131  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
132  return (++pin_count_);
133  }
134 
135  inline int unPin() override {
136  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
137  return (--pin_count_);
138  }
139  inline int getPinCount() override {
140  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
141  return (pin_count_);
142  }
143 
144  protected:
145  int8_t* mem_;
146 
147  private:
148  Buffer(const Buffer&); // private copy constructor
149  Buffer& operator=(const Buffer&); // private overloaded assignment operator
150  virtual void readData(int8_t* const dst,
151  const size_t num_bytes,
152  const size_t offset = 0,
153  const MemoryLevel dst_buffer_type = CPU_LEVEL,
154  const int dst_device_id = -1) = 0;
155  virtual void writeData(int8_t* const src,
156  const size_t num_bytes,
157  const size_t offset = 0,
158  const MemoryLevel src_buffer_type = CPU_LEVEL,
159  const int src_device_id = -1) = 0;
160 
162  BufferList::iterator seg_it_;
163  size_t page_size_;
164  size_t num_pages_;
165  int epoch_;
166  std::vector<bool> page_dirty_flags_;
168  std::mutex pin_mutex_;
169 };
170 
171 } // namespace Buffer_Namespace
size_t num_pages_
the size of each page in the buffer
Definition: Buffer.h:164
BufferList::iterator seg_it_
Definition: Buffer.h:162
bool isDirty() const override
Returns whether or not the buffer has been modified since the last flush/checkpoint.
Definition: Buffer.h:128
std::mutex pin_mutex_
Definition: Buffer.h:168
int64_t * src
int getPinCount() override
Definition: Buffer.h:139
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:97
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
std::vector< bool > page_dirty_flags_
indicates when the buffer was last flushed
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:122
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:125
int unPin() override
Definition: Buffer.h:135
size_t reservedSize() const override
Returns the total number of bytes allocated for the buffer.
Definition: Buffer.h:119
int pin() override
Definition: Buffer.h:130
size_t size() const override
Definition: Buffer.h:116
Note(s): Forbid Copying Idiom 4.1.
Definition: Buffer.h:42