OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AbstractBuffer.cpp
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 
17 /*
18 This file implements some of the longer functions used by the AbstractBuffer interface
19 outside of the header file in order to prevent unnecessary re-compilation.
20 */
21 
22 #include "AbstractBuffer.h"
23 
24 namespace Data_Namespace {
25 
26 void AbstractBuffer::initEncoder(const SQLTypeInfo& tmp_sql_type) {
27  sql_type_ = tmp_sql_type;
28  encoder_.reset(Encoder::Create(this, sql_type_));
29  LOG_IF(FATAL, encoder_ == nullptr)
30  << "Failed to create encoder for SQL Type " << sql_type_.get_type_name();
31 }
32 
34  if (src_buffer->hasEncoder()) {
35  if (!hasEncoder()) {
36  initEncoder(src_buffer->sql_type_);
37  }
38  encoder_->copyMetadata(src_buffer->encoder_.get());
39  } else {
40  encoder_ = nullptr;
41  }
42 }
43 
44 void AbstractBuffer::copyTo(AbstractBuffer* destination_buffer, const size_t num_bytes) {
45  CHECK_GE(size(), num_bytes) << "Attempting to copy more bytes than a buffer contains";
46  size_t chunk_size = (num_bytes == 0) ? size() : num_bytes;
47  destination_buffer->reserve(chunk_size);
48  if (isUpdated()) {
49  read(destination_buffer->getMemoryPtr(),
50  chunk_size,
51  0,
52  destination_buffer->getType(),
53  destination_buffer->getDeviceId());
54  } else {
55  read(destination_buffer->getMemoryPtr() + destination_buffer->size(),
56  chunk_size - destination_buffer->size(),
57  destination_buffer->size(),
58  destination_buffer->getType(),
59  destination_buffer->getDeviceId());
60  }
61  destination_buffer->setSize(chunk_size);
62  destination_buffer->syncEncoder(this);
63 }
64 
66  encoder_ = nullptr;
67  size_ = 0;
68 }
69 } // namespace Data_Namespace
void syncEncoder(const AbstractBuffer *src_buffer)
virtual int8_t * getMemoryPtr()=0
static Encoder * Create(Data_Namespace::AbstractBuffer *buffer, const SQLTypeInfo sqlType)
Definition: Encoder.cpp:26
virtual MemoryLevel getType() const =0
#define CHECK_GE(x, y)
Definition: Logger.h:306
void initEncoder(const SQLTypeInfo &tmp_sql_type)
#define LOG_IF(severity, condition)
Definition: Logger.h:384
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
An AbstractBuffer is a unit of data management for a data manager.
std::string get_type_name() const
Definition: sqltypes.h:482
std::unique_ptr< Encoder > encoder_
void setSize(const size_t size)
void copyTo(AbstractBuffer *destination_buffer, const size_t num_bytes=0)
virtual void reserve(size_t num_bytes)=0