OmniSciDB  340b00dbf6
 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 
117  inline size_t reservedSize() const override { return page_size_ * num_pages_; }
118 
120  inline size_t pageCount() const override { return num_pages_; }
121 
123  inline size_t pageSize() const override { return page_size_; }
124 
125  inline int pin() override {
126  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
127  return (++pin_count_);
128  }
129 
130  inline int unPin() override {
131  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
132  return (--pin_count_);
133  }
134  inline int getPinCount() override {
135  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
136  return (pin_count_);
137  }
138 
139  protected:
140  int8_t* mem_;
141 
142  private:
143  Buffer(const Buffer&); // private copy constructor
144  Buffer& operator=(const Buffer&); // private overloaded assignment operator
145  virtual void readData(int8_t* const dst,
146  const size_t num_bytes,
147  const size_t offset = 0,
148  const MemoryLevel dst_buffer_type = CPU_LEVEL,
149  const int dst_device_id = -1) = 0;
150  virtual void writeData(int8_t* const src,
151  const size_t num_bytes,
152  const size_t offset = 0,
153  const MemoryLevel src_buffer_type = CPU_LEVEL,
154  const int src_device_id = -1) = 0;
155 
157  BufferList::iterator seg_it_;
158  size_t page_size_;
159  size_t num_pages_;
160  int epoch_;
161  std::vector<bool> page_dirty_flags_;
163  std::mutex pin_mutex_;
164 };
165 
166 } // namespace Buffer_Namespace
size_t num_pages_
the size of each page in the buffer
Definition: Buffer.h:159
BufferList::iterator seg_it_
Definition: Buffer.h:157
std::mutex pin_mutex_
Definition: Buffer.h:163
int getPinCount() override
Definition: Buffer.h:134
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:118
std::vector< bool > page_dirty_flags_
indicates when the buffer was last flushed
Definition: Buffer.h:161
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:141
size_t pageCount() const override
Returns the number of pages in the buffer.
Definition: Buffer.h:120
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:126
size_t pageSize() const override
Returns the size in bytes of each page in the buffer.
Definition: Buffer.h:123
int unPin() override
Definition: Buffer.h:130
size_t reservedSize() const override
Returns the total number of bytes allocated for the buffer.
Definition: Buffer.h:117
int pin() override
Definition: Buffer.h:125
Note(s): Forbid Copying Idiom 4.1.
Definition: Buffer.h:42