OmniSciDB  1dac507f6e
 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 "Shared/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)
51  , size_(0)
52  , is_dirty_(false)
55  , device_id_(device_id) {}
56  AbstractBuffer(const int device_id, const SQLTypeInfo sql_type)
57  : size_(0)
58  , is_dirty_(false)
61  , device_id_(device_id) {
62  initEncoder(sql_type);
63  }
64  virtual ~AbstractBuffer() {}
65 
66  virtual void read(int8_t* const dst,
67  const size_t num_bytes,
68  const size_t offset = 0,
69  const MemoryLevel dst_buffer_type = CPU_LEVEL,
70  const int dst_device_id = -1) = 0;
71  virtual void write(int8_t* src,
72  const size_t num_bytes,
73  const size_t offset = 0,
74  const MemoryLevel src_buffer_type = CPU_LEVEL,
75  const int src_device_id = -1) = 0;
76  virtual void reserve(size_t num_bytes) = 0;
77  virtual void append(int8_t* src,
78  const size_t num_bytes,
79  const MemoryLevel src_buffer_type = CPU_LEVEL,
80  const int device_id = -1) = 0;
81  virtual int8_t* getMemoryPtr() = 0;
82 
83  virtual size_t pageCount() const = 0;
84  virtual size_t pageSize() const = 0;
85  virtual size_t size() const = 0;
86  virtual size_t reservedSize() const = 0;
87  virtual int getDeviceId() const { return device_id_; }
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  virtual inline bool isDirty() const { return is_dirty_; }
96  virtual inline bool isAppended() const { return is_appended_; }
97  virtual inline bool isUpdated() const { return is_updated_; }
98 
99  virtual inline void setDirty() { is_dirty_ = true; }
100 
101  virtual inline void setUpdated() {
102  is_updated_ = true;
103  is_dirty_ = true;
104  }
105 
106  virtual inline void setAppended() {
107  is_appended_ = true;
108  is_dirty_ = true;
109  }
110 
111  void setSize(const size_t size) { size_ = size; }
112  void clearDirtyBits() {
113  is_appended_ = false;
114  is_updated_ = false;
115  is_dirty_ = false;
116  }
117  void initEncoder(const SQLTypeInfo tmp_sql_type) {
118  has_encoder = true;
119  sql_type = tmp_sql_type;
120  encoder.reset(Encoder::Create(this, sql_type));
121  LOG_IF(FATAL, encoder == nullptr)
122  << "Failed to create encoder for SQL Type " << sql_type.get_type_name();
123  }
124 
125  void syncEncoder(const AbstractBuffer* src_buffer) {
126  has_encoder = src_buffer->has_encoder;
127  if (has_encoder) {
128  if (!encoder) { // Encoder not initialized
129  initEncoder(src_buffer->sql_type);
130  }
131  encoder->copyMetadata(src_buffer->encoder.get());
132  }
133  }
134 
135  std::unique_ptr<Encoder> encoder;
138 
139  protected:
140  size_t size_;
141  bool is_dirty_;
145 
146 #ifdef BUFFER_MUTEX
147  boost::shared_mutex read_write_mutex_;
148  boost::shared_mutex append_mutex_;
149 #endif
150 };
151 
152 } // namespace Data_Namespace
void initEncoder(const SQLTypeInfo tmp_sql_type)
void syncEncoder(const AbstractBuffer *src_buffer)
virtual size_t size() const =0
virtual int8_t * getMemoryPtr()=0
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
virtual MemoryLevel getType() const =0
int64_t * src
virtual size_t reservedSize() const =0
virtual size_t pageCount() const =0
Constants for Builtin SQL Types supported by MapD.
virtual bool isAppended() const
virtual bool isDirty() const
#define LOG_IF(severity, condition)
Definition: Logger.h:276
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
std::string get_type_name() const
Definition: sqltypes.h:429
AbstractBuffer(const int device_id)
virtual size_t pageSize() const =0
An AbstractBuffer is a unit of data management for a data manager.
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 bool isUpdated() const
AbstractBuffer(const int device_id, const SQLTypeInfo sql_type)
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)
bool g_enable_watchdog false
Definition: Execute.cpp:71
virtual int getDeviceId() const
virtual void reserve(size_t num_bytes)=0
std::unique_ptr< Encoder > encoder