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