OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractBuffer.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 
23 #pragma once
24 
25 #include <memory>
26 
27 #ifdef BUFFER_MUTEX
28 #include <boost/thread/locks.hpp>
29 #include <boost/thread/shared_mutex.hpp>
30 #endif
31 
32 #include "Encoder.h"
33 #include "MemoryLevel.h"
34 
35 #include "Logger/Logger.h"
36 #include "Shared/sqltypes.h"
37 #include "Shared/types.h"
38 
39 namespace Data_Namespace {
40 
46 // enum BufferType {FILE_BUFFER, CPU_BUFFER, GPU_BUFFER};
47 
49  public:
50  AbstractBuffer(const int device_id)
51  : encoder_(nullptr)
52  , size_(0)
53  , device_id_(device_id)
54  , is_dirty_(false)
56  , is_updated_(false) {}
57 
58  AbstractBuffer(const int device_id, const SQLTypeInfo sql_type)
59  : size_(0)
60  , device_id_(device_id)
61  , is_dirty_(false)
63  , is_updated_(false) {
64  initEncoder(sql_type);
65  }
66  virtual ~AbstractBuffer() {}
67 
68  virtual void read(int8_t* const dst,
69  const size_t num_bytes,
70  const size_t offset = 0,
71  const MemoryLevel dst_buffer_type = CPU_LEVEL,
72  const int dst_device_id = -1) = 0;
73  virtual void write(int8_t* src,
74  const size_t num_bytes,
75  const size_t offset = 0,
76  const MemoryLevel src_buffer_type = CPU_LEVEL,
77  const int src_device_id = -1) = 0;
78  virtual void reserve(size_t num_bytes) = 0;
79  virtual void append(int8_t* src,
80  const size_t num_bytes,
81  const MemoryLevel src_buffer_type = CPU_LEVEL,
82  const int device_id = -1) = 0;
83  virtual int8_t* getMemoryPtr() = 0;
84  virtual void setMemoryPtr(int8_t* new_ptr) { CHECK(false); }
85  virtual size_t pageCount() const = 0;
86  virtual size_t pageSize() const = 0;
87  virtual size_t reservedSize() const = 0;
88  virtual MemoryLevel getType() const = 0;
89 
90  // Next three methods are dummy methods so FileBuffer does not implement these
91  virtual inline int pin() { return 0; }
92  virtual inline int unPin() { return 0; }
93  virtual inline int getPinCount() { return 0; }
94 
95  // These getters should not vary when inherited and therefore don't need to be virtual.
96  inline size_t size() const { return size_; }
97  inline int getDeviceId() const { return device_id_; }
98  inline bool isDirty() const { return is_dirty_; }
99  inline bool isAppended() const { return is_appended_; }
100  inline bool isUpdated() const { return is_updated_; }
101  inline bool hasEncoder() const { return (encoder_ != nullptr); }
102  inline SQLTypeInfo getSqlType() const { return sql_type_; }
103  inline void setSqlType(const SQLTypeInfo& sql_type) { sql_type_ = sql_type; }
104  inline Encoder* getEncoder() const {
105  CHECK(hasEncoder());
106  return encoder_.get();
107  }
108 
109  inline void setDirty() { is_dirty_ = true; }
110 
111  inline void setUpdated() {
112  is_updated_ = true;
113  is_dirty_ = true;
114  }
115 
116  inline void setAppended() {
117  is_appended_ = true;
118  is_dirty_ = true;
119  }
120 
121  inline void setSize(const size_t size) { size_ = size; }
122  inline void clearDirtyBits() {
123  is_appended_ = false;
124  is_updated_ = false;
125  is_dirty_ = false;
126  }
127 
128  void initEncoder(const SQLTypeInfo& tmp_sql_type);
129  void syncEncoder(const AbstractBuffer* src_buffer);
130  void copyTo(AbstractBuffer* destination_buffer, const size_t num_bytes = 0);
131  void resetToEmpty();
132 
133  protected:
134  std::unique_ptr<Encoder> encoder_;
136  size_t size_;
138 
139  private:
140  bool is_dirty_;
143 
144 #ifdef BUFFER_MUTEX
145  boost::shared_mutex read_write_mutex_;
146  boost::shared_mutex append_mutex_;
147 #endif
148 };
149 
150 } // namespace Data_Namespace
void syncEncoder(const AbstractBuffer *src_buffer)
virtual int8_t * getMemoryPtr()=0
virtual MemoryLevel getType() const =0
virtual size_t reservedSize() const =0
virtual size_t pageCount() const =0
Constants for Builtin SQL Types supported by HEAVY.AI.
void initEncoder(const SQLTypeInfo &tmp_sql_type)
virtual void read(int8_t *const dst, const size_t num_bytes, const size_t offset=0, const MemoryLevel dst_buffer_type=CPU_LEVEL, const int dst_device_id=-1)=0
AbstractBuffer(const int device_id)
virtual size_t pageSize() const =0
An AbstractBuffer is a unit of data management for a data manager.
void setSqlType(const SQLTypeInfo &sql_type)
virtual void write(int8_t *src, const size_t num_bytes, const size_t offset=0, const MemoryLevel src_buffer_type=CPU_LEVEL, const int src_device_id=-1)=0
virtual void setMemoryPtr(int8_t *new_ptr)
AbstractBuffer(const int device_id, const SQLTypeInfo sql_type)
std::unique_ptr< Encoder > encoder_
virtual void append(int8_t *src, const size_t num_bytes, const MemoryLevel src_buffer_type=CPU_LEVEL, const int device_id=-1)=0
void setSize(const size_t size)
void copyTo(AbstractBuffer *destination_buffer, const size_t num_bytes=0)
SQLTypeInfo getSqlType() const
bool g_enable_watchdog false
Definition: Execute.cpp:80
#define CHECK(condition)
Definition: Logger.h:291
std::shared_timed_mutex shared_mutex
virtual void reserve(size_t num_bytes)=0