OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  // Added for testing.
141  int32_t getSlabNum() const { return seg_it_->slab_num; }
142 
143  protected:
144  int8_t* mem_;
145 
146  private:
147  Buffer(const Buffer&); // private copy constructor
148  Buffer& operator=(const Buffer&); // private overloaded assignment operator
149  virtual void readData(int8_t* const dst,
150  const size_t num_bytes,
151  const size_t offset = 0,
152  const MemoryLevel dst_buffer_type = CPU_LEVEL,
153  const int dst_device_id = -1) = 0;
154  virtual void writeData(int8_t* const src,
155  const size_t num_bytes,
156  const size_t offset = 0,
157  const MemoryLevel src_buffer_type = CPU_LEVEL,
158  const int src_device_id = -1) = 0;
159 
161  BufferList::iterator seg_it_;
162  size_t page_size_;
163  size_t num_pages_;
164  int epoch_;
165  std::vector<bool> page_dirty_flags_;
167  std::mutex pin_mutex_;
168 };
169 
170 } // namespace Buffer_Namespace
size_t num_pages_
the size of each page in the buffer
Definition: Buffer.h:163
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
Definition: File.cpp:159
BufferList::iterator seg_it_
Definition: Buffer.h:161
std::mutex pin_mutex_
Definition: Buffer.h:167
int getPinCount() override
Definition: Buffer.h:135
size_t write(FILE *f, const size_t offset, const size_t size, const int8_t *buf)
Writes the specified number of bytes to the offset position in file f from buf.
Definition: File.cpp:141
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:96
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:133
std::vector< bool > page_dirty_flags_
indicates when the buffer was last flushed
Definition: Buffer.h:165
An AbstractBuffer is a unit of data management for a data manager.
size_t pageCount() const override
Returns the number of pages in the buffer.
Definition: Buffer.h:121
int32_t getSlabNum() const
Definition: Buffer.h:141
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