OmniSciDB  c1a53651b2
 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 2022 HEAVY.AI, 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  CHECK(pin_count_ > 0);
134  return (--pin_count_);
135  }
136  inline int getPinCount() override {
137  std::lock_guard<std::mutex> pin_lock(pin_mutex_);
138  return (pin_count_);
139  }
140 
141  // Added for testing.
142  int32_t getSlabNum() const { return seg_it_->slab_num; }
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
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:178
BufferList::iterator seg_it_
Definition: Buffer.h:162
std::mutex pin_mutex_
Definition: Buffer.h:168
int getPinCount() override
Definition: Buffer.h:136
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:160
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:96
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 read(FILE *f, const size_t offset, const size_t size, int8_t *buf, const std::string &file_path)
Reads the specified number of bytes from the offset position in file f into buf.
Definition: File.cpp:142
size_t pageCount() const override
Returns the number of pages in the buffer.
Definition: Buffer.h:121
int32_t getSlabNum() const
Definition: Buffer.h:142
#define CHECK(condition)
Definition: Logger.h:291
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