OmniSciDB  c07336695a
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 #ifndef DATAMGR_MEMORY_ABSTRACTBUFFER_H
23 #define DATAMGR_MEMORY_ABSTRACTBUFFER_H
24 
25 #include "Encoder.h"
26 #include "MemoryLevel.h"
27 #include "Shared/Logger.h"
28 #include "Shared/sqltypes.h"
29 #include "Shared/types.h"
30 
31 #ifdef BUFFER_MUTEX
32 #include <boost/thread/locks.hpp>
33 #include <boost/thread/shared_mutex.hpp>
34 #endif
35 
36 #include <memory>
37 
38 namespace Data_Namespace {
39 
45 // enum BufferType {FILE_BUFFER, CPU_BUFFER, GPU_BUFFER};
46 
48  public:
49  AbstractBuffer(const int deviceId)
50  : hasEncoder(false)
51  , size_(0)
52  , isDirty_(false)
53  , isAppended_(false)
54  , isUpdated_(false)
55  , deviceId_(deviceId) {}
56  AbstractBuffer(const int deviceId, const SQLTypeInfo sqlType)
57  : size_(0)
58  , isDirty_(false)
59  , isAppended_(false)
60  , isUpdated_(false)
61  , deviceId_(deviceId) {
62  initEncoder(sqlType);
63  }
64  virtual ~AbstractBuffer() {}
65 
66  virtual void read(int8_t* const dst,
67  const size_t numBytes,
68  const size_t offset = 0,
69  const MemoryLevel dstBufferType = CPU_LEVEL,
70  const int dstDeviceId = -1) = 0;
71  virtual void write(int8_t* src,
72  const size_t numBytes,
73  const size_t offset = 0,
74  const MemoryLevel srcBufferType = CPU_LEVEL,
75  const int srcDeviceId = -1) = 0;
76  virtual void reserve(size_t numBytes) = 0;
77  virtual void append(int8_t* src,
78  const size_t numBytes,
79  const MemoryLevel srcBufferType = CPU_LEVEL,
80  const int deviceId = -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 size_t used() const = 0;
88  virtual int getDeviceId() const { return deviceId_; }
89  virtual MemoryLevel getType() const = 0;
90 
91  // Next three methods are dummy methods so FileBuffer does not implement these
92  virtual inline int pin() { return 0; }
93  virtual inline int unPin() { return 0; }
94  virtual inline int getPinCount() { return 0; }
95 
96  virtual inline bool isDirty() const { return isDirty_; }
97  virtual inline bool isAppended() const { return isAppended_; }
98  virtual inline bool isUpdated() const { return isUpdated_; }
99 
100  virtual inline void setDirty() { isDirty_ = true; }
101 
102  virtual inline void setUpdated() {
103  isUpdated_ = true;
104  isDirty_ = true;
105  }
106 
107  virtual inline void setAppended() {
108  isAppended_ = true;
109  isDirty_ = true;
110  }
111 
112  void setSize(const size_t size) { size_ = size; }
113  void clearDirtyBits() {
114  isAppended_ = false;
115  isUpdated_ = false;
116  isDirty_ = false;
117  }
118  void initEncoder(const SQLTypeInfo tmpSqlType) {
119  hasEncoder = true;
120  sqlType = tmpSqlType;
121  encoder.reset(Encoder::Create(this, sqlType));
122  LOG_IF(FATAL, encoder == nullptr)
123  << "Failed to create encoder for SQL Type " << sqlType.get_type_name();
124  }
125 
126  void syncEncoder(const AbstractBuffer* srcBuffer) {
127  hasEncoder = srcBuffer->hasEncoder;
128  if (hasEncoder) {
129  if (!encoder) { // Encoder not initialized
130  initEncoder(srcBuffer->sqlType);
131  }
132  encoder->copyMetadata(srcBuffer->encoder.get());
133  }
134  }
135 
136  std::unique_ptr<Encoder> encoder;
139 
140  protected:
141  size_t size_;
142  bool isDirty_;
146 
147 #ifdef BUFFER_MUTEX
148  boost::shared_mutex readWriteMutex_;
149  boost::shared_mutex appendMutex_;
150 #endif
151 };
152 
153 } // namespace Data_Namespace
154 
155 #endif // DATAMGR_MEMORY_ABSTRACTBUFFER_H
virtual void write(int8_t *src, const size_t numBytes, const size_t offset=0, const MemoryLevel srcBufferType=CPU_LEVEL, const int srcDeviceId=-1)=0
void initEncoder(const SQLTypeInfo tmpSqlType)
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.
#define LOG_IF(severity, condition)
Definition: Logger.h:273
AbstractBuffer(const int deviceId)
virtual void reserve(size_t numBytes)=0
std::string get_type_name() const
Definition: sqltypes.h:422
virtual size_t pageSize() const =0
virtual void append(int8_t *src, const size_t numBytes, const MemoryLevel srcBufferType=CPU_LEVEL, const int deviceId=-1)=0
An AbstractBuffer is a unit of data management for a data manager.
AbstractBuffer(const int deviceId, const SQLTypeInfo sqlType)
virtual void read(int8_t *const dst, const size_t numBytes, const size_t offset=0, const MemoryLevel dstBufferType=CPU_LEVEL, const int dstDeviceId=-1)=0
virtual int getDeviceId() const
void setSize(const size_t size)
virtual bool isAppended() const
virtual bool isDirty() const
void syncEncoder(const AbstractBuffer *srcBuffer)
std::unique_ptr< Encoder > encoder
virtual bool isUpdated() const