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